Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / test / java / org / onap / appc / executor / impl / TestCommandExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.executor.impl;
27
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.onap.appc.domainmodel.lcm.*;
33 import org.onap.appc.exceptions.APPCException;
34 import org.onap.appc.executionqueue.ExecutionQueueService;
35 import org.onap.appc.executor.impl.objects.CommandRequest;
36 import org.onap.appc.executor.objects.CommandExecutorInput;
37 import org.onap.appc.requesthandler.RequestHandler;
38 import org.onap.appc.workflow.WorkFlowManager;
39 import org.powermock.api.mockito.PowerMockito;
40
41 import java.util.Date;
42 import java.util.concurrent.TimeUnit;
43
44
45 public class TestCommandExecutor {
46
47     private static final String API_VERSION = "2.0.0";
48     private static final String ORIGINATOR_ID = "1";
49
50     private CommandExecutorImpl commandExecutor;
51
52     private RequestHandler requestHandler;
53     private WorkFlowManager workflowManager;
54     private ExecutionQueueService executionQueueService;
55
56     private Date timeStamp = new Date();
57     private String requestId = "1";
58     private CommandExecutorInput commandExecutorInputConfigure = populateCommandExecutorInput("FIREWALL", 30000, "1.0",
59             timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure, "15", "");
60     private CommandExecutorInput commandExecutorInputSync = populateCommandExecutorInput("FIREWALL", 30, "1.0",
61             timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync, "15", "");
62     private CommandTask commandTask;
63
64     @Before
65     public void init() throws Exception {
66         requestHandler = Mockito.mock(RequestHandler.class);
67         workflowManager = Mockito.mock(WorkFlowManager.class);
68
69         executionQueueService = Mockito.mock(ExecutionQueueService.class);
70
71         commandExecutor = Mockito.spy(new CommandExecutorImpl());
72         commandExecutor.setExecutionQueueService(executionQueueService);
73         commandExecutor.setRequestHandler(requestHandler);
74         commandExecutor.setWorkflowManager(workflowManager);
75         commandExecutor.initialize();
76         commandTask = Mockito.mock(CommandTask.class);
77         Mockito.when(commandTask.getCommandRequest()).thenReturn(new CommandRequest(commandExecutorInputConfigure));
78         PowerMockito.whenNew(CommandTask.class)
79                 .withParameterTypes(RequestHandler.class, WorkFlowManager.class)
80                 .withArguments(requestHandler, workflowManager)
81                 .thenReturn(commandTask);
82     }
83
84     @Test
85     public void testPositiveFlow_LCM() throws Exception {
86         try {
87             Mockito.doReturn(commandTask).when(commandExecutor).getCommandTask(Mockito.any(), Mockito.any());
88             commandExecutor.executeCommand(commandExecutorInputConfigure);
89         } catch (APPCException e) {
90             Assert.fail(e.toString());
91         }
92     }
93
94     @Test(expected = APPCException.class)
95     public void testNegativeFlow_LCM() throws APPCException {
96             Mockito.doThrow(new APPCException("Failed to enqueue request"))
97                     .when(executionQueueService)
98                     .putMessage((Runnable) Mockito.anyObject(), Mockito.anyLong(), (TimeUnit) Mockito.anyObject());
99             commandExecutor.executeCommand(commandExecutorInputSync);
100     }
101
102     private CommandExecutorInput populateCommandExecutorInput(String vnfType, int ttl, String vnfVersion,
103             Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID,
104             VNFOperation action, String vnfId, String payload) {
105         CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects();
106         RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext();
107         RequestContext requestContext = runtimeContext.getRequestContext();
108         CommonHeader commonHeader = requestContext.getCommonHeader();
109         commonHeader.getFlags().setTtl(ttl);
110         commonHeader.setApiVer(apiVersion);
111         commonHeader.setTimestamp(timeStamp);
112         commonHeader.setRequestId(requestId);
113         commonHeader.setSubRequestId(subRequestID);
114         commonHeader.setOriginatorId(originatorID);
115         requestContext.setAction(action);
116         requestContext.setPayload(payload);
117         requestContext.getActionIdentifiers().setVnfId(vnfId);
118         VNFContext vnfContext = runtimeContext.getVnfContext();
119         vnfContext.setType(vnfType);
120         vnfContext.setId(vnfId);
121         vnfContext.setVersion(vnfVersion);
122         return commandExecutorInput;
123     }
124
125     private CommandExecutorInput createCommandExecutorInputWithSubObjects() {
126         CommandExecutorInput commandExecutorInput = new CommandExecutorInput();
127         RuntimeContext runtimeContext = new RuntimeContext();
128         commandExecutorInput.setRuntimeContext(runtimeContext);
129         RequestContext requestContext = new RequestContext();
130         runtimeContext.setRequestContext(requestContext);
131         CommonHeader commonHeader = new CommonHeader();
132         requestContext.setCommonHeader(commonHeader);
133         Flags flags = new Flags();
134         commonHeader.setFlags(flags);
135         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
136         requestContext.setActionIdentifiers(actionIdentifiers);
137         VNFContext vnfContext = new VNFContext();
138         runtimeContext.setVnfContext(vnfContext);
139         return commandExecutorInput;
140     }
141 }