2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.. All rights reserved.
7 * Modifications Copyright (C) 2018-2019 AT&T Corporation. All rights reserved.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
23 package org.onap.policy.vfc;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.ArgumentMatchers.endsWith;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.ArgumentMatchers.startsWith;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.UUID;
40 import org.drools.core.WorkingMemory;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.policy.drools.system.PolicyEngine;
46 import org.onap.policy.rest.RestManager;
47 import org.onap.policy.rest.RestManager.Pair;
48 import org.onap.policy.vfc.util.Serialization;
50 public class VfcManagerTest {
51 private static WorkingMemory mockedWorkingMemory;
53 private RestManager mockedRestManager;
55 private Pair<Integer, String> httpResponsePutOk;
56 private Pair<Integer, String> httpResponseGetOk;
57 private Pair<Integer, String> httpResponseBadResponse;
58 private Pair<Integer, String> httpResponseErr;
60 private VfcRequest request;
61 private VfcResponse response;
64 public static void beforeTestVfcManager() {
65 mockedWorkingMemory = mock(WorkingMemory.class);
69 * Set up the mocked REST manager.
72 public void setupMockedRest() {
73 mockedRestManager = mock(RestManager.class);
75 httpResponsePutOk = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
76 httpResponseGetOk = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
77 httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
78 httpResponseErr = mockedRestManager.new Pair<>(200, null);
82 * Create the request and response before.
85 public void createRequestAndResponse() {
86 VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
87 actionInfo.setVmid("TheWizard");
88 actionInfo.setVmname("The Wizard of Oz");
90 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
91 additionalParams.setAction("Go Home");
92 additionalParams.setActionInfo(actionInfo);
94 VfcHealRequest healRequest = new VfcHealRequest();
95 healRequest.setAdditionalParams(additionalParams);
96 healRequest.setCause("WestWitch");
97 healRequest.setVnfInstanceId("EmeraldCity");
99 final UUID requestId = UUID.randomUUID();
100 request = new VfcRequest();
101 request.setHealRequest(healRequest);
102 request.setNsInstanceId("Dorothy");
103 request.setRequestId(requestId);
105 List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
107 VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor();
108 responseDescriptor.setErrorCode("1234");
109 responseDescriptor.setProgress("Follow The Yellow Brick Road");
110 responseDescriptor.setResponseHistoryList(responseHistoryList);
111 responseDescriptor.setResponseId(UUID.randomUUID().toString());
112 responseDescriptor.setStatus("finished");
113 responseDescriptor.setStatusDescription("There's no place like home");
115 response = new VfcResponse();
116 response.setJobId("1234");
117 response.setRequestId(request.getRequestId().toString());
118 response.setResponseDescriptor(responseDescriptor);
122 * Remove the environnment.
125 public void tearDown() {
126 PolicyEngine.manager.getEnvironment().remove("vfc.password");
127 PolicyEngine.manager.getEnvironment().remove("vfc.username");
128 PolicyEngine.manager.getEnvironment().remove("vfc.url");
132 public void testVfcInitiation() {
134 new VfcManager(null, null);
135 fail("test should throw an exception here");
137 catch (IllegalArgumentException e) {
138 assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null",
143 new VfcManager(mockedWorkingMemory, null);
144 fail("test should throw an exception here");
146 catch (IllegalArgumentException e) {
147 assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null",
152 new VfcManager(mockedWorkingMemory, request);
153 fail("test should throw an exception here");
155 catch (IllegalArgumentException e) {
156 assertEquals("The value of policy engine manager environment property \"vfc.url\" may not be null",
160 // add url; username & password are not required
161 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
162 new VfcManager(mockedWorkingMemory, request);
164 // url & username, but no password
165 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
167 // url, username, and password
168 PolicyEngine.manager.getEnvironment().put("vfc.password", "Toto");
169 new VfcManager(mockedWorkingMemory, request);
173 public void testVfcExecutionException() throws InterruptedException {
174 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
175 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
176 PolicyEngine.manager.getEnvironment().put("vfc.password", "Exception");
178 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
179 manager.setRestManager(mockedRestManager);
181 Thread managerThread = new Thread(manager);
182 managerThread.start();
184 when(mockedRestManager.post(
185 startsWith("http://somewhere.over.the.rainbow"),
191 .thenThrow(new RuntimeException("OzException"));
193 managerThread.join();
195 PolicyEngine.manager.getEnvironment().remove("vfc.password");
196 PolicyEngine.manager.getEnvironment().remove("vfc.username");
197 PolicyEngine.manager.getEnvironment().remove("vfc.url");
201 public void testVfcExecutionNull() throws InterruptedException {
202 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
203 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
204 PolicyEngine.manager.getEnvironment().put("vfc.password", "Null");
206 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
207 manager.setRestManager(mockedRestManager);
209 Thread managerThread = new Thread(manager);
210 managerThread.start();
212 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
213 eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
216 managerThread.join();
218 PolicyEngine.manager.getEnvironment().remove("vfc.password");
219 PolicyEngine.manager.getEnvironment().remove("vfc.username");
220 PolicyEngine.manager.getEnvironment().remove("vfc.url");
224 public void testVfcExecutionError0() throws InterruptedException {
225 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
226 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
227 PolicyEngine.manager.getEnvironment().put("vfc.password", "Error0");
229 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
230 manager.setRestManager(mockedRestManager);
232 Thread managerThread = new Thread(manager);
233 managerThread.start();
235 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
236 eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
237 .thenReturn(httpResponseErr);
239 managerThread.join();
241 PolicyEngine.manager.getEnvironment().remove("vfc.password");
242 PolicyEngine.manager.getEnvironment().remove("vfc.username");
243 PolicyEngine.manager.getEnvironment().remove("vfc.url");
247 public void testVfcExecutionBadResponse() throws InterruptedException {
248 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
249 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
250 PolicyEngine.manager.getEnvironment().put("vfc.password", "BadResponse");
252 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
253 manager.setRestManager(mockedRestManager);
255 Thread managerThread = new Thread(manager);
256 managerThread.start();
258 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
259 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
260 .thenReturn(httpResponseBadResponse);
262 managerThread.join();
264 PolicyEngine.manager.getEnvironment().remove("vfc.password");
265 PolicyEngine.manager.getEnvironment().remove("vfc.username");
266 PolicyEngine.manager.getEnvironment().remove("vfc.url");
270 public void testVfcExecutionOk() throws InterruptedException {
271 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
272 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
273 PolicyEngine.manager.getEnvironment().put("vfc.password", "OK");
275 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
276 manager.setRestManager(mockedRestManager);
278 Thread managerThread = new Thread(manager);
279 managerThread.start();
281 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
282 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
283 .thenReturn(httpResponsePutOk);
285 when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
286 .thenReturn(httpResponseGetOk);
288 managerThread.join();
290 PolicyEngine.manager.getEnvironment().remove("vfc.password");
291 PolicyEngine.manager.getEnvironment().remove("vfc.username");
292 PolicyEngine.manager.getEnvironment().remove("vfc.url");