aboutsummaryrefslogtreecommitdiffstats
path: root/asdc-controller/src/test/java/org/onap/so/asdc/client/SdcNotificationWithSol004PackageTest.java
blob: 2e8da35f3eab0032ac4f67cca4499519e5297f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.asdc.client;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_ERROR;
import static org.onap.sdc.utils.DistributionStatusEnum.COMPONENT_DONE_OK;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.so.asdc.BaseTest;
import org.onap.so.asdc.client.exceptions.ASDCControllerException;
import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
import org.onap.so.db.catalog.data.repository.ServiceRepository;
import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
import org.onap.so.db.request.beans.WatchdogComponentDistributionStatusId;
import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import com.google.gson.GsonBuilder;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
public class SdcNotificationWithSol004PackageTest extends BaseTest {

    private final static Logger LOGGER = LoggerFactory.getLogger(SdcNotificationWithSol004PackageTest.class);
    private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_URL = "/api/catalog/v1/vnfpackages";
    private static final String COMPONENT_NAME = "SO";
    private static final String CSAR_ID = "8f0b72e1-b6d6-42b6-a808-c60b17f04d7a";
    private static final String SDC_GET_RESOURCE_URL = "/sdc/v1/catalog/resources/" + CSAR_ID + "/toscaModel";
    private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST = "{\"csarId\": \"" + CSAR_ID + "\"}";
    private static final String DISTRIBUTION_ID = "35f20eb9-238a-4cc2-96dc-0a08f71bc209";
    private static final String VGW_RESOURCE_PATH = "src/test/resources/resource-examples/vgw";
    private static final String SERVICE_UUID = "e051ff77-fb79-451c-8457-1cbf94e4db8f";
    private static final String SERVICE_INVARIANT_UUID = "c2ce924f-0aa1-4777-9b42-c0fec006a883";
    private static final String JOB_ID = "57c13120-0a03-4d2e-837a-7c41d61e4a30";
    private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL = "/api/catalog/v1/jobs/" + JOB_ID;
    private static final String ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE = "{\"jobId\": \"" + JOB_ID + "\"}";

    @Autowired
    private ASDCController asdcController;

    private DistributionClientEmulator distributionClient;

    @Autowired
    private WatchdogComponentDistributionStatusRepository watchdogComponentDistributionStatusRepository;

    @Autowired
    protected ServiceRepository serviceRepository;

    @Before
    public void setUp() {
        distributionClient = new DistributionClientEmulator();
        distributionClient.setResourcePath(getAbsolutePath(VGW_RESOURCE_PATH));
        asdcController.setDistributionClient(distributionClient);
        try {
            asdcController.initASDC();
        } catch (final ASDCControllerException controllerException) {
            LOGGER.error(controllerException.getMessage(), controllerException);
            fail(controllerException.getMessage());
        }
    }

    @After
    public void shutDown() {
        try {
            if (serviceRepository.existsById(SERVICE_UUID)) {
                LOGGER.debug("Deleting existing service using {} ", SERVICE_UUID);
                serviceRepository.deleteById(SERVICE_UUID);
            }

            final WatchdogComponentDistributionStatusId distributionId = new WatchdogComponentDistributionStatusId();
            distributionId.setDistributionId(DISTRIBUTION_ID);
            distributionId.setComponentName(COMPONENT_NAME);
            if (watchdogComponentDistributionStatusRepository.existsById(distributionId)) {
                LOGGER.debug("Deleting existing WatchdogComponentDistributionStatus using {} ", distributionId);
                watchdogComponentDistributionStatusRepository.deleteById(distributionId);
            }
            asdcController.closeASDC();
        } catch (final ASDCControllerException asdcControllerException) {
            LOGGER.error(asdcControllerException.getMessage(), asdcControllerException);
            fail(asdcControllerException.getMessage());
        }
    }

    @Test
    public void testTreatNotification_vgwServiceContainingSol004Package_successfullyOnboard() throws IOException {
        initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);

        wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
                .willReturn(aResponse().withBody(getFileContent(
                        Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
                .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));

        wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));

        wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
                .willReturn(okJson(new String(getFileContent(Paths.get(getAbsolutePath(VGW_RESOURCE_PATH),
                        "etsi-catalog-package-onboading-job-status-successful.json"))))));

        asdcController.treatNotification(getNotificationDataImplObject());

        final List<WatchdogComponentDistributionStatus> distributionList =
                watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
        assertNotNull(distributionList);
        assertEquals(1, distributionList.size());
        final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
        assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
        assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());

        verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));

        verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
    }

    @Test
    public void testTreatNotification_vgwServiceUnableToGeSdcResource_successfullyOnboard() throws IOException {
        initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);

        wireMockServer
                .stubFor(get(SDC_GET_RESOURCE_URL).willReturn(aResponse().withStatus(HttpStatus.NOT_FOUND.value()))
                        .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));

        asdcController.treatNotification(getNotificationDataImplObject());

        final List<WatchdogComponentDistributionStatus> distributionList =
                watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
        assertNotNull(distributionList);
        assertEquals(1, distributionList.size());
        final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
        assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
        assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());
    }

    @Test
    public void testTreatNotification_vgwServiceContainingNonEtsiSdcResource_successfullyOnboard() throws IOException {

        initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);

        wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
                .willReturn(aResponse().withBody(getFileContent(
                        Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "service-Vgwservicev1-csar.csar"))))
                .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));

        asdcController.treatNotification(getNotificationDataImplObject());

        final List<WatchdogComponentDistributionStatus> distributionList =
                watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
        assertNotNull(distributionList);
        assertEquals(1, distributionList.size());
        final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
        assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
        assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());

    }

    @Test
    public void testTreatNotification_vgwServiceContainingSol004Package_onnboardRequestToEtsiCatalogReturnsBadGatway_distributionStatusError()
            throws IOException {
        initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);

        wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
                .willReturn(aResponse().withBody(getFileContent(
                        Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
                .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));

        wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .willReturn(aResponse().withStatus(HttpStatus.BAD_GATEWAY.value())));

        asdcController.treatNotification(getNotificationDataImplObject());

        final List<WatchdogComponentDistributionStatus> distributionList =
                watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
        assertNotNull(distributionList);
        assertEquals(1, distributionList.size());
        final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
        assertEquals(COMPONENT_DONE_ERROR.toString(), distributionStatus.getComponentDistributionStatus());
        assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());

        verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));

    }

    @Test
    public void testTreatNotification_vgwServiceContainingSol004Package_getJobStatusReturnsBadGatway_distributionStatusError()
            throws IOException {
        initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);

        wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
                .willReturn(aResponse().withBody(getFileContent(
                        Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
                .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));

        wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));

        wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
                .willReturn(aResponse().withStatus(HttpStatus.BAD_GATEWAY.value())));

        asdcController.treatNotification(getNotificationDataImplObject());

        final List<WatchdogComponentDistributionStatus> distributionList =
                watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
        assertNotNull(distributionList);
        assertEquals(1, distributionList.size());
        final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
        assertEquals(COMPONENT_DONE_ERROR.toString(), distributionStatus.getComponentDistributionStatus());
        assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());

        verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));

        verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));

    }

    @Test
    public void testTreatNotification_vgwServiceContainingSol004Package_getJobStatusBodyWithStatusError_distributionStatusError()
            throws IOException {

        initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);

        wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
                .willReturn(aResponse().withBody(getFileContent(
                        Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
                .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));

        wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));

        wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
                .willReturn(okJson(new String(getFileContent(Paths.get(getAbsolutePath(VGW_RESOURCE_PATH),
                        "etsi-catalog-package-onboading-job-status-error.json"))))));

        asdcController.treatNotification(getNotificationDataImplObject());

        final List<WatchdogComponentDistributionStatus> distributionList =
                watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
        assertNotNull(distributionList);
        assertEquals(1, distributionList.size());
        final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
        assertEquals(COMPONENT_DONE_ERROR.toString(), distributionStatus.getComponentDistributionStatus());
        assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());

        verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));

        verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));


    }

    @Test
    public void testTreatNotification_vgwServiceContainingSol004PackageAndPackageAlreadyExistsInEtsiCatalog_successfullyOnboard()
            throws IOException {
        initMockAaiServer(SERVICE_UUID, SERVICE_INVARIANT_UUID);

        wireMockServer.stubFor(get(SDC_GET_RESOURCE_URL)
                .willReturn(aResponse().withBody(getFileContent(
                        Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "SDC_RESOURCE_CSAR", "vgw_sdc_resource.csar"))))
                .withHeader(ACCEPT, equalTo(APPLICATION_OCTET_STREAM_VALUE)));

        wireMockServer.stubFor(post(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .willReturn(okJson(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_RESPONSE)));

        wireMockServer.stubFor(get(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL))
                .willReturn(okJson(new String(getFileContent(Paths.get(getAbsolutePath(VGW_RESOURCE_PATH),
                        "etsi-catalog-package-onboading-job-status-error-package-exists.json"))))));

        asdcController.treatNotification(getNotificationDataImplObject());

        final List<WatchdogComponentDistributionStatus> distributionList =
                watchdogComponentDistributionStatusRepository.findByDistributionId(DISTRIBUTION_ID);
        assertNotNull(distributionList);
        assertEquals(1, distributionList.size());
        final WatchdogComponentDistributionStatus distributionStatus = distributionList.get(0);
        assertEquals(COMPONENT_DONE_OK.toString(), distributionStatus.getComponentDistributionStatus());
        assertEquals(COMPONENT_NAME, distributionStatus.getComponentName());

        verify(postRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_URL))
                .withRequestBody(equalToJson(ETSI_CATALOG_PACKAGE_ONBOARDING_REQUEST)));

        verify(getRequestedFor(urlEqualTo(ETSI_CATALOG_PACKAGE_ONBOARDING_JOB_STATUS_URL)));
    }


    private byte[] getFileContent(final Path path) throws IOException {
        return Files.readAllBytes(path);
    }

    private NotificationDataImpl getNotificationDataImplObject() throws IOException {
        final Path filePath = Paths.get(getAbsolutePath(VGW_RESOURCE_PATH), "dmaap-notification-message.json");
        final byte[] bytes = Files.readAllBytes(filePath);

        return new GsonBuilder().setPrettyPrinting().create().fromJson(new String(bytes), NotificationDataImpl.class);
    }

    /**
     * Mock the AAI using wireshark.
     */
    private void initMockAaiServer(final String serviceUuid, final String serviceInvariantUuid) {
        final String modelEndpoint = "/aai/v21/service-design-and-creation/models/model/" + serviceInvariantUuid
                + "/model-vers/model-ver/" + serviceUuid + "?depth=0";

        wireMockServer.stubFor(post(urlEqualTo(modelEndpoint)).willReturn(ok()));
    }

    private String getAbsolutePath(final String path) {
        final File file = new File(path);
        return file.getAbsolutePath();
    }
}