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.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.DataInputStream;
29 import java.io.DataOutputStream;
30 import java.io.FileNotFoundException;
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.HashSet;
39 import java.util.List;
41 import org.eclipse.jetty.util.ConcurrentHashSet;
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;
53 import org.onap.aaf.misc.env.Env;
54 import org.onap.aaf.misc.env.LogTarget;
55 import org.onap.aaf.misc.env.TimeTaken;
57 import com.datastax.driver.core.Cluster;
58 import com.datastax.driver.core.Row;
60 public class JU_OAuthTokenDAO {
68 public void setUp() throws APIException, IOException {
73 public void testInit() {
74 OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, cluster, "test");
78 public void testReadByUser() {
79 OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, cluster, "test");
81 PSInfo psObj = Mockito.mock(PSInfo.class);
82 setPsByStartAndTarget(daoObj, psObj, "psByUser");
84 Result<List<OAuthTokenDAO.Data>> rs1 = new Result<List<OAuthTokenDAO.Data>>(null,0,"test",new String[0]);
85 Mockito.doReturn(rs1).when(psObj).read(trans, "OAuthTokenDAO READ", new Object[]{"test"});
87 daoObj.readByUser(trans, "test");
90 public void setPsByStartAndTarget(OAuthTokenDAO OAuthTokenDAOObj, PSInfo psInfoObj, String fieldName) {
93 nsDaoField = OAuthTokenDAO.class.getDeclaredField(fieldName);
95 nsDaoField.setAccessible(true);
96 // remove final modifier from field
97 Field modifiersField = Field.class.getDeclaredField("modifiers");
98 modifiersField.setAccessible(true);
99 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
101 nsDaoField.set(OAuthTokenDAOObj, psInfoObj);
102 } catch (NoSuchFieldException | SecurityException e) {
103 // TODO Auto-generated catch block
105 } catch (IllegalArgumentException e) {
106 // TODO Auto-generated catch block
108 } catch (IllegalAccessException e) {
109 // TODO Auto-generated catch block
115 public void testWasMOdified() {
116 TimeTaken tt = Mockito.mock(TimeTaken.class);
117 Mockito.doReturn(tt).when(trans).start("OAuthTokenDAO CREATE", Env.REMOTE);
118 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
119 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
120 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on OAuthTokenDAO", Env.SUB);
121 Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE);
122 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
123 Mockito.doNothing().when(tt).done();
124 OAuthTokenDAO.Data data = new OAuthTokenDAO.Data();
126 OAuthTokenDAO daoObj = null;
127 daoObj = new OAuthTokenDAO(trans, cluster, "test" );
128 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
133 public void testSecondConstructor() {
134 AbsCassDAO absCassDAO = Mockito.mock(AbsCassDAO.class);
136 OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, absCassDAO);
141 public void testData(){
142 OAuthTokenDAO.Data data = new OAuthTokenDAO.Data();
146 data.scopes = new HashSet<>();
150 data.scopes = new ConcurrentHashSet<>();
153 data.expires = new Date();
162 ByteBuffer bb = data.bytify();
163 data.reconstitute(bb);
164 } catch (IOException e) {
165 // TODO Auto-generated catch block
170 public void testOAuthLoader(){
171 Class<?> innerClass = null;
172 Class<?>[] innerClassArr = OAuthTokenDAO.class.getDeclaredClasses();
173 for(Class indCls:innerClassArr) {
174 if(indCls.getName().contains("OAuthLoader")) {
180 Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
181 constructor.setAccessible(true);
185 Object obj = constructor.newInstance(1);
186 Method innnerClassMtd;
188 OAuthTokenDAO.Data data = new OAuthTokenDAO.Data();
189 Row row = Mockito.mock(Row.class);
190 ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
192 bbObj.put(0, new Byte("0"));
193 bbObj.put(1, new Byte("1"));
194 bbObj.put(2, new Byte("2"));
195 Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
197 innnerClassMtd = innerClass.getMethod("load", new Class[] {OAuthTokenDAO.Data.class, Row.class});
198 innnerClassMtd.invoke(obj, new Object[] {data, row});
200 innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {OAuthTokenDAO.Data.class, Integer.TYPE, Object[].class });
201 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
203 innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {OAuthTokenDAO.Data.class, Integer.TYPE, Object[].class });
204 innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test","test"} });
206 ByteArrayOutputStream baos = new ByteArrayOutputStream();
207 DataOutputStream dos = new DataOutputStream(baos);
208 innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {OAuthTokenDAO.Data.class, DataOutputStream.class });
209 innnerClassMtd.invoke(obj, new Object[] {data, dos });
211 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
212 DataInputStream dis = new DataInputStream(bais);
213 innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {OAuthTokenDAO.Data.class, DataInputStream.class });
214 innnerClassMtd.invoke(obj, new Object[] {data, dis });
216 } catch (InstantiationException e) {
217 // TODO Auto-generated catch block
219 } catch (IllegalAccessException e) {
220 // TODO Auto-generated catch block
222 } catch (IllegalArgumentException e) {
223 // TODO Auto-generated catch block
225 } catch (InvocationTargetException e) {
226 // TODO Auto-generated catch block
228 } catch (NoSuchMethodException e) {
229 // TODO Auto-generated catch block
231 } catch (SecurityException e) {
232 // TODO Auto-generated catch block
239 class OAuthTokenDAOImpl extends OAuthTokenDAO{
242 public OAuthTokenDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS ) throws APIException, IOException {
243 super(trans, historyDAO);
244 setPs(this, readPS, "createPS");
248 public void setPs(OAuthTokenDAOImpl OAuthTokenDAOObj, PSInfo psInfoObj, String methodName) {
251 nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
253 nsDaoField.setAccessible(true);
254 // remove final modifier from field
255 Field modifiersField = Field.class.getDeclaredField("modifiers");
256 modifiersField.setAccessible(true);
257 // modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
259 nsDaoField.set(OAuthTokenDAOObj, psInfoObj);
260 } catch (NoSuchFieldException | SecurityException e) {
261 // TODO Auto-generated catch block
263 } catch (IllegalArgumentException e) {
264 // TODO Auto-generated catch block
266 } catch (IllegalAccessException e) {
267 // TODO Auto-generated catch block