29d1039f0d34fed73918d8a1ad7ae1aaf4f78a44
[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.when;
27
28 import java.net.URI;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
34 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
35 import org.springframework.http.HttpEntity;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.client.RestTemplate;
40 import reactor.test.StepVerifier;
41
42
43
44 /**
45  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
46  */
47
48 class DMaaPProducerReactiveHttpClientTest {
49
50     private DMaaPProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
51
52     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(
53         DmaapPublisherConfiguration.class);
54     private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
55
56
57     @BeforeEach
58     void setUp() {
59         when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
60         when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn("https");
61         when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(1234);
62         when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("PRH");
63         when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
64         when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json");
65         when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.PNF_READY");
66         dmaapProducerReactiveHttpClient = new DMaaPProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
67
68     }
69
70     @Test
71     void getHttpResponse_Success() {
72         //given
73         int responseSuccess = 200;
74         ResponseEntity<String> mockedResponseEntity = mock(ResponseEntity.class);
75         RestTemplate restTemplate = mock(RestTemplate.class);
76         //when
77         when(mockedResponseEntity.getStatusCode()).thenReturn(HttpStatus.valueOf(responseSuccess));
78         doReturn(mockedResponseEntity).when(restTemplate)
79             .exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), (Class<Object>) any());
80         dmaapProducerReactiveHttpClient.createDMaaPWebClient(restTemplate);
81
82         //then
83         StepVerifier.create(dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(consumerDmaapModel))
84             .expectSubscription().expectNext(mockedResponseEntity).verifyComplete();
85     }
86
87     @Test
88     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
89         Assertions.assertEquals(dmaapProducerReactiveHttpClient.getUri(),
90             URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY"));
91     }
92 }