Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / actions / test / JU_URModify.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.util.ArrayList;
31 import java.util.Calendar;
32 import java.util.Date;
33 import java.util.List;
34 import java.util.Properties;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.onap.aaf.auth.batch.actions.ActionDAO;
41 import org.onap.aaf.auth.batch.actions.URModify;
42 import org.onap.aaf.auth.batch.actions.URPunt;
43 import org.onap.aaf.auth.batch.actions.URModify.Modify;
44 import org.onap.aaf.auth.batch.actions.test.JU_URPunt.URPuntImpl;
45 import org.onap.aaf.auth.batch.helpers.UserRole;
46 import org.onap.aaf.auth.common.Define;
47 import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO;
48 import org.onap.aaf.auth.dao.cass.Status;
49 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
50 import org.onap.aaf.auth.dao.hl.Question;
51 import org.onap.aaf.auth.env.AuthzTrans;
52 import org.onap.aaf.auth.layer.Result;
53 import org.onap.aaf.cadi.CadiException;
54 import org.onap.aaf.cadi.PropAccess;
55 import org.onap.aaf.cadi.config.Config;
56 import org.onap.aaf.misc.env.APIException;
57 import org.onap.aaf.misc.env.LogTarget;
58 import org.onap.aaf.misc.env.TimeTaken;
59
60 import com.datastax.driver.core.Cluster;
61 import com.datastax.driver.core.ColumnDefinitions;
62 import com.datastax.driver.core.PreparedId;
63 import com.datastax.driver.core.PreparedStatement;
64 import com.datastax.driver.core.Session;
65
66 public class JU_URModify {
67         @Mock
68         AuthzTrans trans;
69         @Mock
70         Cluster cluster;
71         @Mock
72         PropAccess access;
73
74         @Mock
75         URModify modifyObj;
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                         modifyObj = new URModify(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 testURPrint() {
113                 UserRole ur = new UserRole("user", "ns", "rname", Calendar.getInstance().getTime());
114                 Result<Void> retVal = modifyObj.exec(trans, ur, Mockito.mock(Modify.class));
115                 assertTrue(retVal.toString().contains("Success"));
116         }
117
118         @Test
119         public void testExecElse() {
120                 Question ques = Mockito.mock(Question.class);
121                 try {
122                         UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
123                         CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
124
125                         List<Data> dataAL = new ArrayList<Data>();
126                         Data data = new Data();
127                         data.expires = new Date();
128                         data.user="test";
129                         data.role="test.test";
130                         dataAL.add(data);
131                         Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 0, "test", new String[0]);
132                         Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL, 0, "test", new String[0]);
133
134                         Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
135                         Mockito.doReturn(retVal1).when(userRoleDaoObj).update(trans, data);
136
137                         modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj);
138                         Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class));
139                         assertTrue(Status.ERR_UserRoleNotFound == session.status);
140                 } catch (APIException | IOException e) {
141                         // TODO Auto-generated catch block
142                         e.printStackTrace();
143                 } catch (IllegalArgumentException e) {
144                         // TODO Auto-generated catch block
145                         e.printStackTrace();
146                 }
147
148         }
149         
150         @Test
151         public void testExecElseUpdateFail() {
152                 Question ques = Mockito.mock(Question.class);
153                 try {
154                         UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
155                         CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
156
157                         List<Data> dataAL = new ArrayList<Data>();
158                         Data data = new Data();
159                         data.expires = new Date();
160                         data.user="test";
161                         data.role="test.test";
162                         dataAL.add(data);
163                         Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 0, "test", new String[0]);
164                         Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL, 1, "test", new String[0]);
165
166                         Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
167                         Mockito.doReturn(retVal1).when(userRoleDaoObj).update(trans, data);
168
169                         modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj);
170                         Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class));
171                         assertTrue(Status.ERR_UserRoleNotFound == session.status);
172                 } catch (APIException | IOException e) {
173                         // TODO Auto-generated catch block
174                         e.printStackTrace();
175                 } catch (IllegalArgumentException e) {
176                         // TODO Auto-generated catch block
177                         e.printStackTrace();
178                 }
179
180         }
181         
182         @Test
183         public void testExecElseUserDiff() {
184                 Question ques = Mockito.mock(Question.class);
185                 try {
186                         UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
187                         CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
188
189                         List<Data> dataAL = new ArrayList<Data>();
190                         Data data = new Data();
191                         data.expires = new Date();
192                         dataAL.add(data);
193                         Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 0, "test", new String[0]);
194
195                         Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
196
197                         modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj);
198                         Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class));
199                         assertTrue(Status.ERR_Denied == session.status);
200                 } catch (APIException | IOException e) {
201                         // TODO Auto-generated catch block
202                         e.printStackTrace();
203                 } catch (IllegalArgumentException e) {
204                         // TODO Auto-generated catch block
205                         e.printStackTrace();
206                 }
207
208         }
209         
210         @Test
211         public void testExecElseReadNOK() {
212                 Question ques = Mockito.mock(Question.class);
213                 try {
214                         UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
215                         CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
216
217                         List<Data> dataAL = new ArrayList<Data>();
218                         Data data = new Data();
219                         data.expires = new Date();
220                         dataAL.add(data);
221                         Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 1, "test", new String[0]);
222
223                         Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
224
225                         modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj);
226                         Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class));
227                         assertTrue(1 == session.status);
228                 } catch (APIException | IOException e) {
229                         // TODO Auto-generated catch block
230                         e.printStackTrace();
231                 } catch (IllegalArgumentException e) {
232                         // TODO Auto-generated catch block
233                         e.printStackTrace();
234                 }
235
236         }
237         
238         @Test
239         public void test2Argonstructor() {
240                 modifyObj = new URModify(trans, Mockito.mock(ActionDAO.class));
241         }
242         
243         class URModifyImpl extends URModify{
244
245                 public URModifyImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedUserRoleDAO userRoleDaoObj) throws APIException, IOException {
246                         super(trans, cluster, dryRun);
247                         setQuestion(ques, userRoleDaoObj);
248 //                      q =new Question(trans, cluster, CassAccess.KEYSPACE, false);
249 //                      q = ques;
250                         // TODO Auto-generated constructor stub
251                 }
252                 
253                 public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) {
254                         Field field, nsDaoField;
255                         try {
256                                 field = URPuntImpl.class.getSuperclass().getSuperclass().getSuperclass().getDeclaredField("q");
257                                 nsDaoField = Question.class.getDeclaredField("userRoleDAO");
258                                 
259                                 field.setAccessible(true);
260                                 nsDaoField.setAccessible(true);
261                         // remove final modifier from field
262                         Field modifiersField = Field.class.getDeclaredField("modifiers");
263                         modifiersField.setAccessible(true);
264                         modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
265                         modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL);
266                         
267                         field.set(this, ques);
268                         nsDaoField.set(ques, userRoleDaoObj);
269                         } catch (NoSuchFieldException | SecurityException e) {
270                                 // TODO Auto-generated catch block
271                                 e.printStackTrace();
272                         } catch (IllegalArgumentException e) {
273                                 // TODO Auto-generated catch block
274                                 e.printStackTrace();
275                         } catch (IllegalAccessException e) {
276                                 // TODO Auto-generated catch block
277                                 e.printStackTrace();
278                         }
279                 }
280     }
281 }