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.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.HashSet;
36 import java.util.List;
37 import java.util.Properties;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.onap.aaf.auth.batch.actions.ActionDAO;
44 import org.onap.aaf.auth.batch.actions.RoleCreate;
45 import org.onap.aaf.auth.batch.helpers.Role;
46 import org.onap.aaf.auth.batch.helpers.UserRole;
47 import org.onap.aaf.auth.common.Define;
48 import org.onap.aaf.auth.dao.cached.CachedRoleDAO;
49 import org.onap.aaf.auth.dao.cass.RoleDAO;
50 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
51 import org.onap.aaf.auth.dao.hl.Question;
52 import org.onap.aaf.auth.env.AuthzTrans;
53 import org.onap.aaf.auth.layer.Result;
54 import org.onap.aaf.cadi.CadiException;
55 import org.onap.aaf.cadi.PropAccess;
56 import org.onap.aaf.cadi.config.Config;
57 import org.onap.aaf.misc.env.APIException;
58 import org.onap.aaf.misc.env.LogTarget;
59 import org.onap.aaf.misc.env.TimeTaken;
61 import com.datastax.driver.core.Cluster;
62 import com.datastax.driver.core.ColumnDefinitions;
63 import com.datastax.driver.core.PreparedId;
64 import com.datastax.driver.core.PreparedStatement;
65 import com.datastax.driver.core.Session;
67 public class JU_RoleCreate {
81 public void setUp() throws APIException, IOException {
83 Session sessionObj=Mockito.mock(Session.class);
84 PreparedStatement psObj =Mockito.mock(PreparedStatement.class);
86 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
87 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
88 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
89 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
90 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
91 Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF);
92 Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt());
93 Mockito.doReturn(sessionObj).when(cluster).connect("authz");
94 Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString());
96 Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables();
97 Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId();
98 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
99 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
101 createObj = new RoleCreate(trans, cluster, true);
102 } catch (APIException e) {
103 // TODO Auto-generated catch block
105 } catch (IOException e) {
106 // TODO Auto-generated catch block
108 } catch (CadiException e) {
109 // TODO Auto-generated catch block
115 public void testExec() {
116 Result<RoleDAO.Data> retVal = createObj.exec(trans,Mockito.mock(Role.class),"test");
117 assertTrue(retVal.toString().contains("Success"));
121 public void testExecElse() {
122 Question ques = Mockito.mock(Question.class);
124 Role roleObj = new Role("test","test","test",new HashSet());
126 CachedRoleDAO roleDaoObj = Mockito.mock(CachedRoleDAO.class);
128 List<Data> dataAL = new ArrayList<Data>();
129 Data data = new Data();
130 data.expires = new Date();
132 Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
134 Mockito.doReturn(retVal1).when(roleDaoObj).create(Mockito.any(), Mockito.any());
136 createObj = new RoleCreateImpl(trans, cluster, false, ques, roleDaoObj);
137 Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test");
138 assertTrue(0 == session.status);
139 } catch (APIException | IOException e) {
140 // TODO Auto-generated catch block
142 } catch (IllegalArgumentException e) {
143 // TODO Auto-generated catch block
150 public void testExecElseDateLess() {
151 Question ques = Mockito.mock(Question.class);
153 Role roleObj = new Role("test","test","test",new HashSet());
155 CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class);
157 List<Data> dataAL = new ArrayList<Data>();
158 Data data = new Data();
159 DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
161 data.expires = sdf.parse("01/01/2100");
162 } catch (ParseException e) {
163 // TODO Auto-generated catch block
167 Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
169 Mockito.doReturn(retVal1).when(userRoleDaoObj).create(Mockito.any(), Mockito.any());
171 createObj = new RoleCreateImpl(trans, cluster, false, ques, userRoleDaoObj);
172 Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test");
173 assertTrue(0 == session.status);
174 } catch (APIException | IOException e) {
175 // TODO Auto-generated catch block
177 } catch (IllegalArgumentException e) {
178 // TODO Auto-generated catch block
185 public void testExecElseNok() {
186 Question ques = Mockito.mock(Question.class);
188 Role roleObj = new Role("test","test","test",new HashSet());
190 CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class);
192 Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]);
194 Mockito.doReturn(retVal1).when(userRoleDaoObj).create(Mockito.any(), Mockito.any());
196 createObj = new RoleCreateImpl(trans, cluster, false, ques, userRoleDaoObj);
197 Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test");
198 assertTrue(session.toString().contains("test"));
199 } catch (APIException | IOException e) {
200 // TODO Auto-generated catch block
202 } catch (IllegalArgumentException e) {
203 // TODO Auto-generated catch block
210 public void test2Argonstructor() {
211 createObj = new RoleCreate(trans, Mockito.mock(ActionDAO.class));
214 class RoleCreateImpl extends RoleCreate{
216 public RoleCreateImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedRoleDAO userRoleDaoObj) throws APIException, IOException {
217 super(trans, cluster, dryRun);
218 setQuestion(ques, userRoleDaoObj);
221 public void setQuestion(Question ques, CachedRoleDAO userRoleDaoObj) {
222 Field field, nsDaoField;
224 field = RoleCreateImpl.class.getSuperclass().getSuperclass().getDeclaredField("q");
225 nsDaoField = Question.class.getDeclaredField("roleDAO");
227 field.setAccessible(true);
228 nsDaoField.setAccessible(true);
229 // remove final modifier from field
230 Field modifiersField = Field.class.getDeclaredField("modifiers");
231 modifiersField.setAccessible(true);
232 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
233 modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL);
235 field.set(this, ques);
236 nsDaoField.set(ques, userRoleDaoObj);
237 } catch (NoSuchFieldException | SecurityException e) {
238 // TODO Auto-generated catch block
240 } catch (IllegalArgumentException e) {
241 // TODO Auto-generated catch block
243 } catch (IllegalAccessException e) {
244 // TODO Auto-generated catch block