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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.asdc.client;
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;
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;
64 * @author Waqas Ikram (waqas.ikram@est.tech)
67 public class SdcNotificationWithSol004PackageTest extends BaseTest {
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 + "\"}";
84 private ASDCController asdcController;
86 private DistributionClientEmulator distributionClient;
89 private WatchdogComponentDistributionStatusRepository watchdogComponentDistributionStatusRepository;
92 protected ServiceRepository serviceRepository;
96 distributionClient = new DistributionClientEmulator();
97 distributionClient.setResourcePath(getAbsolutePath(VGW_RESOURCE_PATH));
98 asdcController.setDistributionClient(distributionClient);
100 asdcController.initASDC();
101 } catch (final ASDCControllerException controllerException) {
102 LOGGER.error(controllerException.getMessage(), controllerException);
103 fail(controllerException.getMessage());
108 public void shutDown() {
110 if (serviceRepository.existsById(SERVICE_UUID)) {
111 LOGGER.debug("Deleting existing service using {} ", SERVICE_UUID);
112 serviceRepository.deleteById(SERVICE_UUID);
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);
122 asdcController.closeASDC();
123 } catch (final ASDCControllerException asdcControllerException) {
124 LOGGER.error(asdcControllerException.getMessage(), asdcControllerException);
125 fail(asdcControllerException.getMessage());
130 public void testTreatNotification_vgwServiceContainingSol004Package_successfullyOnboard() throws IOException {
131 initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
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)));
138 wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
139 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
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"))))));
145 asdcController.treatNotification(getNotificationDataImplObject());
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());
155 verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
156 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
158 verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
162 public void testTreatNotification_vgwServiceUnableToGeSdcResource_successfullyOnboard() throws IOException {
163 initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
166 .stubFor(get(SDC_GET_RESOURCE_URL).willReturn(aResponse().withStatus(HttpStatus.NOT_FOUND.value()))
167 .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));
169 asdcController.treatNotification(getNotificationDataImplObject());
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());
181 public void testTreatNotification_vgwServiceContainingNonEtsiSdcResource_successfullyOnboard() throws IOException {
183 initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
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)));
190 asdcController.treatNotification(getNotificationDataImplObject());
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());
203 public void testTreatNotification_vgwServiceContainingSol004Package_onnboardRequestToEtsiCatalogReturnsBadGatway_distributionStatusError()
205 initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
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)));
212 wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
213 .willReturn(aResponse().withStatus(HttpStatus.BAD_GATEWAY.value())));
215 asdcController.treatNotification(getNotificationDataImplObject());
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());
225 verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
226 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
231 public void testTreatNotification_vgwServiceContainingSol004Package_getJobStatusReturnsBadGatway_distributionStatusError()
233 initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
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)));
240 wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
241 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
243 wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
244 .willReturn(aResponse().withStatus(HttpStatus.BAD_GATEWAY.value())));
246 asdcController.treatNotification(getNotificationDataImplObject());
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());
256 verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
257 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
259 verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
264 public void testTreatNotification_vgwServiceContainingSol004Package_getJobStatusBodyWithStatusError_distributionStatusError()
267 initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
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)));
274 wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
275 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
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"))))));
281 asdcController.treatNotification(getNotificationDataImplObject());
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());
291 verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
292 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
294 verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
300 public void testTreatNotification_vgwServiceContainingSol004PackageAndPackageAlreadyExistsInEtsiCatalog_successfullyOnboard()
302 initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);
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)));
309 wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
310 .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));
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"))))));
316 asdcController.treatNotification(getNotificationDataImplObject());
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());
326 verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
327 .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));
329 verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
333 private byte[] getFileContent(final Path path) throws IOException {
334 return Files.readAllBytes(path);
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);
341 return new GsonBuilder().setPrettyPrinting().create().fromJson(new String(bytes), NotificationDataImpl.class);
345 * Mock the AAI using wireshark.
347 private void initMockAaiServer(final String serviceUuid, final String serviceInvariantUuid) {
348 final String modelEndpoint = "/aai/v19/service-design-and-creation/models/model/" + serviceInvariantUuid
349 + "/model-vers/model-ver/" + serviceUuid + "?depth=0";
351 wireMockServer.stubFor(post(urlEqualTo(modelEndpoint)).willReturn(ok()));
354 private String getAbsolutePath(final String path) {
355 final File file = new File(path);
356 return file.getAbsolutePath();