2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
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.prh.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 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.services.prh.config.DmaapConsumerConfiguration;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.web.reactive.function.client.WebClient;
38 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
39 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 static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}";
49 private DMaaPConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
50 private DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class);
51 private Mono<String> expectedResult = Mono.empty();
52 private WebClient webClient;
53 private RequestHeadersUriSpec requestHeadersSpec;
54 private ResponseSpec responseSpec;
59 when(consumerConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
60 when(consumerConfigurationMock.dmaapProtocol()).thenReturn("https");
61 when(consumerConfigurationMock.dmaapPortNumber()).thenReturn(1234);
62 when(consumerConfigurationMock.dmaapUserName()).thenReturn("PRH");
63 when(consumerConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
64 when(consumerConfigurationMock.dmaapContentType()).thenReturn("application/json");
65 when(consumerConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT");
66 when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
67 when(consumerConfigurationMock.consumerId()).thenReturn("c12");
69 webClient = spy(WebClient.builder()
70 .defaultHeader(HttpHeaders.CONTENT_TYPE, consumerConfigurationMock.dmaapContentType())
71 .filter(basicAuthentication(consumerConfigurationMock.dmaapUserName(),
72 consumerConfigurationMock.dmaapUserPassword()))
74 dmaapConsumerReactiveHttpClient = new DMaaPConsumerReactiveHttpClient(consumerConfigurationMock, webClient);
75 requestHeadersSpec = mock(RequestHeadersUriSpec.class);
76 responseSpec = mock(ResponseSpec.class);
81 void getHttpResponse_Success() {
83 expectedResult = Mono.just(JSON_MESSAGE);
86 mockDependantObjects();
87 doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
88 Mono<String> response = dmaapConsumerReactiveHttpClient.getDMaaPConsumerResponse();
91 StepVerifier.create(response).expectSubscription()
92 .expectNextMatches(results -> {
93 Assertions.assertEquals(results, expectedResult.block());
99 void getAppropriateUri_whenPassingCorrectedPathForPnf() {
100 Assertions.assertEquals(dmaapConsumerReactiveHttpClient.getUri(),
101 URI.create("https://54.45.33.2:1234/unauthenticated.SEC_OTHER_OUTPUT/OpenDCAE-c12/c12"));
104 private void mockDependantObjects() {
105 when(webClient.get()).thenReturn(requestHeadersSpec);
106 when(requestHeadersSpec.uri((URI) any())).thenReturn(requestHeadersSpec);
107 when(requestHeadersSpec.headers(any())).thenReturn(requestHeadersSpec);
108 when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
109 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());