Add more junits to auth cass
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cass / JU_LocateDAO.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.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import java.io.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.Field;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.nio.ByteBuffer;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Set;
41 import java.util.TreeSet;
42
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.onap.aaf.auth.dao.AbsCassDAO;
48 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
49 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
50 import org.onap.aaf.auth.dao.CassDAOImpl;
51 import org.onap.aaf.auth.env.AuthzTrans;
52 import org.onap.aaf.auth.layer.Result;
53 import org.onap.aaf.misc.env.APIException;
54
55 import com.datastax.driver.core.Cluster;
56 import com.datastax.driver.core.Row;
57
58 public class JU_LocateDAO {
59
60         @Mock
61     AuthzTrans trans;
62         @Mock
63         Cluster cluster;
64         
65         @Before
66         public void setUp() throws APIException, IOException {
67                 initMocks(this);
68         }
69
70         @Test
71         public void testInit() {
72                 try {
73                         LocateDAO daoObj = new LocateDAO(trans, cluster, "test");
74                 } catch (APIException | IOException e) {
75                         // TODO Auto-generated catch block
76                         e.printStackTrace();
77                 }
78         }
79         @Test
80         public void testReadByStartAndTarget() {
81                 LocateDAO daoObj = null;
82                 try {
83                         daoObj = new LocateDAO(trans, cluster, "test");
84                 } catch (APIException | IOException e) {
85                         // TODO Auto-generated catch block
86                         e.printStackTrace();
87                 }
88                 
89                 PSInfo psObj = Mockito.mock(PSInfo.class);
90                 setPsByStartAndTarget(daoObj, psObj, "psName");
91                 
92                 Result<List<LocateDAO.Data>>  rs1 = new Result<List<LocateDAO.Data>>(null,0,"test",new String[0]);
93                 Mockito.doReturn(rs1).when(psObj).read(trans, "LocateDAO READ", new Object[]{"test"});
94                 
95                 daoObj.readByName(trans, "test");
96         }
97         
98         
99         public void setPsByStartAndTarget(LocateDAO LocateDAOObj, PSInfo psInfoObj, String fieldName) {
100                 Field nsDaoField;
101                 try {
102                         nsDaoField = LocateDAO.class.getDeclaredField(fieldName);
103                         
104                         nsDaoField.setAccessible(true);
105                 // remove final modifier from field
106                 Field modifiersField = Field.class.getDeclaredField("modifiers");
107                 modifiersField.setAccessible(true);
108 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
109                 
110                 nsDaoField.set(LocateDAOObj, psInfoObj);
111                 } catch (NoSuchFieldException | SecurityException e) {
112                         // TODO Auto-generated catch block
113                         e.printStackTrace();
114                 } catch (IllegalArgumentException e) {
115                         // TODO Auto-generated catch block
116                         e.printStackTrace();
117                 } catch (IllegalAccessException e) {
118                         // TODO Auto-generated catch block
119                         e.printStackTrace();
120                 }
121         }
122         
123         @Test
124         public void testWasMOdified() {
125                 
126                 LocateDAO.Data data  = new LocateDAO.Data();
127                 
128                 HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class);
129                 
130                 LocateDAO daoObj = null;
131                 try {
132                         daoObj = new LocateDAO(trans, historyDAO );
133                 } catch (APIException | IOException e) {
134                         // TODO Auto-generated catch block
135                         e.printStackTrace();
136                 }
137                 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
138                 
139                 
140         }
141         
142         @Test
143         public void testSecondConstructor() {
144                 AbsCassDAO historyDAO = Mockito.mock(AbsCassDAO.class);
145
146                 try {
147                         LocateDAO daoObj = new LocateDAO(trans, historyDAO);
148                 } catch (APIException | IOException e) {
149                         // TODO Auto-generated catch block
150                         e.printStackTrace();
151                 }
152                 
153         }
154
155         @Test
156         public void testLocateLoader(){
157                 Class<?> innerClass = null;
158                 Class<?>[] innerClassArr = LocateDAO.class.getDeclaredClasses();
159                 for(Class indCls:innerClassArr) {
160                         if(indCls.getName().contains("LocateLoader")) {
161                                 innerClass = indCls;
162                                 break;
163                         }
164                 }
165                 
166         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
167         constructor.setAccessible(true);
168         
169         try {
170                 
171                         Object obj = constructor.newInstance(1);
172                         Method innnerClassMtd;
173                                 
174                         LocateDAO.Data data  = new LocateDAO.Data();
175                         Row row = Mockito.mock(Row.class);
176                         ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
177                         bbObj.limit(7);
178                         bbObj.put(0, new Byte("0"));
179                         bbObj.put(1, new Byte("1"));
180                         bbObj.put(2, new Byte("2"));
181                         Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
182                         
183                         innnerClassMtd = innerClass.getMethod("load", new Class[] {LocateDAO.Data.class, Row.class});
184                         innnerClassMtd.invoke(obj, new Object[] {data, row});
185                         
186                         innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {LocateDAO.Data.class, Integer.TYPE, Object[].class });
187                         innnerClassMtd.invoke(obj, new Object[] {data, 0, new Object[] {"test","test","test"} });
188                         
189                         innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {LocateDAO.Data.class, Integer.TYPE, Object[].class });
190                         innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
191                         
192                         DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_LocateDAOTest.java"));
193                         innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {LocateDAO.Data.class, DataOutputStream.class });
194                         innnerClassMtd.invoke(obj, new Object[] {data, dos });
195
196                         DataInputStream dis = new DataInputStream(new FileInputStream("JU_LocateDAOTest.java"));
197                         innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {LocateDAO.Data.class, DataInputStream.class });
198                         innnerClassMtd.invoke(obj, new Object[] {data, dis });
199                         
200                 } catch (InstantiationException e) {
201                         // TODO Auto-generated catch block
202                         e.printStackTrace();
203                 } catch (IllegalAccessException e) {
204                         // TODO Auto-generated catch block
205                         e.printStackTrace();
206                 } catch (IllegalArgumentException e) {
207                         // TODO Auto-generated catch block
208                         e.printStackTrace();
209                 } catch (InvocationTargetException e) {
210                         // TODO Auto-generated catch block
211                         e.printStackTrace();
212                 } catch (NoSuchMethodException e) {
213                         // TODO Auto-generated catch block
214                         e.printStackTrace();
215                 } catch (SecurityException e) {
216                         // TODO Auto-generated catch block
217                         e.printStackTrace();
218                 } catch (FileNotFoundException e) {
219                         // TODO Auto-generated catch block
220                         e.printStackTrace();
221                 } 
222         }
223         
224         @Test
225         public void testData(){
226                 LocateDAO.Data data  = new LocateDAO.Data();
227                 data.name="name";
228                 data.hostname="hostname";
229                 try {
230                         data.bytify();
231                 } catch (IOException e) {
232                         // TODO Auto-generated catch block
233                         e.printStackTrace();
234                 }
235                 
236                 data.subprotocol(true);
237                 
238                 Set<String> subProt = new HashSet<String>();
239                 Field protField;
240                 try {
241                         protField = LocateDAO.Data.class.getDeclaredField("subprotocol");
242                         
243                         protField.setAccessible(true);
244                 
245                         protField.set(data, subProt);
246                 } catch (NoSuchFieldException | SecurityException e) {
247                         // TODO Auto-generated catch block
248                         e.printStackTrace();
249                 } catch (IllegalArgumentException e) {
250                         // TODO Auto-generated catch block
251                         e.printStackTrace();
252                 } catch (IllegalAccessException e) {
253                         // TODO Auto-generated catch block
254                         e.printStackTrace();
255                 }
256                 
257                 data.subprotocol(true);
258                 subProt = new TreeSet<String>();
259                 subProt.add("test");
260                 try {
261                         protField = LocateDAO.Data.class.getDeclaredField("subprotocol");
262                         
263                         protField.setAccessible(true);
264                 
265                         protField.set(data, subProt);
266                 } catch (NoSuchFieldException | SecurityException e) {
267                         // TODO Auto-generated catch block
268                         e.printStackTrace();
269                 } catch (IllegalArgumentException e) {
270                         // TODO Auto-generated catch block
271                         e.printStackTrace();
272                 } catch (IllegalAccessException e) {
273                         // TODO Auto-generated catch block
274                         e.printStackTrace();
275                 }
276                 
277                 data.subprotocol(true);
278                 data.subprotocol(false);
279                 
280                 LocateDAO.Data newDate = data.copy();
281                 assertTrue(data.name.equals(newDate.name));
282         }
283         
284 }
285
286 class LocateDAOImpl extends LocateDAO{
287
288         
289         public LocateDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS  ) throws APIException, IOException {
290                 super(trans, historyDAO);
291                 setPs(this, readPS, "createPS");
292         }
293         
294
295         public void setPs(LocateDAOImpl LocateDAOObj, PSInfo psInfoObj, String methodName) {
296                 Field nsDaoField;
297                 try {
298                         nsDaoField = CassDAOImpl.class.getDeclaredField(methodName);
299                         
300                         nsDaoField.setAccessible(true);
301                 // remove final modifier from field
302                 Field modifiersField = Field.class.getDeclaredField("modifiers");
303                 modifiersField.setAccessible(true);
304 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
305                 
306                 nsDaoField.set(LocateDAOObj, psInfoObj);
307                 } catch (NoSuchFieldException | SecurityException e) {
308                         // TODO Auto-generated catch block
309                         e.printStackTrace();
310                 } catch (IllegalArgumentException e) {
311                         // TODO Auto-generated catch block
312                         e.printStackTrace();
313                 } catch (IllegalAccessException e) {
314                         // TODO Auto-generated catch block
315                         e.printStackTrace();
316                 }
317         }
318         
319 }