Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / actions / test / JU_URAdd.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.batch.actions.test;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Modifier;
30 import java.text.DateFormat;
31 import java.text.ParseException;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.Properties;
37
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.onap.aaf.auth.batch.actions.ActionDAO;
43 import org.onap.aaf.auth.batch.actions.URAdd;
44 import org.onap.aaf.auth.batch.helpers.UserRole;
45 import org.onap.aaf.auth.common.Define;
46 import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO;
47 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
48 import org.onap.aaf.auth.dao.hl.Question;
49 import org.onap.aaf.auth.env.AuthzTrans;
50 import org.onap.aaf.auth.layer.Result;
51 import org.onap.aaf.cadi.CadiException;
52 import org.onap.aaf.cadi.PropAccess;
53 import org.onap.aaf.cadi.config.Config;
54 import org.onap.aaf.misc.env.APIException;
55 import org.onap.aaf.misc.env.LogTarget;
56 import org.onap.aaf.misc.env.TimeTaken;
57
58 import com.datastax.driver.core.Cluster;
59 import com.datastax.driver.core.ColumnDefinitions;
60 import com.datastax.driver.core.PreparedId;
61 import com.datastax.driver.core.PreparedStatement;
62 import com.datastax.driver.core.Session;
63
64 public class JU_URAdd {
65     
66         @Mock
67     AuthzTrans trans;
68         @Mock
69         Cluster cluster;
70         @Mock
71         PropAccess access;
72         
73         @Mock
74         URAdd addObj;
75
76     
77     @Before
78     public void setUp() throws APIException, IOException {
79         initMocks(this);
80         Session sessionObj=Mockito.mock(Session.class);
81         PreparedStatement psObj =Mockito.mock(PreparedStatement.class);
82                 try {
83                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
84                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
85                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
86                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
87                         Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
88                         Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF);
89                         Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt());
90                         Mockito.doReturn(sessionObj).when(cluster).connect("authz");
91                         Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString());
92                         
93                         Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables();
94                         Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId();
95                         Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
96                         Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
97                         Define.set(access);
98                         addObj = new URAdd(trans, cluster, true);
99                 } catch (APIException e) {
100                         // TODO Auto-generated catch block
101                         e.printStackTrace();
102                 } catch (IOException e) {
103                         // TODO Auto-generated catch block
104                         e.printStackTrace();
105                 } catch (CadiException e) {
106                         // TODO Auto-generated catch block
107                         e.printStackTrace();
108                 }
109     }
110     
111     @Test
112         public void testExec() {
113                 Result<Data> retVal = addObj.exec(trans,Mockito.mock(UserRole.class),"test");
114                 assertTrue(retVal.toString().contains("Success"));
115                 
116         }
117     @Test
118         public void testExecElse() {
119         Question ques = Mockito.mock(Question.class);
120                 try {
121                         UserRole userRoleObj = new UserRole("test","test","test",new Date());
122                         CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
123                         
124                         List<Data> dataAL = new ArrayList<Data>();
125                         Data data = new Data();
126                         data.expires = new Date();
127                         dataAL.add(data);
128                         Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
129
130                         Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd());
131                         
132                         addObj = new URAddImpl(trans,  cluster, false, ques, userRoleDaoObj);
133                 Result<Data> session = addObj.exec(trans, userRoleObj, "test");
134                 assertTrue(0 == session.status);
135                 } catch (APIException | IOException e) {
136                         // TODO Auto-generated catch block
137                         e.printStackTrace();
138                 } catch (IllegalArgumentException e) {
139                         // TODO Auto-generated catch block
140                         e.printStackTrace();
141                 }
142                 
143         }
144     
145     @Test
146         public void testExecElseDateLess() {
147         Question ques = Mockito.mock(Question.class);
148                 try {
149                         UserRole userRoleObj = new UserRole("test","test","test",new Date());
150                         CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
151                         
152                         List<Data> dataAL = new ArrayList<Data>();
153                         Data data = new Data();
154                         DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
155                         try {
156                                 data.expires = sdf.parse("01/01/2100");
157                         } catch (ParseException e) {
158                                 // TODO Auto-generated catch block
159                                 e.printStackTrace();
160                         }
161                         dataAL.add(data);
162                         Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
163
164                         Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd());
165                         
166                         addObj = new URAddImpl(trans,  cluster, false, ques, userRoleDaoObj);
167                 Result<Data> session = addObj.exec(trans, userRoleObj, "test");
168                 assertTrue(0 == session.status);
169                 } catch (APIException | IOException e) {
170                         // TODO Auto-generated catch block
171                         e.printStackTrace();
172                 } catch (IllegalArgumentException e) {
173                         // TODO Auto-generated catch block
174                         e.printStackTrace();
175                 }
176                 
177         }
178     
179     @Test
180         public void testExecElseNok() {
181         Question ques = Mockito.mock(Question.class);
182                 try {
183                         UserRole userRoleObj = new UserRole("test","test","test",new Date());
184                         CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
185                         
186                         Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]);
187
188                         Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd());
189                         
190                         addObj = new URAddImpl(trans,  cluster, false, ques, userRoleDaoObj);
191                 Result<Data> session = addObj.exec(trans, userRoleObj, "test");
192                 assertTrue(session.toString().contains("test"));
193                 } catch (APIException | IOException e) {
194                         // TODO Auto-generated catch block
195                         e.printStackTrace();
196                 } catch (IllegalArgumentException e) {
197                         // TODO Auto-generated catch block
198                         e.printStackTrace();
199                 }
200                 
201         }
202
203     @Test
204         public void test2Argonstructor() {
205                 addObj = new URAdd(trans, Mockito.mock(ActionDAO.class));
206         }
207    
208     class URAddImpl extends URAdd{
209
210                 public URAddImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedUserRoleDAO userRoleDaoObj) throws APIException, IOException {
211                         super(trans, cluster, dryRun);
212                         setQuestion(ques, userRoleDaoObj);
213 //                      q =new Question(trans, cluster, CassAccess.KEYSPACE, false);
214 //                      q = ques;
215                         // TODO Auto-generated constructor stub
216                 }
217                 
218                 public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) {
219                         Field field, nsDaoField;
220                         try {
221                                 field = URAddImpl.class.getSuperclass().getSuperclass().getDeclaredField("q");
222                                 nsDaoField = Question.class.getDeclaredField("userRoleDAO");
223                                 
224                                 field.setAccessible(true);
225                                 nsDaoField.setAccessible(true);
226                         // remove final modifier from field
227                         Field modifiersField = Field.class.getDeclaredField("modifiers");
228                         modifiersField.setAccessible(true);
229                         modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
230                         modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL);
231                         
232                         field.set(this, ques);
233                         nsDaoField.set(ques, userRoleDaoObj);
234                         } catch (NoSuchFieldException | SecurityException e) {
235                                 // TODO Auto-generated catch block
236                                 e.printStackTrace();
237                         } catch (IllegalArgumentException e) {
238                                 // TODO Auto-generated catch block
239                                 e.printStackTrace();
240                         } catch (IllegalAccessException e) {
241                                 // TODO Auto-generated catch block
242                                 e.printStackTrace();
243                         }
244                 }
245     }
246 }