Proposal of enhanced DMaaP API
[dcaegen2/services/sdk.git] / rest-services / dmaap-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / dmaap / client / service / consumer / DMaaPConsumerReactiveHttpClientTest.java
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.consumer;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import com.google.gson.JsonArray;
27 import java.net.URI;
28 import java.util.Optional;
29 import org.apache.http.entity.ContentType;
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils;
36 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
37 import reactor.core.publisher.Mono;
38 import reactor.test.StepVerifier;
39
40 /**
41  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/27/18
42  */
43 class DMaaPConsumerReactiveHttpClientTest {
44
45     private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}";
46     private DMaaPConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
47     private DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class);
48     private Mono<JsonArray> expectedResult;
49     private CloudHttpClient httpClient = mock(CloudHttpClient.class);
50     private URI exampleTestUri = URI
51         .create("https://54.45.33.2:1234/unauthenticated.SEC_OTHER_OUTPUT/OpenDCAE-c12/c12");
52     private RequestDiagnosticContext requestDiagnosticContext = mock(RequestDiagnosticContext.class);
53
54     @BeforeEach
55     void setUp() {
56         when(consumerConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
57         when(consumerConfigurationMock.dmaapProtocol()).thenReturn("https");
58         when(consumerConfigurationMock.dmaapPortNumber()).thenReturn(1234);
59         when(consumerConfigurationMock.dmaapUserName()).thenReturn("PRH");
60         when(consumerConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
61         when(consumerConfigurationMock.dmaapContentType()).thenReturn(ContentType.APPLICATION_JSON.getMimeType());
62         when(consumerConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT");
63         when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
64         when(consumerConfigurationMock.consumerId()).thenReturn("c12");
65         dmaapConsumerReactiveHttpClient = new DMaaPConsumerReactiveHttpClient(consumerConfigurationMock, httpClient);
66     }
67
68     @Test
69     void getHttpResponse_Success() {
70         //given
71         expectedResult = Mono.just(mock(JsonArray.class));
72         when(httpClient.get(exampleTestUri.toString(), requestDiagnosticContext, DMaaPClientServiceUtils.getHeaders(ContentType.APPLICATION_JSON.getMimeType()), JsonArray.class))
73             .thenReturn(expectedResult);
74         //when
75         Mono<JsonArray> response = dmaapConsumerReactiveHttpClient
76             .getDMaaPConsumerResponse(Optional.of(requestDiagnosticContext));
77         //then
78         StepVerifier.create(response).expectSubscription()
79             .expectNextMatches(results -> {
80                 Assertions.assertEquals(results, expectedResult.block());
81                 return true;
82             }).verifyComplete();
83     }
84
85     @Test
86     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
87         Assertions.assertEquals(dmaapConsumerReactiveHttpClient.getUri(), exampleTestUri);
88     }
89
90
91 }