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.cass.HistoryDAO.Data;
44 import org.onap.aaf.auth.dao.AbsCassDAO;
45 import org.onap.aaf.auth.dao.CassDAOImpl;
46 import org.onap.aaf.auth.dao.Loader;
47 import org.onap.aaf.auth.env.AuthzTrans;
48 import org.onap.aaf.auth.layer.Result;
49 import org.onap.aaf.misc.env.APIException;
50 import org.onap.aaf.misc.env.Env;
51 import org.onap.aaf.misc.env.LogTarget;
52 import org.onap.aaf.misc.env.TimeTaken;
54 import com.datastax.driver.core.Cluster;
55 import com.datastax.driver.core.ResultSet;
56 import com.datastax.driver.core.Row;
58 public class JU_HistoryDAO {
66 public void setUp() throws APIException, IOException {
71 public void testInit() {
72 HistoryDAO daoObj = new HistoryDAO(trans, cluster, "test");
75 public void testNewInitedData() {
76 HistoryDAO daoObj = new HistoryDAO(trans, cluster, "test");
77 HistoryDAO.Data data = daoObj.newInitedData();
78 assertTrue( Integer.toString(((new Date()).getYear())+1900).equalsIgnoreCase(Integer.toString(data.yr_mon).substring(0,4)) );
82 public void testCreateBatch() {
83 HistoryDAO daoObj = new HistoryDAO(trans, cluster, "test");
84 StringBuilder sb = new StringBuilder();
85 HistoryDAO.Data data = new HistoryDAO.Data();
86 daoObj.createBatch(sb, data);
87 assertTrue(sb.toString().contains("INSERT INTO history"));
91 public void testReadByYYYYMM() {
92 HistoryDAO daoObj = new HistoryDAO(trans, cluster, "test");
93 AbsCassDAO<AuthzTrans, Data>.PSInfo psInfoObj = Mockito.mock(PSInfo.class);
94 setAbsCassDAO(daoObj, psInfoObj, "readByYRMN");
96 ResultSet rs = Mockito.mock(ResultSet.class);
97 Result<ResultSet> rs1 = new Result<ResultSet>(rs,0,"test",new String[0]);
98 Mockito.doReturn(rs1).when(psInfoObj).exec(trans, "yr_mon", 201905);
100 Result<List<Data>> retVal = daoObj.readByYYYYMM(trans, 201905);
101 assertTrue(retVal.status !=1);
103 rs1 = new Result<ResultSet>(rs,1,"test",new String[0]);
104 Mockito.doReturn(rs1).when(psInfoObj).exec(trans, "yr_mon", 201905);
105 retVal = daoObj.readByYYYYMM(trans, 201905);
106 assertTrue(retVal.status !=0);
110 public void testReadByUser() {
111 HistoryDAO daoObj = new HistoryDAO(trans, cluster, "test");
112 AbsCassDAO<AuthzTrans, Data>.PSInfo psInfoObj = Mockito.mock(PSInfo.class);
113 setAbsCassDAO(daoObj, psInfoObj, "readByUser");
115 ResultSet rs = Mockito.mock(ResultSet.class);
116 Result<ResultSet> rs1 = new Result<ResultSet>(rs,0,"test",new String[0]);
117 Mockito.doReturn(rs1).when(psInfoObj).exec(trans, "user", "test");
119 Result<List<Data>> retVal = daoObj.readByUser(trans, "test", 201905);
120 assertTrue(retVal.status !=1);
122 rs1 = new Result<ResultSet>(rs,1,"test",new String[0]);
123 Mockito.doReturn(rs1).when(psInfoObj).exec(trans, "user", "test");
124 retVal = daoObj.readByUser(trans,"test", 201905);
125 assertTrue(retVal.status !=0);
127 retVal = daoObj.readByUser(trans,"test");
128 assertTrue(retVal.status !=0);
132 public void testReadBySubject() {
133 HistoryDAO daoObj = new HistoryDAO(trans, cluster, "test");
134 AbsCassDAO<AuthzTrans, Data>.PSInfo psInfoObj = Mockito.mock(PSInfo.class);
135 setAbsCassDAO(daoObj, psInfoObj, "readBySubject");
137 ResultSet rs = Mockito.mock(ResultSet.class);
138 Result<ResultSet> rs1 = new Result<ResultSet>(rs,0,"test",new String[0]);
139 Mockito.doReturn(rs1).when(psInfoObj).exec(trans, "subject", "test", "test");
141 Result<List<Data>> retVal = daoObj.readBySubject(trans, "test", "test", 201905);
142 assertTrue(retVal.status !=1);
144 rs1 = new Result<ResultSet>(rs,1,"test",new String[0]);
145 Mockito.doReturn(rs1).when(psInfoObj).exec(trans, "subject", "test", "test");
146 retVal = daoObj.readBySubject(trans,"test", "test", 201905);
147 assertTrue(retVal.status !=0);
149 retVal = daoObj.readBySubject(trans,"test", "test");
150 assertTrue(retVal.status !=0);
153 public void setAbsCassDAO(HistoryDAO HistoryDAOObj, PSInfo psInfoObj, String fieldName) {
156 nsDaoField = HistoryDAO.class.getDeclaredField(fieldName);
158 nsDaoField.setAccessible(true);
159 // remove final modifier from field
160 Field modifiersField = Field.class.getDeclaredField("modifiers");
161 modifiersField.setAccessible(true);
162 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
164 nsDaoField.set(HistoryDAOObj, psInfoObj);
165 } catch (NoSuchFieldException | SecurityException e) {
166 // TODO Auto-generated catch block
168 } catch (IllegalArgumentException e) {
169 // TODO Auto-generated catch block
171 } catch (IllegalAccessException e) {
172 // TODO Auto-generated catch block
179 public void testSecondConstructor() {
180 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
182 HistoryDAO daoObj = new HistoryDAO(trans, historyDAO);
187 public void testHistoryLoader(){
188 Class<?> innerClass = null;
189 Class<?>[] innerClassArr = HistoryDAO.class.getDeclaredClasses();
190 for(Class indCls:innerClassArr) {
191 if(indCls.getName().contains("HistLoader")) {
197 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
198 constructor.setAccessible(true);
201 Object obj = constructor.newInstance(1);
202 Method innnerClassMtd;
204 HistoryDAO.Data data = new HistoryDAO.Data();
205 Row row = Mockito.mock(Row.class);
206 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
208 bbObj.put(0, new Byte("0"));
209 bbObj.put(1, new Byte("1"));
210 bbObj.put(2, new Byte("2"));
211 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
213 innnerClassMtd = innerClass.getMethod("load", new Class[] {HistoryDAO.Data.class, Row.class});
214 innnerClassMtd.invoke(obj, new Object[] {data, row});
216 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {HistoryDAO.Data.class, Integer.TYPE, Object[].class });
217 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
219 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {HistoryDAO.Data.class, Integer.TYPE, Object[].class });
220 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
222 } catch (InstantiationException e) {
223 // TODO Auto-generated catch block
225 } catch (IllegalAccessException e) {
226 // TODO Auto-generated catch block
228 } catch (IllegalArgumentException e) {
229 // TODO Auto-generated catch block
231 } catch (InvocationTargetException e) {
232 // TODO Auto-generated catch block
234 } catch (NoSuchMethodException e) {
235 // TODO Auto-generated catch block
237 } catch (SecurityException e) {
238 // TODO Auto-generated catch block
244 public void testYYYYMM(){
245 Class<?> innerClass = null;
246 Class<?>[] innerClassArr = HistoryDAO.class.getDeclaredClasses();
247 for(Class indCls:innerClassArr) {
248 if(indCls.getName().contains("YYYYMM")) {
254 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
255 constructor.setAccessible(true);
256 int yyyymm[] = new int[2];
258 Object obj = constructor.newInstance(new HistoryDAO(trans, cluster, "test"), yyyymm);
259 Method innnerClassMtd;
261 HistoryDAO.Data data = new HistoryDAO.Data();
262 Row row = Mockito.mock(Row.class);
263 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
265 bbObj.put(0, new Byte("0"));
266 bbObj.put(1, new Byte("1"));
267 bbObj.put(2, new Byte("2"));
268 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
270 innnerClassMtd = innerClass.getMethod("ok", new Class[] {HistoryDAO.Data.class});
271 innnerClassMtd.invoke(obj, new Object[] {data});
274 innnerClassMtd.invoke(obj, new Object[] {data});
276 } catch (InstantiationException e) {
277 // TODO Auto-generated catch block
279 } catch (IllegalAccessException e) {
280 // TODO Auto-generated catch block
282 } catch (IllegalArgumentException e) {
283 // TODO Auto-generated catch block
285 } catch (InvocationTargetException e) {
286 // TODO Auto-generated catch block
288 } catch (NoSuchMethodException e) {
289 // TODO Auto-generated catch block
291 } catch (SecurityException e) {
292 // TODO Auto-generated catch block
299 class HistoryDAOImpl extends HistoryDAO{
302 public HistoryDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS ) throws APIException, IOException {
303 super(trans, historyDAO);
304 setPs(this, readPS, "createPS");
307 public void setPs(HistoryDAOImpl HistoryDAOObj, PSInfo psInfoObj, String methodName) {
310 nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
312 nsDaoField.setAccessible(true);
313 // remove final modifier from field
314 Field modifiersField = Field.class.getDeclaredField("modifiers");
315 modifiersField.setAccessible(true);
316 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
318 nsDaoField.set(HistoryDAOObj, psInfoObj);
319 } catch (NoSuchFieldException | SecurityException e) {
320 // TODO Auto-generated catch block
322 } catch (IllegalArgumentException e) {
323 // TODO Auto-generated catch block
325 } catch (IllegalAccessException e) {
326 // TODO Auto-generated catch block