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