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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.tasks;
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;
28 import java.time.Duration;
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;
40 import reactor.core.publisher.Flux;
41 import reactor.test.StepVerifier;
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>
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";
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;
63 public static void setUp() {
65 dmaapPublisherConfiguration = new ImmutableDmaapPublisherConfiguration.Builder()
66 .dmaapContentType("application/json")
67 .dmaapHostName("54.45.33.2")
68 .dmaapPortNumber(1234)
69 .dmaapProtocol("https")
71 .dmaapUserPassword("DFC")
72 .dmaapTopicName("unauthenticated.VES_NOTIFICATION_OUTPUT")
73 .trustStorePath("trustStorePath")
74 .trustStorePasswordPath("trustStorePasswordPath")
75 .keyStorePath("keyStorePath")
76 .keyStorePasswordPath("keyStorePasswordPath")
77 .enableDmaapCertAuth(true)
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)
87 .location("ftpes://192.168.0.101:22/ftp/rop/" + PM_FILE_NAME)
88 .internalLocation("target/" + PM_FILE_NAME)
90 .fileFormatType("org.3GPP.32.435#measCollec")
91 .fileFormatVersion("V10")
93 appConfig = mock(AppConfig.class);
98 public void whenPassedObjectFits_ReturnsCorrectStatus() {
99 prepareMocksForTests(Flux.just(HttpStatus.OK));
101 StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
102 .expectNext(consumerDmaapModel).verifyComplete();
104 verify(dMaaPProducerReactiveHttpClient, times(1)).getDmaapProducerResponse(any());
105 verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
109 public void whenPassedObjectFits_firstFailsThenSucceeds() {
110 prepareMocksForTests(Flux.just(HttpStatus.BAD_GATEWAY), Flux.just(HttpStatus.OK));
112 StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
113 .expectNext(consumerDmaapModel).verifyComplete();
115 verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any());
116 verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
120 public void whenPassedObjectFits_firstFailsThenFails() {
121 prepareMocksForTests(Flux.just(HttpStatus.BAD_GATEWAY), Flux.just(HttpStatus.BAD_GATEWAY));
123 StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
124 .expectErrorMessage("Retries exhausted: 1/1").verify();
126 verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any());
127 verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
131 final void prepareMocksForTests(Flux<HttpStatus> firstResponse, Flux<HttpStatus>... nextHttpResponses) {
132 dMaaPProducerReactiveHttpClient = mock(DmaapProducerReactiveHttpClient.class);
133 when(dMaaPProducerReactiveHttpClient.getDmaapProducerResponse(any())).thenReturn(firstResponse,
135 when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
136 dmaapPublisherTask = spy(new DataRouterPublisher(appConfig));
137 when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration);
138 doReturn(dMaaPProducerReactiveHttpClient).when(dmaapPublisherTask).resolveClient();