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