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.dao.cass;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.DataInputStream;
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Method;
36 import java.nio.ByteBuffer;
37 import java.util.List;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.onap.aaf.auth.dao.AbsCassDAO;
44 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
45 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
46 import org.onap.aaf.auth.dao.CassDAOImpl;
47 import org.onap.aaf.auth.dao.hl.Question;
48 import org.onap.aaf.auth.env.AuthzTrans;
49 import org.onap.aaf.auth.layer.Result;
50 import org.onap.aaf.cadi.config.Config;
51 import org.onap.aaf.misc.env.APIException;
52 import org.onap.aaf.misc.env.Env;
53 import org.onap.aaf.misc.env.LogTarget;
54 import org.onap.aaf.misc.env.TimeTaken;
56 import com.datastax.driver.core.Cluster;
57 import com.datastax.driver.core.ResultSet;
58 import com.datastax.driver.core.Row;
59 import com.datastax.driver.core.Session;
61 public class JU_UserRoleDAO {
71 public void setUp() throws APIException, IOException {
73 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
74 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
75 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
76 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
77 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
78 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE);
79 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE);
80 Mockito.doReturn(session).when(cluster).connect("test");
84 public void testInit() {
85 TimeTaken tt = Mockito.mock(TimeTaken.class);
86 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
87 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
88 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
89 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
91 new UserRoleDAO(trans, cluster, "test");
92 } catch (APIException | IOException e) {
93 // TODO Auto-generated catch block
99 public void testReadByUser() {
100 TimeTaken tt = Mockito.mock(TimeTaken.class);
101 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
102 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
103 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
104 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
105 UserRoleDAO daoObj = null;
107 daoObj = new UserRoleDAO(trans, cluster, "test");
108 } catch (APIException | IOException e) {
109 // TODO Auto-generated catch block
113 PSInfo psObj = Mockito.mock(PSInfo.class);
114 setPsByStartAndTarget(daoObj, psObj, "psByUser");
116 Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]);
117 Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"});
119 daoObj.readByUser(trans, "test");
122 public void testReadByRole() {
123 TimeTaken tt = Mockito.mock(TimeTaken.class);
124 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
125 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
126 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
127 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
128 UserRoleDAO daoObj = null;
130 daoObj = new UserRoleDAO(trans, cluster, "test");
131 } catch (APIException | IOException e) {
132 // TODO Auto-generated catch block
136 PSInfo psObj = Mockito.mock(PSInfo.class);
137 setPsByStartAndTarget(daoObj, psObj, "psByRole");
139 Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]);
140 Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"});
142 daoObj.readByRole(trans, "test");
145 public void testReadByUserRole() {
146 TimeTaken tt = Mockito.mock(TimeTaken.class);
147 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
148 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
149 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
150 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
151 UserRoleDAO daoObj = null;
153 daoObj = new UserRoleDAO(trans, cluster, "test");
154 } catch (APIException | IOException e) {
155 // TODO Auto-generated catch block
159 PSInfo psObj = Mockito.mock(PSInfo.class);
160 setPsByStartAndTarget(daoObj, psObj, "psUserInRole");
162 Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]);
163 Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"});
165 daoObj.readByUserRole(trans, "test","test");
169 public void setPsByStartAndTarget(UserRoleDAO UserRoleDAOObj, PSInfo psInfoObj, String fieldName) {
170 Field UserRoleDAOField;
172 UserRoleDAOField = UserRoleDAO.class.getDeclaredField(fieldName);
174 UserRoleDAOField.setAccessible(true);
175 // remove final modifier from field
176 Field modifiersField = Field.class.getDeclaredField("modifiers");
177 modifiersField.setAccessible(true);
178 // modifiersField.setInt(UserRoleDAOField, UserRoleDAOField.getModifiers() & ~Modifier.FINAL);
180 UserRoleDAOField.set(UserRoleDAOObj, psInfoObj);
181 } catch (NoSuchFieldException | SecurityException e) {
182 // TODO Auto-generated catch block
184 } catch (IllegalArgumentException e) {
185 // TODO Auto-generated catch block
187 } catch (IllegalAccessException e) {
188 // TODO Auto-generated catch block
194 public void testWasMOdified() {
195 TimeTaken tt = Mockito.mock(TimeTaken.class);
196 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
197 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
198 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
199 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
200 Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE);
201 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
202 Mockito.doNothing().when(tt).done();
203 UserRoleDAO.Data data = new UserRoleDAO.Data();
204 PSInfo createPS = Mockito.mock(PSInfo.class);
206 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
207 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new Object[0]);
208 Result<Void> rs2 = new Result<Void>(null,0,"test",new Object[0]);
209 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
211 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
212 Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg());
214 UserRoleDAOImpl daoObj = null;
216 daoObj = new UserRoleDAOImpl(trans, historyDAO, cacheInfoDAO, createPS );
217 } catch (APIException | IOException e) {
218 // TODO Auto-generated catch block
221 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
223 daoObj.wasModified(trans, CRUD.update, data, new String[] {});
224 daoObj.wasModified(trans, CRUD.create, data, new String[] {null});
225 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null});
226 daoObj.wasModified(trans, CRUD.update, data, new String[] {"test","test"});
228 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"});
229 daoObj.wasModified(trans, CRUD.delete, data, new String[] {});
231 rs2 = new Result<Void>(null,1,"test",new Object[0]);
232 Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg());
233 daoObj.wasModified(trans, CRUD.read, data, new String[] {"test","test"});
234 daoObj.wasModified(trans, CRUD.read, data, new String[] {});
236 rs1 = new Result<ResultSet>(null,1,"test",new String[0]);
237 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
238 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"});
242 public void testSecondConstructor() {
243 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
244 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
246 UserRoleDAO daoObj = new UserRoleDAO(trans, historyDAO, cacheInfoDAO);
251 public void testFutureLoader(){
252 Class<?> innerClass = null;
253 Class<?>[] innerClassArr = UserRoleDAO.class.getDeclaredClasses();
254 for(Class indCls:innerClassArr) {
255 if(indCls.getName().contains("URLoader")) {
261 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
262 constructor.setAccessible(true);
266 Object obj = constructor.newInstance(1);
267 Method innnerClassMtd;
269 UserRoleDAO.Data data = new UserRoleDAO.Data();
270 Row row = Mockito.mock(Row.class);
271 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
273 bbObj.put(0, new Byte("0"));
274 bbObj.put(1, new Byte("1"));
275 bbObj.put(2, new Byte("2"));
276 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
278 innnerClassMtd = innerClass.getMethod("load", new Class[] {UserRoleDAO.Data.class, Row.class});
279 innnerClassMtd.invoke(obj, new Object[] {data, row});
281 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {UserRoleDAO.Data.class, Integer.TYPE, Object[].class });
282 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test"} });
284 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {UserRoleDAO.Data.class, Integer.TYPE, Object[].class });
285 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
287 ByteArrayOutputStream baos = new ByteArrayOutputStream();
288 DataOutputStream dos = new DataOutputStream(baos);
289 innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {UserRoleDAO.Data.class, DataOutputStream.class });
290 innnerClassMtd.invoke(obj, new Object[] {data, dos });
292 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
293 DataInputStream dis = new DataInputStream(bais);
294 innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {UserRoleDAO.Data.class, DataInputStream.class });
295 innnerClassMtd.invoke(obj, new Object[] {data, dis });
297 } catch (InstantiationException e) {
298 // TODO Auto-generated catch block
300 } catch (IllegalAccessException e) {
301 // TODO Auto-generated catch block
303 } catch (IllegalArgumentException e) {
304 // TODO Auto-generated catch block
306 } catch (InvocationTargetException e) {
307 // TODO Auto-generated catch block
309 } catch (NoSuchMethodException e) {
310 // TODO Auto-generated catch block
312 } catch (SecurityException e) {
313 // TODO Auto-generated catch block
319 public void testData() {
320 UserRoleDAO.Data data = new UserRoleDAO.Data();
321 NsSplit nss = new NsSplit("test", "test");
322 data = new UserRoleDAO.Data();
325 data.role("test", "test");
326 assertTrue("test".equals(data.ns));
328 RoleDAO.Data rdd = new RoleDAO.Data();
331 assertTrue("test".equals(data.ns));
333 Question q = Mockito.mock( Question.class);
334 Result<NsSplit> rs = new Result<NsSplit>(nss,0,"test",new Object[0]);
335 Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test");
337 data.role(trans, q, "test");
339 rs = new Result<NsSplit>(nss,1,"test",new Object[0]);
340 Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test");
342 data.role(trans, q, "test");
347 class UserRoleDAOImpl extends UserRoleDAO{
348 public UserRoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS) throws APIException, IOException {
349 super(trans, historyDAO, cacheDao);
350 setPs(this, readPS, "createPS");
353 public UserRoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS, Session session ) throws APIException, IOException {
354 super(trans, historyDAO, cacheDao);
355 setPs(this, readPS, "createPS");
356 setSession(this, session);
360 public void setPs(UserRoleDAOImpl UserRoleDAOObj, PSInfo psInfoObj, String methodName) {
361 Field UserRoleDAOField;
363 UserRoleDAOField = CassDAOImpl.class.getDeclaredField(methodName);
365 UserRoleDAOField.setAccessible(true);
366 // remove final modifier from field
367 Field modifiersField = Field.class.getDeclaredField("modifiers");
368 modifiersField.setAccessible(true);
369 // modifiersField.setInt(UserRoleDAOField, UserRoleDAOField.getModifiers() & ~Modifier.FINAL);
371 UserRoleDAOField.set(UserRoleDAOObj, psInfoObj);
372 } catch (NoSuchFieldException | SecurityException e) {
373 // TODO Auto-generated catch block
375 } catch (IllegalArgumentException e) {
376 // TODO Auto-generated catch block
378 } catch (IllegalAccessException e) {
379 // TODO Auto-generated catch block
384 public void setSession(UserRoleDAOImpl approvalDaoObj, Session session) {
387 nsDaoField = AbsCassDAO.class.getDeclaredField("session");
389 nsDaoField.setAccessible(true);
390 // remove final modifier from field
391 Field modifiersField = Field.class.getDeclaredField("modifiers");
392 modifiersField.setAccessible(true);
393 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
395 nsDaoField.set(approvalDaoObj, session);
396 } catch (NoSuchFieldException | SecurityException e) {
397 // TODO Auto-generated catch block
399 } catch (IllegalArgumentException e) {
400 // TODO Auto-generated catch block
402 } catch (IllegalAccessException e) {
403 // TODO Auto-generated catch block