2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.service.test;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
28 import java.util.ArrayList;
29 import java.util.GregorianCalendar;
30 import java.util.List;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.Spy;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.aaf.auth.common.Define;
37 import org.onap.aaf.auth.dao.cached.CachedCertDAO;
38 import org.onap.aaf.auth.dao.cached.CachedCredDAO;
39 import org.onap.aaf.auth.dao.cached.CachedNSDAO;
40 import org.onap.aaf.auth.dao.cached.CachedPermDAO;
41 import org.onap.aaf.auth.dao.cached.CachedRoleDAO;
42 import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO;
43 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
44 import org.onap.aaf.auth.dao.cass.CacheInfoDAO;
45 import org.onap.aaf.auth.dao.cass.DelegateDAO;
46 import org.onap.aaf.auth.dao.cass.FutureDAO;
47 import org.onap.aaf.auth.dao.cass.HistoryDAO;
48 import org.onap.aaf.auth.dao.cass.LocateDAO;
49 import org.onap.aaf.auth.dao.cass.NsDAO;
50 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
51 import org.onap.aaf.auth.dao.hl.Question;
52 import org.onap.aaf.auth.env.AuthzEnv;
53 import org.onap.aaf.auth.env.AuthzTrans;
54 import org.onap.aaf.auth.layer.Result;
55 import org.onap.aaf.auth.service.AuthzCassServiceImpl;
56 import org.onap.aaf.auth.service.mapper.Mapper_2_0;
57 import org.onap.aaf.cadi.PropAccess;
58 import org.onap.aaf.cadi.config.Config;
59 import org.onap.aaf.org.DefaultOrg;
60 import org.onap.aaf.org.DefaultOrgIdentity;
62 import aaf.v2_0.Approvals;
63 import aaf.v2_0.Certs;
64 import aaf.v2_0.Delgs;
65 import aaf.v2_0.Error;
66 import aaf.v2_0.History;
69 import aaf.v2_0.Perms;
71 import aaf.v2_0.Request;
72 import aaf.v2_0.Roles;
73 import aaf.v2_0.UserRoles;
74 import aaf.v2_0.Users;
76 @RunWith(MockitoJUnitRunner.class)
77 public abstract class JU_BaseServiceImpl {
78 protected AuthzCassServiceImpl<Nss, Perms, Pkey, Roles, Users, UserRoles, Delgs, Certs, Keys, Request, History, Error, Approvals>
80 protected Mapper_2_0 mapper;
83 protected DefaultOrg org;
85 protected DefaultOrgIdentity orgIdentity;
88 // NOTE: Annotation format (@Mock and @Spy) do NOT seem to always work as a Base Class,
89 // so we construct manually.
92 protected HistoryDAO historyDAO = mock(HistoryDAO.class);
93 protected CacheInfoDAO cacheInfoDAO = mock(CacheInfoDAO.class);
94 protected CachedNSDAO nsDAO = mock(CachedNSDAO.class);
95 protected CachedPermDAO permDAO = mock(CachedPermDAO.class);
96 protected CachedRoleDAO roleDAO = mock(CachedRoleDAO.class);
97 protected CachedUserRoleDAO userRoleDAO = mock(CachedUserRoleDAO.class);
98 protected CachedCredDAO credDAO = mock(CachedCredDAO.class);
99 protected CachedCertDAO certDAO = mock(CachedCertDAO.class);
100 protected LocateDAO locateDAO = mock(LocateDAO.class);
101 protected FutureDAO futureDAO = mock(FutureDAO.class);
102 protected DelegateDAO delegateDAO = mock(DelegateDAO.class);
103 protected ApprovalDAO approvalDAO = mock(ApprovalDAO.class);
107 protected static PropAccess access = new PropAccess();
109 protected static AuthzEnv env = new AuthzEnv(access);
111 protected static AuthzTrans trans = env.newTransNoAvg();
113 // @Spy doesn't seem to work on Question.
115 protected Question question = spy(new Question(trans,
116 historyDAO,cacheInfoDAO,nsDAO,permDAO,
117 roleDAO,userRoleDAO,credDAO,certDAO,
118 locateDAO,futureDAO,delegateDAO,approvalDAO));
120 public void setUp() throws Exception {
121 when(trans.org()).thenReturn(org);
122 when(org.getDomain()).thenReturn("org.onap");
124 access.setProperty(Config.CADI_LATITUDE, "38.0");
125 access.setProperty(Config.CADI_LONGITUDE, "-72.0");
127 mapper = new Mapper_2_0(question);
128 acsi = new AuthzCassServiceImpl<>(trans, mapper, question);
132 // Common Data Objects
134 protected List<NsDAO.Data> nsData(String name) {
135 NsDAO.Data ndd = new NsDAO.Data();
137 int dot = name.lastIndexOf('.');
141 ndd.parent=name.substring(0,dot);
143 List<NsDAO.Data> rv = new ArrayList<NsDAO.Data>();
149 * Setup Role Data for Mock Usages
157 protected void whenRole(AuthzTrans trans, String user, String ns, String role, boolean exists, int days) {
158 Result<List<UserRoleDAO.Data>> result;
160 result = Result.ok(listOf(urData(user,ns,role,days)));
162 result = Result.ok(emptyList(UserRoleDAO.Data.class));
164 when(question.userRoleDAO().read(trans, user, ns+'.'+role)).thenReturn(result);
167 protected UserRoleDAO.Data urData(String user, String ns, String rname, int days) {
168 UserRoleDAO.Data urdd = new UserRoleDAO.Data();
172 urdd.role = ns + '.' + rname;
173 GregorianCalendar gc = new GregorianCalendar();
174 gc.add(GregorianCalendar.DAY_OF_YEAR, days);
175 urdd.expires = gc.getTime();
180 protected <T> List<T> listOf(T t) {
181 List<T> list = new ArrayList<>();
186 protected <T> List<T> emptyList(Class<T> cls) {
187 return new ArrayList<>();