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