c84ca7d260d2b33eb54b8194cdfc67daf5dc48e3
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
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.sdk.rest.services.dmaap.client.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.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
33
34 import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel;
35 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
36 import org.springframework.http.HttpEntity;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.client.RestTemplate;
41 import reactor.core.publisher.Mono;
42 import reactor.test.StepVerifier;
43
44 /**
45  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
46  */
47
48 class DMaaPPublisherReactiveHttpClientTest {
49
50     private DMaaPPublisherReactiveHttpClient dmaapPublisherReactiveHttpClient;
51     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
52
53     private RestTemplate restTemplate = mock(RestTemplate.class);
54
55     private DmaapModel dmaapModel = mock(DmaapModel.class);
56     private JsonBodyBuilder<DmaapModel> jsonBodyBuilder = mock(JsonBodyBuilder.class);
57
58
59     @BeforeEach
60     void setUp() {
61         when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
62         when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn("https");
63         when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(1234);
64         when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("PRH");
65         when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
66         when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json");
67         when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.PNF_READY");
68
69         when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(
70                 "{\"correlationId\":\"NOKnhfsadhff\"," +
71                         "\"ipaddress-v4\":\"256.22.33.155\", " +
72                         "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}");
73
74         dmaapPublisherReactiveHttpClient =
75                 new DMaaPPublisherReactiveHttpClient(dmaapPublisherConfigurationMock, Mono.just(restTemplate),jsonBodyBuilder);
76     }
77
78     @Test
79     void getHttpResponse_Success() {
80         //given
81         int responseSuccess = 200;
82         ResponseEntity<String> mockedResponseEntity = mock(ResponseEntity.class);
83         //when
84         when(mockedResponseEntity.getStatusCode()).thenReturn(HttpStatus.valueOf(responseSuccess));
85         doReturn(mockedResponseEntity).when(restTemplate)
86                 .exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), (Class<Object>) any());
87
88         //then
89         StepVerifier.create(dmaapPublisherReactiveHttpClient.getDMaaPProducerResponse(dmaapModel))
90                 .expectSubscription().expectNext(mockedResponseEntity).verifyComplete();
91     }
92
93     @Test
94     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
95         Assertions.assertEquals(dmaapPublisherReactiveHttpClient.getUri(),
96                 URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY"));
97     }
98 }