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