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