Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cass / JU_OAuthTokenDAO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.dao.cass;
23
24 import static org.mockito.MockitoAnnotations.initMocks;
25
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;
40
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;
56
57 import com.datastax.driver.core.Cluster;
58 import com.datastax.driver.core.Row;
59
60 public class JU_OAuthTokenDAO {
61
62     @Mock
63     AuthzTrans trans;
64     @Mock
65     Cluster cluster;
66     
67     @Before
68     public void setUp() throws APIException, IOException {
69         initMocks(this);
70     }
71
72     @Test
73     public void testInit() {
74         OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, cluster, "test");
75 //        daoObj.
76     }
77     @Test
78     public void testReadByUser() {
79         OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, cluster, "test");
80         
81         PSInfo psObj = Mockito.mock(PSInfo.class);
82         setPsByStartAndTarget(daoObj, psObj, "psByUser");
83         
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"});
86         
87         daoObj.readByUser(trans, "test");
88     }
89     
90     public void setPsByStartAndTarget(OAuthTokenDAO OAuthTokenDAOObj, PSInfo psInfoObj, String fieldName) {
91         Field nsDaoField;
92         try {
93             nsDaoField = OAuthTokenDAO.class.getDeclaredField(fieldName);
94             
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);
100             
101             nsDaoField.set(OAuthTokenDAOObj, psInfoObj);
102         } catch (NoSuchFieldException | SecurityException e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         } catch (IllegalArgumentException e) {
106             // TODO Auto-generated catch block
107             e.printStackTrace();
108         } catch (IllegalAccessException e) {
109             // TODO Auto-generated catch block
110             e.printStackTrace();
111         }
112     }
113     
114     @Test
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();
125
126         OAuthTokenDAO daoObj = null;
127         daoObj = new OAuthTokenDAO(trans, cluster, "test" );
128         daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
129         
130     }
131     
132     @Test
133     public void testSecondConstructor() {
134         AbsCassDAO absCassDAO = Mockito.mock(AbsCassDAO.class);
135
136         OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, absCassDAO);
137         
138     }
139
140     @Test
141     public void testData(){
142         OAuthTokenDAO.Data data = new OAuthTokenDAO.Data();
143         data.scopes = null;
144         data.scopes(true);
145
146         data.scopes = new HashSet<>();
147         data.scopes(true);
148
149         data.scopes(false);
150         data.scopes = new ConcurrentHashSet<>();
151         data.scopes(true);
152         
153         data.expires = new Date();
154         data.user="test";
155         data.id="id";
156         data.toString();
157         
158         data.active=true;
159         data.toString();
160         
161         try {
162             ByteBuffer bb = data.bytify();
163             data.reconstitute(bb);
164         } catch (IOException e) {
165             // TODO Auto-generated catch block
166             e.printStackTrace();
167         }
168     }
169     @Test
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")) {
175                 innerClass = indCls;
176                 break;
177             }
178         }
179         
180         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
181         constructor.setAccessible(true);
182         
183         try {
184             
185             Object obj = constructor.newInstance(1);
186             Method innnerClassMtd;
187                 
188             OAuthTokenDAO.Data data  = new OAuthTokenDAO.Data();
189             Row row = Mockito.mock(Row.class);
190             ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
191             bbObj.limit(7);
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);
196             
197             innnerClassMtd = innerClass.getMethod("load", new Class[] {OAuthTokenDAO.Data.class, Row.class});
198             innnerClassMtd.invoke(obj, new Object[] {data, row});
199             
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"} });
202 //            
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"} });
205             
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 });
210
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 });
215             
216         } catch (InstantiationException e) {
217             // TODO Auto-generated catch block
218             e.printStackTrace();
219         } catch (IllegalAccessException e) {
220             // TODO Auto-generated catch block
221             e.printStackTrace();
222         } catch (IllegalArgumentException e) {
223             // TODO Auto-generated catch block
224             e.printStackTrace();
225         } catch (InvocationTargetException e) {
226             // TODO Auto-generated catch block
227             e.printStackTrace();
228         } catch (NoSuchMethodException e) {
229             // TODO Auto-generated catch block
230             e.printStackTrace();
231         } catch (SecurityException e) {
232             // TODO Auto-generated catch block
233             e.printStackTrace();
234         } 
235     }
236     
237 }
238
239 class OAuthTokenDAOImpl extends OAuthTokenDAO{
240
241     
242     public OAuthTokenDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS  ) throws APIException, IOException {
243         super(trans, historyDAO);
244         setPs(this, readPS, "createPS");
245     }
246     
247
248     public void setPs(OAuthTokenDAOImpl OAuthTokenDAOObj, PSInfo psInfoObj, String methodName) {
249         Field nsDaoField;
250         try {
251             nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
252             
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);
258             
259             nsDaoField.set(OAuthTokenDAOObj, psInfoObj);
260         } catch (NoSuchFieldException | SecurityException e) {
261             // TODO Auto-generated catch block
262             e.printStackTrace();
263         } catch (IllegalArgumentException e) {
264             // TODO Auto-generated catch block
265             e.printStackTrace();
266         } catch (IllegalAccessException e) {
267             // TODO Auto-generated catch block
268             e.printStackTrace();
269         }
270     }
271     
272 }