213e8d774f81245edd987bd3f49fa7802c8f69cf
[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.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.List;
33
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;
45
46 import reactor.core.publisher.Mono;
47
48 /**
49  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
50  */
51 class DmaapProducerReactiveHttpClientTest {
52
53     private static final String LOCATION = "location";
54     private static final String X_ATT_DR_META = "X-ATT-DR-META";
55
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";
61
62     private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
63
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;
69
70
71     @BeforeEach
72     void setUp() {
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);
80
81         dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
82
83         webClientMock = spy(WebClient.builder()
84                 .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
85                 .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
86                         dmaapPublisherConfigurationMock.dmaapUserPassword()))
87                 .build());
88         requestBodyUriSpecMock = mock(RequestBodyUriSpec.class);
89         responseSpecMock = mock(ResponseSpec.class);
90     }
91
92     @Test
93     void getHttpResponse_Success() {
94         // given
95
96         // when
97         mockWebClientDependantObject();
98         dmaapProducerReactiveHttpClient.createDmaapWebClient(webClientMock);
99         List<ConsumerDmaapModel> consumerDmaapModelList = new ArrayList<ConsumerDmaapModel>();
100         consumerDmaapModelList.add(consumerDmaapModel);
101
102         dmaapProducerReactiveHttpClient.getDmaapProducerResponse(Mono.just(consumerDmaapModelList));
103
104         // then
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;
110         try {
111             expectedUri = new URIBuilder().setScheme(HTTPS_SCHEME).setHost(HOST).setPort(1234).setPath(FILE_READY_TOPIC)
112                     .build();
113         } catch (URISyntaxException e) {
114             // Nothing
115         }
116         verify(requestBodyUriSpecMock).uri(expectedUri);
117         verify(requestBodyUriSpecMock).body(any());
118     }
119
120     private void mockWebClientDependantObject() {
121         when(webClientMock.post()).thenReturn(requestBodyUriSpecMock);
122         when(requestBodyUriSpecMock.uri((URI) any())).thenReturn(requestBodyUriSpecMock);
123
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);
128     }
129 }