2e8da35f3eab0032ac4f67cca4499519e5297f13
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / client / SdcNotificationWithSol004PackageTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.asdc.client;
21
22 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
23 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
24 import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
28 import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
29 import static com.github.tomakehurst.wiremock.client.WireMock.post;
30 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
31 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
32 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertNotNull;
35 import static org.junit.Assert.fail;
36 import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_ERROR;
37 import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_OK;
38 import static org.springframework.http.HttpHeaders.ACCEPT;
39 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
40 import java.io.File;
41 import java.io.IOException;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.List;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.onap.so.asdc.BaseTest;
50 import org.onap.so.asdc.client.exceptions.ASDCControllerException;
51 import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
52 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
53 import org.onap.so.db.catalog.data.repository.ServiceRepository;
54 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
55 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatusId;
56 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.http.HttpStatus;
61 import com.google.gson.GsonBuilder;
62
63 /**
64  * @author Waqas Ikram (waqas.ikram@est.tech)
65  *
66  */
67 public class SdcNotificationWithSol004PackageTest extends BaseTest {
68
69     private final static Logger LOGGER = LoggerFactory.getLogger(SdcNotificationWithSol004PackageTest.class);
70     private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_URL = "/api/catalog/v1/vnfpackages";
71     private static final String COMPONENT_NAME = "SO";
72     private static final String CSAR_ID = "8f0b72e1-b6d6-42b6-a808-c60b17f04d7a";
73     private static final String SDC_GET_RESOURCE_URL = "/sdc/v1/catalog/resources/" + CSAR_ID + "/toscaModel";
74     private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST = "{\"csarId\": \"" + CSAR_ID + "\"}";
75     private static final String DISTRIBUTION_ID = "35f20eb9-238a-4cc2-96dc-0a08f71bc209";
76     private static final String VGW_RESOURCE_PATH = "src/test/resources/resource-examples/vgw";
77     private static final String SERVICE_UUID = "e051ff77-fb79-451c-8457-1cbf94e4db8f";
78     private static final String SERVICE_INVARIANT_UUID = "c2ce924f-0aa1-4777-9b42-c0fec006a883";
79     private static final String JOB_ID = "57c13120-0a03-4d2e-837a-7c41d61e4a30";
80     private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL = "/api/catalog/v1/jobs/" + JOB_ID;
81     private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE = "{\"jobId\": \"" + JOB_ID + "\"}";
82
83     @Autowired
84     private ASDCController asdcController;
85
86     private DistributionClientEmulator distributionClient;
87
88     @Autowired
89     private WatchdogComponentDistributionStatusRepository watchdogComponentDistributionStatusRepository;
90
91     @Autowired
92     protected ServiceRepository serviceRepository;
93
94     @Before
95     public void setUp() {
96         distributionClient = new DistributionClientEmulator();
97         distributionClient.setResourcePath(getAbsolutePath(VGW_RESOURCE_PATH));
98         asdcController.setDistributionClient(distributionClient);
99         try {
100             asdcController.initASDC();
101         } catch (final ASDCControllerException controllerException) {
102             LOGGER.error(controllerException.getMessage(), controllerException);
103             fail(controllerException.getMessage());
104         }
105     }
106
107     @After
108     public void shutDown() {
109         try {
110             if (serviceRepository.existsById(SERVICE_UUID)) {
111                 LOGGER.debug("Deleting existing service using {} ", SERVICE_UUID);
112                 serviceRepository.deleteById(SERVICE_UUID);
113             }
114
115             final WatchdogComponentDistributionStatusId distributionId = new WatchdogComponentDistributionStatusId();
116             distributionId.setDistributionId(DISTRIBUTION_ID);
117             distributionId.setComponentName(COMPONENT_NAME);
118             if (watchdogComponentDistributionStatusRepository.existsById(distributionId)) {
119                 LOGGER.debug("Deleting existing WatchdogComponentDistributionStatus using {} ", distributionId);
120                 watchdogComponentDistributionStatusRepository.deleteById(distributionId);
121             }
122             asdcController.closeASDC();
123         } catch (final ASDCControllerException asdcControllerException) {
124             LOGGER.error(asdcControllerException.getMessage(), asdcControllerException);
125             fail(asdcControllerException.getMessage());
126         }
127     }
128
129     @Test
130     public void testTreatNotification_vgwServiceContainingSol004Package_successfullyOnboard() throws IOException {
131         initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
132
133         wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
134                 .willReturn(aResponse().withBody(getFileContent(
135                         Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
136                 .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
137
138         wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
139                 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
140
141         wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
142                 .willReturn(okJson(new String(getFileContent(Paths.get(getAbsolutePath(VGW_RESOURCE_PATH),
143                         "etsi-catalog-package-onboading-job-status-successful.json"))))));
144
145         asdcController.treatNotification(getNotificationDataImplObject());
146
147         final List<WatchdogComponentDistributionStatus> distributionList =
148                 watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
149         assertNotNull(distributionList);
150         assertEquals(1, distributionList.size());
151         final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
152         assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
153         assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
154
155         verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
156                 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
157
158         verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
159     }
160
161     @Test
162     public void testTreatNotification_vgwServiceUnableToGeSdcResource_successfullyOnboard() throws IOException {
163         initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
164
165         wireMockServer
166                 .stubFor(get(SDC_GET_RESOURCE_URL).willReturn(aResponse().withStatus(HttpStatus.NOT_FOUND.value()))
167                         .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
168
169         asdcController.treatNotification(getNotificationDataImplObject());
170
171         final List<WatchdogComponentDistributionStatus> distributionList =
172                 watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
173         assertNotNull(distributionList);
174         assertEquals(1, distributionList.size());
175         final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
176         assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
177         assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
178     }
179
180     @Test
181     public void testTreatNotification_vgwServiceContainingNonEtsiSdcResource_successfullyOnboard() throws IOException {
182
183         initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
184
185         wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
186                 .willReturn(aResponse().withBody(getFileContent(
187                         Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "service-Vgwservicev1-csar.csar"))))
188                 .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
189
190         asdcController.treatNotification(getNotificationDataImplObject());
191
192         final List<WatchdogComponentDistributionStatus> distributionList =
193                 watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
194         assertNotNull(distributionList);
195         assertEquals(1, distributionList.size());
196         final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
197         assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
198         assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
199
200     }
201
202     @Test
203     public void testTreatNotification_vgwServiceContainingSol004Package_onnboardRequestToEtsiCatalogReturnsBadGatway_distributionStatusError()
204             throws IOException {
205         initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
206
207         wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
208                 .willReturn(aResponse().withBody(getFileContent(
209                         Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
210                 .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
211
212         wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
213                 .willReturn(aResponse().withStatus(HttpStatus.BAD_GATEWAY.value())));
214
215         asdcController.treatNotification(getNotificationDataImplObject());
216
217         final List<WatchdogComponentDistributionStatus> distributionList =
218                 watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
219         assertNotNull(distributionList);
220         assertEquals(1, distributionList.size());
221         final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
222         assertEquals(COMPONENT_DONE_ERROR.toString(), distributionStatus.getComponentDistributionStatus());
223         assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
224
225         verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
226                 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
227
228     }
229
230     @Test
231     public void testTreatNotification_vgwServiceContainingSol004Package_getJobStatusReturnsBadGatway_distributionStatusError()
232             throws IOException {
233         initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
234
235         wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
236                 .willReturn(aResponse().withBody(getFileContent(
237                         Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
238                 .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
239
240         wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
241                 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
242
243         wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
244                 .willReturn(aResponse().withStatus(HttpStatus.BAD_GATEWAY.value())));
245
246         asdcController.treatNotification(getNotificationDataImplObject());
247
248         final List<WatchdogComponentDistributionStatus> distributionList =
249                 watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
250         assertNotNull(distributionList);
251         assertEquals(1, distributionList.size());
252         final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
253         assertEquals(COMPONENT_DONE_ERROR.toString(), distributionStatus.getComponentDistributionStatus());
254         assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
255
256         verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
257                 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
258
259         verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
260
261     }
262
263     @Test
264     public void testTreatNotification_vgwServiceContainingSol004Package_getJobStatusBodyWithStatusError_distributionStatusError()
265             throws IOException {
266
267         initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
268
269         wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
270                 .willReturn(aResponse().withBody(getFileContent(
271                         Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
272                 .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
273
274         wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
275                 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
276
277         wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
278                 .willReturn(okJson(new String(getFileContent(Paths.get(getAbsolutePath(VGW_RESOURCE_PATH),
279                         "etsi-catalog-package-onboading-job-status-error.json"))))));
280
281         asdcController.treatNotification(getNotificationDataImplObject());
282
283         final List<WatchdogComponentDistributionStatus> distributionList =
284                 watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
285         assertNotNull(distributionList);
286         assertEquals(1, distributionList.size());
287         final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
288         assertEquals(COMPONENT_DONE_ERROR.toString(), distributionStatus.getComponentDistributionStatus());
289         assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
290
291         verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
292                 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
293
294         verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
295
296
297     }
298
299     @Test
300     public void testTreatNotification_vgwServiceContainingSol004PackageAndPackageAlreadyExistsInEtsiCatalog_successfullyOnboard()
301             throws IOException {
302         initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
303
304         wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
305                 .willReturn(aResponse().withBody(getFileContent(
306                         Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
307                 .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
308
309         wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
310                 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
311
312         wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
313                 .willReturn(okJson(new String(getFileContent(Paths.get(getAbsolutePath(VGW_RESOURCE_PATH),
314                         "etsi-catalog-package-onboading-job-status-error-package-exists.json"))))));
315
316         asdcController.treatNotification(getNotificationDataImplObject());
317
318         final List<WatchdogComponentDistributionStatus> distributionList =
319                 watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
320         assertNotNull(distributionList);
321         assertEquals(1, distributionList.size());
322         final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
323         assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
324         assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
325
326         verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
327                 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
328
329         verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
330     }
331
332
333     private byte[] getFileContent(final Path path) throws IOException {
334         return Files.readAllBytes(path);
335     }
336
337     private NotificationDataImpl getNotificationDataImplObject() throws IOException {
338         final Path filePath = Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "dmaap-notification-message.json");
339         final byte[] bytes = Files.readAllBytes(filePath);
340
341         return new GsonBuilder().setPrettyPrinting().create().fromJson(new String(bytes), NotificationDataImpl.class);
342     }
343
344     /**
345      * Mock the AAI using wireshark.
346      */
347     private void initMockAaiServer(final String serviceUuid, final String serviceInvariantUuid) {
348         final String modelEndpoint = "/aai/v21/service-design-and-creation/models/model/" + serviceInvariantUuid
349                 + "/model-vers/model-ver/" + serviceUuid + "?depth=0";
350
351         wireMockServer.stubFor(post(urlEqualTo(modelEndpoint)).willReturn(ok()));
352     }
353
354     private String getAbsolutePath(final String path) {
355         final File file = new File(path);
356         return file.getAbsolutePath();
357     }
358 }