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