Post Init Service Starter
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cass / JU_ConfigDAOTest.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.FileInputStream;
31 import java.io.FileNotFoundException;
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.List;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.onap.aaf.auth.dao.AbsCassDAO;
45 import org.onap.aaf.auth.dao.AbsCassDAO.CRUD;
46 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
47 import org.onap.aaf.auth.dao.CassAccess;
48 import org.onap.aaf.auth.dao.cass.ConfigDAO.Data;
49 import org.onap.aaf.auth.env.AuthzEnv;
50 import org.onap.aaf.auth.env.AuthzTrans;
51 import org.onap.aaf.auth.layer.Result;
52 import org.onap.aaf.cadi.config.Config;
53 import org.onap.aaf.misc.env.APIException;
54 import org.onap.aaf.misc.env.Decryptor;
55 import org.onap.aaf.misc.env.Env;
56 import org.onap.aaf.misc.env.LogTarget;
57 import org.onap.aaf.misc.env.TimeTaken;
58
59 import com.datastax.driver.core.Cluster;
60 import com.datastax.driver.core.ResultSet;
61 import com.datastax.driver.core.Row;
62 import com.datastax.driver.core.Session;
63
64 public class JU_ConfigDAOTest {
65
66         @Mock
67     AuthzTrans trans;
68         @Mock
69         Cluster cluster;
70         @Mock
71         Session session;
72         @Mock
73         AuthzEnv env;
74         @Mock
75         LogTarget logTarget;
76         
77         @Before
78         public void setUp() throws APIException, IOException {
79                 initMocks(this);
80                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
81                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
82                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
83                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
84                 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
85                 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE);
86                 Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE);
87                 Mockito.doReturn(session).when(cluster).connect("test");
88         }
89         
90         @Test
91         public void testInit() {
92                 TimeTaken tt = Mockito.mock(TimeTaken.class);
93                 Mockito.doReturn(tt).when(trans).start("ConfigDAO CREATE", Env.REMOTE);
94                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
95                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
96                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on ConfigDAO", Env.SUB);
97                 Mockito.doNothing().when(tt).done();
98                 ConfigDAO.Data data  = new ConfigDAO.Data();
99                 PSInfo createPS = Mockito.mock(PSInfo.class);
100                 Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]);
101                 Mockito.doReturn(rs).when(createPS).exec(trans, "ConfigDAOImpl CREATE", data);
102                 
103                 ConfigDAO daoObj=null;
104                 try {
105                         daoObj = new ConfigDAO(trans, cluster, "test");
106                 } catch (APIException | IOException e) {
107                         // TODO Auto-generated catch block
108                         e.printStackTrace();
109                 }
110
111         }
112         
113         @Test
114         public void testConfigLoader(){
115                 
116                 Class<?> innerClass = ConfigDAO.class.getDeclaredClasses()[0];
117         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
118         constructor.setAccessible(true);
119         try {
120                         Object obj = constructor.newInstance(1);
121                         Method innnerClassMtd;
122                                 
123                         ConfigDAO.Data data  = new ConfigDAO.Data();
124                         Row row = Mockito.mock(Row.class);
125                         ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
126                         bbObj.limit(7);
127                         bbObj.put(0, new Byte("0"));
128                         bbObj.put(1, new Byte("1"));
129                         bbObj.put(2, new Byte("2"));
130                         Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
131                         
132                         innnerClassMtd = innerClass.getMethod("load", new Class[] {ConfigDAO.Data.class, Row.class});
133                         innnerClassMtd.invoke(obj, new Object[] {data, row});
134                         
135                         innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {ConfigDAO.Data.class, Integer.TYPE, Object[].class });
136                         innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
137 //                      
138                         innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {ConfigDAO.Data.class, Integer.TYPE, Object[].class });
139                         innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
140                         
141                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
142                         DataOutputStream dos = new DataOutputStream(baos);
143                         innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {ConfigDAO.Data.class, DataOutputStream.class });
144                         innnerClassMtd.invoke(obj, new Object[] {data, dos });
145
146                         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
147                         DataInputStream dis = new DataInputStream(bais);
148                         innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {ConfigDAO.Data.class, DataInputStream.class });
149                         innnerClassMtd.invoke(obj, new Object[] {data, dis });
150                         
151                 } catch (InstantiationException e) {
152                         // TODO Auto-generated catch block
153                         e.printStackTrace();
154                 } catch (IllegalAccessException e) {
155                         // TODO Auto-generated catch block
156                         e.printStackTrace();
157                 } catch (IllegalArgumentException e) {
158                         // TODO Auto-generated catch block
159                         e.printStackTrace();
160                 } catch (InvocationTargetException e) {
161                         // TODO Auto-generated catch block
162                         e.printStackTrace();
163                 } catch (NoSuchMethodException e) {
164                         // TODO Auto-generated catch block
165                         e.printStackTrace();
166                 } catch (SecurityException e) {
167                         // TODO Auto-generated catch block
168                         e.printStackTrace();
169                 } 
170         }
171         
172         @Test
173         public void testWasMOdified() {
174                 TimeTaken tt = Mockito.mock(TimeTaken.class);
175                 Mockito.doReturn(tt).when(trans).start("ConfigDAO CREATE", Env.REMOTE);
176                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
177                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
178                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on ConfigDAO", Env.SUB);
179                 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
180                 Mockito.doNothing().when(tt).done();
181                 ConfigDAO.Data data  = new ConfigDAO.Data();
182                 PSInfo createPS = Mockito.mock(PSInfo.class);
183                 
184                 ConfigDAO daoObj = null;
185                 try {
186                         daoObj = new ConfigDAO(trans, cluster, "test");
187                 } catch (APIException | IOException e) {
188                         // TODO Auto-generated catch block
189                         e.printStackTrace();
190                 }
191                 
192                 daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"});
193         
194                 
195                 
196         }
197         
198         @Test
199         public void testRead() {
200                 TimeTaken tt = Mockito.mock(TimeTaken.class);
201                 Mockito.doReturn(tt).when(trans).start("ConfigDAO CREATE", Env.REMOTE);
202                 Mockito.doReturn(tt).when(trans).start("ConfigDAO READ", Env.REMOTE);
203                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
204                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
205                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo READ on ConfigDAO", Env.SUB);
206                 Mockito.doReturn(tt).when(trans).start(Mockito.anyString(),Mockito.anyInt());
207                 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100");
208                 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042");
209                 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100");
210                 Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null);
211                 Mockito.doReturn(Mockito.mock(Decryptor.class)).when(trans).decryptor();
212                 Mockito.doNothing().when(tt).done();
213                 
214                 Result<List<Data>>  rs1 = new Result<List<Data>>(null,0,"test",new String[0]);
215                 
216
217                 PSInfo psObj = Mockito.mock(PSInfo.class);
218                 ConfigDAOImpl daoObj = null;
219                 try {
220                         daoObj = new ConfigDAOImpl(trans, cluster, "test",psObj);
221                 } catch (APIException | IOException e) {
222                         // TODO Auto-generated catch block
223                         e.printStackTrace();
224                 }
225                 Mockito.doReturn(rs1).when(psObj).read(trans, "ConfigDAO READ", new Object[]{"test"});
226                 daoObj.readName(trans, "test");
227                 
228                 
229         }
230
231         
232         
233         @Test
234         public void testSecondConstructor() {
235                 TimeTaken tt = Mockito.mock(TimeTaken.class);
236                 Mockito.doReturn(tt).when(trans).start("ConfigDAO CREATE", Env.REMOTE);
237                 Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB);
238                 Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB);
239                 Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on ConfigDAO", Env.SUB);
240                 Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE);
241                 Mockito.doNothing().when(tt).done();
242                 AbsCassDAO absDAO = Mockito.mock(AbsCassDAO.class);
243
244                 try {
245                         ConfigDAO daoObj = new ConfigDAO(trans, absDAO);
246                 } catch (APIException | IOException e) {
247                         // TODO Auto-generated catch block
248                         e.printStackTrace();
249                 }
250         }
251 }
252
253 class ConfigDAOImpl extends ConfigDAO{
254
255         
256         public ConfigDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS  ) throws APIException, IOException {
257                 super(trans, cluster, keyspace);
258                 setPs(this, readPS, "psName");
259         }
260         
261
262         public void setPs(ConfigDAOImpl ConfigDAOObj, PSInfo psInfoObj, String methodName) {
263                 Field nsDaoField;
264                 try {
265                         nsDaoField = ConfigDAO.class.getDeclaredField(methodName);
266                         
267                         nsDaoField.setAccessible(true);
268                 // remove final modifier from field
269                 Field modifiersField = Field.class.getDeclaredField("modifiers");
270                 modifiersField.setAccessible(true);
271 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
272                 
273                 nsDaoField.set(ConfigDAOObj, psInfoObj);
274                 } catch (NoSuchFieldException | SecurityException e) {
275                         // TODO Auto-generated catch block
276                         e.printStackTrace();
277                 } catch (IllegalArgumentException e) {
278                         // TODO Auto-generated catch block
279                         e.printStackTrace();
280                 } catch (IllegalAccessException e) {
281                         // TODO Auto-generated catch block
282                         e.printStackTrace();
283                 }
284         }
285
286         
287         public void setSession(ConfigDAOImpl ConfigDAOObj, Session session) {
288                 Field nsDaoField;
289                 try {
290                         nsDaoField = AbsCassDAO.class.getDeclaredField("session");
291                         
292                         nsDaoField.setAccessible(true);
293                 // remove final modifier from field
294                 Field modifiersField = Field.class.getDeclaredField("modifiers");
295                 modifiersField.setAccessible(true);
296 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
297                 nsDaoField.set(ConfigDAOObj, session);
298                 } catch (NoSuchFieldException | SecurityException e) {
299                         // TODO Auto-generated catch block
300                         e.printStackTrace();
301                 } catch (IllegalArgumentException e) {
302                         // TODO Auto-generated catch block
303                         e.printStackTrace();
304                 } catch (IllegalAccessException e) {
305                         // TODO Auto-generated catch block
306                         e.printStackTrace();
307                 }
308         }
309         
310 }