24b82fe6f5dbd46e018bdb36e30b8b610c662d92
[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
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
95         doReturn(dmaapPublisherConfiguration).when(appConfig).getDmaapPublisherConfiguration();
96     }
97
98     @Test
99     public void whenPassedObjectFits_ReturnsCorrectStatus() {
100         prepareMocksForTests(Mono.just(HttpStatus.OK));
101
102         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
103                 .expectNext(consumerDmaapModel).verifyComplete();
104
105         verify(dMaaPProducerReactiveHttpClient, times(1)).getDmaapProducerResponse(any());
106         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
107     }
108
109     @Test
110     public void whenPassedObjectFits_firstFailsThenSucceeds() {
111         prepareMocksForTests(Mono.just(HttpStatus.BAD_GATEWAY), Mono.just(HttpStatus.OK));
112
113         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
114                 .expectNext(consumerDmaapModel).verifyComplete();
115
116         verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any());
117         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
118     }
119
120     @Test
121     public void whenPassedObjectFits_firstFailsThenFails() {
122         prepareMocksForTests(Mono.just(HttpStatus.BAD_GATEWAY), Mono.just(HttpStatus.BAD_GATEWAY));
123
124         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0)))
125                 .expectErrorMessage("Retries exhausted: 1/1").verify();
126
127         verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any());
128         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
129     }
130
131     @SafeVarargs
132     final void prepareMocksForTests(Mono<HttpStatus> firstResponse, Mono<HttpStatus>... nextHttpResponses) {
133         dMaaPProducerReactiveHttpClient = mock(DmaapProducerReactiveHttpClient.class);
134         when(dMaaPProducerReactiveHttpClient.getDmaapProducerResponse(any())).thenReturn(firstResponse,
135                 nextHttpResponses);
136
137         dmaapPublisherTask = spy(new DataRouterPublisher(appConfig));
138         when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration);
139         doReturn(dMaaPProducerReactiveHttpClient).when(dmaapPublisherTask).resolveClient();
140     }
141 }