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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
19 package org.onap.dcaegen2.collectors.datafile.service.consumer;
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;
29 import java.net.URISyntaxException;
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;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
44 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/27/18
46 class DmaapConsumerReactiveHttpClientTest {
48 private DmaapConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
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;
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");
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()))
76 requestHeadersSpec = mock(RequestHeadersUriSpec.class);
77 responseSpec = mock(ResponseSpec.class);
82 void getHttpResponse_Success() {
84 expectedResult = Mono.just(JSON_MESSAGE);
87 mockDependantObjects();
88 doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
89 dmaapConsumerReactiveHttpClient.createDmaapWebClient(webClient);
90 Mono<String> response = dmaapConsumerReactiveHttpClient.getDmaapConsumerResponse();
93 StepVerifier.create(response).expectSubscription()
94 .expectNextMatches(results -> {
95 Assertions.assertEquals(results, expectedResult.block());
101 void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
103 dmaapConsumerReactiveHttpClient = spy(dmaapConsumerReactiveHttpClient);
105 when(webClient.get()).thenReturn(requestHeadersSpec);
106 dmaapConsumerReactiveHttpClient.createDmaapWebClient(webClient);
107 when(dmaapConsumerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
110 StepVerifier.create(dmaapConsumerReactiveHttpClient.getDmaapConsumerResponse()).expectSubscription()
111 .expectError(Exception.class).verify();
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());