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