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.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.Field;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.nio.ByteBuffer;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.TreeSet;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.onap.aaf.auth.dao.AbsCassDAO;
47 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
48 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
49 import org.onap.aaf.auth.dao.CassAccess;
50 import org.onap.aaf.auth.dao.CassDAOImpl;
51 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
52 import org.onap.aaf.auth.dao.hl.Question;
53 import org.onap.aaf.auth.env.AuthzTrans;
54 import org.onap.aaf.auth.layer.Result;
55 import org.onap.aaf.cadi.config.Config;
56 import org.onap.aaf.misc.env.APIException;
57 import org.onap.aaf.misc.env.Env;
58 import org.onap.aaf.misc.env.LogTarget;
59 import org.onap.aaf.misc.env.TimeTaken;
61 import com.datastax.driver.core.Cluster;
62 import com.datastax.driver.core.ResultSet;
63 import com.datastax.driver.core.Row;
64 import com.datastax.driver.core.Session;
66 public class JU_UserRoleDAO {
76 public void setUp() throws APIException, IOException {
78 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
79 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
80 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
81 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
82 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
83 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE);
84 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE);
85 Mockito.doReturn(session).when(cluster).connect("test");
89 public void testInit() {
90 TimeTaken tt = Mockito.mock(TimeTaken.class);
91 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
92 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
93 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
94 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
96 new UserRoleDAO(trans, cluster, "test");
97 } catch (APIException | IOException e) {
98 // TODO Auto-generated catch block
104 public void testReadByUser() {
105 TimeTaken tt = Mockito.mock(TimeTaken.class);
106 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
107 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
108 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
109 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
110 UserRoleDAO daoObj = null;
112 daoObj = new UserRoleDAO(trans, cluster, "test");
113 } catch (APIException | IOException e) {
114 // TODO Auto-generated catch block
118 PSInfo psObj = Mockito.mock(PSInfo.class);
119 setPsByStartAndTarget(daoObj, psObj, "psByUser");
121 Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]);
122 Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"});
124 daoObj.readByUser(trans, "test");
127 public void testReadByRole() {
128 TimeTaken tt = Mockito.mock(TimeTaken.class);
129 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
130 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
131 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
132 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
133 UserRoleDAO daoObj = null;
135 daoObj = new UserRoleDAO(trans, cluster, "test");
136 } catch (APIException | IOException e) {
137 // TODO Auto-generated catch block
141 PSInfo psObj = Mockito.mock(PSInfo.class);
142 setPsByStartAndTarget(daoObj, psObj, "psByRole");
144 Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]);
145 Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"});
147 daoObj.readByRole(trans, "test");
150 public void testReadByUserRole() {
151 TimeTaken tt = Mockito.mock(TimeTaken.class);
152 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
153 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
154 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
155 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
156 UserRoleDAO daoObj = null;
158 daoObj = new UserRoleDAO(trans, cluster, "test");
159 } catch (APIException | IOException e) {
160 // TODO Auto-generated catch block
164 PSInfo psObj = Mockito.mock(PSInfo.class);
165 setPsByStartAndTarget(daoObj, psObj, "psUserInRole");
167 Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]);
168 Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"});
170 daoObj.readByUserRole(trans, "test","test");
174 public void setPsByStartAndTarget(UserRoleDAO UserRoleDAOObj, PSInfo psInfoObj, String fieldName) {
175 Field UserRoleDAOField;
177 UserRoleDAOField = UserRoleDAO.class.getDeclaredField(fieldName);
179 UserRoleDAOField.setAccessible(true);
180 // remove final modifier from field
181 Field modifiersField = Field.class.getDeclaredField("modifiers");
182 modifiersField.setAccessible(true);
183 // modifiersField.setInt(UserRoleDAOField, UserRoleDAOField.getModifiers() & ~Modifier.FINAL);
185 UserRoleDAOField.set(UserRoleDAOObj, psInfoObj);
186 } catch (NoSuchFieldException | SecurityException e) {
187 // TODO Auto-generated catch block
189 } catch (IllegalArgumentException e) {
190 // TODO Auto-generated catch block
192 } catch (IllegalAccessException e) {
193 // TODO Auto-generated catch block
199 public void testWasMOdified() {
200 TimeTaken tt = Mockito.mock(TimeTaken.class);
201 Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE);
202 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
203 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
204 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB);
205 Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE);
206 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
207 Mockito.doNothing().when(tt).done();
208 UserRoleDAO.Data data = new UserRoleDAO.Data();
209 PSInfo createPS = Mockito.mock(PSInfo.class);
211 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
212 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new Object[0]);
213 Result<Void> rs2 = new Result<Void>(null,0,"test",new Object[0]);
214 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
216 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
217 Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg());
219 UserRoleDAOImpl daoObj = null;
221 daoObj = new UserRoleDAOImpl(trans, historyDAO, cacheInfoDAO, createPS );
222 } catch (APIException | IOException e) {
223 // TODO Auto-generated catch block
226 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
228 daoObj.wasModified(trans, CRUD.update, data, new String[] {});
229 daoObj.wasModified(trans, CRUD.create, data, new String[] {null});
230 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null});
231 daoObj.wasModified(trans, CRUD.update, data, new String[] {"test","test"});
233 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"});
234 daoObj.wasModified(trans, CRUD.delete, data, new String[] {});
236 rs2 = new Result<Void>(null,1,"test",new Object[0]);
237 Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg());
238 daoObj.wasModified(trans, CRUD.read, data, new String[] {"test","test"});
239 daoObj.wasModified(trans, CRUD.read, data, new String[] {});
241 rs1 = new Result<ResultSet>(null,1,"test",new String[0]);
242 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
243 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"});
247 public void testSecondConstructor() {
248 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
249 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
251 UserRoleDAO daoObj = new UserRoleDAO(trans, historyDAO, cacheInfoDAO);
256 public void testFutureLoader(){
257 Class<?> innerClass = null;
258 Class<?>[] innerClassArr = UserRoleDAO.class.getDeclaredClasses();
259 for(Class indCls:innerClassArr) {
260 if(indCls.getName().contains("URLoader")) {
266 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
267 constructor.setAccessible(true);
271 Object obj = constructor.newInstance(1);
272 Method innnerClassMtd;
274 UserRoleDAO.Data data = new UserRoleDAO.Data();
275 Row row = Mockito.mock(Row.class);
276 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
278 bbObj.put(0, new Byte("0"));
279 bbObj.put(1, new Byte("1"));
280 bbObj.put(2, new Byte("2"));
281 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
283 innnerClassMtd = innerClass.getMethod("load", new Class[] {UserRoleDAO.Data.class, Row.class});
284 innnerClassMtd.invoke(obj, new Object[] {data, row});
286 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {UserRoleDAO.Data.class, Integer.TYPE, Object[].class });
287 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test"} });
289 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {UserRoleDAO.Data.class, Integer.TYPE, Object[].class });
290 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
292 DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_UserRoleDAOTest.java"));
293 innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {UserRoleDAO.Data.class, DataOutputStream.class });
294 innnerClassMtd.invoke(obj, new Object[] {data, dos });
296 DataInputStream dis = new DataInputStream(new FileInputStream("JU_UserRoleDAOTest.java"));
297 innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {UserRoleDAO.Data.class, DataInputStream.class });
298 innnerClassMtd.invoke(obj, new Object[] {data, dis });
300 } catch (InstantiationException e) {
301 // TODO Auto-generated catch block
303 } catch (IllegalAccessException e) {
304 // TODO Auto-generated catch block
306 } catch (IllegalArgumentException e) {
307 // TODO Auto-generated catch block
309 } catch (InvocationTargetException e) {
310 // TODO Auto-generated catch block
312 } catch (NoSuchMethodException e) {
313 // TODO Auto-generated catch block
315 } catch (SecurityException e) {
316 // TODO Auto-generated catch block
318 } catch (FileNotFoundException e) {
319 // TODO Auto-generated catch block
325 public void testData() {
326 UserRoleDAO.Data data = new UserRoleDAO.Data();
327 NsSplit nss = new NsSplit("test", "test");
328 data = new UserRoleDAO.Data();
331 data.role("test", "test");
332 assertTrue("test".equals(data.ns));
334 RoleDAO.Data rdd = new RoleDAO.Data();
337 assertTrue("test".equals(data.ns));
339 Question q = Mockito.mock( Question.class);
340 Result<NsSplit> rs = new Result<NsSplit>(nss,0,"test",new Object[0]);
341 Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test");
343 data.role(trans, q, "test");
345 rs = new Result<NsSplit>(nss,1,"test",new Object[0]);
346 Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test");
348 data.role(trans, q, "test");
353 class UserRoleDAOImpl extends UserRoleDAO{
354 public UserRoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS) throws APIException, IOException {
355 super(trans, historyDAO, cacheDao);
356 setPs(this, readPS, "createPS");
359 public UserRoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS, Session session ) throws APIException, IOException {
360 super(trans, historyDAO, cacheDao);
361 setPs(this, readPS, "createPS");
362 setSession(this, session);
366 public void setPs(UserRoleDAOImpl UserRoleDAOObj, PSInfo psInfoObj, String methodName) {
367 Field UserRoleDAOField;
369 UserRoleDAOField = CassDAOImpl.class.getDeclaredField(methodName);
371 UserRoleDAOField.setAccessible(true);
372 // remove final modifier from field
373 Field modifiersField = Field.class.getDeclaredField("modifiers");
374 modifiersField.setAccessible(true);
375 // modifiersField.setInt(UserRoleDAOField, UserRoleDAOField.getModifiers() & ~Modifier.FINAL);
377 UserRoleDAOField.set(UserRoleDAOObj, psInfoObj);
378 } catch (NoSuchFieldException | SecurityException e) {
379 // TODO Auto-generated catch block
381 } catch (IllegalArgumentException e) {
382 // TODO Auto-generated catch block
384 } catch (IllegalAccessException e) {
385 // TODO Auto-generated catch block
390 public void setSession(UserRoleDAOImpl approvalDaoObj, Session session) {
393 nsDaoField = AbsCassDAO.class.getDeclaredField("session");
395 nsDaoField.setAccessible(true);
396 // remove final modifier from field
397 Field modifiersField = Field.class.getDeclaredField("modifiers");
398 modifiersField.setAccessible(true);
399 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
401 nsDaoField.set(approvalDaoObj, session);
402 } catch (NoSuchFieldException | SecurityException e) {
403 // TODO Auto-generated catch block
405 } catch (IllegalArgumentException e) {
406 // TODO Auto-generated catch block
408 } catch (IllegalAccessException e) {
409 // TODO Auto-generated catch block