summaryrefslogtreecommitdiffstats
path: root/components/bbs-event-processor/src/test/java/org/onap/bbs/event/processor/tasks/DmaapReRegistrationConsumerTaskImplTest.java
blob: 72e28987a24be0c0f947c4bcc49bb2fa90a2fc80 (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
/*
 * ============LICENSE_START=======================================================
 * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
 * ================================================================================
 * Copyright (C) 2019 NOKIA Intellectual Property. 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.bbs.event.processor.tasks;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

import java.util.Optional;

import javax.net.ssl.SSLException;

import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.bbs.event.processor.config.ApplicationConfiguration;
import org.onap.bbs.event.processor.exceptions.EmptyDmaapResponseException;
import org.onap.bbs.event.processor.model.ImmutableReRegistrationConsumerDmaapModel;
import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
import org.onap.bbs.event.processor.utilities.ReRegistrationDmaapConsumerJsonParser;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.ConsumerReactiveHttpClientFactory;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;

import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

class DmaapReRegistrationConsumerTaskImplTest {

    private static final String RE_REGISTRATION_EVENT_TEMPLATE = "{\"event\": {"
            + "\"commonEventHeader\": { \"sourceName\":\"%s\"},"
            + "\"additionalFields\": {"
            + " \"attachment-point\": \"%s\","
            + " \"remote-id\": \"%s\","
            + " \"cvlan\": \"%s\","
            + " \"svlan\": \"%s\""
            + "}}}";

    private static DmaapReRegistrationConsumerTaskImpl dmaapConsumerTask;
    private static ReRegistrationConsumerDmaapModel reRegistrationConsumerDmaapModel;
    private static DMaaPConsumerReactiveHttpClient dMaaPConsumerReactiveHttpClient;
    private static String eventsArray;
    private static Gson gson = new Gson();

    @BeforeAll
    static void setUp() throws SSLException {

        final String sourceName = "PNF-CorrelationId";
        final String attachmentPoint = "olt2/2/2";
        final String remoteId = "remoteId";
        final String cvlan = "1005";
        final String svlan = "100";

        // Mock Re-registration configuration
        DmaapConsumerConfiguration dmaapConsumerConfiguration = testVersionOfDmaapConsumerConfiguration();
        ApplicationConfiguration configuration = mock(ApplicationConfiguration.class);
        when(configuration.getDmaapReRegistrationConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);

        // Mock reactive DMaaP client
        ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class);
        dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
        doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration);

        dmaapConsumerTask = new DmaapReRegistrationConsumerTaskImpl(configuration,
                new ReRegistrationDmaapConsumerJsonParser(), httpClientFactory);

        reRegistrationConsumerDmaapModel = ImmutableReRegistrationConsumerDmaapModel.builder()
                .correlationId(sourceName)
                .attachmentPoint(attachmentPoint)
                .remoteId(remoteId)
                .cVlan(cvlan)
                .sVlan(svlan)
                .build();

        String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE, sourceName, attachmentPoint, remoteId,
                cvlan, svlan);

        eventsArray = "[" + event + "]";
    }

    @AfterEach
    void resetMock() {
        reset(dMaaPConsumerReactiveHttpClient);
    }

    @Test
    void passingEmptyMessage_NothingHappens() {
        JsonElement empty = gson.toJsonTree("");
        when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty())).thenReturn(Mono.just(empty));

        StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
                .expectSubscription()
                .expectError(EmptyDmaapResponseException.class);
        verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
    }

    @Test
    void passingNormalMessage_ResponseSucceeds() {
        JsonElement normalEventsArray = gson.toJsonTree(eventsArray);
        when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty()))
                .thenReturn(Mono.just(normalEventsArray));

        StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
                .expectSubscription()
                .consumeNextWith(e -> Assert.assertEquals(e, reRegistrationConsumerDmaapModel));
        verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
    }

    private static DmaapConsumerConfiguration testVersionOfDmaapConsumerConfiguration() {
        return new ImmutableDmaapConsumerConfiguration.Builder()
                .consumerGroup("OpenDCAE-c12")
                .consumerId("c12")
                .dmaapContentType("application/json")
                .dmaapHostName("message-router.onap.svc.cluster.local")
                .dmaapPortNumber(3904)
                .dmaapProtocol("http")
                .dmaapUserName("admin")
                .dmaapUserPassword("admin")
                .trustStorePath("/opt/app/bbs/local/org.onap.bbs.trust.jks")
                .trustStorePasswordPath("change_it")
                .keyStorePath("/opt/app/bbs/local/org.onap.bbs.p12")
                .keyStorePasswordPath("change_it")
                .enableDmaapCertAuth(false)
                .dmaapTopicName("/events/unauthenticated.PNF_REREGISTRATION")
                .timeoutMs(-1)
                .messageLimit(-1)
                .build();
    }
}