Create Service JUnit Base Class, and first method update
[aaf/authz.git] / auth / auth-service / src / test / java / org / onap / aaf / auth / service / test / JU_BaseServiceImpl.java
index c9ebc28..5e6cfb6 100644 (file)
@@ -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<Nss, Perms, Pkey, Roles, Users, UserRoles, Delgs, Certs, Keys, Request, History, Error, Approvals> 
                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<List<UserRoleDAO.Data>> 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;