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.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
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.nio.ByteBuffer;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.UUID;
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.CRUD;
42 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
43 import org.onap.aaf.auth.dao.CassDAOImpl;
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;
55 public class JU_FutureDAO {
63 public void setUp() throws APIException, IOException {
68 public void testInit() {
69 FutureDAO daoObj = new FutureDAO(trans, cluster, "test");
73 public void testReadByStartAndTarget() {
74 FutureDAO daoObj = new FutureDAO(trans, cluster, "test");
76 PSInfo psObj = Mockito.mock(PSInfo.class);
77 setPsByStartAndTarget(daoObj, psObj, "psByStartAndTarget");
79 Result<List<FutureDAO.Data>> rs1 = new Result<List<FutureDAO.Data>>(null,0,"test",new String[0]);
80 Mockito.doReturn(rs1).when(psObj).read(trans, "FutureDAO READ", new Object[]{"test"});
82 daoObj.readByStartAndTarget(trans,new Date(), "test");
85 public void testCreate() {
86 PSInfo psObj = Mockito.mock(PSInfo.class);
88 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
89 FutureDAO.Data data = new FutureDAO.Data();
91 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]);
92 Mockito.doReturn(rs1).when(psObj).exec(trans, "FutureDAOImpl CREATE", data);
93 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
95 FutureDAOImpl daoObj=null;
97 daoObj = new FutureDAOImpl(trans, historyDAO, psObj);
98 } catch (APIException | IOException e) {
99 // TODO Auto-generated catch block
102 Mockito.doReturn("test user").when(trans).user();
104 Result<FutureDAO.Data> retVal = daoObj.create(trans,data, "test");
105 assertTrue(retVal.status == 0);
107 StringBuilder sb = new StringBuilder(trans.user());
108 sb.append(data.target);
109 sb.append(System.currentTimeMillis());
110 data.id = UUID.nameUUIDFromBytes(sb.toString().getBytes());
112 rs1 = new Result<ResultSet>(null,1,"test",new String[0]);
113 Mockito.doReturn(rs1).when(psObj).exec(trans, "FutureDAOImpl CREATE", data);
115 retVal = daoObj.create(trans,data, "test");
116 assertTrue(retVal.status != 0);
121 public void setPsByStartAndTarget(FutureDAO FutureDAOObj, PSInfo psInfoObj, String fieldName) {
124 nsDaoField = FutureDAO.class.getDeclaredField(fieldName);
126 nsDaoField.setAccessible(true);
127 // remove final modifier from field
128 Field modifiersField = Field.class.getDeclaredField("modifiers");
129 modifiersField.setAccessible(true);
130 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
132 nsDaoField.set(FutureDAOObj, psInfoObj);
133 } catch (NoSuchFieldException | SecurityException e) {
134 // TODO Auto-generated catch block
136 } catch (IllegalArgumentException e) {
137 // TODO Auto-generated catch block
139 } catch (IllegalAccessException e) {
140 // TODO Auto-generated catch block
146 public void testWasMOdified() {
147 TimeTaken tt = Mockito.mock(TimeTaken.class);
148 Mockito.doReturn(tt).when(trans).start("FutureDAO CREATE", Env.REMOTE);
149 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
150 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
151 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on FutureDAO", Env.SUB);
152 Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE);
153 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
154 Mockito.doNothing().when(tt).done();
155 FutureDAO.Data data = new FutureDAO.Data();
156 PSInfo createPS = Mockito.mock(PSInfo.class);
158 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
159 Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]);
160 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
162 FutureDAOImpl daoObj = null;
164 daoObj = new FutureDAOImpl(trans, historyDAO, createPS );
165 } catch (APIException | IOException e) {
166 // TODO Auto-generated catch block
169 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
171 daoObj.wasModified(trans, CRUD.create, data, new String[] {});
172 daoObj.wasModified(trans, CRUD.create, data, new String[] {null});
173 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null});
174 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"});
176 rs1 = new Result<ResultSet>(null,1,"test",new String[0]);
177 Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any());
178 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"});
182 public void testSecondConstructor() {
183 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
185 FutureDAO daoObj = new FutureDAO(trans, historyDAO);
190 public void testFutureLoader(){
191 Class<?> innerClass = null;
192 Class<?>[] innerClassArr = FutureDAO.class.getDeclaredClasses();
193 for(Class indCls:innerClassArr) {
194 if(indCls.getName().contains("FLoader")) {
200 Constructor<?> constructor = innerClass.getDeclaredConstructors()[1];
201 constructor.setAccessible(true);
203 Constructor<?> constructor1 = innerClass.getDeclaredConstructors()[0];
204 constructor1.setAccessible(true);
207 Object obj = constructor.newInstance(1);
208 obj = constructor1.newInstance();
209 Method innnerClassMtd;
211 FutureDAO.Data data = new FutureDAO.Data();
212 Row row = Mockito.mock(Row.class);
213 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
215 bbObj.put(0, new Byte("0"));
216 bbObj.put(1, new Byte("1"));
217 bbObj.put(2, new Byte("2"));
218 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
220 innnerClassMtd = innerClass.getMethod("load", new Class[] {FutureDAO.Data.class, Row.class});
221 innnerClassMtd.invoke(obj, new Object[] {data, row});
223 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {FutureDAO.Data.class, Integer.TYPE, Object[].class });
224 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
226 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {FutureDAO.Data.class, Integer.TYPE, Object[].class });
227 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
229 // DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_FutureDAOTest.java"));
230 // innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {FutureDAO.Data.class, DataOutputStream.class });
231 // innnerClassMtd.invoke(obj, new Object[] {data, dos });
233 // DataInputStream dis = new DataInputStream(new FileInputStream("JU_FutureDAOTest.java"));
234 // innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {FutureDAO.Data.class, DataInputStream.class });
235 // innnerClassMtd.invoke(obj, new Object[] {data, dis });
237 } catch (InstantiationException e) {
238 // TODO Auto-generated catch block
240 } catch (IllegalAccessException e) {
241 // TODO Auto-generated catch block
243 } catch (IllegalArgumentException e) {
244 // TODO Auto-generated catch block
246 } catch (InvocationTargetException e) {
247 // TODO Auto-generated catch block
249 } catch (NoSuchMethodException e) {
250 // TODO Auto-generated catch block
252 } catch (SecurityException e) {
253 // TODO Auto-generated catch block
260 class FutureDAOImpl extends FutureDAO{
263 public FutureDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS ) throws APIException, IOException {
264 super(trans, historyDAO);
265 setPs(this, readPS, "createPS");
269 public void setPs(FutureDAOImpl FutureDAOObj, PSInfo psInfoObj, String methodName) {
272 nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
274 nsDaoField.setAccessible(true);
275 // remove final modifier from field
276 Field modifiersField = Field.class.getDeclaredField("modifiers");
277 modifiersField.setAccessible(true);
278 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
280 nsDaoField.set(FutureDAOObj, psInfoObj);
281 } catch (NoSuchFieldException | SecurityException e) {
282 // TODO Auto-generated catch block
284 } catch (IllegalArgumentException e) {
285 // TODO Auto-generated catch block
287 } catch (IllegalAccessException e) {
288 // TODO Auto-generated catch block