bb1ce19da85e305b4cc426036a15679499196073
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
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.service.producer;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
29
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
36 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
37 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest;
38 import org.onap.dcaegen2.collectors.datafile.service.producer.DMaaPProducerReactiveHttpClient;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.web.reactive.function.client.WebClient;
41 import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
42 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
43 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
46
47 /**
48  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
49  */
50 class DMaaPProducerReactiveHttpClientTest {
51
52     private DMaaPProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
53
54     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(
55         DmaapPublisherConfiguration.class);
56     private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
57     private WebClient webClient = mock(WebClient.class);
58     private RequestBodyUriSpec requestBodyUriSpec;
59     private ResponseSpec responseSpec;
60
61
62     @BeforeEach
63     void setUp() {
64         when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
65         when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn("https");
66         when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(1234);
67         when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("Datafile");
68         when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("Datafile");
69         when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json");
70         when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("pnfReady");
71
72         dmaapProducerReactiveHttpClient = new DMaaPProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
73
74         webClient = spy(WebClient.builder()
75             .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
76             .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
77                 dmaapPublisherConfigurationMock.dmaapUserPassword()))
78             .build());
79         requestBodyUriSpec = mock(RequestBodyUriSpec.class);
80         responseSpec = mock(ResponseSpec.class);
81     }
82
83     @Test
84     void getHttpResponse_Success() {
85         //given
86         Integer responseSuccess = 200;
87         Mono<Integer> expectedResult = Mono.just(responseSuccess);
88
89         //when
90         mockWebClientDependantObject();
91         doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
92         dmaapProducerReactiveHttpClient.createDMaaPWebClient(webClient);
93         Mono<String> response = dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel));
94
95         //then
96         Assertions.assertEquals(response.block(), expectedResult.block());
97     }
98
99     @Test
100     void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
101         //given
102         dmaapProducerReactiveHttpClient = spy(dmaapProducerReactiveHttpClient);
103         //when
104         when(webClient.post()).thenReturn(requestBodyUriSpec);
105         dmaapProducerReactiveHttpClient.createDMaaPWebClient(webClient);
106         when(dmaapProducerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
107
108         //then
109         StepVerifier.create(dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(any())).expectSubscription()
110             .expectError(Exception.class).verify();
111     }
112
113     private void mockWebClientDependantObject() {
114         RequestHeadersSpec requestHeadersSpec = mock(RequestHeadersSpec.class);
115         when(webClient.post()).thenReturn(requestBodyUriSpec);
116         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
117         when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
118         doReturn(responseSpec).when(requestHeadersSpec).retrieve();
119         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
120     }
121 }