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