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