Post Init Service Starter
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cass / JU_CacheInfoDAO.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.IOException;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Field;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.onap.aaf.auth.dao.AbsCassDAO;
42 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
43 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
44 import org.onap.aaf.auth.dao.cass.CacheInfoDAO.Data;
45 import org.onap.aaf.auth.env.AuthzEnv;
46 import org.onap.aaf.auth.env.AuthzTrans;
47 import org.onap.aaf.auth.layer.Result;
48 import org.onap.aaf.cadi.CadiException;
49 import org.onap.aaf.cadi.SecuritySetter;
50 import org.onap.aaf.cadi.client.Rcli;
51 import org.onap.aaf.cadi.config.Config;
52 import org.onap.aaf.cadi.http.HMangr;
53 import org.onap.aaf.misc.env.APIException;
54 import org.onap.aaf.misc.env.Env;
55 import org.onap.aaf.misc.env.LogTarget;
56 import org.onap.aaf.misc.env.TimeTaken;
57
58 import com.datastax.driver.core.Cluster;
59 import com.datastax.driver.core.ColumnDefinitions;
60 import com.datastax.driver.core.PreparedId;
61 import com.datastax.driver.core.PreparedStatement;
62 import com.datastax.driver.core.ResultSet;
63 import com.datastax.driver.core.Row;
64 import com.datastax.driver.core.Session;
65 import com.datastax.driver.core.Statement;
66 import com.datastax.driver.core.exceptions.DriverException;
67
68 public class JU_CacheInfoDAO {
69
70         @Mock
71     AuthzTrans trans;
72         @Mock
73         Cluster cluster;
74         @Mock
75         Session session;
76         @Mock
77         AuthzEnv env;
78         @Mock
79         LogTarget logTarget;
80         
81         @Before
82         public void setUp() throws APIException, IOException {
83                 initMocks(this);
84                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
85                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
86                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
87                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
88                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
89                 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE);
90                 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE);
91                 Mockito.doReturn(session).when(cluster).connect("test");
92         }
93         
94         @Test
95         public void testInit() {
96                 TimeTaken tt = Mockito.mock(TimeTaken.class);
97                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
98                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
99                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
100                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
101                 Mockito.doNothing().when(tt).done();
102                 CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
103                 PSInfo createPS = Mockito.mock(PSInfo.class);
104                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
105                 Mockito.doReturn(rs).when(createPS).exec(trans, "CacheInfoDAOImpl CREATE", data);
106                 
107                 CacheInfoDAOImpl daoObj=null;
108                 try {
109                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
110                 } catch (APIException | IOException e) {
111                         // TODO Auto-generated catch block
112                         e.printStackTrace();
113                 }
114 //              data.id
115                 Result<Data> retVal = daoObj.create(trans, data);
116                 assertTrue(retVal.status == 0);
117         }
118         
119         @Test
120         public void testTouch() {
121                 TimeTaken tt = Mockito.mock(TimeTaken.class);
122                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
123                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO Touch segments test: 1", Env.REMOTE);
124                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO Touch segments test: 1,2", 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 CacheInfoDAO", Env.SUB);
128                 Mockito.doNothing().when(tt).done();
129
130                 Mockito.doReturn(logTarget).when(env).debug();
131                 
132                 CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
133                 PSInfo createPS = Mockito.mock(PSInfo.class);
134                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
135                 Mockito.doReturn(rs).when(createPS).exec(trans, "CacheInfoDAOImpl CREATE", data);
136                 
137                 CacheInfoDAOImpl daoObj=null;
138                 try {
139                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
140                 } catch (APIException | IOException e) {
141                         // TODO Auto-generated catch block
142                         e.printStackTrace();
143                 }
144 //              data.id
145                 Result<Void> retVal = daoObj.touch(trans, "test", 1, 2);
146                 assertTrue(retVal.status == 0);
147                 Mockito.doThrow(DriverException.class).when(session).executeAsync(Mockito.anyString());
148                 daoObj.startUpdate(env, Mockito.mock(HMangr.class), Mockito.mock(SecuritySetter.class), "12.0.0.1", 8080);
149                 retVal = daoObj.touch(trans, "test", 1, 2);
150                 
151                 
152                 try {
153                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
154                 } catch (APIException | IOException e) {
155                         // TODO Auto-generated catch block
156                         e.printStackTrace();
157                 }
158                 assertTrue(retVal.status == Result.ERR_Backend);
159                 Mockito.doThrow(APIException.class).when(session).executeAsync(Mockito.anyString());
160                 retVal = daoObj.touch(trans, "test", 1, 2);
161                 try {
162                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
163                 } catch (APIException | IOException e) {
164                         // TODO Auto-generated catch block
165                         e.printStackTrace();
166                 }
167                 assertTrue(retVal.status == Result.ERR_Backend);
168                 Mockito.doThrow(IOException.class).when(session).executeAsync(Mockito.anyString());
169                 try {
170                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
171                 } catch (APIException | IOException e) {
172                         // TODO Auto-generated catch block
173                         e.printStackTrace();
174                 }
175                 retVal = daoObj.touch(trans, "test", 1, 2);
176                 assertTrue(retVal.status == Result.ERR_Backend);
177         }
178         
179         @Test
180         public void testCheck() {
181                 TimeTaken tt = Mockito.mock(TimeTaken.class);
182                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
183                 Mockito.doReturn(tt).when(trans).start("Check Table Timestamps", Env.REMOTE);
184                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
185                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
186                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
187                 Mockito.doNothing().when(tt).done();
188                 CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
189                 PSInfo createPS = Mockito.mock(PSInfo.class);
190                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
191                 Mockito.doReturn(rs).when(createPS).exec(trans, "CacheInfoDAOImpl CREATE", data);
192                 
193                 CacheInfoDAOImpl daoObj=null;
194                 PreparedStatement ps = Mockito.mock(PreparedStatement.class);
195                 Mockito.doReturn(ps).when(session).prepare(Mockito.anyString());
196                 Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(ps).getVariables();
197                 Mockito.doReturn(Mockito.mock(PreparedId.class)).when(ps).getPreparedId();
198                 try {
199                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
200                 } catch (APIException | IOException e) {
201                         // TODO Auto-generated catch block
202                         e.printStackTrace();
203                 }
204 //              data.id
205                 ResultSet resultSet = Mockito.mock(ResultSet.class);
206                 List<Row> rowList = new ArrayList<>();
207                 Mockito.doReturn(rowList).when(resultSet).all();
208                 Mockito.doReturn(resultSet).when(session).execute(Mockito.any(Statement.class));
209                 Result<Void> retVal = daoObj.check(trans);
210                 assertTrue(retVal.status == 0);
211                 
212                 Row row  = Mockito.mock(Row.class);
213                 Mockito.doReturn("test").when(row).getString(Mockito.anyInt());
214                 rowList.add(row);
215                 row  = Mockito.mock(Row.class);
216                 Mockito.doReturn("test").when(row).getString(Mockito.anyInt());
217                 Mockito.doReturn(100).when(row).getInt(Mockito.anyInt());
218                 rowList.add(row);
219                 retVal = daoObj.check(trans);
220                 assertTrue(retVal.status == 0);
221
222                 Mockito.doThrow(DriverException.class).when(session).execute(Mockito.any(Statement.class));
223                 retVal = daoObj.check(trans);
224                 assertTrue(retVal.status == Result.ERR_Backend);
225                 try {
226                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
227                 } catch (APIException | IOException e) {
228                         // TODO Auto-generated catch block
229                         e.printStackTrace();
230                 }
231                 Mockito.doThrow(APIException.class).when(session).execute(Mockito.any(Statement.class));
232                 retVal = daoObj.check(trans);
233                 assertTrue(retVal.status == Result.ERR_Backend);
234                 try {
235                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
236                 } catch (APIException | IOException e) {
237                         // TODO Auto-generated catch block
238                         e.printStackTrace();
239                 }
240                 Mockito.doThrow(IOException.class).when(session).execute(Mockito.any(Statement.class));
241                 retVal = daoObj.check(trans);
242                 assertTrue(retVal.status == Result.ERR_Backend);
243         }
244         @Test
245         public void testStopUpdate() {
246                 
247                 CacheInfoDAO.stopUpdate();
248                 
249         }
250
251
252 /*
253  *      This has intermittent Null Pointers, JUnit only
254         @Test
255         public void testGet() {
256                 TimeTaken tt = Mockito.mock(TimeTaken.class);
257                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
258                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO Touch segments test1: 1011", Env.REMOTE);
259                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
260                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
261                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
262                 Mockito.doNothing().when(tt).done();
263                 CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
264                 PSInfo createPS = Mockito.mock(PSInfo.class);
265                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
266                 Mockito.doReturn(rs).when(createPS).exec(trans, "CacheInfoDAOImpl CREATE", data);
267                 
268                 CacheInfoDAOImpl daoObj=null;
269                 try {
270                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test",data, createPS);
271                         Date retVal = daoObj.get(trans, "test", 1011);
272 //                      assertTrue(retVal.status == 0);
273                         retVal = daoObj.get(trans, "test1", 1011);
274                 } catch (APIException | IOException e) {
275                         e.printStackTrace();
276                 }
277         }
278 */
279         
280         @Test
281         public void testWasMOdified() {
282                 TimeTaken tt = Mockito.mock(TimeTaken.class);
283                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
284                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
285                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
286                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
287                 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
288                 Mockito.doNothing().when(tt).done();
289                 CacheInfoDAO.Data data  = new CacheInfoDAO.Data("test",1);
290                 PSInfo createPS = Mockito.mock(PSInfo.class);
291                 
292                 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
293                 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]);
294                 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
295                 
296                 CacheInfoDAOImpl daoObj=null;
297                 try {
298                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test", createPS);
299                 } catch (APIException | IOException e) {
300                         // TODO Auto-generated catch block
301                         e.printStackTrace();
302                 }
303                 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
304                 
305                 
306         }
307         
308         @Test
309         public void testInfoLoader(){
310                 TimeTaken tt = Mockito.mock(TimeTaken.class);
311                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
312                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO Touch segments test1: 1011", Env.REMOTE);
313                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
314                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
315                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
316                 Mockito.doNothing().when(tt).done();
317                 CacheInfoDAO.Data data1  = new CacheInfoDAO.Data();
318                 PSInfo createPS = Mockito.mock(PSInfo.class);
319                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
320                 Mockito.doReturn(rs).when(createPS).exec(trans, "CacheInfoDAOImpl CREATE", data1);
321
322                 CacheInfoDAOImpl daoObj=null;
323                 try {
324                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test", createPS);
325                 } catch (APIException | IOException e) {
326                         // TODO Auto-generated catch block
327                         e.printStackTrace();
328                 }
329                 Class[] classList = CacheInfoDAO.class.getDeclaredClasses();
330                 Class<?> innerClass = null;
331                 for(Class indCls:classList) {
332                         if(indCls.getName().contains("InfoLoader")) {
333                                 innerClass = indCls;
334                         }
335                 }
336         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
337         constructor.setAccessible(true);
338         try {
339                         Object obj = constructor.newInstance(1);
340                         Method innnerClassMtd;
341                                 
342                         CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
343                         Row row = Mockito.mock(Row.class);
344                         innnerClassMtd = innerClass.getMethod("load", new Class[] {CacheInfoDAO.Data.class, Row.class});
345                         innnerClassMtd.invoke(obj, new Object[] {data, row});
346                         
347                         innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {CacheInfoDAO.Data.class, Integer.TYPE, Object[].class });
348                         innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"}});
349
350                         innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {CacheInfoDAO.Data.class, Integer.TYPE, Object[].class });
351                         innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"}});
352                 } catch (InstantiationException e) {
353                         // TODO Auto-generated catch block
354                         e.printStackTrace();
355                 } catch (IllegalAccessException e) {
356                         // TODO Auto-generated catch block
357                         e.printStackTrace();
358                 } catch (IllegalArgumentException e) {
359                         // TODO Auto-generated catch block
360                         e.printStackTrace();
361                 } catch (InvocationTargetException e) {
362                         // TODO Auto-generated catch block
363                         e.printStackTrace();
364                 } catch (NoSuchMethodException e) {
365                         // TODO Auto-generated catch block
366                         e.printStackTrace();
367                 } catch (SecurityException e) {
368                         // TODO Auto-generated catch block
369                         e.printStackTrace();
370                 } 
371         }
372         
373         @Test
374         public void testCacheUpdate(){
375                 TimeTaken tt = Mockito.mock(TimeTaken.class);
376                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
377                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO Touch segments test1: 1011", Env.REMOTE);
378                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
379                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
380                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
381                 Mockito.doNothing().when(tt).done();
382                 CacheInfoDAO.Data data1  = new CacheInfoDAO.Data();
383                 PSInfo createPS = Mockito.mock(PSInfo.class);
384                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
385                 Mockito.doReturn(rs).when(createPS).exec(trans, "CacheInfoDAOImpl CREATE", data1);
386
387                 CacheInfoDAOImpl daoObj=null;
388                 try {
389                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test", createPS);
390                 } catch (APIException | IOException e) {
391                         // TODO Auto-generated catch block
392                         e.printStackTrace();
393                 }
394                 Class[] classList = CacheInfoDAO.class.getDeclaredClasses();
395                 Class<?> innerClass = null;
396                 for(Class indCls:classList) {
397                         if(indCls.getName().contains("CacheUpdate")) {
398                                 innerClass = indCls;
399                         }
400                 }
401         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
402         constructor.setAccessible(true);
403         try {
404                         Object obj = constructor.newInstance(env,Mockito.mock(HMangr.class), Mockito.mock(SecuritySetter.class), "12.0.0.1", 8080);
405
406                 Class<?> innerInnerClass = Class.forName("org.onap.aaf.auth.dao.cass.CacheInfoDAO$CacheUpdate$CacheClear");
407             Constructor<?> innerConstructor = innerInnerClass.getDeclaredConstructors()[0];
408             innerConstructor.setAccessible(true);
409             Object innerClassObj = innerConstructor.newInstance(obj, trans);
410                 
411                         Method innnerClassMtd;
412                                 
413                         CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
414                         Row row = Mockito.mock(Row.class);
415                         Rcli rcli = Mockito.mock(Rcli.class);
416                         URI uri = new URI("ftp","admin:admin@geeksforgeeks.org:1234","/path/data","tt","ttd");
417                         Mockito.doReturn(uri).when(rcli).getURI();
418                         Mockito.doReturn(Mockito.mock(org.onap.aaf.cadi.client.Future.class)).when(rcli).delete("/mgmt/cache/null/null", "application/Void+json;q=1.0;charset=utf-8;version=2.0,application/json;q=1.0;version=2.0,*/*;q=1.0");
419                         
420                         innnerClassMtd = innerInnerClass.getMethod("code", new Class[] {Rcli.class});
421                         innnerClassMtd.invoke(innerClassObj, new Object[] {rcli});
422                         
423                         org.onap.aaf.cadi.client.Future futureObj = Mockito.mock(org.onap.aaf.cadi.client.Future.class);
424                         Mockito.doReturn(futureObj).when(rcli).delete("/mgmt/cache/null/null", "application/Void+json;q=1.0;charset=utf-8;version=2.0,application/json;q=1.0;version=2.0,*/*;q=1.0");
425                         Mockito.doReturn(true).when(futureObj).get(0);
426                         innnerClassMtd.invoke(innerClassObj, new Object[] {rcli});
427                         
428                         uri = new URI("ftp","12.0.0.1:8080","/path/data","tt","ttd");
429                         Mockito.doReturn(uri).when(rcli).getURI();
430                         innnerClassMtd.invoke(innerClassObj, new Object[] {rcli});
431                 } catch (InstantiationException e) {
432                         // TODO Auto-generated catch block
433                         e.printStackTrace();
434                 } catch (IllegalAccessException e) {
435                         // TODO Auto-generated catch block
436                         e.printStackTrace();
437                 } catch (IllegalArgumentException e) {
438                         // TODO Auto-generated catch block
439                         e.printStackTrace();
440                 } catch (InvocationTargetException e) {
441                         // TODO Auto-generated catch block
442                         e.printStackTrace();
443                 }  catch (SecurityException e) {
444                         // TODO Auto-generated catch block
445                         e.printStackTrace();
446                 } catch (ClassNotFoundException e) {
447                         // TODO Auto-generated catch block
448                         e.printStackTrace();
449                 } catch (NoSuchMethodException e) {
450                         // TODO Auto-generated catch block
451                         e.printStackTrace();
452                 } catch (URISyntaxException e) {
453                         // TODO Auto-generated catch block
454                         e.printStackTrace();
455                 } catch (APIException e) {
456                         // TODO Auto-generated catch block
457                         e.printStackTrace();
458                 } catch (CadiException e) {
459                         // TODO Auto-generated catch block
460                         e.printStackTrace();
461                 } 
462         }
463         
464         @Test
465         public void testIntHolder(){
466                 TimeTaken tt = Mockito.mock(TimeTaken.class);
467                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
468                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO Touch segments test1: 1011", Env.REMOTE);
469                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
470                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
471                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
472                 Mockito.doNothing().when(tt).done();
473                 CacheInfoDAO.Data data1  = new CacheInfoDAO.Data();
474                 PSInfo createPS = Mockito.mock(PSInfo.class);
475                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
476                 Mockito.doReturn(rs).when(createPS).exec(trans, "CacheInfoDAOImpl CREATE", data1);
477
478                 CacheInfoDAOImpl daoObj=null;
479                 try {
480                         daoObj = new CacheInfoDAOImpl(trans, cluster, "test", createPS);
481                 } catch (APIException | IOException e) {
482                         // TODO Auto-generated catch block
483                         e.printStackTrace();
484                 }
485                 Class[] classList = CacheInfoDAO.class.getDeclaredClasses();
486                 Class<?> innerClass = null;
487                 for(Class indCls:classList) {
488                         if(indCls.getName().contains("CacheUpdate")) {
489                                 innerClass = indCls;
490                         }
491                 }
492         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
493         constructor.setAccessible(true);
494         try {
495                         Object obj = constructor.newInstance(env,Mockito.mock(HMangr.class), Mockito.mock(SecuritySetter.class), "12.0.0.1", 8080);
496
497                 Class<?> innerInnerClass = Class.forName("org.onap.aaf.auth.dao.cass.CacheInfoDAO$CacheUpdate$IntHolder");
498             Constructor<?> innerConstructor = innerInnerClass.getDeclaredConstructors()[0];
499             innerConstructor.setAccessible(true);
500             int[] a = new int[10];
501             Object innerClassObj = innerConstructor.newInstance(obj, a);
502                 
503                         Method innnerClassMtd=null;
504                                 
505                         CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
506                         Row row = Mockito.mock(Row.class);
507                         Rcli rcli = Mockito.mock(Rcli.class);
508                         URI uri = new URI("ftp","admin:admin@geeksforgeeks.org:1234","/path/data","tt","ttd");
509                         Mockito.doReturn(uri).when(rcli).getURI();
510                         Mockito.doReturn(Mockito.mock(org.onap.aaf.cadi.client.Future.class)).when(rcli).delete("/mgmt/cache/null/null", "application/Void+json;q=1.0;charset=utf-8;version=2.0,application/json;q=1.0;version=2.0,*/*;q=1.0");
511                         
512                         Method[] allMtds = innerInnerClass.getDeclaredMethods();
513                         for(Method indMtd:allMtds) {
514                                 if(indMtd.getName().contains("add")) {
515                                         innnerClassMtd = indMtd;
516                                 }
517                         }
518                         innnerClassMtd.invoke(innerClassObj, new Object[] {a});
519                         
520                         
521                 } catch (InstantiationException e) {
522                         // TODO Auto-generated catch block
523                         e.printStackTrace();
524                 } catch (IllegalAccessException e) {
525                         // TODO Auto-generated catch block
526                         e.printStackTrace();
527                 } catch (IllegalArgumentException e) {
528                         // TODO Auto-generated catch block
529                         e.printStackTrace();
530                 } catch (InvocationTargetException e) {
531                         // TODO Auto-generated catch block
532                         e.printStackTrace();
533                 }  catch (SecurityException e) {
534                         // TODO Auto-generated catch block
535                         e.printStackTrace();
536                 } catch (ClassNotFoundException e) {
537                         // TODO Auto-generated catch block
538                         e.printStackTrace();
539                 } catch (URISyntaxException e) {
540                         // TODO Auto-generated catch block
541                         e.printStackTrace();
542                 } catch (APIException e) {
543                         // TODO Auto-generated catch block
544                         e.printStackTrace();
545                 } catch (CadiException e) {
546                         // TODO Auto-generated catch block
547                         e.printStackTrace();
548                 } 
549         }
550         
551         //@Test
552         public void testSecondConstructor() {
553                 TimeTaken tt = Mockito.mock(TimeTaken.class);
554                 Mockito.doReturn(tt).when(trans).start("CacheInfoDAO CREATE", Env.REMOTE);
555                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
556                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
557                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CacheInfoDAO", Env.SUB);
558                 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
559                 Mockito.doNothing().when(tt).done();
560                 CacheInfoDAO.Data data  = new CacheInfoDAO.Data();
561                 AbsCassDAO absCassDAO = Mockito.mock(AbsCassDAO.class);
562
563                 try {
564                         CacheInfoDAO daoObj = new CacheInfoDAO(trans, absCassDAO);
565                 } catch (APIException | IOException e) {
566                         // TODO Auto-generated catch block
567                         e.printStackTrace();
568                 }
569         }
570 }
571
572 class CacheInfoDAOImpl extends CacheInfoDAO{
573
574         public CacheInfoDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,CacheInfoDAO.Data data,PSInfo createPS  ) throws APIException, IOException {
575                 super(trans, cluster, keyspace);
576                 this.createPS = createPS;
577 //              setPs(this, createPS, "psByUser");
578 //              setPs(this, createPS, "psByApprover");
579 //              setPs(this, createPS, "psByTicket");
580 //              setPs(this, createPS, "psByStatus");
581 //              setSession(this, Mockito.mock(Session.class));
582         }
583         
584         public CacheInfoDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS  ) throws APIException, IOException {
585                 super(trans, cluster, keyspace);
586                 this.readPS = readPS;
587         }
588         
589
590         public void setPs(CacheInfoDAOImpl CacheInfoDAOObj, PSInfo psInfoObj, String methodName) {
591                 Field nsDaoField;
592                 try {
593                         nsDaoField = CacheInfoDAO.class.getDeclaredField(methodName);
594                         
595                         nsDaoField.setAccessible(true);
596                 // remove final modifier from field
597                 Field modifiersField = Field.class.getDeclaredField("modifiers");
598                 modifiersField.setAccessible(true);
599 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
600                 
601                 nsDaoField.set(CacheInfoDAOObj, psInfoObj);
602                 } catch (NoSuchFieldException | SecurityException e) {
603                         // TODO Auto-generated catch block
604                         e.printStackTrace();
605                 } catch (IllegalArgumentException e) {
606                         // TODO Auto-generated catch block
607                         e.printStackTrace();
608                 } catch (IllegalAccessException e) {
609                         // TODO Auto-generated catch block
610                         e.printStackTrace();
611                 }
612         }
613
614         
615         public void setSession(CacheInfoDAOImpl CacheInfoDAOObj, Session session) {
616                 Field nsDaoField;
617                 try {
618                         nsDaoField = AbsCassDAO.class.getDeclaredField("session");
619                         
620                         nsDaoField.setAccessible(true);
621                 // remove final modifier from field
622                 Field modifiersField = Field.class.getDeclaredField("modifiers");
623                 modifiersField.setAccessible(true);
624 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
625                 nsDaoField.set(CacheInfoDAOObj, session);
626                 } catch (NoSuchFieldException | SecurityException e) {
627                         // TODO Auto-generated catch block
628                         e.printStackTrace();
629                 } catch (IllegalArgumentException e) {
630                         // TODO Auto-generated catch block
631                         e.printStackTrace();
632                 } catch (IllegalAccessException e) {
633                         // TODO Auto-generated catch block
634                         e.printStackTrace();
635                 }
636         }
637         
638 }