4f96a9030e1fe867174caa197c4b787f7d23cc04
[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.service.consumer;
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.when;
26 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
27
28 import java.net.URI;
29 import java.net.URISyntaxException;
30
31 import org.apache.http.HttpHeaders;
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.DmaapConsumerConfiguration;
36 import org.springframework.web.reactive.function.client.WebClient;
37 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
38 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
39
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 6/27/18
45  */
46 class DmaapConsumerReactiveHttpClientTest {
47
48     private DmaapConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
49
50     private DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class);
51     private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}";
52     private Mono<String> expectedResult = Mono.empty();
53     private WebClient webClient;
54     private RequestHeadersUriSpec requestHeadersSpec;
55     private ResponseSpec responseSpec;
56
57
58     @BeforeEach
59     void setUp() {
60         when(consumerConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
61         when(consumerConfigurationMock.dmaapProtocol()).thenReturn("https");
62         when(consumerConfigurationMock.dmaapPortNumber()).thenReturn(1234);
63         when(consumerConfigurationMock.dmaapUserName()).thenReturn("DATAFILE");
64         when(consumerConfigurationMock.dmaapUserPassword()).thenReturn("DATFILE");
65         when(consumerConfigurationMock.dmaapContentType()).thenReturn("application/json");
66         when(consumerConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.VES_NOTIFICATION_OUTPUT");
67         when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
68         when(consumerConfigurationMock.consumerId()).thenReturn("c12");
69
70         dmaapConsumerReactiveHttpClient = new DmaapConsumerReactiveHttpClient(consumerConfigurationMock);
71         webClient = spy(WebClient.builder()
72             .defaultHeader(HttpHeaders.CONTENT_TYPE, consumerConfigurationMock.dmaapContentType())
73             .filter(basicAuthentication(consumerConfigurationMock.dmaapUserName(),
74                 consumerConfigurationMock.dmaapUserPassword()))
75             .build());
76         requestHeadersSpec = mock(RequestHeadersUriSpec.class);
77         responseSpec = mock(ResponseSpec.class);
78     }
79
80
81     @Test
82     void getHttpResponse_Success() {
83         //given
84         expectedResult = Mono.just(JSON_MESSAGE);
85
86         //when
87         mockDependantObjects();
88         doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
89         dmaapConsumerReactiveHttpClient.createDmaapWebClient(webClient);
90         Mono<String> response = dmaapConsumerReactiveHttpClient.getDmaapConsumerResponse();
91
92         //then
93         StepVerifier.create(response).expectSubscription()
94             .expectNextMatches(results -> {
95                 Assertions.assertEquals(results, expectedResult.block());
96                 return true;
97             }).verifyComplete();
98     }
99
100     @Test
101     void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
102         //given
103         dmaapConsumerReactiveHttpClient = spy(dmaapConsumerReactiveHttpClient);
104         //when
105         when(webClient.get()).thenReturn(requestHeadersSpec);
106         dmaapConsumerReactiveHttpClient.createDmaapWebClient(webClient);
107         when(dmaapConsumerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
108
109         //then
110         StepVerifier.create(dmaapConsumerReactiveHttpClient.getDmaapConsumerResponse()).expectSubscription()
111             .expectError(Exception.class).verify();
112     }
113
114     private void mockDependantObjects() {
115         when(webClient.get()).thenReturn(requestHeadersSpec);
116         when(requestHeadersSpec.uri((URI) any())).thenReturn(requestHeadersSpec);
117         when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
118         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
119     }
120
121 }