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.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.DataInputStream;
30 import java.io.DataOutputStream;
31 import java.io.IOException;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Method;
36 import java.nio.ByteBuffer;
37 import java.util.HashSet;
38 import java.util.List;
40 import java.util.TreeSet;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.onap.aaf.auth.dao.AbsCassDAO;
47 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
48 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
49 import org.onap.aaf.auth.dao.CassDAOImpl;
50 import org.onap.aaf.auth.env.AuthzTrans;
51 import org.onap.aaf.auth.layer.Result;
52 import org.onap.aaf.misc.env.APIException;
54 import com.datastax.driver.core.Cluster;
55 import com.datastax.driver.core.Row;
57 public class JU_LocateDAO {
65 public void setUp() throws APIException, IOException {
70 public void testInit() {
72 LocateDAO daoObj = new LocateDAO(trans, cluster, "test");
73 } catch (APIException | IOException e) {
74 // TODO Auto-generated catch block
79 public void testReadByStartAndTarget() {
80 LocateDAO daoObj = null;
82 daoObj = new LocateDAO(trans, cluster, "test");
83 } catch (APIException | IOException e) {
84 // TODO Auto-generated catch block
88 PSInfo psObj = Mockito.mock(PSInfo.class);
89 setPsByStartAndTarget(daoObj, psObj, "psName");
91 Result<List<LocateDAO.Data>> rs1 = new Result<List<LocateDAO.Data>>(null,0,"test",new String[0]);
92 Mockito.doReturn(rs1).when(psObj).read(trans, "LocateDAO READ", new Object[]{"test"});
94 daoObj.readByName(trans, "test");
98 public void setPsByStartAndTarget(LocateDAO LocateDAOObj, PSInfo psInfoObj, String fieldName) {
101 nsDaoField = LocateDAO.class.getDeclaredField(fieldName);
103 nsDaoField.setAccessible(true);
104 // remove final modifier from field
105 Field modifiersField = Field.class.getDeclaredField("modifiers");
106 modifiersField.setAccessible(true);
107 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
109 nsDaoField.set(LocateDAOObj, psInfoObj);
110 } catch (NoSuchFieldException | SecurityException e) {
111 // TODO Auto-generated catch block
113 } catch (IllegalArgumentException e) {
114 // TODO Auto-generated catch block
116 } catch (IllegalAccessException e) {
117 // TODO Auto-generated catch block
123 public void testWasMOdified() {
125 LocateDAO.Data data = new LocateDAO.Data();
127 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
129 LocateDAO daoObj = null;
131 daoObj = new LocateDAO(trans, historyDAO );
132 } catch (APIException | IOException e) {
133 // TODO Auto-generated catch block
136 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
142 public void testSecondConstructor() {
143 AbsCassDAO historyDAO = Mockito.mock(AbsCassDAO.class);
146 LocateDAO daoObj = new LocateDAO(trans, historyDAO);
147 } catch (APIException | IOException e) {
148 // TODO Auto-generated catch block
155 public void testLocateLoader(){
156 Class<?> innerClass = null;
157 Class<?>[] innerClassArr = LocateDAO.class.getDeclaredClasses();
158 for(Class indCls:innerClassArr) {
159 if(indCls.getName().contains("LocateLoader")) {
165 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
166 constructor.setAccessible(true);
170 Object obj = constructor.newInstance(1);
171 Method innnerClassMtd;
173 LocateDAO.Data data = new LocateDAO.Data();
174 Row row = Mockito.mock(Row.class);
175 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
177 bbObj.put(0, new Byte("0"));
178 bbObj.put(1, new Byte("1"));
179 bbObj.put(2, new Byte("2"));
180 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
182 innnerClassMtd = innerClass.getMethod("load", new Class[] {LocateDAO.Data.class, Row.class});
183 innnerClassMtd.invoke(obj, new Object[] {data, row});
185 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {LocateDAO.Data.class, Integer.TYPE, Object[].class });
186 innnerClassMtd.invoke(obj, new Object[] {data, 0, new Object[] {"test","test","test"} });
188 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {LocateDAO.Data.class, Integer.TYPE, Object[].class });
189 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
191 ByteArrayOutputStream baos = new ByteArrayOutputStream();
192 DataOutputStream dos = new DataOutputStream(baos);
193 innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {LocateDAO.Data.class, DataOutputStream.class });
194 innnerClassMtd.invoke(obj, new Object[] {data, dos });
196 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
197 DataInputStream dis = new DataInputStream(bais);
198 innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {LocateDAO.Data.class, DataInputStream.class });
199 innnerClassMtd.invoke(obj, new Object[] {data, dis });
201 } catch (InstantiationException e) {
202 // TODO Auto-generated catch block
204 } catch (IllegalAccessException e) {
205 // TODO Auto-generated catch block
207 } catch (IllegalArgumentException e) {
208 // TODO Auto-generated catch block
210 } catch (InvocationTargetException e) {
211 // TODO Auto-generated catch block
213 } catch (NoSuchMethodException e) {
214 // TODO Auto-generated catch block
216 } catch (SecurityException e) {
217 // TODO Auto-generated catch block
223 public void testData(){
224 LocateDAO.Data data = new LocateDAO.Data();
226 data.hostname="hostname";
229 } catch (IOException e) {
230 // TODO Auto-generated catch block
234 data.subprotocol(true);
236 Set<String> subProt = new HashSet<String>();
239 protField = LocateDAO.Data.class.getDeclaredField("subprotocol");
241 protField.setAccessible(true);
243 protField.set(data, subProt);
244 } catch (NoSuchFieldException | SecurityException e) {
245 // TODO Auto-generated catch block
247 } catch (IllegalArgumentException e) {
248 // TODO Auto-generated catch block
250 } catch (IllegalAccessException e) {
251 // TODO Auto-generated catch block
255 data.subprotocol(true);
256 subProt = new TreeSet<String>();
259 protField = LocateDAO.Data.class.getDeclaredField("subprotocol");
261 protField.setAccessible(true);
263 protField.set(data, subProt);
264 } catch (NoSuchFieldException | SecurityException e) {
265 // TODO Auto-generated catch block
267 } catch (IllegalArgumentException e) {
268 // TODO Auto-generated catch block
270 } catch (IllegalAccessException e) {
271 // TODO Auto-generated catch block
275 data.subprotocol(true);
276 data.subprotocol(false);
278 LocateDAO.Data newDate = data.copy();
279 assertTrue(data.name.equals(newDate.name));
284 class LocateDAOImpl extends LocateDAO{
287 public LocateDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS ) throws APIException, IOException {
288 super(trans, historyDAO);
289 setPs(this, readPS, "createPS");
293 public void setPs(LocateDAOImpl LocateDAOObj, PSInfo psInfoObj, String methodName) {
296 nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
298 nsDaoField.setAccessible(true);
299 // remove final modifier from field
300 Field modifiersField = Field.class.getDeclaredField("modifiers");
301 modifiersField.setAccessible(true);
302 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
304 nsDaoField.set(LocateDAOObj, psInfoObj);
305 } catch (NoSuchFieldException | SecurityException e) {
306 // TODO Auto-generated catch block
308 } catch (IllegalArgumentException e) {
309 // TODO Auto-generated catch block
311 } catch (IllegalAccessException e) {
312 // TODO Auto-generated catch block