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.producer;
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;
30 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.DmaapPublisherConfiguration;
36 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
37 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.client.RestTemplate;
43 import reactor.test.StepVerifier;
46 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
49 class DMaaPProducerReactiveHttpClientTest {
51 private DMaaPProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
53 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(
54 DmaapPublisherConfiguration.class);
55 private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
60 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
61 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn("https");
62 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(1234);
63 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("PRH");
64 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
65 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json");
66 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.PNF_READY");
67 dmaapProducerReactiveHttpClient = new DMaaPProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
72 void getHttpResponse_Success() {
74 int responseSuccess = 200;
75 ResponseEntity<String> mockedResponseEntity = mock(ResponseEntity.class);
76 RestTemplate restTemplate = mock(RestTemplate.class);
78 when(mockedResponseEntity.getStatusCode()).thenReturn(HttpStatus.valueOf(responseSuccess));
79 doReturn(mockedResponseEntity).when(restTemplate)
80 .exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), (Class<Object>) any());
81 dmaapProducerReactiveHttpClient.createDMaaPWebClient(restTemplate);
84 StepVerifier.create(dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(consumerDmaapModel))
85 .expectSubscription().expectNext(mockedResponseEntity).verifyComplete();
89 void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
91 dmaapProducerReactiveHttpClient = spy(dmaapProducerReactiveHttpClient);
93 when(dmaapProducerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
96 StepVerifier.create(dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(any())).expectSubscription()
97 .expectError(Exception.class).verify();
101 void getAppropriateUri_whenPassingCorrectedPathForPnf() throws URISyntaxException {
102 Assertions.assertEquals(dmaapProducerReactiveHttpClient.getUri(),
103 URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY"));