Add new JUnit Structure
[aaf/authz.git] / auth / auth-service / src / test / java / org / onap / aaf / auth / service / test / JU_BaseServiceImpl.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.service.test;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.util.ArrayList;
27 import java.util.GregorianCalendar;
28 import java.util.List;
29
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Spy;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.onap.aaf.auth.common.Define;
35 import org.onap.aaf.auth.dao.cached.CachedCertDAO;
36 import org.onap.aaf.auth.dao.cached.CachedCredDAO;
37 import org.onap.aaf.auth.dao.cached.CachedNSDAO;
38 import org.onap.aaf.auth.dao.cached.CachedPermDAO;
39 import org.onap.aaf.auth.dao.cached.CachedRoleDAO;
40 import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO;
41 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
42 import org.onap.aaf.auth.dao.cass.CacheInfoDAO;
43 import org.onap.aaf.auth.dao.cass.DelegateDAO;
44 import org.onap.aaf.auth.dao.cass.FutureDAO;
45 import org.onap.aaf.auth.dao.cass.HistoryDAO;
46 import org.onap.aaf.auth.dao.cass.LocateDAO;
47 import org.onap.aaf.auth.dao.cass.NsDAO;
48 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
49 import org.onap.aaf.auth.dao.hl.Question;
50 import org.onap.aaf.auth.env.AuthzEnv;
51 import org.onap.aaf.auth.env.AuthzTrans;
52 import org.onap.aaf.auth.service.AuthzCassServiceImpl;
53 import org.onap.aaf.auth.service.mapper.Mapper_2_0;
54 import org.onap.aaf.cadi.PropAccess;
55 import org.onap.aaf.cadi.config.Config;
56 import org.onap.aaf.org.DefaultOrg;
57 import org.onap.aaf.org.DefaultOrgIdentity;
58
59 import aaf.v2_0.Approvals;
60 import aaf.v2_0.Certs;
61 import aaf.v2_0.Delgs;
62 import aaf.v2_0.Error;
63 import aaf.v2_0.History;
64 import aaf.v2_0.Keys;
65 import aaf.v2_0.Nss;
66 import aaf.v2_0.Perms;
67 import aaf.v2_0.Pkey;
68 import aaf.v2_0.Request;
69 import aaf.v2_0.Roles;
70 import aaf.v2_0.UserRoles;
71 import aaf.v2_0.Users;
72
73 @RunWith(MockitoJUnitRunner.class)
74 public abstract class JU_BaseServiceImpl {
75         protected AuthzCassServiceImpl<Nss, Perms, Pkey, Roles, Users, UserRoles, Delgs, Certs, Keys, Request, History, Error, Approvals> 
76                 acsi;
77         protected Mapper_2_0 mapper;
78         
79     @Mock
80     protected DefaultOrg org;
81     @Mock
82     protected DefaultOrgIdentity orgIdentity;
83         
84     protected HistoryDAO historyDAO = mock(HistoryDAO.class);
85     protected CacheInfoDAO cacheInfoDAO = mock(CacheInfoDAO.class);
86     protected CachedNSDAO nsDAO = mock(CachedNSDAO.class);
87     protected CachedPermDAO permDAO = mock(CachedPermDAO.class);
88     protected CachedRoleDAO roleDAO = mock(CachedRoleDAO.class);
89     protected CachedUserRoleDAO userRoleDAO = mock(CachedUserRoleDAO.class);
90     protected CachedCredDAO credDAO = mock(CachedCredDAO.class);
91     protected CachedCertDAO certDAO = mock(CachedCertDAO.class);
92     protected LocateDAO locateDAO = mock(LocateDAO.class);
93     protected FutureDAO futureDAO = mock(FutureDAO.class);
94     protected DelegateDAO delegateDAO = mock(DelegateDAO.class);
95     protected ApprovalDAO approvalDAO = mock(ApprovalDAO.class);
96         
97     @Spy
98     protected static PropAccess access = new PropAccess();
99     
100     @Spy
101         protected static AuthzEnv env = new AuthzEnv(access);
102         
103     @Spy
104     protected static AuthzTrans trans = env.newTransNoAvg();
105     
106
107     @Spy
108     protected Question question = new Question(trans,historyDAO,cacheInfoDAO,nsDAO,permDAO,roleDAO,userRoleDAO,
109                 credDAO,certDAO,locateDAO,futureDAO,delegateDAO,approvalDAO);
110     
111         public void setUp() throws Exception {
112             when(trans.org()).thenReturn(org);
113             when(org.getDomain()).thenReturn("org.onap");
114             Define.set(access);
115                 access.setProperty(Config.CADI_LATITUDE, "38.0");
116                 access.setProperty(Config.CADI_LONGITUDE, "-72.0");
117
118             mapper = new Mapper_2_0(question);
119                 acsi = new AuthzCassServiceImpl<>(trans, mapper, question);
120         }
121         
122         //////////
123         //  Common Data Objects
124         /////////
125     protected List<NsDAO.Data> nsData(String name) {
126         NsDAO.Data ndd = new NsDAO.Data();
127         ndd.name=name;
128         int dot = name.lastIndexOf('.');
129         if(dot<0) {
130                 ndd.parent=".";
131         } else {
132                 ndd.parent=name.substring(0,dot);
133         }
134         List<NsDAO.Data> rv = new ArrayList<NsDAO.Data>();
135         rv.add(ndd);
136         return rv;
137     }
138     
139     protected UserRoleDAO.Data urData(String user, String ns, String rname, int days) {
140         UserRoleDAO.Data urdd = new UserRoleDAO.Data();
141         urdd.user = user;
142         urdd.ns = ns;
143         urdd.rname = rname;
144         urdd.role = ns + '.' + rname;
145         GregorianCalendar gc = new GregorianCalendar();
146         gc.add(GregorianCalendar.DAY_OF_YEAR, days);
147         urdd.expires = gc.getTime();
148         return urdd;
149     }
150
151
152     protected <T> List<T> listOf(T t) {
153         List<T> list = new ArrayList<>();
154         list.add(t);
155         return list;
156     }
157     
158     protected <T> List<T> emptyList(Class<T> cls) {
159         return new ArrayList<>();
160     }
161
162 }