19ab1ab965d058a38d1ad976da70441bc2a31212
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.tasks;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonParser;
33 import java.util.Optional;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.Mockito;
37 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
38 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
39 import org.onap.dcaegen2.collectors.datafile.exceptions.DmaapEmptyResponseException;
40 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
41 import org.onap.dcaegen2.collectors.datafile.service.DmaapConsumerJsonParser;
42 import org.onap.dcaegen2.collectors.datafile.service.consumer.DMaaPConsumerReactiveHttpClient;
43 import org.onap.dcaegen2.collectors.datafile.tasks.DmaapConsumerTaskImpl;
44 import org.onap.dcaegen2.collectors.datafile.config.ImmutableDmaapConsumerConfiguration;
45 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
46
47 import reactor.core.publisher.Mono;
48 import reactor.test.StepVerifier;
49
50 /**
51  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18
52  */
53 class DmaapConsumerTaskImplTest {
54
55     private static ConsumerDmaapModel consumerDmaapModel;
56     private static DmaapConsumerTaskImpl dmaapConsumerTask;
57     private static DMaaPConsumerReactiveHttpClient dMaaPConsumerReactiveHttpClient;
58     private static AppConfig appConfig;
59     private static DmaapConsumerConfiguration dmaapConsumerConfiguration;
60     private static String message;
61     private static String parsed;
62
63     @BeforeAll
64     static void setUp() {
65         dmaapConsumerConfiguration = new ImmutableDmaapConsumerConfiguration.Builder().consumerGroup("OpenDCAE-c12")
66             .consumerId("c12").dmaapContentType("application/json").dmaapHostName("54.45.33.2").dmaapPortNumber(1234)
67             .dmaapProtocol("https").dmaapUserName("Datafile").dmaapUserPassword("Datafile")
68             .dmaapTopicName("unauthenticated.SEC_OTHER_OUTPUT").timeoutMs(-1).messageLimit(-1).build();
69
70         consumerDmaapModel = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
71             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
72             .pnfName("NOKQTFCOC540002E").build();
73         appConfig = mock(AppConfig.class);
74         message =
75             "[{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\","
76                 + "\"eventName\":\"pnfRegistration_5GDU\",\"eventType\":\"pnfRegistration\",\"internalHeaderFields\""
77                 + ":{},\"lastEpochMicrosec\":1519837825682,\"nfNamingCode\":\"5GRAN\",\"nfcNamingCode\":\"5DU\","
78                 + "\"priority\":\"Normal\",\"reportingEntityName\":\"5GRAN_DU\",\"sequence\":0,\"sourceId\":"
79                 + "\"<<SerialNumber>>\",\"sourceName\":\"5GRAN_DU\",\"startEpochMicrosec\":1519837825682,\"version\""
80                 + ":3},\"otherFields\":{\"otherFieldsVersion\":1,\"pnfFamily\":\"BBU\",\"pnfLastServiceDate\":"
81                 + "1517206400,\"pnfManufactureDate\":1516406400,\"pnfModelNumber\":\"AJ02\",\"pnfOamIpv4Address\":"
82                 + "\"10.16.123.234\",\"pnfOamIpv6Address\":\"0:0:0:0:0:FFFF:0A10:7BEA\",\"pnfSerialNumber\":"
83                 + "\"QTFCOC540002E\",\"pnfSoftwareVersion\":\"v4.5.0.1\",\"pnfType\":\"AirScale\",\"pnfVendorName\":"
84                 + "\"Nokia\"}}}]";
85         parsed =
86             "{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\""
87                 + ":\"pnfRegistration_5GDU\",\"eventType\":\"pnfRegistration\",\"internalHeaderFields\":{},"
88                 + "\"lastEpochMicrosec\":1519837825682,\"nfNamingCode\":\"5GRAN\",\"nfcNamingCode\":\"5DU\","
89                 + "\"priority\":\"Normal\",\"reportingEntityName\":\"5GRAN_DU\",\"sequence\":0,\"sourceId\":"
90                 + "\"<<SerialNumber>>\",\"sourceName\":\"5GRAN_DU\",\"startEpochMicrosec\":1519837825682,"
91                 + "\"version\":3},\"otherFields\":{\"otherFieldsVersion\":1,\"pnfFamily\":\"BBU\","
92                 + "\"pnfLastServiceDate\":1517206400,\"pnfManufactureDate\":1516406400,\"pnfModelNumber\":\"AJ02\","
93                 + "\"pnfOamIpv4Address\":\"10.16.123.234\",\"pnfOamIpv6Address\":\"0:0:0:0:0:FFFF:0A10:7BEA\","
94                 + "\"pnfSerialNumber\":\"QTFCOC540002E\",\"pnfSoftwareVersion\":\"v4.5.0.1\",\"pnfType\":\"AirScale\","
95                 + "\"pnfVendorName\":\"Nokia\"}}}";
96     }
97
98     @Test
99     void whenPassedObjectDoesntFit_DoesNotThrowDatafileTaskException() {
100         //given
101         prepareMocksForDmaapConsumer(Optional.empty());
102
103         //then
104         StepVerifier.create(dmaapConsumerTask.execute("Sample input")).expectSubscription()
105             .expectError(DmaapEmptyResponseException.class).verify();
106
107         verify(dMaaPConsumerReactiveHttpClient, times(1)).getDMaaPConsumerResponse();
108     }
109
110     @Test
111     void whenPassedObjectFits_ReturnsCorrectResponse() {
112         //given
113         prepareMocksForDmaapConsumer(Optional.of(message));
114         //when
115         Mono<ConsumerDmaapModel> response = dmaapConsumerTask.execute("Sample input");
116
117         //then
118         verify(dMaaPConsumerReactiveHttpClient, times(1)).getDMaaPConsumerResponse();
119         assertEquals(consumerDmaapModel, response.block());
120
121
122     }
123
124     private void prepareMocksForDmaapConsumer(Optional<String> message) {
125         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
126         JsonElement jsonElement = new JsonParser().parse(parsed);
127         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
128             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
129         dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
130         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()).thenReturn(Mono.just(message.orElse("")));
131         when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);
132         dmaapConsumerTask = spy(new DmaapConsumerTaskImpl(appConfig, dmaapConsumerJsonParser));
133         when(dmaapConsumerTask.resolveConfiguration()).thenReturn(dmaapConsumerConfiguration);
134         doReturn(dMaaPConsumerReactiveHttpClient).when(dmaapConsumerTask).resolveClient();
135     }
136 }