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.IOException;
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31 import java.nio.ByteBuffer;
32 import java.util.Date;
33 import java.util.List;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.onap.aaf.auth.dao.AbsCassDAO;
40 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
41 import org.onap.aaf.auth.env.AuthzTrans;
42 import org.onap.aaf.auth.layer.Result;
43 import org.onap.aaf.misc.env.APIException;
45 import com.datastax.driver.core.Cluster;
46 import com.datastax.driver.core.Row;
48 public class JU_DelegateDAO {
56 public void setUp() throws APIException, IOException {
61 public void testInit() {
62 DelegateDAO daoObj = new DelegateDAO(trans, cluster, "test");
66 public void testReadByDelegate() {
67 DelegateDAO daoObj = new DelegateDAO(trans, cluster, "test");
69 PSInfo psObj = Mockito.mock(PSInfo.class);
70 setPsDelegate(daoObj, psObj, "psByDelegate");
72 Result<List<DelegateDAO.Data>> rs1 = new Result<List<DelegateDAO.Data>>(null,0,"test",new String[0]);
73 Mockito.doReturn(rs1).when(psObj).read(trans, "DelegateDAO READ", new Object[]{"test"});
75 daoObj.readByDelegate(trans, "test");
78 public void setPsDelegate(DelegateDAO delegateDAOObj, PSInfo psInfoObj, String fieldName) {
81 nsDaoField = DelegateDAO.class.getDeclaredField(fieldName);
83 nsDaoField.setAccessible(true);
84 // remove final modifier from field
85 Field modifiersField = Field.class.getDeclaredField("modifiers");
86 modifiersField.setAccessible(true);
87 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
89 nsDaoField.set(delegateDAOObj, psInfoObj);
90 } catch (NoSuchFieldException | SecurityException e) {
91 // TODO Auto-generated catch block
93 } catch (IllegalArgumentException e) {
94 // TODO Auto-generated catch block
96 } catch (IllegalAccessException e) {
97 // TODO Auto-generated catch block
103 public void testSecondConstructor() {
104 AbsCassDAO absDAO = Mockito.mock(AbsCassDAO.class);
106 DelegateDAO daoObj = new DelegateDAO(trans, absDAO);
111 public void testDelegateLoader(){
112 Class<?> innerClass = null;
113 Class<?>[] innerClassArr = DelegateDAO.class.getDeclaredClasses();
114 for(Class indCls:innerClassArr) {
115 if(indCls.getName().contains("DelegateLoader")) {
121 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
122 constructor.setAccessible(true);
124 Object obj = constructor.newInstance(1);
125 Method innnerClassMtd;
127 DelegateDAO.Data data = new DelegateDAO.Data();
128 Row row = Mockito.mock(Row.class);
129 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
131 bbObj.put(0, new Byte("0"));
132 bbObj.put(1, new Byte("1"));
133 bbObj.put(2, new Byte("2"));
134 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
136 innnerClassMtd = innerClass.getMethod("load", new Class[] {DelegateDAO.Data.class, Row.class});
137 innnerClassMtd.invoke(obj, new Object[] {data, row});
139 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {DelegateDAO.Data.class, Integer.TYPE, Object[].class });
140 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
142 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {DelegateDAO.Data.class, Integer.TYPE, Object[].class });
143 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
145 // DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_DelegateDAOTest.java"));
146 // innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {DelegateDAO.Data.class, DataOutputStream.class });
147 // innnerClassMtd.invoke(obj, new Object[] {data, dos });
149 // DataInputStream dis = new DataInputStream(new FileInputStream("JU_DelegateDAOTest.java"));
150 // innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {DelegateDAO.Data.class, DataInputStream.class });
151 // innnerClassMtd.invoke(obj, new Object[] {data, dis });
153 } catch (InstantiationException e) {
154 // TODO Auto-generated catch block
156 } catch (IllegalAccessException e) {
157 // TODO Auto-generated catch block
159 } catch (IllegalArgumentException e) {
160 // TODO Auto-generated catch block
162 } catch (InvocationTargetException e) {
163 // TODO Auto-generated catch block
165 } catch (NoSuchMethodException e) {
166 // TODO Auto-generated catch block
168 } catch (SecurityException e) {
169 // TODO Auto-generated catch block
175 public void testData(){
176 DelegateDAO.Data data = new DelegateDAO.Data();
178 data.delegate="delegate";
179 data.expires = new Date();
182 } catch (IOException e) {
183 // TODO Auto-generated catch block