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