f4d703756655e2bab4838676e329fa4da167498c
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / test / java / org / openecomp / appc / requesthandler / TestRequestValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.requesthandler;
23
24
25 import static junit.framework.TestCase.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.Matchers.anyBoolean;
28 import static org.mockito.Matchers.anyObject;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Matchers.eq;
31
32 import java.time.Instant;
33 import java.util.Map;
34 import java.util.UUID;
35
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.mockito.invocation.InvocationOnMock;
42 import org.mockito.stubbing.Answer;
43 import org.openecomp.appc.domainmodel.lcm.ActionIdentifiers;
44 import org.openecomp.appc.domainmodel.lcm.CommonHeader;
45 import org.openecomp.appc.domainmodel.lcm.Flags;
46 import org.openecomp.appc.domainmodel.lcm.RequestContext;
47 import org.openecomp.appc.domainmodel.lcm.ResponseContext;
48 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
49 import org.openecomp.appc.domainmodel.lcm.Status;
50 import org.openecomp.appc.domainmodel.lcm.VNFContext;
51 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
52 import org.openecomp.appc.executor.UnstableVNFException;
53 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
54 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
55 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
56 import org.openecomp.appc.requesthandler.exceptions.DGWorkflowNotFoundException;
57 import org.openecomp.appc.requesthandler.exceptions.DuplicateRequestException;
58 import org.openecomp.appc.requesthandler.exceptions.InvalidInputException;
59 import org.openecomp.appc.requesthandler.exceptions.RequestExpiredException;
60 import org.openecomp.appc.requesthandler.exceptions.VNFNotFoundException;
61 import org.openecomp.appc.requesthandler.exceptions.WorkflowNotFoundException;
62 import org.openecomp.appc.requesthandler.impl.RequestHandlerImpl;
63 import org.openecomp.appc.requesthandler.impl.RequestValidatorImpl;
64 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
65 import org.openecomp.appc.transactionrecorder.TransactionRecorder;
66 import org.openecomp.appc.workflow.WorkFlowManager;
67 import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
68 import org.openecomp.appc.workflow.objects.WorkflowRequest;
69 import org.openecomp.appc.workingstatemanager.WorkingStateManager;
70 import org.openecomp.sdnc.sli.SvcLogicContext;
71 import org.openecomp.sdnc.sli.SvcLogicResource;
72 import org.openecomp.sdnc.sli.aai.AAIService;
73 import org.osgi.framework.Bundle;
74 import org.osgi.framework.BundleContext;
75 import org.osgi.framework.FrameworkUtil;
76 import org.osgi.framework.ServiceReference;
77 import org.powermock.api.mockito.PowerMockito;
78 import org.powermock.core.classloader.annotations.PrepareForTest;
79 import org.powermock.modules.junit4.PowerMockRunner;
80
81 import com.att.eelf.configuration.EELFLogger;
82 import com.att.eelf.configuration.EELFManager;
83
84
85 @RunWith(PowerMockRunner.class)
86 @PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class})
87 @Ignore
88 public class TestRequestValidator {
89
90     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);
91
92     private static final String TTL_FLAG= "TTL";
93
94     private RequestValidatorImpl requestValidator;
95
96     AAIService aaiAdapter ;
97     LifecycleManager lifecyclemanager;
98     WorkFlowManager workflowManager;
99     WorkingStateManager workingStateManager ;
100 //    AppcDAOImpl dao ;
101
102     private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
103     private final Bundle bundleService=Mockito.mock(Bundle.class);
104     private final ServiceReference sref=Mockito.mock(ServiceReference.class);
105
106
107
108     @Before
109     public void init() throws Exception {
110
111 //        dao = Mockito.mock(AppcDAOImpl.class);
112 //        PowerMockito.whenNew(AppcDAOImpl.class).withNoArguments().thenReturn(dao);
113 //        Mockito.doNothing().when(dao).storeTransactionRecord((TransactionRecord)anyObject());
114         //   PowerMockito.when(dao.queryWorkflow(anyString(),anyString())).thenReturn(true);
115
116         // ***
117         AAIService aaiService = Mockito.mock(AAIService.class);;
118         PowerMockito.mockStatic(FrameworkUtil.class);
119         PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
120         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
121         PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
122         PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
123         PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
124             @Override
125             public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
126                 Object[] args = invocation.getArguments();
127                 SvcLogicContext ctx =(SvcLogicContext)args[6];
128                 String prefix = (String)args[4];
129                 String key = (String)args[3];
130                 if(key.contains("'28'")){
131                     return  SvcLogicResource.QueryStatus.FAILURE ;
132                 }else if ( key.contains("'8'")) {
133                     return  SvcLogicResource.QueryStatus.NOT_FOUND ;
134                 }else {
135                     ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
136                     ctx.setAttribute(prefix + ".orchestration-status", "Instantiated");
137                 }
138                 return  SvcLogicResource.QueryStatus.SUCCESS ;
139             }
140         });
141         PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
142         //PowerMockito.when(requestHandler.getVnfdata(anyString(), anyString(), (SvcLogicContext)anyObject())).thenReturn()
143         //  ***
144
145
146         aaiAdapter = Mockito.mock(AAIService.class);
147         lifecyclemanager= Mockito.mock(LifecycleManager.class);
148         workflowManager= Mockito.mock(WorkFlowManager.class);
149         workingStateManager = Mockito.mock(WorkingStateManager.class);
150
151         //  transactionRecorder= spy(TransactionRecorder.class);
152         requestValidator = new RequestValidatorImpl();
153 //        requestValidator = Mockito.mock(RequestValidator.class);
154         requestValidator.setWorkflowManager(workflowManager);
155         requestValidator.setLifecyclemanager(lifecyclemanager);
156         requestValidator.setWorkingStateManager(workingStateManager);
157
158
159        /* Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
160         Mockito.when(aaiAdapter.requestGenericVnfData("1")).thenReturn(getGenericVnf("FIREWALL","INSTNATIATED"));*/
161         // Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(true);
162
163         /*PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
164         Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
165         PowerMockito.when(getAaiadapter().requestGenericVnfData("8")).thenThrow(new AAIAdapterException("404"));
166         Mockito.when(workingStateManager.isVNFStable("8")).thenReturn(true);
167         PowerMockito.when(getAaiadapter().requestGenericVnfData("9")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
168         Mockito.when(workingStateManager.isVNFStable("9")).thenReturn(true);
169         PowerMockito.when(getAaiadapter().requestGenericVnfData("10")).thenReturn(getGenericVnf("WrongRouter","INSTANTIATED"));
170         Mockito.when(workingStateManager.isVNFStable("10")).thenReturn(true);
171         PowerMockito.when(getAaiadapter().requestGenericVnfData("11")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
172         Mockito.when(workingStateManager.isVNFStable("11")).thenReturn(true);
173         PowerMockito.when(getAaiadapter().requestGenericVnfData("12")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
174         Mockito.when(workingStateManager.isVNFStable("12")).thenReturn(true);
175         PowerMockito.when(getAaiadapter().requestGenericVnfData("13")).thenReturn(getGenericVnf("FIREWALL","TESTING"));
176         Mockito.when(workingStateManager.isVNFStable("13")).thenReturn(true);
177         PowerMockito.when(getAaiadapter().requestGenericVnfData("14")).thenReturn(getGenericVnf("FIREWALL","REBUILDING"));
178         Mockito.when(workingStateManager.isVNFStable("14")).thenReturn(true);
179         PowerMockito.when(getAaiadapter().requestGenericVnfData("26")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
180         Mockito.when(workingStateManager.isVNFStable("26")).thenReturn(true);
181         PowerMockito.when(getAaiadapter().requestGenericVnfData("27")).thenReturn(getGenericVnf("FIREWALL","RESTARTING"));
182         Mockito.when(workingStateManager.isVNFStable("27")).thenReturn(true);
183         PowerMockito.when(getAaiadapter().requestGenericVnfData("28")).thenThrow(new RuntimeException("AAI Down Excpetion"));
184         Mockito.when(workingStateManager.isVNFStable("28")).thenReturn(true);
185         PowerMockito.when(getAaiadapter().requestGenericVnfData("35")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
186         Mockito.when(workingStateManager.isVNFStable("35")).thenReturn(true);*/
187
188         /*for(Integer i=130; i<=140 ; i++)
189         {
190             PowerMockito.when(getAaiadapter().requestGenericVnfData(i.toString())).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
191             Mockito.when(workingStateManager.isVNFStable(i.toString())).thenReturn(true);
192         }
193         PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
194         Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
195         PowerMockito.when(getAaiadapter().requestGenericVnfData("40")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
196         Mockito.when(workingStateManager.isVNFStable("40")).thenReturn(true).thenReturn(false);
197
198
199         PowerMockito.when(getAaiadapter().requestGenericVnfData("38")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
200         Mockito.when(workingStateManager.isVNFStable("38")).thenReturn(true).thenReturn(false);
201
202
203         PowerMockito.when(getAaiadapter().requestGenericVnfData("201")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","CONFIGURED"));
204         Mockito.when(workingStateManager.isVNFStable("201")).thenReturn(true);
205         PowerMockito.when(getAaiadapter().requestGenericVnfData("202")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","ERROR"));
206         Mockito.when(workingStateManager.isVNFStable("202")).thenReturn(true).thenReturn(false);
207
208         PowerMockito.when(getAaiadapter().requestGenericVnfData("301")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
209         Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true).thenReturn(false);
210
211         PowerMockito.when(getAaiadapter().requestGenericVnfData("302")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
212         Mockito.when(workingStateManager.isVNFStable("302")).thenReturn(true).thenReturn(true);
213
214         PowerMockito.when(getAaiadapter().requestGenericVnfData("303")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
215         Mockito.when(workingStateManager.isVNFStable("303")).thenReturn(true).thenReturn(true);
216
217         PowerMockito.when(getAaiadapter().requestGenericVnfData("309")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
218         Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true).thenReturn(true);
219
220         PowerMockito.when(getAaiadapter().requestGenericVnfData("310")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
221         Mockito.when(workingStateManager.isVNFStable("310")).thenReturn(true).thenReturn(true);*/
222     }
223     public AAIService getAaiadapter() {
224         return this.aaiAdapter;
225     }
226 /*    public GenericVnf getGenericVnf(String vnfType, String operationalState) {
227         GenericVnf genericVnf = new GenericVnf();
228         genericVnf.setVnfType(vnfType);
229         // genericVnf.setOperationalState(operationalState);
230         genericVnf.setOrchestrationStatus(operationalState);
231         return genericVnf;
232     }*/
233     private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Instant timeStamp){
234         String API_VERSION= "2.0.0";
235         RequestHandlerInput input = new RequestHandlerInput();
236         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
237         RequestContext requestContext = runtimeContext.getRequestContext();
238         input.setRequestContext(requestContext);
239         requestContext.getActionIdentifiers().setVnfId(vnfID);
240         requestContext.setAction(action);
241         if (action != null) {
242             input.setRpcName(convertActionNameToUrl(action.name()));
243         }
244         else{
245             input.setRpcName(null);
246         }
247         requestContext.getCommonHeader().setRequestId(requestId);
248         requestContext.getCommonHeader().setSubRequestId(subRequestId);
249         requestContext.getCommonHeader().setOriginatorId(originatorId);
250         requestContext.getCommonHeader().setFlags(new Flags(null, force, ttl));
251         requestContext.getCommonHeader().getTimeStamp();
252         requestContext.getCommonHeader().setApiVer(API_VERSION);
253         requestContext.getCommonHeader().setTimestamp(timeStamp);
254         return input;
255     }
256     @Test
257     public void testNullVnfID() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
258         logger.debug("=====================testNullVnfID=============================");
259         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
260         RequestHandlerInput input = this.getRequestHandlerInput(null, VNFOperation.Configure, 30,
261                 false, UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
262         Exception ex =null;
263         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
264         try {
265             requestValidator.validateRequest(runtimeContext);
266         }catch(InvalidInputException e ) {
267             ex = e;
268         }
269 //        assertEquals(new InvalidInputException("vnfID or command is null") ,ex);
270         assertNotNull(ex);
271         logger.debug("=====================testNullVnfID=============================");
272     }
273
274     @Test
275     public void testPositiveFlowWithConfigure() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
276         logger.debug("=====================testPositiveFlowWithConfigure=============================");
277         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
278         Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
279         RequestHandlerInput input = this.getRequestHandlerInput("1", VNFOperation.Configure, 30,
280                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
281         Exception ex =null;
282         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
283         try {
284             requestValidator.validateRequest(runtimeContext);
285         }catch(Exception e ) {
286             ex = e;
287         }
288         assertNull(ex);
289         logger.debug("testPositiveFlowWithConfigure");
290         logger.debug("=====================testPositiveFlowWithConfigure=============================");
291     }
292
293     @Test
294     public void testVnfNotFound() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
295         logger.debug("=====================testVnfNotFound=============================");
296         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
297         RequestHandlerInput input = this.getRequestHandlerInput("8", VNFOperation.Configure, 30,
298                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
299         Exception ex =null;
300         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
301         try {
302             requestValidator.validateRequest(runtimeContext);
303         }catch(Exception e ) {
304             ex = e;
305         }
306         assertNotNull(ex);
307         logger.debug("=====================testVnfNotFound=============================");
308     }
309
310
311
312    @Test
313     public void testNullCommand() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
314         logger.debug("=====================testNullCommand=============================");
315         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
316         RequestHandlerInput input = this.getRequestHandlerInput("7", null,30,
317                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
318        Exception ex =null;
319        RuntimeContext runtimeContext = putInputToRuntimeContext(input);
320        try {
321            requestValidator.validateRequest(runtimeContext);
322        }catch(InvalidInputException e ) {
323            ex = e;
324        }
325        assertNotNull(ex);
326         logger.debug("=====================testNullCommand=============================");
327     }
328
329     @Test
330     public void testNullVnfIDAndCommand() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
331         logger.debug("=====================testNullVnfIDAndCommand=============================");
332         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
333         RequestHandlerInput input = this.getRequestHandlerInput(null, null,30,
334                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
335         Exception ex =null;
336         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
337         try {
338             requestValidator.validateRequest(runtimeContext);
339         }catch(InvalidInputException e ) {
340             ex = e;
341         }
342         assertNotNull(ex);
343         logger.debug("=====================testNullVnfIDAndCommand=============================");
344     }
345
346     @Test
347     public void testWorkflowNotFound() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
348         logger.debug("=====================testWorkflowNotFound=============================");
349         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(false,false));
350         RequestHandlerInput input = this.getRequestHandlerInput("10", VNFOperation.Configure, 30,
351                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
352         Exception ex =null;
353         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
354         try {
355             requestValidator.validateRequest(runtimeContext);
356         }catch(Exception e ) {
357             ex = e;
358         }
359         assertNotNull(ex);
360         logger.debug("=====================testWorkflowNotFound=============================");
361     }
362
363     @Test
364     public void testUnstableVnfWithConfigure() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
365         logger.debug("=====================testUnstableVnfWithConfigure=============================");
366         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
367         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
368
369         RequestHandlerInput input = this.getRequestHandlerInput("11", VNFOperation.Configure, 30,
370                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
371         Exception ex =null;
372         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
373         try {
374             requestValidator.validateRequest(runtimeContext);
375         }catch(Exception e ) {
376             ex = e;
377         }
378         assertNotNull(ex);
379         logger.debug("=====================testUnstableVnfWithConfigure=============================");
380     }
381
382     @Test
383     public void testUnstableVnfWithTest() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
384         logger.debug("=====================testUnstableVnfWithTest=============================");
385         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
386         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
387         RequestHandlerInput input = this.getRequestHandlerInput("12", VNFOperation.Test,30,
388                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
389         Exception ex =null;
390         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
391         try {
392             requestValidator.validateRequest(runtimeContext);
393         }catch(Exception e ) {
394             ex = e;
395         }
396         assertNotNull(ex);
397         logger.debug("=====================testUnstableVnfWithTest=============================");
398     }
399
400     @Test
401     public void testUnstableVnfWithStart() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
402         logger.debug("=====================testUnstableVnfWithStart=============================");
403         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
404
405         RequestHandlerInput input = this.getRequestHandlerInput("13", VNFOperation.Start,30,
406                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
407         Exception ex =null;
408         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
409         try {
410             requestValidator.validateRequest(runtimeContext);
411         }catch(Exception e ) {
412             ex = e;
413         }
414         assertNotNull(ex);
415         logger.debug("=====================testUnstableVnfWithStart=============================");
416     }
417
418     @Test
419     public void testUnstableVnfWithTerminate() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
420         logger.debug("=====================testUnstableVnfWithTerminate=============================");
421         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
422         RequestHandlerInput input = this.getRequestHandlerInput("14", VNFOperation.Terminate,30,
423                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
424         Exception ex =null;
425         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
426         try {
427             requestValidator.validateRequest(runtimeContext);
428         }catch(Exception e ) {
429             ex = e;
430         }
431         assertNotNull(ex);
432         logger.debug("=====================testUnstableVnfWithTerminate=============================");
433     }
434
435     @Test
436     public void testUnstableVnfWithRestart() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
437         logger.debug("=====================testUnstableVnfWithRestart=============================");
438         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
439
440         RequestHandlerInput input = this.getRequestHandlerInput("26", VNFOperation.Restart,30,
441                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
442         Exception ex =null;
443         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
444         try {
445             requestValidator.validateRequest(runtimeContext);
446         }catch(Exception e ) {
447             ex = e;
448         }
449         assertNotNull(ex);
450         logger.debug("=====================testUnstableVnfWithRestart=============================");
451     }
452
453     @Test
454     public void testUnstableVnfWithRebuild() throws  LifecycleException, NoTransitionDefinedException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
455         logger.debug("=====================testUnstableVnfWithRebuild=============================");
456         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(),anyString())).thenThrow( new NoTransitionDefinedException("","",""));
457
458         // Mockito.doReturn(this.getGenericVnf("Firewall", "NOT_INSTANTIATED")).when(getAaiadapter()).requestGenericVnfData("8");
459         RequestHandlerInput input = this.getRequestHandlerInput("27", VNFOperation.Rebuild,30,
460                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
461         Exception ex =null;
462         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
463         try {
464             requestValidator.validateRequest(runtimeContext);
465         }catch(Exception e ) {
466             ex = e;
467         }
468         assertNotNull(ex);
469         logger.debug("=====================testUnstableVnfWithRebuild=============================");
470     }
471
472
473
474
475     @Test
476     public void testAAIDown() throws Exception {
477         logger.debug("=====================testAAIDown=============================");
478         //            AAIAdapter aaiAdapter = Mockito.mock(AAIAdapterImpl.class);
479         //            RequestHandler requestHandler=RequestHandlerSingleton.getRequestHandler(new WorkFlowManagerImpl(),aaiAdapter,new LifecycleManagerImpl());
480         //            RequestHandler requestHandler = new RequestHandlerImpl(new WorkFlowManagerImpl(),aaiAdapter,new LifecycleManagerImpl());
481         RequestHandlerInput input = this.getRequestHandlerInput("28", VNFOperation.Configure, 30,
482                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(), Instant.now());
483                 Exception ex =null;
484         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
485         try {
486             requestValidator.validateRequest(runtimeContext);
487
488         }catch(Exception e ) {
489             ex = e;
490         }
491         assertNotNull(ex);
492         logger.debug("=====================testAAIDown=============================");
493     }
494
495     @Test
496     public void testNegativeFlowWithTimeStamp() throws  NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
497         logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
498         Instant now =  Instant.now();
499         Instant past =  now.minusMillis(1000000);
500         RequestHandlerInput input = this.getRequestHandlerInput("35", VNFOperation.Configure, 30,
501                 false,UUID.randomUUID().toString(),UUID.randomUUID().toString(),UUID.randomUUID().toString(),past);
502         Exception ex =null;
503         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
504        
505         try {
506             requestValidator.validateRequest(runtimeContext);
507         }catch(Exception e ) {
508             ex = e;
509         }
510         assertNotNull(ex);
511         logger.debug("testNegativeFlowWithTimeStamp");
512         logger.debug("=====================testNegativeFlowWithTimeStamp=============================");
513     }
514
515
516     @Test
517     public void rejectDuplicateRequest() throws NoTransitionDefinedException, LifecycleException, InvalidInputException, RequestExpiredException, UnstableVNFException, DuplicateRequestException, VNFNotFoundException, WorkflowNotFoundException,DGWorkflowNotFoundException {
518         String originatorID = UUID.randomUUID().toString();
519         String requestID = UUID.randomUUID().toString();
520         String subRequestID = UUID.randomUUID().toString();
521
522         Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(new WorkflowExistsOutput(true,true));
523         Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true);
524         Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true);
525         RequestHandlerInput input = this.getRequestHandlerInput("301", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
526
527         RequestHandlerInput input1 = this.getRequestHandlerInput("309", VNFOperation.Configure,0,false,originatorID, requestID, subRequestID, Instant.now());
528         Exception ex =null;
529         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
530         RuntimeContext runtimeContext1 = putInputToRuntimeContext(input1);
531
532         try {
533             requestValidator.validateRequest(runtimeContext);
534         }catch(Exception e ) {
535             ex = e;
536         }
537         assertNull(ex);
538
539         try {
540             requestValidator.validateRequest(runtimeContext1);
541         }catch(Exception e ) {
542             ex = e;
543         }
544         assertNotNull(ex);
545     }
546
547     @Test
548     public void testLockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
549         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
550         testOperation("no-matter", VNFOperation.Lock);
551     }
552
553     @Test
554     public void testUnlockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
555         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
556         testOperation("no-matter", VNFOperation.Unlock);
557     }
558
559     @Test
560     public void testCheckLockOperation() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
561         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
562         testOperation("no-matter", VNFOperation.CheckLock);
563     }
564
565     @Test(expected = NoTransitionDefinedException.class)
566     public void testLockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
567         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Lock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
568         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
569         testOperation("no-matter", VNFOperation.Lock);
570     }
571
572     @Test(expected = NoTransitionDefinedException.class)
573     public void testUnlockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
574         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.Unlock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
575         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
576         testOperation("no-matter", VNFOperation.Unlock);
577     }
578
579     @Test(expected = NoTransitionDefinedException.class)
580     public void testCheckLockOperationNegative() throws RequestExpiredException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, WorkflowNotFoundException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, InvalidInputException {
581         Mockito.when(lifecyclemanager.getNextState(anyString(), anyString(), eq(VNFOperation.CheckLock.toString()))).thenThrow(new NoTransitionDefinedException("", "", ""));
582         Mockito.when(workingStateManager.isVNFStable("no-matter")).thenReturn(true);
583         testOperation("no-matter", VNFOperation.CheckLock);
584     }
585
586     private void testOperation(String resource, VNFOperation operation) throws WorkflowNotFoundException, DuplicateRequestException, DGWorkflowNotFoundException, VNFNotFoundException, InvalidInputException, LifecycleException, UnstableVNFException, NoTransitionDefinedException, RequestExpiredException {
587         String originatorID = UUID.randomUUID().toString();
588         String requestID = UUID.randomUUID().toString();
589         String subRequestID = UUID.randomUUID().toString();
590
591         RequestHandlerInput input = this.getRequestHandlerInput(resource, operation, 0, false, originatorID, requestID, subRequestID,  Instant.now());
592         RuntimeContext runtimeContext = putInputToRuntimeContext(input);
593         requestValidator.validateRequest(runtimeContext);
594     }
595
596
597     private RuntimeContext createRuntimeContextWithSubObjects() {
598         RuntimeContext runtimeContext = new RuntimeContext();
599         RequestContext requestContext = new RequestContext();
600         runtimeContext.setRequestContext(requestContext);
601         ResponseContext responseContext = createResponseContextWithSuObjects();
602         runtimeContext.setResponseContext(responseContext);
603         CommonHeader commonHeader = new CommonHeader();
604         requestContext.setCommonHeader(commonHeader);
605         commonHeader.setFlags(new Flags(null, false, 0));
606         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
607         requestContext.setActionIdentifiers(actionIdentifiers);
608         VNFContext vnfContext = new VNFContext();
609         runtimeContext.setVnfContext(vnfContext);
610         return runtimeContext;
611
612     }
613
614     private ResponseContext createResponseContextWithSuObjects(){
615         ResponseContext responseContext = new ResponseContext();
616         CommonHeader commonHeader = new CommonHeader();
617         responseContext.setCommonHeader(commonHeader);
618         responseContext.setStatus(new Status(0, null));
619         commonHeader.setFlags(new Flags(null, false, 0));
620         return responseContext;
621     }
622
623     private String convertActionNameToUrl(String action) {
624         String regex = "([a-z])([A-Z]+)";
625         String replacement = "$1-$2";
626         return action.replaceAll(regex, replacement)
627                 .toLowerCase();
628     }
629
630     private RuntimeContext putInputToRuntimeContext(RequestHandlerInput input) {
631         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
632         runtimeContext.setRequestContext(input.getRequestContext());
633         runtimeContext.setRpcName(input.getRpcName());
634         runtimeContext.getVnfContext().setId(input.getRequestContext().getActionIdentifiers().getVnfId());
635         //runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(input.getRequestContext().getActionIdentifiers().getVnfId());
636         return runtimeContext;
637
638         //String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Date timeStamp
639     }
640 }