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.mockito.MockitoAnnotations.initMocks;
26 import java.io.DataInputStream;
27 import java.io.DataOutputStream;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.FileOutputStream;
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.Date;
38 import java.util.List;
39 import java.util.TreeSet;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.onap.aaf.auth.dao.AbsCassDAO;
46 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
47 import org.onap.aaf.auth.env.AuthzTrans;
48 import org.onap.aaf.auth.layer.Result;
49 import org.onap.aaf.misc.env.APIException;
51 import com.datastax.driver.core.Cluster;
52 import com.datastax.driver.core.Row;
54 public class JU_DelegateDAO {
62 public void setUp() throws APIException, IOException {
67 public void testInit() {
68 DelegateDAO daoObj = new DelegateDAO(trans, cluster, "test");
72 public void testReadByDelegate() {
73 DelegateDAO daoObj = new DelegateDAO(trans, cluster, "test");
75 PSInfo psObj = Mockito.mock(PSInfo.class);
76 setPsDelegate(daoObj, psObj, "psByDelegate");
78 Result<List<DelegateDAO.Data>> rs1 = new Result<List<DelegateDAO.Data>>(null,0,"test",new String[0]);
79 Mockito.doReturn(rs1).when(psObj).read(trans, "DelegateDAO READ", new Object[]{"test"});
81 daoObj.readByDelegate(trans, "test");
84 public void setPsDelegate(DelegateDAO delegateDAOObj, PSInfo psInfoObj, String fieldName) {
87 nsDaoField = DelegateDAO.class.getDeclaredField(fieldName);
89 nsDaoField.setAccessible(true);
90 // remove final modifier from field
91 Field modifiersField = Field.class.getDeclaredField("modifiers");
92 modifiersField.setAccessible(true);
93 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
95 nsDaoField.set(delegateDAOObj, psInfoObj);
96 } catch (NoSuchFieldException | SecurityException e) {
97 // TODO Auto-generated catch block
99 } catch (IllegalArgumentException e) {
100 // TODO Auto-generated catch block
102 } catch (IllegalAccessException e) {
103 // TODO Auto-generated catch block
109 public void testSecondConstructor() {
110 AbsCassDAO absDAO = Mockito.mock(AbsCassDAO.class);
112 DelegateDAO daoObj = new DelegateDAO(trans, absDAO);
117 public void testDelegateLoader(){
119 Class<?> innerClass = DelegateDAO.class.getDeclaredClasses()[1];
120 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
121 constructor.setAccessible(true);
123 Object obj = constructor.newInstance(1);
124 Method innnerClassMtd;
126 DelegateDAO.Data data = new DelegateDAO.Data();
127 Row row = Mockito.mock(Row.class);
128 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
130 bbObj.put(0, new Byte("0"));
131 bbObj.put(1, new Byte("1"));
132 bbObj.put(2, new Byte("2"));
133 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
135 innnerClassMtd = innerClass.getMethod("load", new Class[] {DelegateDAO.Data.class, Row.class});
136 innnerClassMtd.invoke(obj, new Object[] {data, row});
138 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {DelegateDAO.Data.class, Integer.TYPE, Object[].class });
139 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
141 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {DelegateDAO.Data.class, Integer.TYPE, Object[].class });
142 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
144 // DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_DelegateDAOTest.java"));
145 // innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {DelegateDAO.Data.class, DataOutputStream.class });
146 // innnerClassMtd.invoke(obj, new Object[] {data, dos });
148 // DataInputStream dis = new DataInputStream(new FileInputStream("JU_DelegateDAOTest.java"));
149 // innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {DelegateDAO.Data.class, DataInputStream.class });
150 // innnerClassMtd.invoke(obj, new Object[] {data, dis });
152 } catch (InstantiationException e) {
153 // TODO Auto-generated catch block
155 } catch (IllegalAccessException e) {
156 // TODO Auto-generated catch block
158 } catch (IllegalArgumentException e) {
159 // TODO Auto-generated catch block
161 } catch (InvocationTargetException e) {
162 // TODO Auto-generated catch block
164 } catch (NoSuchMethodException e) {
165 // TODO Auto-generated catch block
167 } catch (SecurityException e) {
168 // TODO Auto-generated catch block
174 public void testData(){
175 DelegateDAO.Data data = new DelegateDAO.Data();
177 data.delegate="delegate";
178 data.expires = new Date();
181 } catch (IOException e) {
182 // TODO Auto-generated catch block