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.mockito.MockitoAnnotations.initMocks;
26 import java.io.IOException;
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31 import java.math.BigInteger;
32 import java.nio.ByteBuffer;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.onap.aaf.auth.dao.AbsCassDAO;
39 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
40 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
41 import org.onap.aaf.auth.dao.CassAccess;
42 import org.onap.aaf.auth.env.AuthzEnv;
43 import org.onap.aaf.auth.env.AuthzTrans;
44 import org.onap.aaf.auth.layer.Result;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.misc.env.APIException;
47 import org.onap.aaf.misc.env.Decryptor;
48 import org.onap.aaf.misc.env.Env;
49 import org.onap.aaf.misc.env.LogTarget;
50 import org.onap.aaf.misc.env.TimeTaken;
52 import com.datastax.driver.core.Cluster;
53 import com.datastax.driver.core.ResultSet;
54 import com.datastax.driver.core.Row;
55 import com.datastax.driver.core.Session;
57 public class JU_CertDAOTest {
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("CertDAO 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 CertDAO", Env.SUB);
90 Mockito.doNothing().when(tt).done();
91 CertDAO.Data data = new CertDAO.Data();
92 PSInfo createPS = Mockito.mock(PSInfo.class);
93 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
94 Mockito.doReturn(rs).when(createPS).exec(trans, "CertDAOImpl CREATE", data);
96 CertDAOImpl daoObj=null;
98 daoObj = new CertDAOImpl(trans, cluster, "test",data, createPS);
99 } catch (APIException | IOException e) {
100 // TODO Auto-generated catch block
107 public void testCertLoader(){
109 Class<?> innerClass = CertDAO.class.getDeclaredClasses()[0];
110 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
111 constructor.setAccessible(true);
113 Object obj = constructor.newInstance(1);
114 Method innnerClassMtd;
116 CertDAO.Data data = new CertDAO.Data();
117 Row row = Mockito.mock(Row.class);
118 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
120 bbObj.put(0, new Byte("0"));
121 bbObj.put(1, new Byte("1"));
122 bbObj.put(2, new Byte("2"));
123 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
125 innnerClassMtd = innerClass.getMethod("load", new Class[] {CertDAO.Data.class, Row.class});
126 innnerClassMtd.invoke(obj, new Object[] {data, row});
128 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {CertDAO.Data.class, Integer.TYPE, Object[].class });
129 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
131 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {CertDAO.Data.class, Integer.TYPE, Object[].class });
132 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
134 // DataInputStream in = Mockito.mock(DataInputStream.class);
135 //// Mockito.doReturn(100).when(in).read();
136 //// Mockito.doReturn(100).when(in).readInt();
137 // innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {ArtiDAO.Data.class, DataInputStream.class });
138 // innnerClassMtd.invoke(obj, new Object[] {data, in});
139 } catch (InstantiationException e) {
140 // TODO Auto-generated catch block
142 } catch (IllegalAccessException e) {
143 // TODO Auto-generated catch block
145 } catch (IllegalArgumentException e) {
146 // TODO Auto-generated catch block
148 } catch (InvocationTargetException e) {
149 // TODO Auto-generated catch block
151 } catch (NoSuchMethodException e) {
152 // TODO Auto-generated catch block
154 } catch (SecurityException e) {
155 // TODO Auto-generated catch block
161 public void testWasMOdified() {
162 TimeTaken tt = Mockito.mock(TimeTaken.class);
163 Mockito.doReturn(tt).when(trans).start("CertDAO CREATE", Env.REMOTE);
164 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
165 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
166 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CertDAO", Env.SUB);
167 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
168 Mockito.doNothing().when(tt).done();
169 CertDAO.Data data = new CertDAO.Data();
170 PSInfo createPS = Mockito.mock(PSInfo.class);
172 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
173 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]);
174 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
176 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
177 Mockito.doReturn(rs1).when(cacheInfoDAO).touch(trans, CertDAO.TABLE, new int[1]);
179 CertDAO daoObj = null;
181 daoObj = new CertDAO(trans, historyDAO, cacheInfoDAO);
182 } catch (APIException | IOException e) {
183 // TODO Auto-generated catch block
187 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
189 rs1 = new Result<ResultSet>(null,1,"test",new String[0]);
190 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
192 Mockito.doReturn(rs1).when(cacheInfoDAO).touch(trans, CertDAO.TABLE, new int[1]);
195 daoObj = new CertDAO(trans, historyDAO, cacheInfoDAO);
196 } catch (APIException | IOException e) {
197 // TODO Auto-generated catch block
201 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
203 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test"});
204 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test", null});
205 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test", "test"});
206 daoObj.wasModified(trans, CRUD.delete, data, new String[] {null});
207 daoObj.wasModified(trans, CRUD.delete, data, new String[] {});
210 CertDAO.Data data1 = Mockito.mock(CertDAO.Data.class);
211 Mockito.doThrow(new IOException()).when(data1).bytify();
212 Mockito.doReturn(new int[1]).when(data1).invalidate(Mockito.any());
213 daoObj.wasModified(trans, CRUD.delete, data1, new String[] {});
214 } catch (IOException e) {
215 // TODO Auto-generated catch block
221 public void testRead() {
222 TimeTaken tt = Mockito.mock(TimeTaken.class);
223 Mockito.doReturn(tt).when(trans).start("CertDAO CREATE", Env.REMOTE);
224 Mockito.doReturn(tt).when(trans).start("CertDAO READ", Env.REMOTE);
225 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
226 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
227 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CertDAO", Env.SUB);
228 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
229 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100");
230 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042");
231 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100");
232 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null);
233 Mockito.doReturn(Mockito.mock(Decryptor.class)).when(trans).decryptor();
234 Mockito.doNothing().when(tt).done();
235 CertDAO.Data data = new CertDAO.Data();
236 PSInfo createPS = Mockito.mock(PSInfo.class);
238 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
239 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]);
240 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
242 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
243 Mockito.doReturn(rs1).when(cacheInfoDAO).touch(trans, CertDAO.TABLE, new int[1]);
245 CertDAO daoObj = null;
247 daoObj = new CertDAO(trans, historyDAO, cacheInfoDAO);
248 } catch (APIException | IOException e) {
249 // TODO Auto-generated catch block
253 daoObj.read(trans, new Object[] {"test", BigInteger.ONE});
256 cbField = CassAccess.class.getDeclaredField("cb");
258 cbField.setAccessible(true);
259 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
261 cbField.set(null, null);
262 } catch (NoSuchFieldException | SecurityException e) {
263 // TODO Auto-generated catch block
265 } catch (IllegalArgumentException e) {
266 // TODO Auto-generated catch block
268 } catch (IllegalAccessException e) {
269 // TODO Auto-generated catch block
273 daoObj.readX500(trans, "test");
276 cbField = CassAccess.class.getDeclaredField("cb");
278 cbField.setAccessible(true);
279 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
281 cbField.set(null, null);
282 } catch (NoSuchFieldException | SecurityException e) {
283 // TODO Auto-generated catch block
285 } catch (IllegalArgumentException e) {
286 // TODO Auto-generated catch block
288 } catch (IllegalAccessException e) {
289 // TODO Auto-generated catch block
293 daoObj.readID(trans, "test");
298 public void testSecondConstructor() {
299 TimeTaken tt = Mockito.mock(TimeTaken.class);
300 Mockito.doReturn(tt).when(trans).start("CertDAO CREATE", Env.REMOTE);
301 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
302 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
303 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CertDAO", Env.SUB);
304 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
305 Mockito.doNothing().when(tt).done();
306 CertDAO.Data data = new CertDAO.Data();
307 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
308 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
311 CertDAO daoObj = new CertDAO(trans, historyDAO, cacheInfoDAO);
312 } catch (APIException | IOException e) {
313 // TODO Auto-generated catch block
319 class CertDAOImpl extends CertDAO{
321 public CertDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,CertDAO.Data data,PSInfo createPS ) throws APIException, IOException {
322 super(trans, cluster, keyspace);
323 this.createPS = createPS;
324 // setPs(this, createPS, "psByUser");
325 // setPs(this, createPS, "psByApprover");
326 // setPs(this, createPS, "psByTicket");
327 // setPs(this, createPS, "psByStatus");
328 // setSession(this, Mockito.mock(Session.class));
331 public CertDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS ) throws APIException, IOException {
332 super(trans, cluster, keyspace);
333 this.readPS = readPS;
337 public void setPs(CertDAOImpl CertDAOObj, PSInfo psInfoObj, String methodName) {
340 nsDaoField = CertDAO.class.getDeclaredField(methodName);
342 nsDaoField.setAccessible(true);
343 // remove final modifier from field
344 Field modifiersField = Field.class.getDeclaredField("modifiers");
345 modifiersField.setAccessible(true);
346 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
348 nsDaoField.set(CertDAOObj, psInfoObj);
349 } catch (NoSuchFieldException | SecurityException e) {
350 // TODO Auto-generated catch block
352 } catch (IllegalArgumentException e) {
353 // TODO Auto-generated catch block
355 } catch (IllegalAccessException e) {
356 // TODO Auto-generated catch block
362 public void setSession(CertDAOImpl CertDAOObj, Session session) {
365 nsDaoField = AbsCassDAO.class.getDeclaredField("session");
367 nsDaoField.setAccessible(true);
368 // remove final modifier from field
369 Field modifiersField = Field.class.getDeclaredField("modifiers");
370 modifiersField.setAccessible(true);
371 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
372 nsDaoField.set(CertDAOObj, session);
373 } catch (NoSuchFieldException | SecurityException e) {
374 // TODO Auto-generated catch block
376 } catch (IllegalArgumentException e) {
377 // TODO Auto-generated catch block
379 } catch (IllegalAccessException e) {
380 // TODO Auto-generated catch block