From d41b490db09178a9da02cd20f6d2368b38ca6475 Mon Sep 17 00:00:00 2001 From: Instrumental Date: Wed, 31 Jul 2019 14:07:07 -0500 Subject: Create Service JUnit Base Class, and first method update Issue-ID: AAF-916 Change-Id: I8bc9c46c04a656e90b52ae54fee59b00ba0c451e Signed-off-by: Instrumental --- .../aaf/auth/service/test/JU_BaseServiceImpl.java | 50 +++++++++++++++++----- .../test/JU_ServiceImpl_createUserCred.java | 14 +++--- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_BaseServiceImpl.java b/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_BaseServiceImpl.java index c9ebc281..5e6cfb62 100644 --- a/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_BaseServiceImpl.java +++ b/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_BaseServiceImpl.java @@ -20,7 +20,9 @@ */ package org.onap.aaf.auth.service.test; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -49,6 +51,7 @@ import org.onap.aaf.auth.dao.cass.UserRoleDAO; import org.onap.aaf.auth.dao.hl.Question; import org.onap.aaf.auth.env.AuthzEnv; import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; import org.onap.aaf.auth.service.AuthzCassServiceImpl; import org.onap.aaf.auth.service.mapper.Mapper_2_0; import org.onap.aaf.cadi.PropAccess; @@ -75,12 +78,17 @@ public abstract class JU_BaseServiceImpl { protected AuthzCassServiceImpl acsi; protected Mapper_2_0 mapper; - - @Mock + + @Mock protected DefaultOrg org; - @Mock + @Mock protected DefaultOrgIdentity orgIdentity; - + +// +// NOTE: Annotation format (@Mock and @Spy) do NOT seem to always work as a Base Class, +// so we construct manually. +// +// Mock Objects protected HistoryDAO historyDAO = mock(HistoryDAO.class); protected CacheInfoDAO cacheInfoDAO = mock(CacheInfoDAO.class); protected CachedNSDAO nsDAO = mock(CachedNSDAO.class); @@ -93,20 +101,21 @@ public abstract class JU_BaseServiceImpl { protected FutureDAO futureDAO = mock(FutureDAO.class); protected DelegateDAO delegateDAO = mock(DelegateDAO.class); protected ApprovalDAO approvalDAO = mock(ApprovalDAO.class); - + + // Spy Objects @Spy protected static PropAccess access = new PropAccess(); - @Spy protected static AuthzEnv env = new AuthzEnv(access); - @Spy protected static AuthzTrans trans = env.newTransNoAvg(); - + // @Spy doesn't seem to work on Question. @Spy - protected Question question = new Question(trans,historyDAO,cacheInfoDAO,nsDAO,permDAO,roleDAO,userRoleDAO, - credDAO,certDAO,locateDAO,futureDAO,delegateDAO,approvalDAO); + protected Question question = spy(new Question(trans, + historyDAO,cacheInfoDAO,nsDAO,permDAO, + roleDAO,userRoleDAO,credDAO,certDAO, + locateDAO,futureDAO,delegateDAO,approvalDAO)); public void setUp() throws Exception { when(trans.org()).thenReturn(org); @@ -114,7 +123,7 @@ public abstract class JU_BaseServiceImpl { Define.set(access); access.setProperty(Config.CADI_LATITUDE, "38.0"); access.setProperty(Config.CADI_LONGITUDE, "-72.0"); - + mapper = new Mapper_2_0(question); acsi = new AuthzCassServiceImpl<>(trans, mapper, question); } @@ -136,6 +145,25 @@ public abstract class JU_BaseServiceImpl { return rv; } + /** + * Setup Role Data for Mock Usages + * @param trans + * @param user + * @param ns + * @param role + * @param exists + * @param days + */ + protected void whenRole(AuthzTrans trans, String user, String ns, String role, boolean exists, int days) { + Result> result; + if(exists) { + result = Result.ok(listOf(urData(user,ns,role,days))); + } else { + result = Result.ok(emptyList(UserRoleDAO.Data.class)); + } + when(question.userRoleDAO().read(trans, user, ns+'.'+role)).thenReturn(result); + } + protected UserRoleDAO.Data urData(String user, String ns, String rname, int days) { UserRoleDAO.Data urdd = new UserRoleDAO.Data(); urdd.user = user; diff --git a/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_ServiceImpl_createUserCred.java b/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_ServiceImpl_createUserCred.java index 1e4e9719..00da6b4c 100644 --- a/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_ServiceImpl_createUserCred.java +++ b/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_ServiceImpl_createUserCred.java @@ -25,7 +25,6 @@ import static org.mockito.Mockito.*; import java.nio.ByteBuffer; import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.List; @@ -33,16 +32,17 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.runners.MockitoJUnitRunner; -import org.onap.aaf.auth.dao.CachedDAO; import org.onap.aaf.auth.dao.cass.CredDAO; import org.onap.aaf.auth.dao.cass.UserRoleDAO; + +import org.onap.aaf.auth.dao.hl.Question; import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.layer.Result; import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.cadi.Hash; import org.onap.aaf.cadi.util.FQI; -import org.onap.aaf.misc.env.Trans; import aaf.v2_0.CredRequest; import junit.framework.Assert; @@ -68,7 +68,7 @@ public class JU_ServiceImpl_createUserCred extends JU_BaseServiceImpl { when(org.getIdentity(trans, cr.getId())).thenReturn(orgIdentity); when(orgIdentity.isFound()).thenReturn(true); final String ns = "org.onap.sample"; - when(question.userRoleDAO().read(trans, fqi, ns+".owner")).thenReturn(Result.ok(listOf(urData(fqi,ns,"owner",100)))); + whenRole(trans, fqi, ns, "owner", false, 100); when(question.nsDAO().read(trans, ns)).thenReturn(Result.ok(nsData(ns))); when(question.credDAO().readID(trans, cr.getId())).thenReturn(Result.ok(emptyList(CredDAO.Data.class))); when(question.credDAO().create(any(AuthzTrans.class), any(CredDAO.Data.class) )).thenReturn(Result.ok(credDataFound(cr,100))); @@ -89,9 +89,9 @@ public class JU_ServiceImpl_createUserCred extends JU_BaseServiceImpl { when(org.getIdentity(trans, cr.getId())).thenReturn(orgIdentity); when(orgIdentity.isFound()).thenReturn(true); final String ns = "org.onap.sample"; - when(question.userRoleDAO().read(trans, fqi, ns+".owner")).thenReturn(Result.ok(emptyList(UserRoleDAO.Data.class))); - when(question.userRoleDAO().read(trans, fqi, ns+".admin")).thenReturn(Result.ok(listOf(urData(fqi,ns,"admin",100)))); - when(question.nsDAO().read(trans, ns)).thenReturn(Result.ok(nsData(ns))); + whenRole(trans,fqi,ns,"owner",false, 100); + whenRole(trans,fqi,ns,"admin",true, 100); + when(question.nsDAO().read(trans, ns)).thenReturn(Result.ok(nsData(ns))); when(question.credDAO().readID(trans, cr.getId())).thenReturn(Result.ok(emptyList(CredDAO.Data.class))); when(question.credDAO().create(any(AuthzTrans.class), any(CredDAO.Data.class) )).thenReturn(Result.ok(credDataFound(cr,100))); when(question.credDAO().readNS(trans, ns)).thenReturn(Result.ok(listOf(credDataFound(cr,100)))); -- cgit 1.2.3-korg