4568bdde33ce074203274ac9c7c858f1f17a555f
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.service.consumer;
20
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
27
28 import java.net.URI;
29 import java.net.URISyntaxException;
30
31 import org.apache.http.HttpHeaders;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
36 import org.springframework.web.reactive.function.client.WebClient;
37 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
38 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
39
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
42
43 /**
44  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/27/18
45  */
46 class DmaapConsumerReactiveHttpClientTest {
47
48     private DmaapConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
49
50     private DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class);
51     private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}";
52     private Mono<String> expectedResult = Mono.empty();
53     private WebClient webClient;
54     private RequestHeadersUriSpec requestHeadersSpecMock;
55     private ResponseSpec responseSpecMock;
56
57
58     @BeforeEach
59     void setUp() {
60         when(consumerConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
61         when(consumerConfigurationMock.dmaapProtocol()).thenReturn("https");
62         when(consumerConfigurationMock.dmaapPortNumber()).thenReturn(1234);
63         when(consumerConfigurationMock.dmaapUserName()).thenReturn("DATAFILE");
64         when(consumerConfigurationMock.dmaapUserPassword()).thenReturn("DATFILE");
65         when(consumerConfigurationMock.dmaapContentType()).thenReturn("application/json");
66         when(consumerConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.VES_NOTIFICATION_OUTPUT");
67         when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
68         when(consumerConfigurationMock.consumerId()).thenReturn("c12");
69
70         dmaapConsumerReactiveHttpClient = new DmaapConsumerReactiveHttpClient(consumerConfigurationMock);
71         webClient = spy(WebClient.builder()
72             .defaultHeader(HttpHeaders.CONTENT_TYPE, consumerConfigurationMock.dmaapContentType())
73             .filter(basicAuthentication(consumerConfigurationMock.dmaapUserName(),
74                 consumerConfigurationMock.dmaapUserPassword()))
75             .build());
76         requestHeadersSpecMock = mock(RequestHeadersUriSpec.class);
77         responseSpecMock = mock(ResponseSpec.class);
78     }
79
80
81     @Test
82     void getHttpResponse_Success() {
83         //given
84         expectedResult = Mono.just(JSON_MESSAGE);
85
86         //when
87         mockDependantObjects();
88         doReturn(expectedResult).when(responseSpecMock).bodyToMono(String.class);
89         dmaapConsumerReactiveHttpClient.createDmaapWebClient(webClient);
90
91         Mono<String> response = dmaapConsumerReactiveHttpClient.getDmaapConsumerResponse();
92
93         //then
94         StepVerifier.create(response).expectSubscription()
95             .expectNextMatches(results -> {
96                 Assertions.assertEquals(results, expectedResult.block());
97                 return true;
98             }).verifyComplete();
99     }
100
101     @Test
102     void getAppropriateUri_whenPassingCorrectedUriData() throws URISyntaxException {
103         Assertions.assertEquals(dmaapConsumerReactiveHttpClient.getUri(),
104             URI.create("https://54.45.33.2:1234/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDCAE-c12/c12"));
105     }
106
107     private void mockDependantObjects() {
108         when(webClient.get()).thenReturn(requestHeadersSpecMock);
109         when(requestHeadersSpecMock.uri((URI) any())).thenReturn(requestHeadersSpecMock);
110         when(requestHeadersSpecMock.headers(any())).thenReturn(requestHeadersSpecMock);
111         when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock);
112         doReturn(responseSpecMock).when(responseSpecMock).onStatus(any(), any());
113     }
114
115 }