c0dbf31b6f038d2c99aaed039ca52ee21afbf8c1
[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"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.service.producer;
18
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;
25
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonParser;
28
29 import java.net.URI;
30 import java.util.ArrayList;
31 import java.util.List;
32
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;
44
45 import reactor.core.publisher.Mono;
46
47 /**
48  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
49  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
50  */
51 class DmaapProducerReactiveHttpClientTest {
52
53     private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
54     private static final String LOCATION_JSON_TAG = "location";
55     private static final String X_ATT_DR_META = "X-ATT-DR-META";
56
57     private static final String HOST = "54.45.33.2";
58     private static final String HTTP_SCHEME = "http";
59     private static final int PORT = 1234;
60     private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
61     private static final String PUBLISH_TOPIC = "publish";
62     private static final String DEFAULT_FEED_ID = "1";
63
64     private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
65
66     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
67     private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
68     private WebClient webClientMock = mock(WebClient.class);
69     private RequestBodyUriSpec requestBodyUriSpecMock;
70     private ResponseSpec responseSpecMock;
71
72
73     @BeforeEach
74     void setUp() {
75         when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
76         when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTP_SCHEME);
77         when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
78         when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("DATAFILE");
79         when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("DATAFILE");
80         when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
81         when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
82
83         dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
84
85         webClientMock = spy(WebClient.builder()
86                 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
87                 .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
88                         dmaapPublisherConfigurationMock.dmaapUserPassword()))
89                 .build());
90         requestBodyUriSpecMock = mock(RequestBodyUriSpec.class);
91         responseSpecMock = mock(ResponseSpec.class);
92     }
93
94     @Test
95     void getHttpResponse_Success() {
96         mockWebClientDependantObject();
97         dmaapProducerReactiveHttpClient.createDmaapWebClient(webClientMock);
98         List<ConsumerDmaapModel> consumerDmaapModelList = new ArrayList<ConsumerDmaapModel>();
99         consumerDmaapModelList.add(consumerDmaapModel);
100
101         dmaapProducerReactiveHttpClient.getDmaapProducerResponse(Mono.just(consumerDmaapModelList));
102
103         verify(requestBodyUriSpecMock).header(HttpHeaders.CONTENT_TYPE, APPLICATION_OCTET_STREAM_CONTENT_TYPE);
104         JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
105         metaData.getAsJsonObject().remove(LOCATION_JSON_TAG);
106         verify(requestBodyUriSpecMock).header(X_ATT_DR_META, metaData.toString());
107         URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTP_SCHEME).host(HOST).port(PORT)
108                 .path(PUBLISH_TOPIC + "/" + DEFAULT_FEED_ID + "/" + FILE_NAME).build();
109         verify(requestBodyUriSpecMock).uri(expectedUri);
110         verify(requestBodyUriSpecMock).body(any());
111     }
112
113     private void mockWebClientDependantObject() {
114         when(webClientMock.post()).thenReturn(requestBodyUriSpecMock);
115         when(requestBodyUriSpecMock.uri((URI) any())).thenReturn(requestBodyUriSpecMock);
116
117         when(requestBodyUriSpecMock.retrieve()).thenReturn(responseSpecMock);
118         when(responseSpecMock.onStatus(any(), any())).thenReturn(responseSpecMock);
119         Mono<String> expectedResult = Mono.just("200");
120         when(responseSpecMock.bodyToMono(String.class)).thenReturn(expectedResult);
121     }
122 }