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=========================================================
20 package org.onap.dcaegen2.services.prh.service.producer;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
30 import java.net.URISyntaxException;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
35 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
36 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.web.reactive.function.client.WebClient;
39 import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
40 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
41 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
42 import reactor.core.publisher.Mono;
43 import reactor.test.StepVerifier;
46 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
48 public class DMaaPProducerReactiveHttpClientTest {
50 private static DMaaPProducerReactiveHttpClient dMaaPProducerReactiveHttpClient;
52 private static DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(
53 DmaapPublisherConfiguration.class);
54 private static final Integer RESPONSE_SUCCESS = 200;
55 private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
56 private static WebClient webClient = mock(WebClient.class);
57 private static RequestBodyUriSpec requestBodyUriSpec;
58 private static ResponseSpec responseSpec;
62 public static void setUp() {
63 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
64 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn("https");
65 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(1234);
66 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("PRH");
67 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
68 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json");
69 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("pnfReady");
71 dMaaPProducerReactiveHttpClient = new DMaaPProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
73 webClient = spy(WebClient.builder()
74 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
75 .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
76 dmaapPublisherConfigurationMock.dmaapUserPassword()))
78 requestBodyUriSpec = mock(RequestBodyUriSpec.class);
79 responseSpec = mock(ResponseSpec.class);
83 public void getHttpResponse_Success() {
85 Mono<Integer> expectedResult = Mono.just(RESPONSE_SUCCESS);
88 mockWebClientDependantObject();
89 doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
90 dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
91 Mono<String> response = dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel));
94 Assertions.assertEquals(response.block(), expectedResult.block());
98 public void getHttpResponse_whenURISyntaxExceptionHasBeenThrown() throws URISyntaxException {
100 dMaaPProducerReactiveHttpClient = spy(dMaaPProducerReactiveHttpClient);
102 when(webClient.post()).thenReturn(requestBodyUriSpec);
103 dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
104 when(dMaaPProducerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
107 StepVerifier.create(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(any())).expectSubscription()
108 .expectError(Exception.class).verify();
111 private void mockWebClientDependantObject() {
112 RequestHeadersSpec requestHeadersSpec = mock(RequestHeadersSpec.class);
113 when(webClient.post()).thenReturn(requestBodyUriSpec);
114 when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
115 when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
116 doReturn(responseSpec).when(requestHeadersSpec).retrieve();
117 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());