2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved
8 * Modifications Copyright (C) 2019-2020 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.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.ArgumentMatchers.anyString;
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.verify;
34 import static org.mockito.Mockito.when;
36 import java.util.UUID;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.rest.RestManager;
41 import org.onap.policy.sdnc.SdncManager.SdncCallback;
42 import org.onap.policy.sdnc.util.Serialization;
44 public class SdncManagerTest implements SdncCallback {
45 private static final String SOMEWHERE_OVER_THE_RAINBOW = "http://somewhere.over.the.rainbow";
47 private static final String DOROTHY = "Dorothy";
49 private RestManager mockedRestManager;
51 private Pair<Integer, String> httpResponsePutOk;
52 private Pair<Integer, String> httpResponseBadResponse;
53 private Pair<Integer, String> httpResponseErr;
55 private SdncRequest request;
56 private SdncResponse response;
59 * Set up the mocked REST manager.
62 public void setupMockedRest() {
63 mockedRestManager = mock(RestManager.class);
65 httpResponsePutOk = Pair.of(202, Serialization.gsonPretty.toJson(response));
66 httpResponseBadResponse = Pair.of(202, Serialization.gsonPretty.toJson(null));
67 httpResponseErr = Pair.of(200, null);
71 * Create the request and response before.
74 public void createRequestAndResponse() {
75 SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
76 serviceInfo.setServiceInstanceId("E-City");
78 SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
79 additionalParams.setSvcAction("Go Home");
80 additionalParams.setSvcRequestId("My Request");
82 SdncHealRequest healRequest = new SdncHealRequest();
83 healRequest.setRequestHeaderInfo(additionalParams);
84 healRequest.setServiceInfo(serviceInfo);
86 UUID requestId = UUID.randomUUID();
87 request = new SdncRequest();
88 request.setRequestId(requestId);
89 request.setHealRequest(healRequest);
90 request.setNsInstanceId(DOROTHY);
92 SdncResponseOutput responseDescriptor = new SdncResponseOutput();
93 responseDescriptor.setSvcRequestId("1234");
94 responseDescriptor.setResponseCode("200");
95 responseDescriptor.setAckFinalIndicator("final-indicator-00");
97 response = new SdncResponse();
98 response.setRequestId(request.getRequestId().toString());
99 response.setResponseOutput(responseDescriptor);
103 public void testSdncInitiation() {
105 assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(null, null, null, null, null))
106 .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null");
108 assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, null, null, null, null))
109 .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null");
111 assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, request, null, null, null))
112 .withMessage("the \"url\" parameter on the SdncManager constructor may not be null");
114 new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Toto");
118 public void testSdncExecutionException() throws InterruptedException {
119 SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Exception");
120 manager.setRestManager(mockedRestManager);
122 when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Exception"), anyMap(),
123 anyString(), anyString())).thenThrow(new RuntimeException("OzException"));
125 Thread managerThread = new Thread(manager);
126 managerThread.start();
128 managerThread.join(1000);
130 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
134 public void testSdncExecutionNull() throws InterruptedException {
135 SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Null");
136 manager.setRestManager(mockedRestManager);
138 when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Null"), anyMap(),
139 anyString(), anyString())).thenReturn(null);
143 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
148 public void testSdncExecutionError0() throws InterruptedException {
149 SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Error0");
150 manager.setRestManager(mockedRestManager);
152 when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Error0"), anyMap(),
153 anyString(), anyString())).thenReturn(httpResponseErr);
157 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
161 public void testSdncExecutionBadResponse() throws InterruptedException {
162 SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "BadResponse");
163 manager.setRestManager(mockedRestManager);
165 when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(),
166 anyString(), anyString())).thenReturn(httpResponseBadResponse);
170 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
174 public void testSdncExecutionOk() throws InterruptedException {
175 SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "OOK");
176 manager.setRestManager(mockedRestManager);
178 when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(),
179 anyString(), anyString())).thenReturn(httpResponsePutOk);
183 verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
187 public void onCallback(SdncResponse response) {
189 // Nothing really to do