2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer;
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;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
35 import org.springframework.http.HttpHeaders;
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 import reactor.core.publisher.Mono;
40 import reactor.test.StepVerifier;
43 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/27/18
45 class DMaaPConsumerReactiveHttpClientTest {
47 private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}";
48 private DMaaPConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
49 private DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class);
50 private Mono<String> expectedResult = Mono.empty();
51 private WebClient webClient;
52 private RequestHeadersUriSpec requestHeadersSpec;
53 private ResponseSpec responseSpec;
58 when(consumerConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
59 when(consumerConfigurationMock.dmaapProtocol()).thenReturn("https");
60 when(consumerConfigurationMock.dmaapPortNumber()).thenReturn(1234);
61 when(consumerConfigurationMock.dmaapUserName()).thenReturn("PRH");
62 when(consumerConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
63 when(consumerConfigurationMock.dmaapContentType()).thenReturn("application/json");
64 when(consumerConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT");
65 when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
66 when(consumerConfigurationMock.consumerId()).thenReturn("c12");
68 webClient = spy(WebClient.builder()
69 .defaultHeader(HttpHeaders.CONTENT_TYPE, consumerConfigurationMock.dmaapContentType())
70 .filter(basicAuthentication(consumerConfigurationMock.dmaapUserName(),
71 consumerConfigurationMock.dmaapUserPassword()))
73 dmaapConsumerReactiveHttpClient = new DMaaPConsumerReactiveHttpClient(consumerConfigurationMock, webClient);
74 requestHeadersSpec = mock(RequestHeadersUriSpec.class);
75 responseSpec = mock(ResponseSpec.class);
80 void getHttpResponse_Success() {
82 expectedResult = Mono.just(JSON_MESSAGE);
85 mockDependantObjects();
86 doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
87 Mono<String> response = dmaapConsumerReactiveHttpClient.getDMaaPConsumerResponse();
90 StepVerifier.create(response).expectSubscription()
91 .expectNextMatches(results -> {
92 Assertions.assertEquals(results, expectedResult.block());
98 void getAppropriateUri_whenPassingCorrectedPathForPnf() {
99 Assertions.assertEquals(dmaapConsumerReactiveHttpClient.getUri(),
100 URI.create("https://54.45.33.2:1234/unauthenticated.SEC_OTHER_OUTPUT/OpenDCAE-c12/c12"));
103 private void mockDependantObjects() {
104 when(webClient.get()).thenReturn(requestHeadersSpec);
105 when(requestHeadersSpec.uri((URI) any())).thenReturn(requestHeadersSpec);
106 when(requestHeadersSpec.headers(any())).thenReturn(requestHeadersSpec);
107 when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
108 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());