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