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;
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.UUID;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.rest.RestManager;
41 import org.onap.policy.rest.RestManager.Pair;
42 import org.onap.policy.sdnc.SdncManager.SdncCallback;
43 import org.onap.policy.sdnc.util.Serialization;
45 public class SdncManagerTest implements SdncCallback {
46 private RestManager mockedRestManager;
48 private Pair<Integer, String> httpResponsePutOk;
49 private Pair<Integer, String> httpResponseGetOk;
50 private Pair<Integer, String> httpResponseBadResponse;
51 private Pair<Integer, String> httpResponseErr;
53 private SdncRequest request;
54 private SdncResponse response;
57 public static void beforeTestSdncManager() {
61 * Set up the mocked REST manager.
64 public void setupMockedRest() {
65 mockedRestManager = mock(RestManager.class);
67 httpResponsePutOk = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
68 httpResponseGetOk = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
69 httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
70 httpResponseErr = mockedRestManager.new Pair<>(200, null);
74 * Create the request and response before.
77 public void createRequestAndResponse() {
78 SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
79 serviceInfo.setServiceInstanceId("E-City");
81 SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
82 additionalParams.setSvcAction("Go Home");
83 additionalParams.setSvcRequestId("My Request");
85 SdncHealRequest healRequest = new SdncHealRequest();
86 healRequest.setRequestHeaderInfo(additionalParams);
87 healRequest.setServiceInfo(serviceInfo);
89 UUID requestId = UUID.randomUUID();
90 request = new SdncRequest();
91 request.setRequestId(requestId);
92 request.setHealRequest(healRequest);
93 request.setNsInstanceId("Dorothy");
95 SdncResponseOutput responseDescriptor = new SdncResponseOutput();
96 responseDescriptor.setSvcRequestId("1234");
97 responseDescriptor.setResponseCode("200");
98 responseDescriptor.setAckFinalIndicator("final-indicator-00");
100 response = new SdncResponse();
101 response.setRequestId(request.getRequestId().toString());
102 response.setResponseOutput(responseDescriptor);
106 public void testSdncInitiation() throws InterruptedException {
108 new SdncManager(null, null, null, null, null);
109 fail("test should throw an exception here");
111 catch (IllegalArgumentException e) {
113 "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null",
119 new SdncManager(this, null, null, null, null);
120 fail("test should throw an exception here");
122 catch (IllegalArgumentException e) {
124 "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null",
130 new SdncManager(this, request, null, null, null);
131 fail("test should throw an exception here");
133 catch (IllegalArgumentException e) {
135 "the \"url\" parameter on the SdncManager constructor may not be null",
140 new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Toto");
144 public void testSdncExecutionException() throws InterruptedException {
145 SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Exception");
146 manager.setRestManager(mockedRestManager);
148 Thread managerThread = new Thread(manager);
149 managerThread.start();
151 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Exception"), anyMap(), anyString(), anyString()))
152 .thenThrow(new RuntimeException("OzException"));
155 managerThread.join(100);
159 public void testSdncExecutionNull() throws InterruptedException {
160 SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Null");
161 manager.setRestManager(mockedRestManager);
163 Thread managerThread = new Thread(manager);
164 managerThread.start();
166 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
169 managerThread.join(100);
174 public void testSdncExecutionError0() throws InterruptedException {
175 SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Error0");
176 manager.setRestManager(mockedRestManager);
178 Thread managerThread = new Thread(manager);
179 managerThread.start();
181 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
182 .thenReturn(httpResponseErr);
184 managerThread.join(100);
188 public void testSdncExecutionBadResponse() throws InterruptedException {
189 SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "BadResponse");
190 manager.setRestManager(mockedRestManager);
192 Thread managerThread = new Thread(manager);
193 managerThread.start();
195 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
196 .thenReturn(httpResponseBadResponse);
198 managerThread.join(100);
202 public void testSdncExecutionOk() throws InterruptedException {
203 SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "OOK");
204 manager.setRestManager(mockedRestManager);
206 Thread managerThread = new Thread(manager);
207 managerThread.start();
209 when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
210 .thenReturn(httpResponsePutOk);
212 when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
213 .thenReturn(httpResponseGetOk);
216 managerThread.join(100);
220 public void onCallback(SdncResponse response) {
222 // Nothing really to do