Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cass / JU_UserRoleDAO.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.dao.cass;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
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;
38
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;
55
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;
60
61 public class JU_UserRoleDAO {
62
63     @Mock
64     AuthzTrans trans;
65     @Mock
66     Cluster cluster;
67     @Mock
68     Session session;
69     
70     @Before
71     public void setUp() throws APIException, IOException {
72         initMocks(this);
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");
81     }
82
83     @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);
90         try {
91             new UserRoleDAO(trans, cluster, "test");
92         } catch (APIException | IOException e) {
93             // TODO Auto-generated catch block
94             e.printStackTrace();
95         }
96 //        daoObj.
97     }
98     @Test
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;
106         try {
107             daoObj = new UserRoleDAO(trans, cluster, "test");
108         } catch (APIException | IOException e) {
109             // TODO Auto-generated catch block
110             e.printStackTrace();
111         }
112         
113         PSInfo psObj = Mockito.mock(PSInfo.class);
114         setPsByStartAndTarget(daoObj, psObj, "psByUser");
115         
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"});
118         
119         daoObj.readByUser(trans, "test");
120     }
121     @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;
129         try {
130             daoObj = new UserRoleDAO(trans, cluster, "test");
131         } catch (APIException | IOException e) {
132             // TODO Auto-generated catch block
133             e.printStackTrace();
134         }
135         
136         PSInfo psObj = Mockito.mock(PSInfo.class);
137         setPsByStartAndTarget(daoObj, psObj, "psByRole");
138         
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"});
141         
142         daoObj.readByRole(trans, "test");
143     }
144     @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;
152         try {
153             daoObj = new UserRoleDAO(trans, cluster, "test");
154         } catch (APIException | IOException e) {
155             // TODO Auto-generated catch block
156             e.printStackTrace();
157         }
158         
159         PSInfo psObj = Mockito.mock(PSInfo.class);
160         setPsByStartAndTarget(daoObj, psObj, "psUserInRole");
161         
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"});
164         
165         daoObj.readByUserRole(trans, "test","test");
166     }
167     
168     
169     public void setPsByStartAndTarget(UserRoleDAO UserRoleDAOObj, PSInfo psInfoObj, String fieldName) {
170         Field UserRoleDAOField;
171         try {
172             UserRoleDAOField = UserRoleDAO.class.getDeclaredField(fieldName);
173             
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);
179             
180             UserRoleDAOField.set(UserRoleDAOObj, psInfoObj);
181         } catch (NoSuchFieldException | SecurityException e) {
182             // TODO Auto-generated catch block
183             e.printStackTrace();
184         } catch (IllegalArgumentException e) {
185             // TODO Auto-generated catch block
186             e.printStackTrace();
187         } catch (IllegalAccessException e) {
188             // TODO Auto-generated catch block
189             e.printStackTrace();
190         }
191     }
192     
193     @Test
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);
205         
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());
210         
211         CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
212         Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg());
213         
214         UserRoleDAOImpl daoObj = null;
215         try {
216             daoObj = new UserRoleDAOImpl(trans, historyDAO, cacheInfoDAO, createPS );
217         } catch (APIException | IOException e) {
218             // TODO Auto-generated catch block
219             e.printStackTrace();
220         }
221         daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
222         
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"});
227
228         daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"});
229         daoObj.wasModified(trans, CRUD.delete, data, new String[] {});
230         
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[] {});
235         
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"});
239     }
240     
241     @Test
242     public void testSecondConstructor() {
243         HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
244         CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
245
246         UserRoleDAO daoObj = new UserRoleDAO(trans, historyDAO, cacheInfoDAO);
247         
248     }
249
250     @Test
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")) {
256                 innerClass = indCls;
257                 break;
258             }
259         }
260         
261         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
262         constructor.setAccessible(true);
263         
264         try {
265             
266             Object obj = constructor.newInstance(1);
267             Method innnerClassMtd;
268                 
269             UserRoleDAO.Data data  = new UserRoleDAO.Data();
270             Row row = Mockito.mock(Row.class);
271             ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
272             bbObj.limit(7);
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);
277             
278             innnerClassMtd = innerClass.getMethod("load", new Class[] {UserRoleDAO.Data.class, Row.class});
279             innnerClassMtd.invoke(obj, new Object[] {data, row});
280             
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"} });
283 //            
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"} });
286             
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 });
291
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 });
296             
297         } catch (InstantiationException e) {
298             // TODO Auto-generated catch block
299             e.printStackTrace();
300         } catch (IllegalAccessException e) {
301             // TODO Auto-generated catch block
302             e.printStackTrace();
303         } catch (IllegalArgumentException e) {
304             // TODO Auto-generated catch block
305             e.printStackTrace();
306         } catch (InvocationTargetException e) {
307             // TODO Auto-generated catch block
308             e.printStackTrace();
309         } catch (NoSuchMethodException e) {
310             // TODO Auto-generated catch block
311             e.printStackTrace();
312         } catch (SecurityException e) {
313             // TODO Auto-generated catch block
314             e.printStackTrace();
315         } 
316     }
317     
318     @Test
319     public void testData() {
320         UserRoleDAO.Data data = new UserRoleDAO.Data();
321         NsSplit nss = new NsSplit("test", "test");
322         data = new UserRoleDAO.Data();
323         
324         data.toString();
325         data.role("test", "test");
326         assertTrue("test".equals(data.ns));
327         
328         RoleDAO.Data rdd = new RoleDAO.Data();
329         rdd.ns="test";
330         data.role(rdd);
331         assertTrue("test".equals(data.ns));
332         
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");
336         
337         data.role(trans, q, "test");
338         
339         rs = new Result<NsSplit>(nss,1,"test",new Object[0]);
340         Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test");
341         
342         data.role(trans, q, "test");
343     }
344     
345 }
346
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");
351     }
352     
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);
357     }
358     
359
360     public void setPs(UserRoleDAOImpl UserRoleDAOObj, PSInfo psInfoObj, String methodName) {
361         Field UserRoleDAOField;
362         try {
363             UserRoleDAOField = CassDAOImpl.class.getDeclaredField(methodName);
364             
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);
370             
371             UserRoleDAOField.set(UserRoleDAOObj, psInfoObj);
372         } catch (NoSuchFieldException | SecurityException e) {
373             // TODO Auto-generated catch block
374             e.printStackTrace();
375         } catch (IllegalArgumentException e) {
376             // TODO Auto-generated catch block
377             e.printStackTrace();
378         } catch (IllegalAccessException e) {
379             // TODO Auto-generated catch block
380             e.printStackTrace();
381         }
382     }
383     
384     public void setSession(UserRoleDAOImpl approvalDaoObj, Session session) {
385         Field nsDaoField;
386         try {
387             nsDaoField = AbsCassDAO.class.getDeclaredField("session");
388             
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);
394             
395             nsDaoField.set(approvalDaoObj, session);
396         } catch (NoSuchFieldException | SecurityException e) {
397             // TODO Auto-generated catch block
398             e.printStackTrace();
399         } catch (IllegalArgumentException e) {
400             // TODO Auto-generated catch block
401             e.printStackTrace();
402         } catch (IllegalAccessException e) {
403             // TODO Auto-generated catch block
404             e.printStackTrace();
405         }
406     }
407
408 }