d612d17c82a74944a7616a37be94b885aabcd8ae
[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.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.verifyNoMoreInteractions;
27 import static org.mockito.Mockito.when;
28 import java.time.Duration;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
34 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
35 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
36 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerReactiveHttpClient;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapPublisherConfiguration;
39 import org.springframework.http.HttpStatus;
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         Map<String, String> contextMap = new HashMap<>();
103         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0), contextMap))
104                 .expectNext(consumerDmaapModel).verifyComplete();
105
106         verify(dMaaPProducerReactiveHttpClient, times(1)).getDmaapProducerResponse(any(), eq(contextMap));
107         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
108     }
109
110     @Test
111     public void whenPassedObjectFits_firstFailsThenSucceeds() {
112         prepareMocksForTests(Mono.just(HttpStatus.BAD_GATEWAY), Mono.just(HttpStatus.OK));
113
114         Map<String, String> contextMap = new HashMap<>();
115         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0), contextMap))
116                 .expectNext(consumerDmaapModel).verifyComplete();
117
118         verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any(), eq(contextMap));
119         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
120     }
121
122     @Test
123     public void whenPassedObjectFits_firstFailsThenFails() {
124         prepareMocksForTests(Mono.just(HttpStatus.BAD_GATEWAY), Mono.just(HttpStatus.BAD_GATEWAY));
125
126         Map<String, String> contextMap = new HashMap<>();
127         StepVerifier.create(dmaapPublisherTask.execute(consumerDmaapModel, 1, Duration.ofSeconds(0), contextMap))
128                 .expectErrorMessage("Retries exhausted: 1/1").verify();
129
130         verify(dMaaPProducerReactiveHttpClient, times(2)).getDmaapProducerResponse(any(), eq(contextMap));
131         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
132     }
133
134     @SafeVarargs
135     final void prepareMocksForTests(Mono<HttpStatus> firstResponse, Mono<HttpStatus>... nextHttpResponses) {
136         dMaaPProducerReactiveHttpClient = mock(DmaapProducerReactiveHttpClient.class);
137         when(dMaaPProducerReactiveHttpClient.getDmaapProducerResponse(any(), any())).thenReturn(firstResponse,
138                 nextHttpResponses);
139
140         dmaapPublisherTask = spy(new DataRouterPublisher(appConfig));
141         when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration);
142         doReturn(dMaaPProducerReactiveHttpClient).when(dmaapPublisherTask).resolveClient();
143     }
144 }