Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / actions / test / JU_URFutureApproveExec.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.Date;
32 import java.util.List;
33 import java.util.Properties;
34
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;
58
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;
64
65 import io.netty.util.internal.SystemPropertyUtil;
66
67
68
69 public class JU_URFutureApproveExec {
70     
71         @Mock
72     AuthzTrans trans;
73         @Mock
74         Cluster cluster;
75         @Mock
76         PropAccess access;
77         
78         @Mock
79         URFutureApproveExec actionObj;
80
81     
82     @Before
83     public void setUp() throws APIException, IOException {
84         initMocks(this);
85         Session sessionObj=Mockito.mock(Session.class);
86         PreparedStatement psObj =Mockito.mock(PreparedStatement.class);
87                 try {
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());
97                         
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");
102                         Define.set(access);
103                 } catch (CadiException e) {
104                         // TODO Auto-generated catch block
105                         e.printStackTrace();
106                 }
107     }
108     
109     @Test
110         public void testExec() {
111                 try {
112                         actionObj = new URFutureApproveExec(trans, cluster, true);
113                 } catch (APIException | IOException e) {
114                         // TODO Auto-generated catch block
115                         e.printStackTrace();
116                 }
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);
122                 
123         }
124     
125     @Test
126         public void testExecElseOpStatusD() {
127                 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.D, 0, "test", new String[0]);
128                 try {
129                         actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
130                 } catch (APIException | IOException e) {
131                         // TODO Auto-generated catch block
132                         e.printStackTrace();
133                 }
134         Approval approval = new Approval(null, null, "", "test", "", "", "", "", 0L);
135         List<Approval> approvalAL = new ArrayList<>();
136         
137         Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
138         
139         approvalAL.add(approval);
140                 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
141                 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
142                 
143         }
144     
145     @Test
146         public void testExecElseOpStatusE() {
147                 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.E, 0, "test", new String[0]);
148                 try {
149                         actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
150                 } catch (APIException | IOException e) {
151                         // TODO Auto-generated catch block
152                         e.printStackTrace();
153                 }
154         Approval approval = new Approval(null, null, "", "test", "", "", "", "", 0L);
155         List<Approval> approvalAL = new ArrayList<>();
156         
157         Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
158         
159         approvalAL.add(approval);
160                 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
161                 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
162                 
163         }
164     
165     @Test
166         public void testExecElseOpStatusL() {
167                 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.L, 0, "test", new String[0]);
168                 try {
169                         actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
170                 } catch (APIException | IOException e) {
171                         // TODO Auto-generated catch block
172                         e.printStackTrace();
173                 }
174         Approval approval = new Approval(null, null, "", "test", "", "", "", "", 0L);
175         List<Approval> approvalAL = new ArrayList<>();
176         
177         Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
178         
179         approvalAL.add(approval);
180                 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
181                 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
182                 
183         }
184     
185     @Test
186         public void testExecElseOpStatusP() {
187                 Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.P, 0, "test", new String[0]);
188                 try {
189                         actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
190                 } catch (APIException | IOException e) {
191                         // TODO Auto-generated catch block
192                         e.printStackTrace();
193                 }
194         Approval approval = new Approval(null, null, "",  "test", "", "", "", "", 0L);
195         List<Approval> approvalAL = new ArrayList<>();
196         
197         Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
198         
199         approvalAL.add(approval);
200                 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
201                 assertTrue(0 == retVal.status && "test".equals(retVal.toString()));
202                 
203         }
204     
205     @Test
206         public void testExecElseNok() {
207                 Result<OP_STATUS> retValD = new Result<OP_STATUS>(null, 1, "test", new String[0]);
208                 try {
209                         actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD);
210                 } catch (APIException | IOException e) {
211                         // TODO Auto-generated catch block
212                         e.printStackTrace();
213                 }
214         Approval approval = new Approval(null, null, "", "test", "", "", "", "", 0L);
215         List<Approval> approvalAL = new ArrayList<>();
216         
217         Future futureObj = new Future(null, "", "", new Date(), new Date(), null);
218         
219         approvalAL.add(approval);
220                 Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj);
221                 System.out.println(retVal);
222                 assertTrue(1 == retVal.status);
223                 
224         }
225     
226     @Test
227         public void test2Argonstructor() {
228                 actionObj = new URFutureApproveExec(trans, Mockito.mock(ActionDAO.class));
229         }
230    
231     class URFutureApproveExecImpl extends URFutureApproveExec{
232                 
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());
238                 }
239
240                 public void setFunction(Function f) {
241                         Field field;
242                         try {
243                                 field = URFutureApproveExecImpl.class.getSuperclass().getSuperclass().getDeclaredField("f");
244                                 
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);
250                         
251                         field.set(this, f);
252                         } catch (NoSuchFieldException | SecurityException e) {
253                                 // TODO Auto-generated catch block
254                                 e.printStackTrace();
255                         } catch (IllegalArgumentException e) {
256                                 // TODO Auto-generated catch block
257                                 e.printStackTrace();
258                         } catch (IllegalAccessException e) {
259                                 // TODO Auto-generated catch block
260                                 e.printStackTrace();
261                         }
262                 }
263     }
264 }