Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cass / JU_HistoryDAO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.dao.cass;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
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;
36
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;
53
54 import com.datastax.driver.core.Cluster;
55 import com.datastax.driver.core.ResultSet;
56 import com.datastax.driver.core.Row;
57
58 public class JU_HistoryDAO {
59
60     @Mock
61     AuthzTrans trans;
62     @Mock
63     Cluster cluster;
64     
65     @Before
66     public void setUp() throws APIException, IOException {
67         initMocks(this);
68     }
69
70     @Test
71     public void testInit() {
72         HistoryDAO daoObj = new HistoryDAO(trans, cluster, "test");
73     }
74     @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)) );
79     }
80     
81     @Test
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"));
88     }
89     
90     @Test
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");
95         
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);
99         
100         Result<List<Data>> retVal = daoObj.readByYYYYMM(trans, 201905);
101         assertTrue(retVal.status !=1);
102         
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);
107     }
108     
109     @Test
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");
114         
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");
118         
119         Result<List<Data>> retVal = daoObj.readByUser(trans, "test", 201905);
120         assertTrue(retVal.status !=1);
121         
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);
126         
127         retVal = daoObj.readByUser(trans,"test");
128         assertTrue(retVal.status !=0);
129     }
130     
131     @Test
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");
136         
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");
140         
141         Result<List<Data>> retVal = daoObj.readBySubject(trans, "test", "test", 201905);
142         assertTrue(retVal.status !=1);
143         
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);
148         
149         retVal = daoObj.readBySubject(trans,"test", "test");
150         assertTrue(retVal.status !=0);
151     }
152     
153     public void setAbsCassDAO(HistoryDAO HistoryDAOObj, PSInfo psInfoObj, String fieldName) {
154         Field nsDaoField;
155         try {
156             nsDaoField = HistoryDAO.class.getDeclaredField(fieldName);
157             
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);
163             
164             nsDaoField.set(HistoryDAOObj, psInfoObj);
165         } catch (NoSuchFieldException | SecurityException e) {
166             // TODO Auto-generated catch block
167             e.printStackTrace();
168         } catch (IllegalArgumentException e) {
169             // TODO Auto-generated catch block
170             e.printStackTrace();
171         } catch (IllegalAccessException e) {
172             // TODO Auto-generated catch block
173             e.printStackTrace();
174         }
175     }
176     
177     
178     @Test
179     public void testSecondConstructor() {
180         HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
181
182         HistoryDAO daoObj = new HistoryDAO(trans, historyDAO);
183         
184     }
185
186     @Test
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")) {
192                 innerClass = indCls;
193                 break;
194             }
195         }
196         
197         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
198         constructor.setAccessible(true);
199         
200         try {
201             Object obj = constructor.newInstance(1);
202             Method innnerClassMtd;
203                 
204             HistoryDAO.Data data  = new HistoryDAO.Data();
205             Row row = Mockito.mock(Row.class);
206             ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
207             bbObj.limit(7);
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);
212             
213             innnerClassMtd = innerClass.getMethod("load", new Class[] {HistoryDAO.Data.class, Row.class});
214             innnerClassMtd.invoke(obj, new Object[] {data, row});
215             
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"} });
218 //            
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"} });
221             
222         } catch (InstantiationException e) {
223             // TODO Auto-generated catch block
224             e.printStackTrace();
225         } catch (IllegalAccessException e) {
226             // TODO Auto-generated catch block
227             e.printStackTrace();
228         } catch (IllegalArgumentException e) {
229             // TODO Auto-generated catch block
230             e.printStackTrace();
231         } catch (InvocationTargetException e) {
232             // TODO Auto-generated catch block
233             e.printStackTrace();
234         } catch (NoSuchMethodException e) {
235             // TODO Auto-generated catch block
236             e.printStackTrace();
237         } catch (SecurityException e) {
238             // TODO Auto-generated catch block
239             e.printStackTrace();
240         } 
241     }
242     
243     @Test
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")) {
249                 innerClass = indCls;
250                 break;
251             }
252         }
253         
254         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
255         constructor.setAccessible(true);
256         int yyyymm[] = new int[2];
257         try {
258             Object obj = constructor.newInstance(new HistoryDAO(trans, cluster, "test"), yyyymm);
259             Method innnerClassMtd;
260                 
261             HistoryDAO.Data data  = new HistoryDAO.Data();
262             Row row = Mockito.mock(Row.class);
263             ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
264             bbObj.limit(7);
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);
269             
270             innnerClassMtd = innerClass.getMethod("ok", new Class[] {HistoryDAO.Data.class});
271             innnerClassMtd.invoke(obj, new Object[] {data});
272             
273             data.yr_mon=201904;
274             innnerClassMtd.invoke(obj, new Object[] {data});
275             
276         } catch (InstantiationException e) {
277             // TODO Auto-generated catch block
278             e.printStackTrace();
279         } catch (IllegalAccessException e) {
280             // TODO Auto-generated catch block
281             e.printStackTrace();
282         } catch (IllegalArgumentException e) {
283             // TODO Auto-generated catch block
284             e.printStackTrace();
285         } catch (InvocationTargetException e) {
286             // TODO Auto-generated catch block
287             e.printStackTrace();
288         } catch (NoSuchMethodException e) {
289             // TODO Auto-generated catch block
290             e.printStackTrace();
291         } catch (SecurityException e) {
292             // TODO Auto-generated catch block
293             e.printStackTrace();
294         } 
295     }
296     
297 }
298
299 class HistoryDAOImpl extends HistoryDAO{
300
301     
302     public HistoryDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS  ) throws APIException, IOException {
303         super(trans, historyDAO);
304         setPs(this, readPS, "createPS");
305     }
306     
307     public void setPs(HistoryDAOImpl HistoryDAOObj, PSInfo psInfoObj, String methodName) {
308         Field nsDaoField;
309         try {
310             nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
311             
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);
317             
318             nsDaoField.set(HistoryDAOObj, psInfoObj);
319         } catch (NoSuchFieldException | SecurityException e) {
320             // TODO Auto-generated catch block
321             e.printStackTrace();
322         } catch (IllegalArgumentException e) {
323             // TODO Auto-generated catch block
324             e.printStackTrace();
325         } catch (IllegalAccessException e) {
326             // TODO Auto-generated catch block
327             e.printStackTrace();
328         }
329     }
330     
331 }