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.util.ArrayList;
31 import java.util.List;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
36 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
37 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
38 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.web.reactive.function.client.WebClient;
41 import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
42 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
43 import org.springframework.web.util.DefaultUriBuilderFactory;
45 import reactor.core.publisher.Flux;
46 import reactor.test.StepVerifier;
49 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
50 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
52 class DmaapProducerReactiveHttpClientTest {
54 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
55 private static final String LOCATION_JSON_TAG = "location";
56 private static final String NAME_JSON_TAG = "name";
57 private static final String X_ATT_DR_META = "X-ATT-DR-META";
59 private static final String HOST = "54.45.33.2";
60 private static final String HTTP_SCHEME = "http";
61 private static final int PORT = 1234;
62 private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
63 private static final String PUBLISH_TOPIC = "publish";
64 private static final String DEFAULT_FEED_ID = "1";
66 private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
68 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
69 private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
70 private WebClient webClientMock = mock(WebClient.class);
71 private RequestBodyUriSpec requestBodyUriSpecMock;
72 private ResponseSpec responseSpecMock;
77 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
78 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTP_SCHEME);
79 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
80 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("DATAFILE");
81 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("DATAFILE");
82 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
83 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
85 dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
87 webClientMock = spy(WebClient.builder()
88 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
89 .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
90 dmaapPublisherConfigurationMock.dmaapUserPassword()))
92 requestBodyUriSpecMock = mock(RequestBodyUriSpec.class);
93 responseSpecMock = mock(ResponseSpec.class);
97 void getHttpResponse_Success() {
98 mockWebClientDependantObject();
99 dmaapProducerReactiveHttpClient.createDmaapWebClient(webClientMock);
100 List<ConsumerDmaapModel> consumerDmaapModelList = new ArrayList<ConsumerDmaapModel>();
101 consumerDmaapModelList.add(consumerDmaapModel);
103 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
104 .expectNext("200").verifyComplete();
106 verify(requestBodyUriSpecMock).header(HttpHeaders.CONTENT_TYPE, APPLICATION_OCTET_STREAM_CONTENT_TYPE);
107 JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
108 metaData.getAsJsonObject().remove(LOCATION_JSON_TAG);
109 metaData.getAsJsonObject().remove(NAME_JSON_TAG);
110 verify(requestBodyUriSpecMock).header(X_ATT_DR_META, metaData.toString());
111 URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTP_SCHEME).host(HOST).port(PORT)
112 .path(PUBLISH_TOPIC + "/" + DEFAULT_FEED_ID + "/" + FILE_NAME).build();
113 verify(requestBodyUriSpecMock).uri(expectedUri);
114 verify(requestBodyUriSpecMock).body(any());
117 private void mockWebClientDependantObject() {
118 when(webClientMock.post()).thenReturn(requestBodyUriSpecMock);
119 when(requestBodyUriSpecMock.uri((URI) any())).thenReturn(requestBodyUriSpecMock);
121 when(requestBodyUriSpecMock.retrieve()).thenReturn(responseSpecMock);
122 when(responseSpecMock.onStatus(any(), any())).thenReturn(responseSpecMock);
123 Flux<String> expectedResult = Flux.just("200");
124 when(responseSpecMock.bodyToFlux(String.class)).thenReturn(expectedResult);