Policy Model Distribution POC Issue-ID: DCAEGEN2-1868
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / service / PolicyModelDistributionServiceImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.platform.mod.web.service;
22
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;
39
40 import java.io.IOException;
41
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;
51
52 @ExtendWith(MockitoExtension.class)
53 class PolicyModelDistributionServiceImplTest {
54
55     @Spy
56     private PolicyModelDistributionServiceImpl pmDistributionImplService = new PolicyModelDistributionServiceImpl();
57
58     @Mock
59     private PolicyModelServiceImpl policyModelServiceImpl;
60
61     @Mock
62     private PolicyModelGateway policyModelGateway;
63
64     @Mock
65     private PolicyModelUtils policyModelUtils;
66
67     @BeforeEach
68     void initialize(){
69         pmDistributionImplService.setPolicyModelServiceImpl(policyModelServiceImpl);
70         pmDistributionImplService.setPolicyModelGateway(policyModelGateway);
71         pmDistributionImplService.setPolicyModelUtils(policyModelUtils);
72     }
73
74     @Test
75     void test_getPolicyModelDistributionByIdReturnSucess() throws IOException {
76         PolicyModel policyModel = getPolicyModelResponse();
77
78         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
79
80         MockWebServer mockBackEnd = initialize_webServer();
81
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");
85
86         ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
87
88         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.OK);
89         verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
90         mockBackEnd.shutdown();
91     }
92
93
94     @Test
95     void test_getPolicyModelDistributionByIdReturnUnauthorized()  throws IOException {
96         PolicyModel policyModel = getPolicyModelResponse();
97
98         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
99
100         MockWebServer mockBackEnd = initialize_webServer();
101
102         initResponse(mockBackEnd,401,"Authentication Error");
103
104         ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
105
106         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
107         verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
108         mockBackEnd.shutdown();
109     }
110
111     @Test
112     void test_getPolicyModelDistributionByIdReturnForbidden() throws IOException {
113         PolicyModel policyModel = getPolicyModelResponse();
114
115         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
116
117         MockWebServer mockBackEnd = initialize_webServer();
118
119         initResponse(mockBackEnd,403,"Authorization Error");
120
121         ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
122
123
124         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
125         verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
126         mockBackEnd.shutdown();
127     }
128
129     @Test
130     void test_getPolicyModelDistributionByIdReturnNotFound()  throws IOException {
131         PolicyModel policyModel = getPolicyModelResponse();
132
133         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
134
135         MockWebServer mockBackEnd = initialize_webServer();
136
137         initResponse(mockBackEnd,404,"Policy Model Not Found");
138
139         ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
140
141         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
142         verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
143         mockBackEnd.shutdown();
144     }
145
146     @Test
147     void test_getPolicyModelDistributionByIdReturnUnsupportedMediaType()  throws IOException {
148         PolicyModel policyModel = getPolicyModelResponse();
149
150         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
151
152         MockWebServer mockBackEnd = initialize_webServer();
153
154         initResponse(mockBackEnd,415,"Unsupported Media Type");
155
156         ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
157
158         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
159         verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
160         mockBackEnd.shutdown();
161     }
162
163     @Test
164     void test_getPolicyModelDistributionByIdReturnInternalServerError()  throws IOException  {
165         PolicyModel policyModel = getPolicyModelResponse();
166
167         when(policyModelServiceImpl.getPolicyModelById("test")).thenReturn(policyModel);
168
169         MockWebServer mockBackEnd = initialize_webServer();
170
171         initResponse(mockBackEnd,500,"Internal Server Error");
172
173         ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,"test");
174
175         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
176         verify(policyModelServiceImpl, times(1)).getPolicyModelById("test");
177         mockBackEnd.shutdown();
178     }
179
180
181     @Test
182     void test_distributePolicyModelReturnSucess() throws IOException {
183         PolicyModel policyModel = getPolicyModelResponse();
184
185         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
186         when(policyModelGateway.save(any())).thenReturn(policyModel);
187
188         MockWebServer mockBackEnd = initialize_webServer();
189
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");
193
194         ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
195
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();
200     }
201
202     @Test
203     void test_distributePolicyModelReturnBadRequest() throws IOException {
204         PolicyModel policyModel = getPolicyModelResponse();
205
206         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
207         when(policyModelGateway.save(any())).thenReturn(policyModel);
208
209         MockWebServer mockBackEnd = initialize_webServer();
210
211         initResponse(mockBackEnd,400,"Invalid Body of Policy Model");
212
213         ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
214
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();
219     }
220
221     @Test
222     void test_distributePolicyModelReturnUnauthorized()  throws IOException  {
223         PolicyModel policyModel = getPolicyModelResponse();
224
225         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
226         when(policyModelGateway.save(any())).thenReturn(policyModel);
227
228         MockWebServer mockBackEnd = initialize_webServer();
229
230         initResponse(mockBackEnd,401,"Authentication Error");
231
232         ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
233
234         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
235         verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
236         verify(policyModelGateway, times(1)).save(any());
237     }
238
239     @Test
240         void test_distributePolicyModelReturnForbidden() throws IOException {
241         PolicyModel policyModel = getPolicyModelResponse();
242
243         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
244         when(policyModelGateway.save(any())).thenReturn(policyModel);
245
246         MockWebServer mockBackEnd = initialize_webServer();
247
248         initResponse(mockBackEnd,403,"Authorization Error");
249
250         ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
251
252         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
253         verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
254         verify(policyModelGateway, times(1)).save(any());
255     }
256
257     @Test
258     void test_distributePolicyModelReturnNotAcceptable() throws IOException {
259         PolicyModel policyModel = getPolicyModelResponse();
260
261         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
262         when(policyModelGateway.save(any())).thenReturn(policyModel);
263
264         MockWebServer mockBackEnd = initialize_webServer();
265
266         initResponse(mockBackEnd,406,"Not Acceptable Policy Model Version");
267
268         ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
269
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());
273     }
274
275     @Test
276     void test_distributePolicyModelReturnUnsupportedMediaType() throws IOException {
277         PolicyModel policyModel = getPolicyModelResponse();
278
279         when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
280         when(policyModelGateway.save(any())).thenReturn(policyModel);
281
282         MockWebServer mockBackEnd = initialize_webServer();
283
284         initResponse(mockBackEnd,415,"Unsupported Media Type");
285
286         ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
287
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());
291     }
292
293     @Test
294     void test_distributePolicyModelReturnInternalServerError() throws IOException {
295         PolicyModel policyModel = getPolicyModelResponse();
296
297         when(policyModelServiceImpl.getPolicyModelById("test")).thenReturn(policyModel);
298         when(policyModelGateway.save(any())).thenReturn(policyModel);
299
300         MockWebServer mockBackEnd = initialize_webServer();
301
302         initResponse(mockBackEnd,500,"Internal Server Error");
303
304         ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,"test");
305
306         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
307         verify(policyModelServiceImpl, times(1)).getPolicyModelById("test");
308         verify(policyModelGateway, times(1)).save(any());
309     }
310
311     private MockWebServer initialize_webServer() throws IOException {
312         MockWebServer mockBackEnd = new MockWebServer();
313         mockBackEnd.start();
314
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));
318
319         return mockBackEnd;
320     }
321
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"));
326     }
327
328
329 }