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.List;
36 import java.util.Properties;
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;
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;
64 public class JU_URAdd {
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 addObj = new URAdd(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 testExec() {
113 Result<Data> retVal = addObj.exec(trans,Mockito.mock(UserRole.class),"test");
114 assertTrue(retVal.toString().contains("Success"));
118 public void testExecElse() {
119 Question ques = Mockito.mock(Question.class);
121 UserRole userRoleObj = new UserRole("test","test","test",new Date());
122 CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
124 List<Data> dataAL = new ArrayList<Data>();
125 Data data = new Data();
126 data.expires = new Date();
128 Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
130 Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd());
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
138 } catch (IllegalArgumentException e) {
139 // TODO Auto-generated catch block
146 public void testExecElseDateLess() {
147 Question ques = Mockito.mock(Question.class);
149 UserRole userRoleObj = new UserRole("test","test","test",new Date());
150 CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
152 List<Data> dataAL = new ArrayList<Data>();
153 Data data = new Data();
154 DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
156 data.expires = sdf.parse("01/01/2100");
157 } catch (ParseException e) {
158 // TODO Auto-generated catch block
162 Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]);
164 Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd());
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
172 } catch (IllegalArgumentException e) {
173 // TODO Auto-generated catch block
180 public void testExecElseNok() {
181 Question ques = Mockito.mock(Question.class);
183 UserRole userRoleObj = new UserRole("test","test","test",new Date());
184 CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class);
186 Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]);
188 Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd());
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
196 } catch (IllegalArgumentException e) {
197 // TODO Auto-generated catch block
204 public void test2Argonstructor() {
205 addObj = new URAdd(trans, Mockito.mock(ActionDAO.class));
208 class URAddImpl extends URAdd{
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);
215 // TODO Auto-generated constructor stub
218 public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) {
219 Field field, nsDaoField;
221 field = URAddImpl.class.getSuperclass().getSuperclass().getDeclaredField("q");
222 nsDaoField = Question.class.getDeclaredField("userRoleDAO");
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);
232 field.set(this, ques);
233 nsDaoField.set(ques, userRoleDaoObj);
234 } catch (NoSuchFieldException | SecurityException e) {
235 // TODO Auto-generated catch block
237 } catch (IllegalArgumentException e) {
238 // TODO Auto-generated catch block
240 } catch (IllegalAccessException e) {
241 // TODO Auto-generated catch block