2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved
8 * Modifications Copyright (C) 2019 Nordix Foundation.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.sdnc;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.fail;
29 import static org.mockito.ArgumentMatchers.anyMap;
30 import static org.mockito.ArgumentMatchers.anyString;
31 import static org.mockito.ArgumentMatchers.endsWith;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.ArgumentMatchers.startsWith;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.UUID;
42 import org.drools.core.WorkingMemory;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.onap.policy.drools.system.PolicyEngine;
49 import org.onap.policy.rest.RestManager;
50 import org.onap.policy.rest.RestManager.Pair;
51 import org.onap.policy.sdnc.util.Serialization;
53 public class SdncManagerTest {
54 private static WorkingMemory mockedWorkingMemory;
56 private RestManager mockedRestManager;
58 private Pair<Integer, String> httpResponsePutOk;
59 private Pair<Integer, String> httpResponseGetOk;
60 private Pair<Integer, String> httpResponseBadResponse;
61 private Pair<Integer, String> httpResponseErr;
63 private SdncRequest request;
64 private SdncResponse response;
67 public static void beforeTestSdncManager() {
68 mockedWorkingMemory = mock(WorkingMemory.class);
72 * Set up the mocked REST manager.
75 public void setupMockedRest() {
76 mockedRestManager = mock(RestManager.class);
78 httpResponsePutOk = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
79 httpResponseGetOk = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
80 httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
81 httpResponseErr = mockedRestManager.new Pair<>(200, null);
85 * Create the request and response before.
88 public void createRequestAndResponse() {
89 SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
90 serviceInfo.setServiceInstanceId("E-City");
92 SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
93 additionalParams.setSvcAction("Go Home");
94 additionalParams.setSvcRequestId("My Request");
96 SdncHealRequest healRequest = new SdncHealRequest();
97 healRequest.setRequestHeaderInfo(additionalParams);
98 healRequest.setServiceInfo(serviceInfo);
100 UUID requestId = UUID.randomUUID();
101 request = new SdncRequest();
102 request.setRequestId(requestId);
103 request.setHealRequest(healRequest);
104 request.setNsInstanceId("Dorothy");
106 SdncResponseOutput responseDescriptor = new SdncResponseOutput();
107 responseDescriptor.setSvcRequestId("1234");
108 responseDescriptor.setResponseCode("200");
109 responseDescriptor.setAckFinalIndicator("final-indicator-00");
111 response = new SdncResponse();
112 response.setRequestId(request.getRequestId().toString());
113 response.setResponseOutput(responseDescriptor);
117 * After Test clean up.
120 public void afterTestSdncManager() throws InterruptedException {
121 PolicyEngine.manager.getEnvironment().remove("sdnc.password");
122 PolicyEngine.manager.getEnvironment().remove("sdnc.username");
123 PolicyEngine.manager.getEnvironment().remove("sdnc.url");
127 public void testSdncInitiation() throws InterruptedException {
129 new SdncManager(null, null);
130 fail("test should throw an exception here");
132 catch (IllegalArgumentException e) {
134 "the parameters \"wm\" and \"request\" on the SdncManager constructor may not be null",
140 new SdncManager(mockedWorkingMemory, null);
141 fail("test should throw an exception here");
143 catch (IllegalArgumentException e) {
145 "the parameters \"wm\" and \"request\" on the SdncManager constructor may not be null",
151 new SdncManager(mockedWorkingMemory, request);
152 fail("test should throw an exception here");
154 catch (IllegalArgumentException e) {
156 "The value of policy engine manager environment property \"sdnc.url\" may not be null",
161 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
163 new SdncManager(mockedWorkingMemory, request);
164 fail("test should throw an exception here");
166 catch (IllegalArgumentException e) {
168 "The value of policy engine manager environment property \"sdnc.username\" may not be null",
173 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
175 new SdncManager(mockedWorkingMemory, request);
176 fail("test should throw an exception here");
178 catch (IllegalArgumentException e) {
180 "The value of policy engine manager environment property \"sdnc.password\" may not be null",
185 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Toto");
186 new SdncManager(mockedWorkingMemory, request);
190 public void testSdncExecutionException() throws InterruptedException {
191 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
192 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
193 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Exception");
195 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
196 manager.setRestManager(mockedRestManager);
198 Thread managerThread = new Thread(manager);
199 managerThread.start();
201 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Exception"), anyMap(), anyString(), anyString()))
202 .thenThrow(new RuntimeException("OzException"));
205 managerThread.join(100);
209 public void testSdncExecutionNull() throws InterruptedException {
210 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
211 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
212 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Null");
214 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
215 manager.setRestManager(mockedRestManager);
217 Thread managerThread = new Thread(manager);
218 managerThread.start();
220 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
223 managerThread.join(100);
228 public void testSdncExecutionError0() throws InterruptedException {
229 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
230 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
231 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Error0");
233 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
234 manager.setRestManager(mockedRestManager);
236 Thread managerThread = new Thread(manager);
237 managerThread.start();
239 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
240 .thenReturn(httpResponseErr);
242 managerThread.join(100);
246 public void testSdncExecutionBadResponse() throws InterruptedException {
247 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
248 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
249 PolicyEngine.manager.getEnvironment().put("sdnc.password", "BadResponse");
251 SdncManager manager = new SdncManager(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"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
258 .thenReturn(httpResponseBadResponse);
260 managerThread.join(100);
264 public void testSdncExecutionOk() throws InterruptedException {
265 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
266 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
267 PolicyEngine.manager.getEnvironment().put("sdnc.password", "OK");
269 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
270 manager.setRestManager(mockedRestManager);
272 Thread managerThread = new Thread(manager);
273 managerThread.start();
275 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
276 .thenReturn(httpResponsePutOk);
278 when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
279 .thenReturn(httpResponseGetOk);
282 managerThread.join(100);