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.Date;
32 import java.util.List;
33 import java.util.Properties;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.onap.aaf.auth.batch.actions.ActionDAO;
40 import org.onap.aaf.auth.batch.actions.URFutureApproveExec;
41 import org.onap.aaf.auth.batch.actions.test.JU_URPunt.URPuntImpl;
42 import org.onap.aaf.auth.batch.helpers.Approval;
43 import org.onap.aaf.auth.batch.helpers.Future;
44 import org.onap.aaf.auth.common.Define;
45 import org.onap.aaf.auth.dao.cass.FutureDAO;
46 import org.onap.aaf.auth.dao.hl.Function;
47 import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP;
48 import org.onap.aaf.auth.dao.hl.Function.OP_STATUS;
49 import org.onap.aaf.auth.dao.hl.Question;
50 import org.onap.aaf.auth.env.AuthzTrans;
51 import org.onap.aaf.auth.layer.Result;
52 import org.onap.aaf.cadi.CadiException;
53 import org.onap.aaf.cadi.PropAccess;
54 import org.onap.aaf.cadi.config.Config;
55 import org.onap.aaf.misc.env.APIException;
56 import org.onap.aaf.misc.env.LogTarget;
57 import org.onap.aaf.misc.env.TimeTaken;
59 import com.datastax.driver.core.Cluster;
60 import com.datastax.driver.core.ColumnDefinitions;
61 import com.datastax.driver.core.PreparedId;
62 import com.datastax.driver.core.PreparedStatement;
63 import com.datastax.driver.core.Session;
65 import io.netty.util.internal.SystemPropertyUtil;
69 public class JU_URFutureApproveExec {
79 URFutureApproveExec actionObj;
83 public void setUp() throws APIException, IOException {
85 Session sessionObj=Mockito.mock(Session.class);
86 PreparedStatement psObj =Mockito.mock(PreparedStatement.class);
88 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init();
89 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn();
90 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug();
91 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info();
92 Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error();
93 Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF);
94 Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt());
95 Mockito.doReturn(sessionObj).when(cluster).connect("authz");
96 Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString());
98 Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables();
99 Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId();
100 Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties();
101 Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
103 } catch (CadiException e) {
104 // TODO Auto-generated catch block
110 public void testExec() {
112 actionObj = new URFutureApproveExec(trans, cluster, true);
113 } catch (APIException | IOException e) {
114 // TODO Auto-generated catch block
117 Approval approval = Mockito.mock(Approval.class);
118 List<Approval> approvalAL = new ArrayList<>();
119 approvalAL.add(approval);
120 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,Mockito.mock(Future.class));
121 assertTrue(8 == retVal.status);
126 public void testExecElseOpStatusD() {
127 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.D, 0, "test", new String[0]);
129 actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
130 } catch (APIException | IOException e) {
131 // TODO Auto-generated catch block
134 Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L);
135 List<Approval> approvalAL = new ArrayList<>();
137 Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
139 approvalAL.add(approval);
140 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
141 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
146 public void testExecElseOpStatusE() {
147 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.E, 0, "test", new String[0]);
149 actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
150 } catch (APIException | IOException e) {
151 // TODO Auto-generated catch block
154 Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L);
155 List<Approval> approvalAL = new ArrayList<>();
157 Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
159 approvalAL.add(approval);
160 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
161 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
166 public void testExecElseOpStatusL() {
167 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.L, 0, "test", new String[0]);
169 actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
170 } catch (APIException | IOException e) {
171 // TODO Auto-generated catch block
174 Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L);
175 List<Approval> approvalAL = new ArrayList<>();
177 Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
179 approvalAL.add(approval);
180 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
181 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
186 public void testExecElseOpStatusP() {
187 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.P, 0, "test", new String[0]);
189 actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
190 } catch (APIException | IOException e) {
191 // TODO Auto-generated catch block
194 Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L);
195 List<Approval> approvalAL = new ArrayList<>();
197 Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
199 approvalAL.add(approval);
200 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
201 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
206 public void testExecElseNok() {
207 Result<OP_STATUS> retValD = new Result<OP_STATUS>(null, 1, "test", new String[0]);
209 actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
210 } catch (APIException | IOException e) {
211 // TODO Auto-generated catch block
214 Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L);
215 List<Approval> approvalAL = new ArrayList<>();
217 Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
219 approvalAL.add(approval);
220 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
221 System.out.println(retVal);
222 assertTrue(1 == retVal.status);
227 public void test2Argonstructor() {
228 actionObj = new URFutureApproveExec(trans, Mockito.mock(ActionDAO.class));
231 class URFutureApproveExecImpl extends URFutureApproveExec{
233 public URFutureApproveExecImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Result<OP_STATUS> retValD)
234 throws APIException, IOException {
235 super(trans, cluster, dryRun);
236 setFunction(Mockito.mock(Function.class));
237 Mockito.doReturn(retValD).when(f).performFutureOp(Mockito.any(), Mockito.any(), Mockito.any(),Mockito.any(),Mockito.any());
240 public void setFunction(Function f) {
243 field = URFutureApproveExecImpl.class.getSuperclass().getSuperclass().getDeclaredField("f");
245 field.setAccessible(true);
246 // remove final modifier from field
247 Field modifiersField = Field.class.getDeclaredField("modifiers");
248 modifiersField.setAccessible(true);
249 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
252 } catch (NoSuchFieldException | SecurityException e) {
253 // TODO Auto-generated catch block
255 } catch (IllegalArgumentException e) {
256 // TODO Auto-generated catch block
258 } catch (IllegalAccessException e) {
259 // TODO Auto-generated catch block