2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.collectors.datafile.tasks;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.verifyNoMoreInteractions;
31 import static org.mockito.Mockito.when;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.function.Executable;
36 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
37 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
38 import org.onap.dcaegen2.collectors.datafile.exceptions.DmaapNotFoundException;
39 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
40 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
41 import org.onap.dcaegen2.collectors.datafile.service.producer.DMaaPProducerReactiveHttpClient;
42 import org.onap.dcaegen2.collectors.datafile.tasks.DmaapPublisherTaskImpl;
43 import org.onap.dcaegen2.collectors.datafile.config.ImmutableDmaapPublisherConfiguration;
44 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
45 import org.springframework.http.HttpStatus;
46 import reactor.core.publisher.Mono;
47 import reactor.test.StepVerifier;
50 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18
52 class DmaapPublisherTaskImplTest {
54 private static ConsumerDmaapModel consumerDmaapModel;
55 private static DmaapPublisherTaskImpl dmaapPublisherTask;
56 private static DMaaPProducerReactiveHttpClient dMaaPProducerReactiveHttpClient;
57 private static AppConfig appConfig;
58 private static DmaapPublisherConfiguration dmaapPublisherConfiguration;
62 dmaapPublisherConfiguration = new ImmutableDmaapPublisherConfiguration.Builder()
63 .dmaapContentType("application/json").dmaapHostName("54.45.33.2").dmaapPortNumber(1234)
64 .dmaapProtocol("https").dmaapUserName("Datafile").dmaapUserPassword("Datafile")
65 .dmaapTopicName("unauthenticated.SEC_OTHER_OUTPUT").build();
66 consumerDmaapModel = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
67 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
68 .pnfName("NOKQTFCOC540002E").build();
69 appConfig = mock(AppConfig.class);
73 void whenPassedObjectDoesntFit_ThrowsDatafileTaskException() {
75 when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
76 dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig);
79 Executable executableFunction = () -> dmaapPublisherTask.execute(null);
82 assertThrows(DatafileTaskException.class, executableFunction, "The specified parameter is incorrect");
86 void whenPassedObjectFits_ReturnsCorrectStatus() throws DatafileTaskException {
88 prepareMocksForTests(HttpStatus.OK.value());
91 StepVerifier.create(dmaapPublisherTask.execute(Mono.just(consumerDmaapModel))).expectSubscription()
92 .expectNext(HttpStatus.OK.toString()).verifyComplete();
95 verify(dMaaPProducerReactiveHttpClient, times(1))
96 .getDMaaPProducerResponse(any(Mono.class));
97 verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
102 void whenPassedObjectFits_butIncorrectResponseReturns() throws DmaapNotFoundException {
104 prepareMocksForTests(HttpStatus.UNAUTHORIZED.value());
107 StepVerifier.create(dmaapPublisherTask.execute(Mono.just(consumerDmaapModel))).expectSubscription()
108 .expectNext(String.valueOf(HttpStatus.UNAUTHORIZED.value())).verifyComplete();
111 verify(dMaaPProducerReactiveHttpClient, times(1)).getDMaaPProducerResponse(any(Mono.class));
112 verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
116 private void prepareMocksForTests(Integer httpResponseCode) {
117 dMaaPProducerReactiveHttpClient = mock(DMaaPProducerReactiveHttpClient.class);
118 when(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(any(Mono.class)))
119 .thenReturn(Mono.just(httpResponseCode.toString()));
120 dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig));
121 when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration);
122 doReturn(dMaaPProducerReactiveHttpClient).when(dmaapPublisherTask).resolveClient();