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