5f4c1a583071a716ebd38a7859ae02f4bacbe152
[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.Flux;
46 import reactor.test.StepVerifier;
47
48 /**
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>
51  */
52 class DmaapProducerReactiveHttpClientTest {
53
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";
58
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";
65
66     private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
67
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;
73
74
75     @BeforeEach
76     void setUp() {
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);
84
85         dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
86
87         webClientMock = spy(WebClient.builder()
88                 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
89                 .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
90                         dmaapPublisherConfigurationMock.dmaapUserPassword()))
91                 .build());
92         requestBodyUriSpecMock = mock(RequestBodyUriSpec.class);
93         responseSpecMock = mock(ResponseSpec.class);
94     }
95
96     @Test
97     void getHttpResponse_Success() {
98         mockWebClientDependantObject();
99         dmaapProducerReactiveHttpClient.createDmaapWebClient(webClientMock);
100         List<ConsumerDmaapModel> consumerDmaapModelList = new ArrayList<ConsumerDmaapModel>();
101         consumerDmaapModelList.add(consumerDmaapModel);
102
103         StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
104                 .expectNext("200").verifyComplete();
105
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());
115     }
116
117     private void mockWebClientDependantObject() {
118         when(webClientMock.post()).thenReturn(requestBodyUriSpecMock);
119         when(requestBodyUriSpecMock.uri((URI) any())).thenReturn(requestBodyUriSpecMock);
120
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);
125     }
126 }