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.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.Field;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.nio.ByteBuffer;
38 import java.util.HashSet;
39 import java.util.List;
41 import java.util.TreeSet;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.onap.aaf.auth.dao.AbsCassDAO;
48 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
49 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
50 import org.onap.aaf.auth.dao.CassDAOImpl;
51 import org.onap.aaf.auth.env.AuthzTrans;
52 import org.onap.aaf.auth.layer.Result;
53 import org.onap.aaf.misc.env.APIException;
55 import com.datastax.driver.core.Cluster;
56 import com.datastax.driver.core.Row;
58 public class JU_LocateDAO {
66 public void setUp() throws APIException, IOException {
71 public void testInit() {
73 LocateDAO daoObj = new LocateDAO(trans, cluster, "test");
74 } catch (APIException | IOException e) {
75 // TODO Auto-generated catch block
80 public void testReadByStartAndTarget() {
81 LocateDAO daoObj = null;
83 daoObj = new LocateDAO(trans, cluster, "test");
84 } catch (APIException | IOException e) {
85 // TODO Auto-generated catch block
89 PSInfo psObj = Mockito.mock(PSInfo.class);
90 setPsByStartAndTarget(daoObj, psObj, "psName");
92 Result<List<LocateDAO.Data>> rs1 = new Result<List<LocateDAO.Data>>(null,0,"test",new String[0]);
93 Mockito.doReturn(rs1).when(psObj).read(trans, "LocateDAO READ", new Object[]{"test"});
95 daoObj.readByName(trans, "test");
99 public void setPsByStartAndTarget(LocateDAO LocateDAOObj, PSInfo psInfoObj, String fieldName) {
102 nsDaoField = LocateDAO.class.getDeclaredField(fieldName);
104 nsDaoField.setAccessible(true);
105 // remove final modifier from field
106 Field modifiersField = Field.class.getDeclaredField("modifiers");
107 modifiersField.setAccessible(true);
108 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
110 nsDaoField.set(LocateDAOObj, psInfoObj);
111 } catch (NoSuchFieldException | SecurityException e) {
112 // TODO Auto-generated catch block
114 } catch (IllegalArgumentException e) {
115 // TODO Auto-generated catch block
117 } catch (IllegalAccessException e) {
118 // TODO Auto-generated catch block
124 public void testWasMOdified() {
126 LocateDAO.Data data = new LocateDAO.Data();
128 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
130 LocateDAO daoObj = null;
132 daoObj = new LocateDAO(trans, historyDAO );
133 } catch (APIException | IOException e) {
134 // TODO Auto-generated catch block
137 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
143 public void testSecondConstructor() {
144 AbsCassDAO historyDAO = Mockito.mock(AbsCassDAO.class);
147 LocateDAO daoObj = new LocateDAO(trans, historyDAO);
148 } catch (APIException | IOException e) {
149 // TODO Auto-generated catch block
156 public void testLocateLoader(){
157 Class<?> innerClass = null;
158 Class<?>[] innerClassArr = LocateDAO.class.getDeclaredClasses();
159 for(Class indCls:innerClassArr) {
160 if(indCls.getName().contains("LocateLoader")) {
166 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
167 constructor.setAccessible(true);
171 Object obj = constructor.newInstance(1);
172 Method innnerClassMtd;
174 LocateDAO.Data data = new LocateDAO.Data();
175 Row row = Mockito.mock(Row.class);
176 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
178 bbObj.put(0, new Byte("0"));
179 bbObj.put(1, new Byte("1"));
180 bbObj.put(2, new Byte("2"));
181 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
183 innnerClassMtd = innerClass.getMethod("load", new Class[] {LocateDAO.Data.class, Row.class});
184 innnerClassMtd.invoke(obj, new Object[] {data, row});
186 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {LocateDAO.Data.class, Integer.TYPE, Object[].class });
187 innnerClassMtd.invoke(obj, new Object[] {data, 0, new Object[] {"test","test","test"} });
189 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {LocateDAO.Data.class, Integer.TYPE, Object[].class });
190 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
192 DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_LocateDAOTest.java"));
193 innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {LocateDAO.Data.class, DataOutputStream.class });
194 innnerClassMtd.invoke(obj, new Object[] {data, dos });
196 DataInputStream dis = new DataInputStream(new FileInputStream("JU_LocateDAOTest.java"));
197 innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {LocateDAO.Data.class, DataInputStream.class });
198 innnerClassMtd.invoke(obj, new Object[] {data, dis });
200 } catch (InstantiationException e) {
201 // TODO Auto-generated catch block
203 } catch (IllegalAccessException e) {
204 // TODO Auto-generated catch block
206 } catch (IllegalArgumentException e) {
207 // TODO Auto-generated catch block
209 } catch (InvocationTargetException e) {
210 // TODO Auto-generated catch block
212 } catch (NoSuchMethodException e) {
213 // TODO Auto-generated catch block
215 } catch (SecurityException e) {
216 // TODO Auto-generated catch block
218 } catch (FileNotFoundException e) {
219 // TODO Auto-generated catch block
225 public void testData(){
226 LocateDAO.Data data = new LocateDAO.Data();
228 data.hostname="hostname";
231 } catch (IOException e) {
232 // TODO Auto-generated catch block
236 data.subprotocol(true);
238 Set<String> subProt = new HashSet<String>();
241 protField = LocateDAO.Data.class.getDeclaredField("subprotocol");
243 protField.setAccessible(true);
245 protField.set(data, subProt);
246 } catch (NoSuchFieldException | SecurityException e) {
247 // TODO Auto-generated catch block
249 } catch (IllegalArgumentException e) {
250 // TODO Auto-generated catch block
252 } catch (IllegalAccessException e) {
253 // TODO Auto-generated catch block
257 data.subprotocol(true);
258 subProt = new TreeSet<String>();
261 protField = LocateDAO.Data.class.getDeclaredField("subprotocol");
263 protField.setAccessible(true);
265 protField.set(data, subProt);
266 } catch (NoSuchFieldException | SecurityException e) {
267 // TODO Auto-generated catch block
269 } catch (IllegalArgumentException e) {
270 // TODO Auto-generated catch block
272 } catch (IllegalAccessException e) {
273 // TODO Auto-generated catch block
277 data.subprotocol(true);
278 data.subprotocol(false);
280 LocateDAO.Data newDate = data.copy();
281 assertTrue(data.name.equals(newDate.name));
286 class LocateDAOImpl extends LocateDAO{
289 public LocateDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS ) throws APIException, IOException {
290 super(trans, historyDAO);
291 setPs(this, readPS, "createPS");
295 public void setPs(LocateDAOImpl LocateDAOObj, PSInfo psInfoObj, String methodName) {
298 nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
300 nsDaoField.setAccessible(true);
301 // remove final modifier from field
302 Field modifiersField = Field.class.getDeclaredField("modifiers");
303 modifiersField.setAccessible(true);
304 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
306 nsDaoField.set(LocateDAOObj, psInfoObj);
307 } catch (NoSuchFieldException | SecurityException e) {
308 // TODO Auto-generated catch block
310 } catch (IllegalArgumentException e) {
311 // TODO Auto-generated catch block
313 } catch (IllegalAccessException e) {
314 // TODO Auto-generated catch block