aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/ves/openapi/manager/service/ArtifactsCollectorTest.java
blob: 3955f9a04124a0ec5047c494664b405a1a350462 (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
/*
 * ============LICENSE_START=======================================================
 * VES-OPENAPI-MANAGER
 * ================================================================================
 * Copyright (C) 2021 Nokia. 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.ves.openapi.manager.service;

import org.junit.jupiter.api.Test;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.http.SdcConnectorClient;
import org.onap.sdc.impl.DistributionClientDownloadResultImpl;
import org.onap.sdc.utils.DistributionActionResultEnum;
import org.onap.ves.openapi.manager.config.DistributionClientConfig;
import org.onap.ves.openapi.manager.exceptions.ArtifactException;
import org.onap.ves.openapi.manager.model.Artifact;
import org.onap.ves.openapi.manager.service.ArtifactsCollector;
import org.onap.ves.openapi.manager.service.testModel.ArtifactInfo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ArtifactsCollectorTest {

    private static final String NOT_VES_ARTIFACT = "NOT_VES_ARTIFACT";
    private final SdcConnectorClient sdcConnectorClient = mock(SdcConnectorClient.class);
    private final ArtifactsCollector artifactsCollector = new ArtifactsCollector(sdcConnectorClient);

    @Test
    void shouldReturnArtifactsContentWhenVESArtifactsAreDistributed() throws IOException {
        //given
        IArtifactInfo vesArtifact = new ArtifactInfo(DistributionClientConfig.VES_EVENTS_ARTIFACT_TYPE);
        List<IArtifactInfo> artifacts = List.of(vesArtifact);

        byte[] payload = getVesArtifactPayload();
        DistributionClientDownloadResultImpl artifactDownloadResult = new DistributionClientDownloadResultImpl(
                DistributionActionResultEnum.SUCCESS, "sample-message", "artifact-name",
                payload);
        when(sdcConnectorClient.downloadArtifact(vesArtifact)).thenReturn(artifactDownloadResult);
        List<Artifact> expectedArtifacts = List.of(new Artifact(vesArtifact, payload));

        //when
        List<Artifact> artifactContents = artifactsCollector.pullArtifacts(artifacts);

        //then
        assertThat(artifactContents).isEqualTo(expectedArtifacts);
    }

    @Test
    void shouldNotReturnArtifactsContentWhenNoVESArtifactsAreDistributed() {
        //given
        IArtifactInfo notVesArtifact = new ArtifactInfo(NOT_VES_ARTIFACT);
        List<IArtifactInfo> artifactsDefinitions = List.of(notVesArtifact);
        List<Artifact> expectedArtifacts = List.of();

        //when
        List<Artifact> artifacts = artifactsCollector.pullArtifacts(artifactsDefinitions);

        //then
        assertThat(artifacts).isEqualTo(expectedArtifacts);
    }

    @Test
    void shouldThrowExceptionWhenArtifactNameIsEmpty() {
        //given
        IArtifactInfo vesArtifact = new ArtifactInfo(DistributionClientConfig.VES_EVENTS_ARTIFACT_TYPE);
        List<IArtifactInfo> artifacts = List.of(vesArtifact);

        byte[] payload = new byte[0];
        DistributionClientDownloadResultImpl artifactDownloadResult = new DistributionClientDownloadResultImpl(
            DistributionActionResultEnum.SUCCESS, "sample-message", "",
            payload);
        when(sdcConnectorClient.downloadArtifact(vesArtifact)).thenReturn(artifactDownloadResult);

        //when then
        assertThrows(
            ArtifactException.class,
            () -> artifactsCollector.pullArtifacts(artifacts)
        );
    }

    private byte[] getVesArtifactPayload() throws IOException {
        return Files.readAllBytes(Paths.get("src/test/resources/ves_artifact_stndDefined_events.yaml"));
    }
}