becd95d462098878c14e59e63730bd0758935c7c
[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.executor.UnstableVNFException;
34 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
35 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
36 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
37 import org.openecomp.appc.requesthandler.exceptions.*;
38 import org.openecomp.appc.requesthandler.impl.RequestHandlerImpl;
39 import org.openecomp.appc.requesthandler.impl.RequestValidatorImpl;
40 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
41 import org.openecomp.appc.transactionrecorder.TransactionRecorder;
42 import org.openecomp.appc.workflow.WorkFlowManager;
43 import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
44 import org.openecomp.appc.workflow.objects.WorkflowRequest;
45 import org.openecomp.appc.workingstatemanager.WorkingStateManager;
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48 import org.openecomp.sdnc.sli.SvcLogicContext;
49 import org.openecomp.sdnc.sli.SvcLogicResource;
50 import org.openecomp.sdnc.sli.aai.AAIService;
51 import org.osgi.framework.Bundle;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.FrameworkUtil;
54 import org.osgi.framework.ServiceReference;
55 import org.powermock.api.mockito.PowerMockito;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
58
59 import java.util.Date;
60 import java.util.Map;
61 import java.util.UUID;
62
63 import static junit.framework.TestCase.assertNotNull;
64 import static org.junit.Assert.assertNull;
65 import static org.mockito.Matchers.*;
66
67
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest( {WorkingStateManager.class,FrameworkUtil.class, TransactionRecorder.class, RequestHandlerImpl.class,RequestValidatorImpl.class, TransactionRecorder.class})
70 @Ignore
71 public class TestRequestValidator {
72
73     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestRequestHandler.class);
74
75     private static final String TTL_FLAG= "TTL";
76
77     private RequestValidatorImpl requestValidator;
78
79     AAIService aaiAdapter ;
80     LifecycleManager lifecyclemanager;
81     WorkFlowManager workflowManager;
82     WorkingStateManager workingStateManager ;
83 //    AppcDAOImpl dao ;
84
85     private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
86     private final Bundle bundleService=Mockito.mock(Bundle.class);
87     private final ServiceReference sref=Mockito.mock(ServiceReference.class);
88
89
90
91     @Before
92     public void init() throws Exception {
93
94 //        dao = Mockito.mock(AppcDAOImpl.class);
95 //        PowerMockito.whenNew(AppcDAOImpl.class).withNoArguments().thenReturn(dao);
96 //        Mockito.doNothing().when(dao).storeTransactionRecord((TransactionRecord)anyObject());
97         //   PowerMockito.when(dao.queryWorkflow(anyString(),anyString())).thenReturn(true);
98
99         // ***
100         AAIService aaiService = Mockito.mock(AAIService.class);;
101         PowerMockito.mockStatic(FrameworkUtil.class);
102         PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService);
103         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
104         PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref);
105         PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService);
106         PowerMockito.when(aaiService.query(anyString(),anyBoolean(),anyString(),anyString(),anyString(),anyString(),(SvcLogicContext)anyObject())).thenAnswer(new Answer<SvcLogicResource.QueryStatus>() {
107             @Override
108             public SvcLogicResource.QueryStatus answer(InvocationOnMock invocation) throws Throwable {
109                 Object[] args = invocation.getArguments();
110                 SvcLogicContext ctx =(SvcLogicContext)args[6];
111                 String prefix = (String)args[4];
112                 String key = (String)args[3];
113                 if(key.contains("'28'")){
114                     return  SvcLogicResource.QueryStatus.FAILURE ;
115                 }else if ( key.contains("'8'")) {
116                     return  SvcLogicResource.QueryStatus.NOT_FOUND ;
117                 }else {
118                     ctx.setAttribute(prefix + ".vnf-type", "FIREWALL");
119                     ctx.setAttribute(prefix + ".orchestration-status", "Instantiated");
120                 }
121                 return  SvcLogicResource.QueryStatus.SUCCESS ;
122             }
123         });
124         PowerMockito.when(aaiService.update(anyString(),anyString(),(Map)anyObject(),anyString(),(SvcLogicContext)anyObject())).thenReturn(SvcLogicResource.QueryStatus.SUCCESS);
125         //PowerMockito.when(requestHandler.getVnfdata(anyString(), anyString(), (SvcLogicContext)anyObject())).thenReturn()
126         //  ***
127
128
129         aaiAdapter = Mockito.mock(AAIService.class);
130         lifecyclemanager= Mockito.mock(LifecycleManager.class);
131         workflowManager= Mockito.mock(WorkFlowManager.class);
132         workingStateManager = Mockito.mock(WorkingStateManager.class);
133
134         //  transactionRecorder= spy(TransactionRecorder.class);
135         requestValidator = new RequestValidatorImpl();
136 //        requestValidator = Mockito.mock(RequestValidator.class);
137         requestValidator.setWorkflowManager(workflowManager);
138         requestValidator.setLifecyclemanager(lifecyclemanager);
139         requestValidator.setWorkingStateManager(workingStateManager);
140
141
142        /* Mockito.when(workingStateManager.isVNFStable("1")).thenReturn(true);
143         Mockito.when(aaiAdapter.requestGenericVnfData("1")).thenReturn(getGenericVnf("FIREWALL","INSTNATIATED"));*/
144         // Mockito.when(workflowManager.workflowExists((WorkflowRequest)anyObject())).thenReturn(true);
145
146         /*PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
147         Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
148         PowerMockito.when(getAaiadapter().requestGenericVnfData("8")).thenThrow(new AAIAdapterException("404"));
149         Mockito.when(workingStateManager.isVNFStable("8")).thenReturn(true);
150         PowerMockito.when(getAaiadapter().requestGenericVnfData("9")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
151         Mockito.when(workingStateManager.isVNFStable("9")).thenReturn(true);
152         PowerMockito.when(getAaiadapter().requestGenericVnfData("10")).thenReturn(getGenericVnf("WrongRouter","INSTANTIATED"));
153         Mockito.when(workingStateManager.isVNFStable("10")).thenReturn(true);
154         PowerMockito.when(getAaiadapter().requestGenericVnfData("11")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
155         Mockito.when(workingStateManager.isVNFStable("11")).thenReturn(true);
156         PowerMockito.when(getAaiadapter().requestGenericVnfData("12")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
157         Mockito.when(workingStateManager.isVNFStable("12")).thenReturn(true);
158         PowerMockito.when(getAaiadapter().requestGenericVnfData("13")).thenReturn(getGenericVnf("FIREWALL","TESTING"));
159         Mockito.when(workingStateManager.isVNFStable("13")).thenReturn(true);
160         PowerMockito.when(getAaiadapter().requestGenericVnfData("14")).thenReturn(getGenericVnf("FIREWALL","REBUILDING"));
161         Mockito.when(workingStateManager.isVNFStable("14")).thenReturn(true);
162         PowerMockito.when(getAaiadapter().requestGenericVnfData("26")).thenReturn(getGenericVnf("FIREWALL","NOT_INSTANTIATED"));
163         Mockito.when(workingStateManager.isVNFStable("26")).thenReturn(true);
164         PowerMockito.when(getAaiadapter().requestGenericVnfData("27")).thenReturn(getGenericVnf("FIREWALL","RESTARTING"));
165         Mockito.when(workingStateManager.isVNFStable("27")).thenReturn(true);
166         PowerMockito.when(getAaiadapter().requestGenericVnfData("28")).thenThrow(new RuntimeException("AAI Down Excpetion"));
167         Mockito.when(workingStateManager.isVNFStable("28")).thenReturn(true);
168         PowerMockito.when(getAaiadapter().requestGenericVnfData("35")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
169         Mockito.when(workingStateManager.isVNFStable("35")).thenReturn(true);*/
170
171         /*for(Integer i=130; i<=140 ; i++)
172         {
173             PowerMockito.when(getAaiadapter().requestGenericVnfData(i.toString())).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
174             Mockito.when(workingStateManager.isVNFStable(i.toString())).thenReturn(true);
175         }
176         PowerMockito.when(getAaiadapter().requestGenericVnfData("39")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
177         Mockito.when(workingStateManager.isVNFStable("39")).thenReturn(true);
178         PowerMockito.when(getAaiadapter().requestGenericVnfData("40")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
179         Mockito.when(workingStateManager.isVNFStable("40")).thenReturn(true).thenReturn(false);
180
181
182         PowerMockito.when(getAaiadapter().requestGenericVnfData("38")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
183         Mockito.when(workingStateManager.isVNFStable("38")).thenReturn(true).thenReturn(false);
184
185
186         PowerMockito.when(getAaiadapter().requestGenericVnfData("201")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","CONFIGURED"));
187         Mockito.when(workingStateManager.isVNFStable("201")).thenReturn(true);
188         PowerMockito.when(getAaiadapter().requestGenericVnfData("202")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED")).thenReturn(getGenericVnf("FIREWALL","ERROR"));
189         Mockito.when(workingStateManager.isVNFStable("202")).thenReturn(true).thenReturn(false);
190
191         PowerMockito.when(getAaiadapter().requestGenericVnfData("301")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
192         Mockito.when(workingStateManager.isVNFStable("301")).thenReturn(true).thenReturn(false);
193
194         PowerMockito.when(getAaiadapter().requestGenericVnfData("302")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
195         Mockito.when(workingStateManager.isVNFStable("302")).thenReturn(true).thenReturn(true);
196
197         PowerMockito.when(getAaiadapter().requestGenericVnfData("303")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
198         Mockito.when(workingStateManager.isVNFStable("303")).thenReturn(true).thenReturn(true);
199
200         PowerMockito.when(getAaiadapter().requestGenericVnfData("309")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
201         Mockito.when(workingStateManager.isVNFStable("309")).thenReturn(true).thenReturn(true);
202
203         PowerMockito.when(getAaiadapter().requestGenericVnfData("310")).thenReturn(getGenericVnf("FIREWALL","INSTANTIATED"));
204         Mockito.when(workingStateManager.isVNFStable("310")).thenReturn(true).thenReturn(true);*/
205     }
206     public AAIService getAaiadapter() {
207         return this.aaiAdapter;
208     }
209 /*    public GenericVnf getGenericVnf(String vnfType, String operationalState) {
210         GenericVnf genericVnf = new GenericVnf();
211         genericVnf.setVnfType(vnfType);
212         // genericVnf.setOperationalState(operationalState);
213         genericVnf.setOrchestrationStatus(operationalState);
214         return genericVnf;
215     }*/
216     private RequestHandlerInput getRequestHandlerInput(String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Date timeStamp){
217         String API_VERSION= "2.0.0";
218         RequestHandlerInput input = new RequestHandlerInput();
219         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
220         RequestContext requestContext = runtimeContext.getRequestContext();
221         input.setRequestContext(requestContext);
222         requestContext.getActionIdentifiers().setVnfId(vnfID);
223         requestContext.setAction(action);
224         if (action != null) {
225             input.setRpcName(convertActionNameToUrl(action.name()));
226         }
227         else{
228             input.setRpcName(null);
229         }
230         requestContext.getCommonHeader().setRequestId(requestId);
231         requestContext.getCommonHeader().setSubRequestId(subRequestId);
232         requestContext.getCommonHeader().setOriginatorId(originatorId);
233         requestContext.getCommonHeader().getFlags().setTtl(ttl);
234         requestContext.getCommonHeader().getFlags().setForce(force);
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         Flags flags = new Flags();
591         commonHeader.setFlags(flags);
592         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
593         requestContext.setActionIdentifiers(actionIdentifiers);
594         VNFContext vnfContext = new VNFContext();
595         runtimeContext.setVnfContext(vnfContext);
596         return runtimeContext;
597
598     }
599
600     private ResponseContext createResponseContextWithSuObjects(){
601         ResponseContext responseContext = new ResponseContext();
602         CommonHeader commonHeader = new CommonHeader();
603         Flags flags = new Flags();
604         responseContext.setCommonHeader(commonHeader);
605         responseContext.setStatus(new Status(0, null));
606         commonHeader.setFlags(flags);
607         return responseContext;
608     }
609
610     private String convertActionNameToUrl(String action) {
611         String regex = "([a-z])([A-Z]+)";
612         String replacement = "$1-$2";
613         return action.replaceAll(regex, replacement)
614                 .toLowerCase();
615     }
616
617     private RuntimeContext putInputToRuntimeContext(RequestHandlerInput input) {
618         RuntimeContext runtimeContext = createRuntimeContextWithSubObjects();
619         runtimeContext.setRequestContext(input.getRequestContext());
620         runtimeContext.setRpcName(input.getRpcName());
621         runtimeContext.getVnfContext().setId(input.getRequestContext().getActionIdentifiers().getVnfId());
622         //runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(input.getRequestContext().getActionIdentifiers().getVnfId());
623         return runtimeContext;
624
625         //String vnfID, VNFOperation action, int ttl, boolean force, String originatorId, String requestId, String subRequestId, Date timeStamp
626     }
627 }