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.verify;
22 import static org.mockito.Mockito.verifyNoMoreInteractions;
23 import static org.mockito.Mockito.when;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
34 import org.apache.commons.codec.binary.Base64;
35 import org.apache.commons.io.IOUtils;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
39 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
40 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
41 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest;
42 import org.springframework.http.HttpEntity;
43 import org.springframework.http.HttpHeaders;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.MediaType;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.web.util.DefaultUriBuilderFactory;
50 import reactor.test.StepVerifier;
53 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
54 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
56 class DmaapProducerReactiveHttpClientTest {
58 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
59 private static final String LOCATION_JSON_TAG = "location";
60 private static final String NAME_JSON_TAG = "name";
61 private static final String X_ATT_DR_META = "X-ATT-DR-META";
63 private static final String HOST = "54.45.33.2";
64 private static final String HTTPS_SCHEME = "https";
65 private static final int PORT = 1234;
66 private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
67 private static final String URI_SEPARATOR = "/";
68 private static final String PUBLISH_TOPIC = "publish";
69 private static final String DEFAULT_FEED_ID = "1";
70 private static final String FILE_CONTENT = "Just a string.";
72 private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
74 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
75 private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
77 private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class);
78 private IRestTemplate restTemplateMock = mock(IRestTemplate.class);
79 private ResponseEntity<String> responseEntityMock = mock(ResponseEntity.class);
80 private InputStream fileStream;
85 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
86 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
87 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
88 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin");
89 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin");
90 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
91 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
93 dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
94 dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
95 dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock);
99 void getHttpResponse_Success() throws Exception {
100 mockWebClientDependantObject();
102 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
103 .expectNext(HttpStatus.OK.toString()).verifyComplete();
105 URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT)
106 .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build();
108 HttpHeaders headers = new HttpHeaders();
110 headers.setContentType(MediaType.parseMediaType(APPLICATION_OCTET_STREAM_CONTENT_TYPE));
112 JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
113 metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
114 metaData.getAsJsonObject().remove(LOCATION_JSON_TAG);
115 headers.set(X_ATT_DR_META, metaData.toString());
117 String plainCreds = "dradmin" + ":" + "dradmin";
118 byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
119 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
120 String base64Creds = new String(base64CredsBytes);
121 headers.add("Authorization", "Basic " + base64Creds);
125 HttpEntity<byte[]> requestEntity = new HttpEntity<>(IOUtils.toByteArray(fileStream), headers);
126 verify(restTemplateMock).exchange(expectedUri, HttpMethod.PUT, requestEntity, String.class);
127 verifyNoMoreInteractions(restTemplateMock);
130 private void mockWebClientDependantObject() throws IOException {
131 fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
132 when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
134 when(restTemplateMock.exchange(any(), any(), any(), any())).thenReturn(responseEntityMock);
135 when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK);