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 * ================================================================================
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.sdnc;
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;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.UUID;
41 import org.drools.core.WorkingMemory;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.onap.policy.drools.system.PolicyEngine;
48 import org.onap.policy.rest.RestManager;
49 import org.onap.policy.rest.RestManager.Pair;
50 import org.onap.policy.sdnc.util.Serialization;
52 public class SdncManagerTest {
53 private static WorkingMemory mockedWorkingMemory;
55 private RestManager mockedRestManager;
57 private Pair<Integer, String> httpResponsePutOk;
58 private Pair<Integer, String> httpResponseGetOk;
59 private Pair<Integer, String> httpResponseBadResponse;
60 private Pair<Integer, String> httpResponseErr;
62 private SdncRequest request;
63 private SdncResponse response;
66 public static void beforeTestSdncManager() {
67 mockedWorkingMemory = mock(WorkingMemory.class);
71 * Set up the mocked REST manager.
74 public void setupMockedRest() {
75 mockedRestManager = mock(RestManager.class);
77 httpResponsePutOk = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
78 httpResponseGetOk = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
79 httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
80 httpResponseErr = mockedRestManager.new Pair<>(200, null);
84 * Create the request and response before.
87 public void createRequestAndResponse() {
88 SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
89 serviceInfo.setServiceInstanceId("E-City");
91 SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
92 additionalParams.setSvcAction("Go Home");
93 additionalParams.setSvcRequestId("My Request");
95 SdncHealRequest healRequest = new SdncHealRequest();
96 healRequest.setRequestHeaderInfo(additionalParams);
97 healRequest.setServiceInfo(serviceInfo);
99 UUID requestId = UUID.randomUUID();
100 request = new SdncRequest();
101 request.setRequestId(requestId);
102 request.setHealRequest(healRequest);
103 request.setNsInstanceId("Dorothy");
105 SdncResponseOutput responseDescriptor = new SdncResponseOutput();
106 responseDescriptor.setSvcRequestId("1234");
107 responseDescriptor.setResponseCode("200");
108 responseDescriptor.setAckFinalIndicator("final-indicator-00");
110 response = new SdncResponse();
111 response.setRequestId(request.getRequestId().toString());
112 response.setResponseOutput(responseDescriptor);
116 * After Test clean up.
119 public void afterTestSdncManager() throws InterruptedException {
120 PolicyEngine.manager.getEnvironment().remove("sdnc.password");
121 PolicyEngine.manager.getEnvironment().remove("sdnc.username");
122 PolicyEngine.manager.getEnvironment().remove("sdnc.url");
126 public void testSdncInitiation() throws InterruptedException {
128 new SdncManager(null, null);
129 fail("test should throw an exception here");
131 catch (IllegalArgumentException e) {
133 "the parameters \"wm\" and \"request\" on the SdncManager constructor may not be null",
139 new SdncManager(mockedWorkingMemory, null);
140 fail("test should throw an exception here");
142 catch (IllegalArgumentException e) {
144 "the parameters \"wm\" and \"request\" on the SdncManager constructor may not be null",
150 new SdncManager(mockedWorkingMemory, request);
151 fail("test should throw an exception here");
153 catch (IllegalArgumentException e) {
155 "The value of policy engine manager environment property \"sdnc.url\" may not be null",
160 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
162 new SdncManager(mockedWorkingMemory, request);
163 fail("test should throw an exception here");
165 catch (IllegalArgumentException e) {
167 "The value of policy engine manager environment property \"sdnc.username\" may not be null",
172 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
174 new SdncManager(mockedWorkingMemory, request);
175 fail("test should throw an exception here");
177 catch (IllegalArgumentException e) {
179 "The value of policy engine manager environment property \"sdnc.password\" may not be null",
184 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Toto");
185 new SdncManager(mockedWorkingMemory, request);
189 public void testSdncExecutionException() throws InterruptedException {
190 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
191 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
192 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Exception");
194 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
195 manager.setRestManager(mockedRestManager);
197 Thread managerThread = new Thread(manager);
198 managerThread.start();
200 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Exception"), anyMap(), anyString(), anyString()))
201 .thenThrow(new RuntimeException("OzException"));
204 managerThread.join(100);
208 public void testSdncExecutionNull() throws InterruptedException {
209 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
210 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
211 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Null");
213 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
214 manager.setRestManager(mockedRestManager);
216 Thread managerThread = new Thread(manager);
217 managerThread.start();
219 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
222 managerThread.join(100);
227 public void testSdncExecutionError0() throws InterruptedException {
228 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
229 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
230 PolicyEngine.manager.getEnvironment().put("sdnc.password", "Error0");
232 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
233 manager.setRestManager(mockedRestManager);
235 Thread managerThread = new Thread(manager);
236 managerThread.start();
238 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
239 .thenReturn(httpResponseErr);
241 managerThread.join(100);
245 public void testSdncExecutionBadResponse() throws InterruptedException {
246 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
247 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
248 PolicyEngine.manager.getEnvironment().put("sdnc.password", "BadResponse");
250 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
251 manager.setRestManager(mockedRestManager);
253 Thread managerThread = new Thread(manager);
254 managerThread.start();
256 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
257 .thenReturn(httpResponseBadResponse);
259 managerThread.join(100);
263 public void testSdncExecutionOk() throws InterruptedException {
264 PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
265 PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
266 PolicyEngine.manager.getEnvironment().put("sdnc.password", "OK");
268 SdncManager manager = new SdncManager(mockedWorkingMemory, request);
269 manager.setRestManager(mockedRestManager);
271 Thread managerThread = new Thread(manager);
272 managerThread.start();
274 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
275 .thenReturn(httpResponsePutOk);
277 when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
278 .thenReturn(httpResponseGetOk);
281 managerThread.join(100);