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"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.service.producer;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParser;
30 import java.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import org.apache.http.client.utils.URIBuilder;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
38 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
39 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
40 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.web.reactive.function.client.WebClient;
43 import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
44 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
46 import reactor.core.publisher.Mono;
49 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
51 class DmaapProducerReactiveHttpClientTest {
53 private static final String LOCATION = "location";
54 private static final String X_ATT_DR_META = "X-ATT-DR-META";
56 private static final String HOST = "54.45.33.2";
57 private static final String HTTPS_SCHEME = "https";
58 private static final int PORT = 1234;
59 private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
60 private static final String FILE_READY_TOPIC = "fileReady";
62 private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
64 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
65 private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
66 private WebClient webClientMock = mock(WebClient.class);
67 private RequestBodyUriSpec requestBodyUriSpecMock;
68 private ResponseSpec responseSpecMock;
73 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
74 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
75 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
76 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("DATAFILE");
77 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("DATAFILE");
78 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
79 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(FILE_READY_TOPIC);
81 dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
83 webClientMock = spy(WebClient.builder()
84 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
85 .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
86 dmaapPublisherConfigurationMock.dmaapUserPassword()))
88 requestBodyUriSpecMock = mock(RequestBodyUriSpec.class);
89 responseSpecMock = mock(ResponseSpec.class);
93 void getHttpResponse_Success() {
97 mockWebClientDependantObject();
98 dmaapProducerReactiveHttpClient.createDmaapWebClient(webClientMock);
99 List<ConsumerDmaapModel> consumerDmaapModelList = new ArrayList<ConsumerDmaapModel>();
100 consumerDmaapModelList.add(consumerDmaapModel);
102 dmaapProducerReactiveHttpClient.getDmaapProducerResponse(Mono.just(consumerDmaapModelList));
105 verify(requestBodyUriSpecMock).header(HttpHeaders.CONTENT_TYPE, APPLICATION_OCTET_STREAM_CONTENT_TYPE);
106 JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
107 metaData.getAsJsonObject().remove(LOCATION);
108 verify(requestBodyUriSpecMock).header(X_ATT_DR_META, metaData.toString());
109 URI expectedUri = null;
111 expectedUri = new URIBuilder().setScheme(HTTPS_SCHEME).setHost(HOST).setPort(1234).setPath(FILE_READY_TOPIC)
113 } catch (URISyntaxException e) {
116 verify(requestBodyUriSpecMock).uri(expectedUri);
117 verify(requestBodyUriSpecMock).body(any());
120 private void mockWebClientDependantObject() {
121 when(webClientMock.post()).thenReturn(requestBodyUriSpecMock);
122 when(requestBodyUriSpecMock.uri((URI) any())).thenReturn(requestBodyUriSpecMock);
124 when(requestBodyUriSpecMock.retrieve()).thenReturn(responseSpecMock);
125 when(responseSpecMock.onStatus(any(), any())).thenReturn(responseSpecMock);
126 Mono<String> expectedResult = Mono.just("200");
127 when(responseSpecMock.bodyToMono(String.class)).thenReturn(expectedResult);