2b79bc40f320e44542bc54750ba2e2b438cb5fc5
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.tasks;
22
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;
32
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;
48
49 /**
50  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18
51  */
52 class DmaapPublisherTaskImplTest {
53
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;
59
60     @BeforeAll
61     static void setUp() {
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);
70     }
71
72     @Test
73     void whenPassedObjectDoesntFit_ThrowsDatafileTaskException() {
74         //given
75         when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
76         dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig);
77
78         //when
79         Executable executableFunction = () -> dmaapPublisherTask.execute(null);
80
81         //then
82         assertThrows(DatafileTaskException.class, executableFunction, "The specified parameter is incorrect");
83     }
84
85     @Test
86     void whenPassedObjectFits_ReturnsCorrectStatus() throws DatafileTaskException {
87         //given
88         prepareMocksForTests(HttpStatus.OK.value());
89
90         //when
91         StepVerifier.create(dmaapPublisherTask.execute(Mono.just(consumerDmaapModel))).expectSubscription()
92             .expectNext(HttpStatus.OK.toString()).verifyComplete();
93
94         //then
95         verify(dMaaPProducerReactiveHttpClient, times(1))
96             .getDMaaPProducerResponse(any(Mono.class));
97         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
98     }
99
100
101     @Test
102     void whenPassedObjectFits_butIncorrectResponseReturns() throws DmaapNotFoundException {
103         //given
104         prepareMocksForTests(HttpStatus.UNAUTHORIZED.value());
105
106         //when
107         StepVerifier.create(dmaapPublisherTask.execute(Mono.just(consumerDmaapModel))).expectSubscription()
108             .expectNext(String.valueOf(HttpStatus.UNAUTHORIZED.value())).verifyComplete();
109
110         //then
111         verify(dMaaPProducerReactiveHttpClient, times(1)).getDMaaPProducerResponse(any(Mono.class));
112         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
113     }
114
115
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();
123     }
124 }