cd33e8b2eced42cc419cde46ee769e7218b6a353
[appc.git] / appc-dispatcher / appc-command-executor / appc-command-executor-core / src / test / java / org / openecomp / appc / executor / TestCommandExecutor.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.executor;
23 /**
24  * 
25  */
26
27
28 import java.time.Instant;
29
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.openecomp.appc.domainmodel.lcm.ActionIdentifiers;
35 import org.openecomp.appc.domainmodel.lcm.CommonHeader;
36 import org.openecomp.appc.domainmodel.lcm.Flags;
37 import org.openecomp.appc.domainmodel.lcm.RequestContext;
38 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
39 import org.openecomp.appc.domainmodel.lcm.VNFContext;
40 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
41 import org.openecomp.appc.exceptions.APPCException;
42 import org.openecomp.appc.executionqueue.ExecutionQueueService;
43 import org.openecomp.appc.executor.impl.CommandExecutorImpl;
44 import org.openecomp.appc.executor.impl.CommandTaskFactory;
45 import org.openecomp.appc.executor.impl.LCMCommandTask;
46 import org.openecomp.appc.executor.impl.LCMReadonlyCommandTask;
47 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
48 import org.openecomp.appc.requesthandler.RequestHandler;
49 import org.openecomp.appc.workflow.WorkFlowManager;
50
51
52 @SuppressWarnings("deprecation")
53 public class TestCommandExecutor {
54
55                 private static final String TTL_FLAG= "TTL";
56                 private static final String API_VERSION= "2.0.0";
57                 private static final String ORIGINATOR_ID= "1";
58
59         CommandExecutorImpl commandExecutor;
60
61         CommandTaskFactory executionTaskFactory;
62
63         private RequestHandler requestHandler;
64         private WorkFlowManager workflowManager;
65         private LifecycleManager lifecyclemanager;
66
67         private ExecutionQueueService executionQueueService;
68
69         @Before
70         public void init()throws Exception {
71                 requestHandler= Mockito.mock(RequestHandler.class);
72                 lifecyclemanager= Mockito.mock(LifecycleManager.class);
73                 workflowManager= Mockito.mock(WorkFlowManager.class);
74
75                 executionQueueService = Mockito.mock(ExecutionQueueService.class);
76
77                 commandExecutor = new CommandExecutorImpl();
78                 executionTaskFactory = Mockito.mock(CommandTaskFactory.class);
79                 commandExecutor.setExecutionTaskFactory(executionTaskFactory);
80                 commandExecutor.setExecutionQueueService(executionQueueService);
81                 LCMCommandTask lcmCommandTask = Mockito.mock(LCMCommandTask.class);
82                 LCMReadonlyCommandTask LCMReadonlyCommandTask = Mockito.mock(LCMReadonlyCommandTask.class);
83                 Mockito.doReturn(lcmCommandTask).when(executionTaskFactory).getExecutionTask("Configure", null);
84                 Mockito.doReturn(LCMReadonlyCommandTask).when(executionTaskFactory).getExecutionTask("Sync", null);
85 //              Mockito.when(executionQueueService.putMessage((Runnable) Mockito.anyObject(),Mockito.anyLong(),(TimeUnit)Mockito.anyObject())).thenReturn(true);
86
87         }
88                 
89
90         @Test
91         public void testPositiveFlow_LCM(){
92                 //Map <String,Object> flags = setTTLInFlags("30");
93                 String requestId = "1";
94                 RuntimeContext commandExecutorInput = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure, "15", "") ;
95                 try {
96                         commandExecutor.executeCommand(commandExecutorInput);
97                 } catch (APPCException e) {
98                         Assert.fail(e.toString());
99                 }
100
101         }
102
103         @Test
104         public void testPositiveFlow_GetConfig(){
105                 String requestId = "1";
106
107                 RuntimeContext commandExecutorInput = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", Instant.now(), API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync,"15","") ;
108                 try {
109                         commandExecutor.executeCommand(commandExecutorInput);
110                 } catch (APPCException e) {
111                         Assert.fail(e.toString());
112                 }
113
114         }
115
116         
117         private RuntimeContext pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Instant timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){
118                 RuntimeContext commandExecutorInput = createCommandExecutorInputWithSubObjects();               
119                 RequestContext requestContext = commandExecutorInput.getRequestContext();
120                 requestContext.getCommonHeader().setFlags(new Flags(null, false, ttl));
121                 requestContext.getCommonHeader().setApiVer(apiVersion);
122                 requestContext.getCommonHeader().setTimestamp(timeStamp);
123                 requestContext.getCommonHeader().setRequestId(requestId);
124                 requestContext.getCommonHeader().setSubRequestId(subRequestID);
125                 requestContext.getCommonHeader().setOriginatorId(originatorID);
126                 requestContext.setAction(action);
127                 requestContext.setPayload(payload);
128                 requestContext.getActionIdentifiers().setVnfId(vnfId);
129                 VNFContext vnfContext = commandExecutorInput.getVnfContext();
130                 vnfContext.setType(vnfType);
131                 vnfContext.setId(vnfId);
132                 vnfContext.setVersion(vnfVersion);
133                 return commandExecutorInput;
134         }
135
136         private RuntimeContext createCommandExecutorInputWithSubObjects() {
137                 RuntimeContext runtimeContext = new RuntimeContext();
138         RequestContext requestContext = new RequestContext();
139                 runtimeContext.setRequestContext(requestContext);
140                 CommonHeader commonHeader = new CommonHeader();
141                 requestContext.setCommonHeader(commonHeader);
142                 commonHeader.setFlags(new Flags(null, false, 0));
143                 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
144                 requestContext.setActionIdentifiers(actionIdentifiers);
145                 VNFContext vnfContext = new VNFContext();
146                 runtimeContext.setVnfContext(vnfContext);
147                 return runtimeContext;
148         }
149
150
151
152 }
153