Add junits to auth cass
[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.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.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                         DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_OAuthTokenDAOTest.java"));
207                         innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {OAuthTokenDAO.Data.class, DataOutputStream.class });
208                         innnerClassMtd.invoke(obj, new Object[] {data, dos });
209
210                         DataInputStream dis = new DataInputStream(new FileInputStream("JU_OAuthTokenDAOTest.java"));
211                         innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {OAuthTokenDAO.Data.class, DataInputStream.class });
212                         innnerClassMtd.invoke(obj, new Object[] {data, dis });
213                         
214                 } catch (InstantiationException e) {
215                         // TODO Auto-generated catch block
216                         e.printStackTrace();
217                 } catch (IllegalAccessException e) {
218                         // TODO Auto-generated catch block
219                         e.printStackTrace();
220                 } catch (IllegalArgumentException e) {
221                         // TODO Auto-generated catch block
222                         e.printStackTrace();
223                 } catch (InvocationTargetException e) {
224                         // TODO Auto-generated catch block
225                         e.printStackTrace();
226                 } catch (NoSuchMethodException e) {
227                         // TODO Auto-generated catch block
228                         e.printStackTrace();
229                 } catch (SecurityException e) {
230                         // TODO Auto-generated catch block
231                         e.printStackTrace();
232                 } catch (FileNotFoundException e) {
233                         // TODO Auto-generated catch block
234                         e.printStackTrace();
235                 } 
236         }
237         
238 }
239
240 class OAuthTokenDAOImpl extends OAuthTokenDAO{
241
242         
243         public OAuthTokenDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS  ) throws APIException, IOException {
244                 super(trans, historyDAO);
245                 setPs(this, readPS, "createPS");
246         }
247         
248
249         public void setPs(OAuthTokenDAOImpl OAuthTokenDAOObj, PSInfo psInfoObj, String methodName) {
250                 Field nsDaoField;
251                 try {
252                         nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
253                         
254                         nsDaoField.setAccessible(true);
255                 // remove final modifier from field
256                 Field modifiersField = Field.class.getDeclaredField("modifiers");
257                 modifiersField.setAccessible(true);
258 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
259                 
260                 nsDaoField.set(OAuthTokenDAOObj, psInfoObj);
261                 } catch (NoSuchFieldException | SecurityException e) {
262                         // TODO Auto-generated catch block
263                         e.printStackTrace();
264                 } catch (IllegalArgumentException e) {
265                         // TODO Auto-generated catch block
266                         e.printStackTrace();
267                 } catch (IllegalAccessException e) {
268                         // TODO Auto-generated catch block
269                         e.printStackTrace();
270                 }
271         }
272         
273 }