2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.actions.test;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
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;
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;
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;
66 public class JU_URModify {
78 public void setUp() throws APIException, IOException {
80 Session sessionObj = Mockito.mock(Session.class);
81 PreparedStatement psObj = Mockito.mock(PreparedStatement.class);
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());
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");
98 modifyObj = new URModify(trans, cluster, true);
99 } catch (APIException e) {
100 // TODO Auto-generated catch block
102 } catch (IOException e) {
103 // TODO Auto-generated catch block
105 } catch (CadiException e) {
106 // TODO Auto-generated catch block
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"));
119 public void testExecElse() {
120 Question ques = Mockito.mock(Question.class);
122 UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
123 CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
125 List<Data> dataAL = new ArrayList<Data>();
126 Data data = new Data();
127 data.expires = new Date();
129 data.role="test.test";
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]);
134 Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
135 Mockito.doReturn(retVal1).when(userRoleDaoObj).update(trans, data);
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
143 } catch (IllegalArgumentException e) {
144 // TODO Auto-generated catch block
151 public void testExecElseUpdateFail() {
152 Question ques = Mockito.mock(Question.class);
154 UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
155 CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
157 List<Data> dataAL = new ArrayList<Data>();
158 Data data = new Data();
159 data.expires = new Date();
161 data.role="test.test";
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]);
166 Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
167 Mockito.doReturn(retVal1).when(userRoleDaoObj).update(trans, data);
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
175 } catch (IllegalArgumentException e) {
176 // TODO Auto-generated catch block
183 public void testExecElseUserDiff() {
184 Question ques = Mockito.mock(Question.class);
186 UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
187 CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
189 List<Data> dataAL = new ArrayList<Data>();
190 Data data = new Data();
191 data.expires = new Date();
193 Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 0, "test", new String[0]);
195 Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
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
203 } catch (IllegalArgumentException e) {
204 // TODO Auto-generated catch block
211 public void testExecElseReadNOK() {
212 Question ques = Mockito.mock(Question.class);
214 UserRole userRoleObj = new UserRole("test", "test", "test", new Date());
215 CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
217 List<Data> dataAL = new ArrayList<Data>();
218 Data data = new Data();
219 data.expires = new Date();
221 Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 1, "test", new String[0]);
223 Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role());
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
231 } catch (IllegalArgumentException e) {
232 // TODO Auto-generated catch block
239 public void test2Argonstructor() {
240 modifyObj = new URModify(trans, Mockito.mock(ActionDAO.class));
243 class URModifyImpl extends URModify{
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);
250 // TODO Auto-generated constructor stub
253 public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) {
254 Field field, nsDaoField;
256 field = URPuntImpl.class.getSuperclass().getSuperclass().getSuperclass().getDeclaredField("q");
257 nsDaoField = Question.class.getDeclaredField("userRoleDAO");
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);
267 field.set(this, ques);
268 nsDaoField.set(ques, userRoleDaoObj);
269 } catch (NoSuchFieldException | SecurityException e) {
270 // TODO Auto-generated catch block
272 } catch (IllegalArgumentException e) {
273 // TODO Auto-generated catch block
275 } catch (IllegalAccessException e) {
276 // TODO Auto-generated catch block