2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson, AT&T. All rights reserved.
6 * Modifications Copyright (C) 2018-2019 AT&T Corporation. All rights 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.vfc;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
27 import static org.mockito.ArgumentMatchers.anyMap;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.endsWith;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.ArgumentMatchers.startsWith;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.UUID;
39 import org.drools.core.WorkingMemory;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.drools.system.PolicyEngine;
45 import org.onap.policy.rest.RestManager;
46 import org.onap.policy.rest.RestManager.Pair;
47 import org.onap.policy.vfc.util.Serialization;
49 public class VfcManagerTest {
50 private static WorkingMemory mockedWorkingMemory;
52 private RestManager mockedRestManager;
54 private Pair<Integer, String> httpResponsePutOk;
55 private Pair<Integer, String> httpResponseGetOk;
56 private Pair<Integer, String> httpResponseBadResponse;
57 private Pair<Integer, String> httpResponseErr;
59 private VfcRequest request;
60 private VfcResponse response;
63 public static void beforeTestVfcManager() {
64 mockedWorkingMemory = mock(WorkingMemory.class);
68 * Set up the mocked REST manager.
71 public void setupMockedRest() {
72 mockedRestManager = mock(RestManager.class);
74 httpResponsePutOk = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
75 httpResponseGetOk = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
76 httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
77 httpResponseErr = mockedRestManager.new Pair<>(200, null);
81 * Create the request and response before.
84 public void createRequestAndResponse() {
85 VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
86 actionInfo.setVmid("TheWizard");
87 actionInfo.setVmname("The Wizard of Oz");
89 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
90 additionalParams.setAction("Go Home");
91 additionalParams.setActionInfo(actionInfo);
93 VfcHealRequest healRequest = new VfcHealRequest();
94 healRequest.setAdditionalParams(additionalParams);
95 healRequest.setCause("WestWitch");
96 healRequest.setVnfInstanceId("EmeraldCity");
98 final UUID requestId = UUID.randomUUID();
99 request = new VfcRequest();
100 request.setHealRequest(healRequest);
101 request.setNsInstanceId("Dorothy");
102 request.setRequestId(requestId);
104 List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
106 VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor();
107 responseDescriptor.setErrorCode("1234");
108 responseDescriptor.setProgress("Follow The Yellow Brick Road");
109 responseDescriptor.setResponseHistoryList(responseHistoryList);
110 responseDescriptor.setResponseId(UUID.randomUUID().toString());
111 responseDescriptor.setStatus("finished");
112 responseDescriptor.setStatusDescription("There's no place like home");
114 response = new VfcResponse();
115 response.setJobId("1234");
116 response.setRequestId(request.getRequestId().toString());
117 response.setResponseDescriptor(responseDescriptor);
121 * Remove the environnment.
124 public void tearDown() {
125 PolicyEngine.manager.getEnvironment().remove("vfc.password");
126 PolicyEngine.manager.getEnvironment().remove("vfc.username");
127 PolicyEngine.manager.getEnvironment().remove("vfc.url");
131 public void testVfcInitiation() {
133 new VfcManager(null, null);
134 fail("test should throw an exception here");
136 catch (IllegalArgumentException e) {
137 assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null",
142 new VfcManager(mockedWorkingMemory, null);
143 fail("test should throw an exception here");
145 catch (IllegalArgumentException e) {
146 assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null",
151 new VfcManager(mockedWorkingMemory, request);
152 fail("test should throw an exception here");
154 catch (IllegalArgumentException e) {
155 assertEquals("The value of policy engine manager environment property \"vfc.url\" may not be null",
159 // add url; username & password are not required
160 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
161 new VfcManager(mockedWorkingMemory, request);
163 // url & username, but no password
164 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
166 // url, username, and password
167 PolicyEngine.manager.getEnvironment().put("vfc.password", "Toto");
168 new VfcManager(mockedWorkingMemory, request);
172 public void testVfcExecutionException() throws InterruptedException {
173 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
174 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
175 PolicyEngine.manager.getEnvironment().put("vfc.password", "Exception");
177 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
178 manager.setRestManager(mockedRestManager);
180 Thread managerThread = new Thread(manager);
181 managerThread.start();
183 when(mockedRestManager.post(
184 startsWith("http://somewhere.over.the.rainbow"),
190 .thenThrow(new RuntimeException("OzException"));
192 managerThread.join();
194 PolicyEngine.manager.getEnvironment().remove("vfc.password");
195 PolicyEngine.manager.getEnvironment().remove("vfc.username");
196 PolicyEngine.manager.getEnvironment().remove("vfc.url");
200 public void testVfcExecutionNull() throws InterruptedException {
201 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
202 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
203 PolicyEngine.manager.getEnvironment().put("vfc.password", "Null");
205 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
206 manager.setRestManager(mockedRestManager);
208 Thread managerThread = new Thread(manager);
209 managerThread.start();
211 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
212 eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
215 managerThread.join();
217 PolicyEngine.manager.getEnvironment().remove("vfc.password");
218 PolicyEngine.manager.getEnvironment().remove("vfc.username");
219 PolicyEngine.manager.getEnvironment().remove("vfc.url");
223 public void testVfcExecutionError0() throws InterruptedException {
224 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
225 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
226 PolicyEngine.manager.getEnvironment().put("vfc.password", "Error0");
228 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
229 manager.setRestManager(mockedRestManager);
231 Thread managerThread = new Thread(manager);
232 managerThread.start();
234 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
235 eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
236 .thenReturn(httpResponseErr);
238 managerThread.join();
240 PolicyEngine.manager.getEnvironment().remove("vfc.password");
241 PolicyEngine.manager.getEnvironment().remove("vfc.username");
242 PolicyEngine.manager.getEnvironment().remove("vfc.url");
246 public void testVfcExecutionBadResponse() throws InterruptedException {
247 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
248 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
249 PolicyEngine.manager.getEnvironment().put("vfc.password", "BadResponse");
251 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
252 manager.setRestManager(mockedRestManager);
254 Thread managerThread = new Thread(manager);
255 managerThread.start();
257 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
258 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
259 .thenReturn(httpResponseBadResponse);
261 managerThread.join();
263 PolicyEngine.manager.getEnvironment().remove("vfc.password");
264 PolicyEngine.manager.getEnvironment().remove("vfc.username");
265 PolicyEngine.manager.getEnvironment().remove("vfc.url");
269 public void testVfcExecutionOk() throws InterruptedException {
270 PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
271 PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
272 PolicyEngine.manager.getEnvironment().put("vfc.password", "OK");
274 VfcManager manager = new VfcManager(mockedWorkingMemory, request);
275 manager.setRestManager(mockedRestManager);
277 Thread managerThread = new Thread(manager);
278 managerThread.start();
280 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
281 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
282 .thenReturn(httpResponsePutOk);
284 when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
285 .thenReturn(httpResponseGetOk);
287 managerThread.join();
289 PolicyEngine.manager.getEnvironment().remove("vfc.password");
290 PolicyEngine.manager.getEnvironment().remove("vfc.username");
291 PolicyEngine.manager.getEnvironment().remove("vfc.url");