Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / test / java / org / onap / appc / executor / TestCommandExecutor.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.onap.appc.executor;
26 /**
27  * 
28  */
29
30
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.onap.appc.domainmodel.lcm.*;
39 import org.onap.appc.exceptions.APPCException;
40 import org.onap.appc.executionqueue.ExecutionQueueService;
41 import org.onap.appc.executor.impl.*;
42 import org.onap.appc.executor.impl.objects.CommandRequest;
43 import org.onap.appc.executor.objects.CommandExecutorInput;
44 import org.onap.appc.lifecyclemanager.LifecycleManager;
45 import org.onap.appc.requesthandler.RequestHandler;
46 import org.onap.appc.workflow.WorkFlowManager;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50
51 import java.util.Date;
52 import java.util.concurrent.TimeUnit;
53
54 import static junit.framework.Assert.assertTrue;
55 import static org.powermock.api.support.membermodification.MemberMatcher.method;
56
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest({CommandTask.class,CommandExecutorImpl.class})
59 public class TestCommandExecutor {
60
61         private static final String TTL_FLAG= "TTL";
62         private static final String API_VERSION= "2.0.0";
63         private static final String ORIGINATOR_ID= "1";
64
65     private CommandExecutorImpl commandExecutor;
66
67     private RequestHandler requestHandler;
68     private WorkFlowManager workflowManager;
69     private ExecutionQueueService executionQueueService;
70
71     private Date timeStamp = new Date();
72     private String requestId = "1";
73     private CommandExecutorInput commandExecutorInputConfigure = pouplateCommandExecutorInput("FIREWALL", 30000, "1.0",
74             timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure,"15","") ;
75     private CommandExecutorInput commandExecutorInputSync = pouplateCommandExecutorInput("FIREWALL", 30, "1.0",
76             timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync,"15","") ;
77
78     @Before
79     public void init()throws Exception {
80         requestHandler= Mockito.mock(RequestHandler.class);
81         workflowManager= Mockito.mock(WorkFlowManager.class);
82
83         executionQueueService = Mockito.mock(ExecutionQueueService.class);
84
85         commandExecutor = new CommandExecutorImpl();
86         commandExecutor.setExecutionQueueService(executionQueueService);
87         commandExecutor.setRequestHandler(requestHandler);
88         commandExecutor.setWorkflowManager(workflowManager);
89         commandExecutor.initialize();
90         CommandTask commandTask = Mockito.mock(CommandTask.class);
91         Mockito.when(commandTask.getCommandRequest()).thenReturn(new CommandRequest(commandExecutorInputConfigure));
92         PowerMockito.whenNew(CommandTask.class).withParameterTypes(RequestHandler.class,WorkFlowManager.class).withArguments(requestHandler,workflowManager).thenReturn(commandTask);
93     }
94         
95
96     @Test
97     public void testPositiveFlow_LCM() throws Exception {
98         //Map <String,Object> flags = setTTLInFlags("30");
99         try {
100             commandExecutor.executeCommand(commandExecutorInputConfigure);
101         } catch (APPCException e) {
102             Assert.fail(e.toString());
103         }
104
105     }
106
107     @Test(expected = APPCException.class)
108     public void testNegativeFlow_LCM() throws APPCException{
109             Mockito.doThrow(new APPCException("Failed to enqueue request")).when(executionQueueService).putMessage((Runnable) Mockito.anyObject(),Mockito.anyLong(),(TimeUnit) Mockito.anyObject());
110             commandExecutor.executeCommand(commandExecutorInputSync);
111     }
112
113     
114     private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){
115         CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
116         RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
117         RequestContext requestContext = runtimeContext.getRequestContext();
118         requestContext.getCommonHeader().getFlags().setTtl(ttl);
119         requestContext.getCommonHeader().setApiVer(apiVersion);
120         requestContext.getCommonHeader().setTimestamp(timeStamp);
121         requestContext.getCommonHeader().setRequestId(requestId);
122         requestContext.getCommonHeader().setSubRequestId(subRequestID);
123         requestContext.getCommonHeader().setOriginatorId(originatorID);
124         requestContext.setAction(action);
125         requestContext.setPayload(payload);
126         requestContext.getActionIdentifiers().setVnfId(vnfId);
127         VNFContext vnfContext = runtimeContext.getVnfContext();
128         vnfContext.setType(vnfType);
129         vnfContext.setId(vnfId);
130         vnfContext.setVersion(vnfVersion);
131         return commandExecutorInput;
132     }
133
134     private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
135         CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
136         RuntimeContext runtimeContext = new RuntimeContext();
137         commandExecutorInput.setRuntimeContext(runtimeContext);
138         RequestContext requestContext = new RequestContext();
139         runtimeContext.setRequestContext(requestContext);
140         CommonHeader commonHeader = new CommonHeader();
141         requestContext.setCommonHeader(commonHeader);
142         Flags flags = new Flags();
143         commonHeader.setFlags(flags);
144         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
145         requestContext.setActionIdentifiers(actionIdentifiers);
146         VNFContext vnfContext = new VNFContext();
147         runtimeContext.setVnfContext(vnfContext);
148         return commandExecutorInput;
149     }
150
151 }
152