d2240f182ef6afb56e178edb750ae99cc53f57b4
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.tasks;
18
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import static org.mockito.Mockito.when;
27
28 import java.time.Duration;
29
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
33 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
34 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
35 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerReactiveHttpClient;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapPublisherConfiguration;
38 import org.springframework.http.HttpStatus;
39
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
42
43 /**
44  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18
45  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
46  */
47 class DataRouterPublisherTest {
48     private static final String PRODUCT_NAME = "NrRadio";
49     private static final String VENDOR_NAME = "Ericsson";
50     private static final String LAST_EPOCH_MICROSEC = "8745745764578";
51     private static final String SOURCE_NAME = "oteNB5309";
52     private static final String START_EPOCH_MICROSEC = "8745745764578";
53     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
54     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
55
56     private static ConsumerDmaapModel consumerDmaapModel;
57     private static DataRouterPublisher dmaapPublisherTask;
58     private static DmaapProducerReactiveHttpClient dMaaPProducerReactiveHttpClient;
59     private static AppConfig appConfig;
60     private static DmaapPublisherConfiguration dmaapPublisherConfiguration;
61
62     @BeforeAll
63     public static void setUp() {
64         //@formatter:off
65         dmaapPublisherConfiguration = new ImmutableDmaapPublisherConfiguration.Builder()
66                 .dmaapContentType("application/json")
67                 .dmaapHostName("54.45.33.2")
68                 .dmaapPortNumber(1234)
69                 .dmaapProtocol("https")
70                 .dmaapUserName("DFC")
71                 .dmaapUserPassword("DFC")
72                 .dmaapTopicName("unauthenticated.VES_NOTIFICATION_OUTPUT")
73                 .trustStorePath("trustStorePath")
74                 .trustStorePasswordPath("trustStorePasswordPath")
75                 .keyStorePath("keyStorePath")
76                 .keyStorePasswordPath("keyStorePasswordPath")
77                 .enableDmaapCertAuth(true)
78                 .build();
79         consumerDmaapModel = ImmutableConsumerDmaapModel.builder()
80                 .productName(PRODUCT_NAME)
81                 .vendorName(VENDOR_NAME)
82                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
83                 .sourceName(SOURCE_NAME)
84                 .startEpochMicrosec(START_EPOCH_MICROSEC)
85                 .timeZoneOffset(TIME_ZONE_OFFSET)
86                 .name(PM_FILE_NAME)
87                 .location("ftpes://192.168.0.101:22/ftp/rop/" + PM_FILE_NAME)
88                 .internalLocation("target/" + PM_FILE_NAME)
89                 .compression("gzip")
90                 .fileFormatType("org.3GPP.32.435#measCollec")
91                 .fileFormatVersion("V10")
92                 .build();
93         appConfig = mock(AppConfig.class);
94         //@formatter:on
95     }
96
97     @Test
98     public void whenPassedObjectFits_ReturnsCorrectStatus() {
99         prepareMocksForTests(Mono.just(HttpStatus.OK));
100
101         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
102                 .expectNext(consumerDmaapModel).verifyComplete();
103
104         verify(dMaaPProducerReactiveHttpClient, times(1)).getDmaapProducerResponse(any());
105         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
106     }
107
108     @Test
109     public void whenPassedObjectFits_firstFailsThenSucceeds() {
110         prepareMocksForTests(Mono.just(HttpStatus.BAD_GATEWAY), Mono.just(HttpStatus.OK));
111
112         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
113                 .expectNext(consumerDmaapModel).verifyComplete();
114
115         verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any());
116         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
117     }
118
119     @Test
120     public void whenPassedObjectFits_firstFailsThenFails() {
121         prepareMocksForTests(Mono.just(HttpStatus.BAD_GATEWAY), Mono.just(HttpStatus.BAD_GATEWAY));
122
123         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
124                 .expectErrorMessage("Retries exhausted: 1/1").verify();
125
126         verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any());
127         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
128     }
129
130     @SafeVarargs
131     final void prepareMocksForTests(Mono<HttpStatus> firstResponse, Mono<HttpStatus>... nextHttpResponses) {
132         dMaaPProducerReactiveHttpClient = mock(DmaapProducerReactiveHttpClient.class);
133         when(dMaaPProducerReactiveHttpClient.getDmaapProducerResponse(any())).thenReturn(firstResponse,
134                 nextHttpResponses);
135         when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
136         dmaapPublisherTask = spy(new DataRouterPublisher(appConfig));
137         when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration);
138         doReturn(dMaaPProducerReactiveHttpClient).when(dmaapPublisherTask).resolveClient();
139     }
140 }