05b748957149da406509d7f0c7ade7bcdab5083a
[dcaegen2/services/prh.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.prh.service.producer;
22
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
29 import java.net.URI;
30 import java.net.URISyntaxException;
31
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;
44
45 /**
46  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
47  */
48
49 class DMaaPProducerReactiveHttpClientTest {
50
51     private DMaaPProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
52
53     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(
54         DmaapPublisherConfiguration.class);
55     private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
56
57
58     @BeforeEach
59     void setUp() {
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);
68
69     }
70
71     @Test
72     void getHttpResponse_Success() {
73         //given
74         int responseSuccess = 200;
75         ResponseEntity<String> mockedResponseEntity = mock(ResponseEntity.class);
76         RestTemplate restTemplate = mock(RestTemplate.class);
77         //when
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);
82
83         //then
84         StepVerifier.create(dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(consumerDmaapModel))
85             .expectSubscription().expectNext(mockedResponseEntity).verifyComplete();
86     }
87
88     @Test
89     void getHttpResponse_whenUriSyntaxExceptionHasBeenThrown() throws URISyntaxException {
90         //given
91         dmaapProducerReactiveHttpClient = spy(dmaapProducerReactiveHttpClient);
92         //when
93         when(dmaapProducerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
94
95         //then
96         StepVerifier.create(dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(any())).expectSubscription()
97             .expectError(Exception.class).verify();
98     }
99
100     @Test
101     void getAppropriateUri_whenPassingCorrectedPathForPnf() throws URISyntaxException {
102         Assertions.assertEquals(dmaapProducerReactiveHttpClient.getUri(),
103             URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY"));
104     }
105 }