4f7787e980dc3341cd3056fc37e816c4f3492af8
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.tasks;
20
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.verifyNoMoreInteractions;
28 import static org.mockito.Mockito.when;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.junit.jupiter.api.Assertions;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.function.Executable;
37 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
38 import org.onap.dcaegen2.collectors.datafile.config.ImmutableDmaapPublisherConfiguration;
39 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
40 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
41 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
42 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
43 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerReactiveHttpClient;
44 import org.springframework.http.HttpStatus;
45
46 import reactor.core.publisher.Mono;
47
48 /**
49  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18
50  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
51  */
52 class DmaapPublisherTaskImplTest {
53
54     private static ConsumerDmaapModel consumerDmaapModel;
55     private static List<ConsumerDmaapModel> listOfConsumerDmaapModel;
56     private static DmaapPublisherTaskImpl dmaapPublisherTask;
57     private static DmaapProducerReactiveHttpClient dMaaPProducerReactiveHttpClient;
58     private static AppConfig appConfig;
59     private static DmaapPublisherConfiguration dmaapPublisherConfiguration;
60
61     @BeforeAll
62     public static void setUp() {
63         dmaapPublisherConfiguration =
64                 new ImmutableDmaapPublisherConfiguration.Builder().dmaapContentType("application/json")
65                         .dmaapHostName("54.45.33.2").dmaapPortNumber(1234).dmaapProtocol("https").dmaapUserName("DFC")
66                         .dmaapUserPassword("DFC").dmaapTopicName("unauthenticated.VES_NOTIFICATION_OUTPUT").build();
67         consumerDmaapModel = ImmutableConsumerDmaapModel.builder().location("target/A20161224.1030-1045.bin.gz")
68                 .compression("gzip").fileFormatType("org.3GPP.32.435#measCollec").fileFormatVersion("V10").build();
69         listOfConsumerDmaapModel = new ArrayList<ConsumerDmaapModel>();
70         listOfConsumerDmaapModel.add(consumerDmaapModel);
71         appConfig = mock(AppConfig.class);
72     }
73
74     @Test
75     public void whenPassedObjectDoesntFit_ThrowsDatafileTaskException() {
76         // given
77         when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
78         dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig);
79
80         // when
81         Executable executableFunction = () -> dmaapPublisherTask.execute(null);
82
83         // then
84         Assertions.assertThrows(DatafileTaskException.class, executableFunction,
85                 "The specified parameter is incorrect");
86     }
87
88     @Test
89     public void whenPassedObjectFits_ReturnsCorrectStatus() throws DatafileTaskException {
90         // given
91         prepareMocksForTests(HttpStatus.OK.value());
92
93         // when
94         dmaapPublisherTask.execute(Mono.just(listOfConsumerDmaapModel));
95
96         // then
97         verify(dMaaPProducerReactiveHttpClient, times(1)).getDmaapProducerResponse(any());
98         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
99     }
100
101     @Test
102     public void whenPassedObjectFits_ReturnsNoContent() throws DatafileTaskException {
103         // given
104         prepareMocksForTests(HttpStatus.NO_CONTENT.value());
105
106         dmaapPublisherTask.execute(Mono.just(listOfConsumerDmaapModel));
107
108         // then
109         verify(dMaaPProducerReactiveHttpClient, times(1)).getDmaapProducerResponse(any());
110         verifyNoMoreInteractions(dMaaPProducerReactiveHttpClient);
111     }
112
113     private void prepareMocksForTests(Integer httpResponseCode) {
114         dMaaPProducerReactiveHttpClient = mock(DmaapProducerReactiveHttpClient.class);
115         when(dMaaPProducerReactiveHttpClient.getDmaapProducerResponse(any()))
116                 .thenReturn(Mono.just(httpResponseCode.toString()));
117         when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
118         dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig));
119         when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration);
120         doReturn(dMaaPProducerReactiveHttpClient).when(dmaapPublisherTask).resolveClient();
121     }
122 }