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.DataInputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.lang.reflect.Constructor;
30 import java.lang.reflect.Field;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.TreeSet;
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.env.AuthzTrans;
45 import org.onap.aaf.auth.layer.Result;
46 import org.onap.aaf.misc.env.APIException;
47 import org.onap.aaf.misc.env.Env;
48 import org.onap.aaf.misc.env.LogTarget;
49 import org.onap.aaf.misc.env.TimeTaken;
51 import com.datastax.driver.core.Cluster;
52 import com.datastax.driver.core.ResultSet;
53 import com.datastax.driver.core.Row;
54 import com.datastax.driver.core.Session;
56 public class JU_ArtiDAO {
64 public void setUp() throws APIException, IOException {
66 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
67 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
68 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
69 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
73 public void testReadByMechID() {
74 TimeTaken tt = Mockito.mock(TimeTaken.class);
75 Mockito.doReturn(tt).when(trans).start("ArtiDAO CREATE", Env.REMOTE);
76 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
77 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
78 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on ArtiDAO", Env.SUB);
79 Mockito.doNothing().when(tt).done();
81 PSInfo psByMechIdObj = Mockito.mock(PSInfo.class);
82 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
83 // Mockito.doReturn(rs).when(createPS).exec(trans, "ArtiDAOImpl CREATE", data);
85 ArtiDAOImpl daoObj = new ArtiDAOImpl(trans, cluster, "test", psByMechIdObj);
87 Result<List<ArtiDAO.Data>> rs1 = new Result<List<ArtiDAO.Data>>(null,0,"test",new String[0]);
88 Mockito.doReturn(rs1).when(psByMechIdObj).read(trans, "ArtiDAOImpl READ", new Object[]{"testMechId"});
89 daoObj.readByMechID(trans, "testMechId");
91 rs1 = new Result<List<ArtiDAO.Data>>(null,0,"test",new String[0]);
92 Mockito.doReturn(rs1).when(psByMechIdObj).read(trans, "ArtiDAOImpl READ", new Object[]{"testMachine"});
93 daoObj.readByMachine(trans, "testMachine");
95 rs1 = new Result<List<ArtiDAO.Data>>(null,0,"test",new String[0]);
96 Mockito.doReturn(rs1).when(psByMechIdObj).read(trans, "ArtiDAOImpl READ", new Object[]{"testNs"});
97 daoObj.readByNs(trans, "testNs");
101 public void testWasMOdified() {
102 TimeTaken tt = Mockito.mock(TimeTaken.class);
103 Mockito.doReturn(tt).when(trans).start("ArtiDAO CREATE", Env.REMOTE);
104 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
105 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
106 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on ArtiDAO", Env.SUB);
107 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
108 Mockito.doNothing().when(tt).done();
109 ArtiDAO.Data data = new ArtiDAO.Data();
110 PSInfo createPS = Mockito.mock(PSInfo.class);
112 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
113 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]);
114 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
116 ArtiDAOImpl daoObj = new ArtiDAOImpl(trans, cluster, "test", createPS, historyDAO);
117 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
119 daoObj.wasModified(trans, CRUD.create, data, new String[] {});
120 daoObj.wasModified(trans, CRUD.create, data, new String[] {null});
121 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null});
122 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"});
124 rs1 = new Result<ResultSet>(null,1,"test",new String[0]);
125 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
126 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"});
129 daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"});
133 public void testData(){
134 ArtiDAO.Data data = new ArtiDAO.Data();
140 data.sans = new TreeSet();
144 data.expires = new Date();
149 public void testArtifactLoader(){
150 ArtiDAO daoObj = new ArtiDAO(trans, cluster, "test");
151 Class<?> innerClass = ArtiDAO.class.getDeclaredClasses()[0];
152 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
153 constructor.setAccessible(true);
155 Object obj = constructor.newInstance(10);
156 Method innnerClassMtd;
158 ArtiDAO.Data data = new ArtiDAO.Data();
159 Row row = Mockito.mock(Row.class);
160 innnerClassMtd = innerClass.getMethod("load", new Class[] {ArtiDAO.Data.class, Row.class});
161 innnerClassMtd.invoke(obj, new Object[] {data, row});
163 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {ArtiDAO.Data.class, Integer.TYPE, Object[].class });
164 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
166 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {ArtiDAO.Data.class, Integer.TYPE, Object[].class });
167 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
169 // DataInputStream in = Mockito.mock(DataInputStream.class);
170 //// Mockito.doReturn(100).when(in).read();
171 //// Mockito.doReturn(100).when(in).readInt();
172 // innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {ArtiDAO.Data.class, DataInputStream.class });
173 // innnerClassMtd.invoke(obj, new Object[] {data, in});
174 } catch (InstantiationException e) {
175 // TODO Auto-generated catch block
177 } catch (IllegalAccessException e) {
178 // TODO Auto-generated catch block
180 } catch (IllegalArgumentException e) {
181 // TODO Auto-generated catch block
183 } catch (InvocationTargetException e) {
184 // TODO Auto-generated catch block
186 } catch (NoSuchMethodException e) {
187 // TODO Auto-generated catch block
189 } catch (SecurityException e) {
190 // TODO Auto-generated catch block
196 public void testSecondConstructor() {
197 TimeTaken tt = Mockito.mock(TimeTaken.class);
198 Mockito.doReturn(tt).when(trans).start("ArtiDAO CREATE", Env.REMOTE);
199 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
200 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
201 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on ArtiDAO", Env.SUB);
202 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
203 Mockito.doNothing().when(tt).done();
204 ArtiDAO.Data data = new ArtiDAO.Data();
205 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
207 ArtiDAO daoObj = new ArtiDAO(trans, historyDAO, Mockito.mock(CacheInfoDAO.class));
213 class ArtiDAOImpl extends ArtiDAO{
215 public ArtiDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace, PSInfo createPS ) {
216 super(trans, cluster, keyspace);
217 this.createPS = createPS;
218 setPs(this, createPS, "psByMechID");
219 setPs(this, createPS, "psByMachine");
220 setPs(this, createPS, "psByNs");
223 public ArtiDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS, HistoryDAO historyDAO ) {
224 super(trans, cluster, keyspace);
225 this.deletePS = readPS;
226 this.readPS = readPS;
227 setHistoryDao(this, historyDAO);
228 setSession(this, Mockito.mock(Session.class));
231 public void setPs(ArtiDAOImpl ArtiDAOObj, PSInfo psInfoObj, String methodName) {
234 nsDaoField = ArtiDAO.class.getDeclaredField(methodName);
236 nsDaoField.setAccessible(true);
237 // remove final modifier from field
238 Field modifiersField = Field.class.getDeclaredField("modifiers");
239 modifiersField.setAccessible(true);
240 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
242 nsDaoField.set(ArtiDAOObj, psInfoObj);
243 } catch (NoSuchFieldException | SecurityException e) {
244 // TODO Auto-generated catch block
246 } catch (IllegalArgumentException e) {
247 // TODO Auto-generated catch block
249 } catch (IllegalAccessException e) {
250 // TODO Auto-generated catch block
255 public void setHistoryDao(ArtiDAOImpl ArtiDAOObj, HistoryDAO historyDAO) {
258 nsDaoField = ArtiDAO.class.getDeclaredField("historyDAO");
260 nsDaoField.setAccessible(true);
261 // remove final modifier from field
262 Field modifiersField = Field.class.getDeclaredField("modifiers");
263 modifiersField.setAccessible(true);
264 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
266 nsDaoField.set(ArtiDAOObj, historyDAO);
267 } catch (NoSuchFieldException | SecurityException e) {
268 // TODO Auto-generated catch block
270 } catch (IllegalArgumentException e) {
271 // TODO Auto-generated catch block
273 } catch (IllegalAccessException e) {
274 // TODO Auto-generated catch block
278 public void setSession(ArtiDAOImpl ArtiDAOObj, Session session) {
281 nsDaoField = AbsCassDAO.class.getDeclaredField("session");
283 nsDaoField.setAccessible(true);
284 // remove final modifier from field
285 Field modifiersField = Field.class.getDeclaredField("modifiers");
286 modifiersField.setAccessible(true);
287 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
289 nsDaoField.set(ArtiDAOObj, session);
290 } catch (NoSuchFieldException | SecurityException e) {
291 // TODO Auto-generated catch block
293 } catch (IllegalArgumentException e) {
294 // TODO Auto-generated catch block
296 } catch (IllegalAccessException e) {
297 // TODO Auto-generated catch block