Add more junits to auth cass
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / cass / JU_DelegateDAO.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.List;
39 import java.util.TreeSet;
40
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.onap.aaf.auth.dao.AbsCassDAO;
46 import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo;
47 import org.onap.aaf.auth.env.AuthzTrans;
48 import org.onap.aaf.auth.layer.Result;
49 import org.onap.aaf.misc.env.APIException;
50
51 import com.datastax.driver.core.Cluster;
52 import com.datastax.driver.core.Row;
53
54 public class JU_DelegateDAO {
55
56         @Mock
57     AuthzTrans trans;
58         @Mock
59         Cluster cluster;
60         
61         @Before
62         public void setUp() throws APIException, IOException {
63                 initMocks(this);
64         }
65
66         @Test
67         public void testInit() {
68                 DelegateDAO daoObj = new DelegateDAO(trans, cluster, "test");
69 //              daoObj.
70         }
71         @Test
72         public void testReadByDelegate() {
73                 DelegateDAO daoObj = new DelegateDAO(trans, cluster, "test");
74                 
75                 PSInfo psObj = Mockito.mock(PSInfo.class);
76                 setPsDelegate(daoObj, psObj, "psByDelegate");
77                 
78                 Result<List<DelegateDAO.Data>>  rs1 = new Result<List<DelegateDAO.Data>>(null,0,"test",new String[0]);
79                 Mockito.doReturn(rs1).when(psObj).read(trans, "DelegateDAO READ", new Object[]{"test"});
80                 
81                 daoObj.readByDelegate(trans, "test");
82         }
83         
84         public void setPsDelegate(DelegateDAO delegateDAOObj, PSInfo psInfoObj, String fieldName) {
85                 Field nsDaoField;
86                 try {
87                         nsDaoField = DelegateDAO.class.getDeclaredField(fieldName);
88                         
89                         nsDaoField.setAccessible(true);
90                 // remove final modifier from field
91                 Field modifiersField = Field.class.getDeclaredField("modifiers");
92                 modifiersField.setAccessible(true);
93 //              modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL);
94                 
95                 nsDaoField.set(delegateDAOObj, psInfoObj);
96                 } catch (NoSuchFieldException | SecurityException e) {
97                         // TODO Auto-generated catch block
98                         e.printStackTrace();
99                 } catch (IllegalArgumentException e) {
100                         // TODO Auto-generated catch block
101                         e.printStackTrace();
102                 } catch (IllegalAccessException e) {
103                         // TODO Auto-generated catch block
104                         e.printStackTrace();
105                 }
106         }
107         
108         @Test
109         public void testSecondConstructor() {
110                 AbsCassDAO absDAO = Mockito.mock(AbsCassDAO.class);
111
112                 DelegateDAO daoObj = new DelegateDAO(trans, absDAO);
113                 
114         }
115
116         @Test
117         public void testDelegateLoader(){
118                 
119                 Class<?> innerClass = DelegateDAO.class.getDeclaredClasses()[1];
120         Constructor<?> constructor = innerClass.getDeclaredConstructors()[0];
121         constructor.setAccessible(true);
122         try {
123                         Object obj = constructor.newInstance(1);
124                         Method innnerClassMtd;
125                                 
126                         DelegateDAO.Data data  = new DelegateDAO.Data();
127                         Row row = Mockito.mock(Row.class);
128                         ByteBuffer bbObj = ByteBuffer.allocateDirect(10);
129                         bbObj.limit(7);
130                         bbObj.put(0, new Byte("0"));
131                         bbObj.put(1, new Byte("1"));
132                         bbObj.put(2, new Byte("2"));
133                         Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1);
134                         
135                         innnerClassMtd = innerClass.getMethod("load", new Class[] {DelegateDAO.Data.class, Row.class});
136                         innnerClassMtd.invoke(obj, new Object[] {data, row});
137                         
138                         innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {DelegateDAO.Data.class, Integer.TYPE, Object[].class });
139                         innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} });
140 //                      
141                         innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {DelegateDAO.Data.class, Integer.TYPE, Object[].class });
142                         innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} });
143                         
144 //                      DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_DelegateDAOTest.java"));
145 //                      innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {DelegateDAO.Data.class, DataOutputStream.class });
146 //                      innnerClassMtd.invoke(obj, new Object[] {data, dos });
147
148 //                      DataInputStream dis = new DataInputStream(new FileInputStream("JU_DelegateDAOTest.java"));
149 //                      innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {DelegateDAO.Data.class, DataInputStream.class });
150 //                      innnerClassMtd.invoke(obj, new Object[] {data, dis });
151                         
152                 } catch (InstantiationException e) {
153                         // TODO Auto-generated catch block
154                         e.printStackTrace();
155                 } catch (IllegalAccessException e) {
156                         // TODO Auto-generated catch block
157                         e.printStackTrace();
158                 } catch (IllegalArgumentException e) {
159                         // TODO Auto-generated catch block
160                         e.printStackTrace();
161                 } catch (InvocationTargetException e) {
162                         // TODO Auto-generated catch block
163                         e.printStackTrace();
164                 } catch (NoSuchMethodException e) {
165                         // TODO Auto-generated catch block
166                         e.printStackTrace();
167                 } catch (SecurityException e) {
168                         // TODO Auto-generated catch block
169                         e.printStackTrace();
170                 } 
171         }
172         
173         @Test
174         public void testData(){
175                 DelegateDAO.Data data  = new DelegateDAO.Data();
176                 data.user="user";
177                 data.delegate="delegate";
178                 data.expires = new Date();
179                 try {
180                         data.bytify();
181                 } catch (IOException e) {
182                         // TODO Auto-generated catch block
183                         e.printStackTrace();
184                 }
185         }
186         
187 }