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.io.IFileSystemResource;
40 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
41 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
42 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
43 import org.onap.dcaegen2.collectors.datafile.web.IRestTemplate;
44 import org.springframework.http.HttpEntity;
45 import org.springframework.http.HttpHeaders;
46 import org.springframework.http.HttpMethod;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.MediaType;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.web.util.DefaultUriBuilderFactory;
52 import reactor.test.StepVerifier;
55 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
56 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
58 class DmaapProducerReactiveHttpClientTest {
60 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
61 private static final String LOCATION_JSON_TAG = "location";
62 private static final String NAME_JSON_TAG = "name";
63 private static final String X_ATT_DR_META = "X-ATT-DR-META";
65 private static final String HOST = "54.45.33.2";
66 private static final String HTTPS_SCHEME = "https";
67 private static final int PORT = 1234;
68 private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
69 private static final String URI_SEPARATOR = "/";
70 private static final String PUBLISH_TOPIC = "publish";
71 private static final String DEFAULT_FEED_ID = "1";
72 private static final String FILE_CONTENT = "Just a string.";
74 private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
76 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
77 private ConsumerDmaapModel consumerDmaapModel;
79 private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class);
80 private IRestTemplate restTemplateMock = mock(IRestTemplate.class);
81 private ResponseEntity<String> responseEntityMock = mock(ResponseEntity.class);
82 private InputStream fileStream;
87 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
88 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
89 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
90 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin");
91 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin");
92 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
93 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
95 consumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(FILE_NAME)
96 .location("target/A20161224.1030-1045.bin.gz").compression("gzip")
97 .fileFormatType("org.3GPP.32.435#measCollec").fileFormatVersion("V10").build();
99 dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
100 dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
101 dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock);
105 void getHttpResponse_Success() throws Exception {
106 mockWebClientDependantObject(true);
108 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
109 .expectNext(HttpStatus.OK.toString()).verifyComplete();
111 URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT)
112 .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build();
114 HttpHeaders headers = new HttpHeaders();
116 headers.setContentType(MediaType.parseMediaType(APPLICATION_OCTET_STREAM_CONTENT_TYPE));
118 JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
119 metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
120 metaData.getAsJsonObject().remove(LOCATION_JSON_TAG);
121 headers.set(X_ATT_DR_META, metaData.toString());
123 String plainCreds = "dradmin" + ":" + "dradmin";
124 byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
125 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
126 String base64Creds = new String(base64CredsBytes);
127 headers.add("Authorization", "Basic " + base64Creds);
131 HttpEntity<byte[]> requestEntity = new HttpEntity<>(IOUtils.toByteArray(fileStream), headers);
132 verify(fileSystemResourceMock).setPath("target/" + FILE_NAME);
133 verify(restTemplateMock).exchange(expectedUri, HttpMethod.PUT, requestEntity, String.class);
134 verifyNoMoreInteractions(restTemplateMock);
138 void getHttpResponse_Fail() throws Exception {
139 mockWebClientDependantObject(false);
141 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
145 private void mockWebClientDependantObject(boolean success) throws IOException {
146 fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
147 when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
150 when(restTemplateMock.exchange(any(), any(), any(), any())).thenReturn(responseEntityMock);
151 when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK);
153 when(restTemplateMock.exchange(any(), any(), any(), any())).thenThrow(new RuntimeException());