a2b3575a98d49b493a87f22a83bf793a7e02f05a
[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.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.net.URI;
27 import java.util.Optional;
28 import org.apache.http.entity.ContentType;
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.adapters.http.CloudHttpClient;
33 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils;
35 import org.onap.dcaegen2.services.sdk.rest.services.model.ClientModel;
36 import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel;
37 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
38 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
39 import reactor.core.publisher.Mono;
40 import reactor.test.StepVerifier;
41 import reactor.netty.http.client.HttpClientResponse;
42
43 /**
44  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
45  */
46
47 class DMaaPPublisherReactiveHttpClientTest {
48
49     private DMaaPPublisherReactiveHttpClient dmaapPublisherReactiveHttpClient;
50     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
51     private CloudHttpClient cloudHttpClientMock = mock(CloudHttpClient.class);
52     private DmaapModel dmaapModelMock = mock(DmaapModel.class);
53     private JsonBodyBuilder<DmaapModel> jsonBodyBuilderMock = mock(JsonBodyBuilder.class);
54     private Optional<RequestDiagnosticContext> requestDiagnosticContextOptionalMock = Optional
55         .of(mock(RequestDiagnosticContext.class));
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
67         when(jsonBodyBuilderMock.createJsonBody(dmaapModelMock)).thenReturn(
68             "{\"correlationId\":\"NOKnhfsadhff\"," +
69                 "\"ipaddress-v4\":\"256.22.33.155\", " +
70                 "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}");
71
72         dmaapPublisherReactiveHttpClient =
73             new DMaaPPublisherReactiveHttpClient(dmaapPublisherConfigurationMock, cloudHttpClientMock,
74                 jsonBodyBuilderMock);
75     }
76
77     @Test
78     void getHttpResponse_Success() {
79         //given
80         Mono<HttpClientResponse> expectedResult = Mono.just(mock(HttpClientResponse.class));
81         //when
82         when(
83             cloudHttpClientMock
84                 .post(getUri().toString(), requestDiagnosticContextOptionalMock.get(),
85                     DMaaPClientServiceUtils.getHeaders(ContentType.APPLICATION_JSON.getMimeType()),
86                     jsonBodyBuilderMock,
87                     mock(ClientModel.class)))
88             .thenReturn(Mono.just(mock(HttpClientResponse.class)));
89         //then
90         StepVerifier.create(expectedResult).expectSubscription()
91             .expectNextMatches(results -> {
92                 Assertions.assertEquals(results, expectedResult.block());
93                 return true;
94             }).verifyComplete();
95     }
96
97     @Test
98     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
99         Assertions.assertEquals(dmaapPublisherReactiveHttpClient.getUri(),
100             getUri());
101     }
102
103     private URI getUri() {
104         return URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY");
105     }
106 }