aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/ves/openapi/manager/service/notification/ArtifactsCollectorStatusSenderTest.java
blob: 377cc4b63bcf22b137cb758bc7050c6730293e0b (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
/*
 * ============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.notification;

import org.junit.jupiter.api.Test;
import org.onap.sdc.api.consumer.IConfiguration;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.api.results.IDistributionClientResult;
import org.onap.sdc.impl.DistributionClientImpl;
import org.onap.sdc.impl.DistributionClientResultImpl;
import org.onap.sdc.utils.DistributionActionResultEnum;
import org.onap.sdc.utils.DistributionStatusEnum;
import org.onap.ves.openapi.manager.config.DistributionClientConfig;
import org.onap.ves.openapi.manager.model.DistributionStatusMessage;
import org.onap.ves.openapi.manager.service.testModel.ArtifactInfo;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ArtifactsCollectorStatusSenderTest {

    private final IConfiguration configuration = mock(IConfiguration.class);
    private final DistributionClientImpl distributionClient = mock(DistributionClientImpl.class);
    private final ArtifactsCollectorStatusSender sender = new ArtifactsCollectorStatusSender(distributionClient);

    @Test
    void shouldSuccessfullySendStatusToEachDownloadedArtifacts() {
        //given
        DistributionClientResultImpl expectedResponse = new DistributionClientResultImpl(
                DistributionActionResultEnum.SUCCESS, "sample-response-message");
        List<DistributionClientResultImpl> expectedResponses = List.of(expectedResponse, expectedResponse);

        ArtifactInfo artifact = new ArtifactInfo(DistributionClientConfig.VES_EVENTS_ARTIFACT_TYPE);
        List<IArtifactInfo> artifacts = List.of(artifact, artifact);
        String consumerId = "ves-consumer";

        when(distributionClient.sendDownloadStatus(any())).thenReturn(expectedResponse);
        when(distributionClient.getConfiguration()).thenReturn(configuration);
        when(configuration.getConsumerID()).thenReturn(consumerId);

        //when
        List<IDistributionClientResult> responses = sender.sendDownloadOk("distribution-id", artifacts);

        //then
        assertThat(responses).isEqualTo(expectedResponses);
    }

    @Test
    void shouldSendDownloadErrorStatusToEachDownloadedArtifacts() {
        //given
        DistributionClientResultImpl expectedResponse = new DistributionClientResultImpl(
                DistributionActionResultEnum.GENERAL_ERROR, "sample-response-message");
        List<DistributionClientResultImpl> expectedResponses = List.of(expectedResponse, expectedResponse);

        ArtifactInfo artifact = new ArtifactInfo(DistributionClientConfig.VES_EVENTS_ARTIFACT_TYPE);
        List<IArtifactInfo> artifacts = List.of(artifact, artifact);
        String consumerId = "ves-consumer";
        when(distributionClient.sendDownloadStatus(any(),any())).thenReturn(expectedResponse);
        when(distributionClient.getConfiguration()).thenReturn(configuration);
        when(configuration.getConsumerID()).thenReturn(consumerId);
        long timestamp = LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli();

        DistributionStatusMessage dm = new DistributionStatusMessage(
                artifact.getArtifactURL(),
                "distribution-id",
                distributionClient.getConfiguration().getConsumerID(),
                timestamp,
                DistributionStatusEnum.DOWNLOAD_OK);

        //when
        List<IDistributionClientResult> responses = sender.sendDownloadError("distribution-id", artifacts);

        //then
        assertThat(responses).isEqualTo(expectedResponses);
        assertThat(dm.getArtifactURL()).isEqualTo(artifact.getArtifactURL());
        assertThat(dm.getDistributionID()).isEqualTo("distribution-id");
        assertThat(dm.getTimestamp()).isEqualTo(timestamp);
        assertThat(dm.getStatus()).isEqualTo(DistributionStatusEnum.DOWNLOAD_OK);
    }

    @Test
    void shouldNotSendAnyStatusWhenNoArtifactsAreDownloaded() {
        //given
        List<DistributionClientResultImpl> expectedResponses = List.of();
        List<IArtifactInfo> artifacts = List.of();

        //when
        List<IDistributionClientResult> responses = sender.sendDownloadOk("distribution-id", artifacts);

        //then
        assertThat(responses).isEqualTo(expectedResponses);
    }

}