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(tt).when(trans).start("CertDAOImpl READ",Env.REMOTE);
230 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100");
231 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042");
232 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100");
233 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null);
234 Mockito.doReturn(Mockito.mock(Decryptor.class)).when(trans).decryptor();
235 Mockito.doNothing().when(tt).done();
236 CertDAO.Data data = new CertDAO.Data();
237 PSInfo createPS = Mockito.mock(PSInfo.class);
239 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
240 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]);
241 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
243 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
244 Mockito.doReturn(rs1).when(cacheInfoDAO).touch(trans, CertDAO.TABLE, new int[1]);
246 CertDAOImpl daoObj = null;
248 daoObj = new CertDAOImpl(trans, historyDAO, cacheInfoDAO, createPS);
249 } catch (APIException | IOException e) {
250 // TODO Auto-generated catch block
254 daoObj.read(trans, new Object[] {"test", BigInteger.ONE});
257 cbField = CassAccess.class.getDeclaredField("cb");
259 cbField.setAccessible(true);
260 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
262 cbField.set(null, null);
263 } catch (NoSuchFieldException | SecurityException e) {
264 // TODO Auto-generated catch block
266 } catch (IllegalArgumentException e) {
267 // TODO Auto-generated catch block
269 } catch (IllegalAccessException e) {
270 // TODO Auto-generated catch block
274 daoObj.readX500(trans, "test");
277 cbField = CassAccess.class.getDeclaredField("cb");
279 cbField.setAccessible(true);
280 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
282 cbField.set(null, null);
283 } catch (NoSuchFieldException | SecurityException e) {
284 // TODO Auto-generated catch block
286 } catch (IllegalArgumentException e) {
287 // TODO Auto-generated catch block
289 } catch (IllegalAccessException e) {
290 // TODO Auto-generated catch block
294 daoObj.readID(trans, "test");
299 public void testSecondConstructor() {
300 TimeTaken tt = Mockito.mock(TimeTaken.class);
301 Mockito.doReturn(tt).when(trans).start("CertDAO CREATE", Env.REMOTE);
302 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
303 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
304 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CertDAO", Env.SUB);
305 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
306 Mockito.doNothing().when(tt).done();
307 CertDAO.Data data = new CertDAO.Data();
308 CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class);
309 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
312 CertDAO daoObj = new CertDAO(trans, historyDAO, cacheInfoDAO);
313 } catch (APIException | IOException e) {
314 // TODO Auto-generated catch block
320 class CertDAOImpl extends CertDAO{
322 public CertDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,CertDAO.Data data,PSInfo createPS ) throws APIException, IOException {
323 super(trans, cluster, keyspace);
324 this.createPS = createPS;
325 // setPs(this, createPS, "psByUser");
326 // setPs(this, createPS, "psByApprover");
327 // setPs(this, createPS, "psByTicket");
328 // setPs(this, createPS, "psByStatus");
329 // setSession(this, Mockito.mock(Session.class));
332 public CertDAOImpl(AuthzTrans trans, HistoryDAO historyDAO, CacheInfoDAO cacheInfoDAO,PSInfo readPS ) throws APIException, IOException {
333 super(trans, historyDAO, cacheInfoDAO);
334 this.readPS = readPS;
338 public void setPs(CertDAOImpl CertDAOObj, PSInfo psInfoObj, String methodName) {
341 nsDaoField = CertDAO.class.getDeclaredField(methodName);
343 nsDaoField.setAccessible(true);
344 // remove final modifier from field
345 Field modifiersField = Field.class.getDeclaredField("modifiers");
346 modifiersField.setAccessible(true);
347 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
349 nsDaoField.set(CertDAOObj, psInfoObj);
350 } catch (NoSuchFieldException | SecurityException e) {
351 // TODO Auto-generated catch block
353 } catch (IllegalArgumentException e) {
354 // TODO Auto-generated catch block
356 } catch (IllegalAccessException e) {
357 // TODO Auto-generated catch block
363 public void setSession(CertDAOImpl CertDAOObj, Session session) {
366 nsDaoField = AbsCassDAO.class.getDeclaredField("session");
368 nsDaoField.setAccessible(true);
369 // remove final modifier from field
370 Field modifiersField = Field.class.getDeclaredField("modifiers");
371 modifiersField.setAccessible(true);
372 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
373 nsDaoField.set(CertDAOObj, session);
374 } catch (NoSuchFieldException | SecurityException e) {
375 // TODO Auto-generated catch block
377 } catch (IllegalArgumentException e) {
378 // TODO Auto-generated catch block
380 } catch (IllegalAccessException e) {
381 // TODO Auto-generated catch block