2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.platform.mod.web.service;
23 import okhttp3.mockwebserver.MockResponse;
24 import okhttp3.mockwebserver.MockWebServer;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.Mock;
29 import org.mockito.Spy;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
32 import org.onap.dcaegen2.platform.mod.util.PolicyModelUtils;
33 import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelDistributionServiceImpl;
34 import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelGateway;
35 import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelServiceImpl;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.reactive.function.client.WebClient;
40 import java.io.IOException;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.Mockito.times;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.when;
47 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_ENV;
48 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_ID;
49 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_ID_ERROR;
50 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelResponse;
52 @ExtendWith(MockitoExtension.class)
53 class PolicyModelDistributionServiceImplTest {
56 private PolicyModelDistributionServiceImpl pmDistributionImplService = new PolicyModelDistributionServiceImpl();
59 private PolicyModelServiceImpl policyModelServiceImpl;
62 private PolicyModelGateway policyModelGateway;
65 private PolicyModelUtils policyModelUtils;
69 pmDistributionImplService.setPolicyModelServiceImpl(policyModelServiceImpl);
70 pmDistributionImplService.setPolicyModelGateway(policyModelGateway);
71 pmDistributionImplService.setPolicyModelUtils(policyModelUtils);
75 void test_getPolicyModelDistributionByIdReturnSucess() throws IOException {
76 PolicyModel policyModel = getPolicyModelResponse();
78 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
80 MockWebServer mockBackEnd = initialize_webServer();
82 // Calling the same method twice to resolve the issue of mock server hanging for response
83 initResponse(mockBackEnd,200,"Operation Successfull");
84 initResponse(mockBackEnd,200,"Operation Successfull");
86 ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
88 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.OK);
89 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
90 mockBackEnd.shutdown();
95 void test_getPolicyModelDistributionByIdReturnUnauthorized() throws IOException {
96 PolicyModel policyModel = getPolicyModelResponse();
98 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
100 MockWebServer mockBackEnd = initialize_webServer();
102 initResponse(mockBackEnd,401,"Authentication Error");
104 ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
106 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
107 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
108 mockBackEnd.shutdown();
112 void test_getPolicyModelDistributionByIdReturnForbidden() throws IOException {
113 PolicyModel policyModel = getPolicyModelResponse();
115 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
117 MockWebServer mockBackEnd = initialize_webServer();
119 initResponse(mockBackEnd,403,"Authorization Error");
121 ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
124 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
125 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
126 mockBackEnd.shutdown();
130 void test_getPolicyModelDistributionByIdReturnNotFound() throws IOException {
131 PolicyModel policyModel = getPolicyModelResponse();
133 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
135 MockWebServer mockBackEnd = initialize_webServer();
137 initResponse(mockBackEnd,404,"Policy Model Not Found");
139 ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
141 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
142 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
143 mockBackEnd.shutdown();
147 void test_getPolicyModelDistributionByIdReturnUnsupportedMediaType() throws IOException {
148 PolicyModel policyModel = getPolicyModelResponse();
150 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
152 MockWebServer mockBackEnd = initialize_webServer();
154 initResponse(mockBackEnd,415,"Unsupported Media Type");
156 ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
158 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
159 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
160 mockBackEnd.shutdown();
164 void test_getPolicyModelDistributionByIdReturnInternalServerError() throws IOException {
165 PolicyModel policyModel = getPolicyModelResponse();
167 when(policyModelServiceImpl.getPolicyModelById("test")).thenReturn(policyModel);
169 MockWebServer mockBackEnd = initialize_webServer();
171 initResponse(mockBackEnd,500,"Internal Server Error");
173 ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,"test");
175 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
176 verify(policyModelServiceImpl, times(1)).getPolicyModelById("test");
177 mockBackEnd.shutdown();
182 void test_distributePolicyModelReturnSucess() throws IOException {
183 PolicyModel policyModel = getPolicyModelResponse();
185 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
186 when(policyModelGateway.save(any())).thenReturn(policyModel);
188 MockWebServer mockBackEnd = initialize_webServer();
190 // Calling the same method twice to resolve the issue of mock server hanging for response
191 initResponse(mockBackEnd,200,"Operation Successfull");
192 initResponse(mockBackEnd,200,"Operation Successfull");
194 ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
196 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.OK);
197 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
198 verify(policyModelGateway, times(1)).save(any());
199 mockBackEnd.shutdown();
203 void test_distributePolicyModelReturnBadRequest() throws IOException {
204 PolicyModel policyModel = getPolicyModelResponse();
206 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
207 when(policyModelGateway.save(any())).thenReturn(policyModel);
209 MockWebServer mockBackEnd = initialize_webServer();
211 initResponse(mockBackEnd,400,"Invalid Body of Policy Model");
213 ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
215 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
216 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
217 verify(policyModelGateway, times(1)).save(any());
218 mockBackEnd.shutdown();
222 void test_distributePolicyModelReturnUnauthorized() throws IOException {
223 PolicyModel policyModel = getPolicyModelResponse();
225 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
226 when(policyModelGateway.save(any())).thenReturn(policyModel);
228 MockWebServer mockBackEnd = initialize_webServer();
230 initResponse(mockBackEnd,401,"Authentication Error");
232 ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
234 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
235 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
236 verify(policyModelGateway, times(1)).save(any());
240 void test_distributePolicyModelReturnForbidden() throws IOException {
241 PolicyModel policyModel = getPolicyModelResponse();
243 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
244 when(policyModelGateway.save(any())).thenReturn(policyModel);
246 MockWebServer mockBackEnd = initialize_webServer();
248 initResponse(mockBackEnd,403,"Authorization Error");
250 ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
252 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
253 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
254 verify(policyModelGateway, times(1)).save(any());
258 void test_distributePolicyModelReturnNotAcceptable() throws IOException {
259 PolicyModel policyModel = getPolicyModelResponse();
261 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
262 when(policyModelGateway.save(any())).thenReturn(policyModel);
264 MockWebServer mockBackEnd = initialize_webServer();
266 initResponse(mockBackEnd,406,"Not Acceptable Policy Model Version");
268 ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
270 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
271 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
272 verify(policyModelGateway, times(1)).save(any());
276 void test_distributePolicyModelReturnUnsupportedMediaType() throws IOException {
277 PolicyModel policyModel = getPolicyModelResponse();
279 when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
280 when(policyModelGateway.save(any())).thenReturn(policyModel);
282 MockWebServer mockBackEnd = initialize_webServer();
284 initResponse(mockBackEnd,415,"Unsupported Media Type");
286 ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
288 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
289 verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
290 verify(policyModelGateway, times(1)).save(any());
294 void test_distributePolicyModelReturnInternalServerError() throws IOException {
295 PolicyModel policyModel = getPolicyModelResponse();
297 when(policyModelServiceImpl.getPolicyModelById("test")).thenReturn(policyModel);
298 when(policyModelGateway.save(any())).thenReturn(policyModel);
300 MockWebServer mockBackEnd = initialize_webServer();
302 initResponse(mockBackEnd,500,"Internal Server Error");
304 ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,"test");
306 assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
307 verify(policyModelServiceImpl, times(1)).getPolicyModelById("test");
308 verify(policyModelGateway, times(1)).save(any());
311 private MockWebServer initialize_webServer() throws IOException {
312 MockWebServer mockBackEnd = new MockWebServer();
315 String baseUrl = String.format("http://localhost:%s", mockBackEnd.getPort());
316 when(policyModelUtils.getPolicyEngineURL(PM_DISTRIBUTION_ENV)).thenReturn(baseUrl);
317 when(policyModelUtils.getWebClient(PM_DISTRIBUTION_ENV)).thenReturn(WebClient.create(baseUrl));
322 private void initResponse(MockWebServer mockBackEnd, int responseCode, String responseBody) {
323 String webClientResponse = responseBody;
324 mockBackEnd.enqueue(new MockResponse().setResponseCode(responseCode).setBody(webClientResponse)
325 .addHeader("Content-Type", "text/plain;charset=ISO-8859-1"));