2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 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.when;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Paths;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.Future;
36 import org.apache.commons.codec.binary.Base64;
37 import org.apache.commons.io.IOUtils;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.StatusLine;
40 import org.apache.http.client.methods.HttpPut;
41 import org.apache.http.entity.ByteArrayEntity;
42 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.onap.dcaegen2.collectors.datafile.io.IFileSystemResource;
46 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
47 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
48 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
49 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
50 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
51 import org.springframework.http.HttpHeaders;
52 import org.springframework.http.HttpStatus;
53 import org.springframework.web.util.DefaultUriBuilderFactory;
55 import reactor.test.StepVerifier;
58 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
59 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
61 class DmaapProducerReactiveHttpClientTest {
63 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
64 private static final String INTERNAL_LOCATION_JSON_TAG = "internalLocation";
65 private static final String NAME_JSON_TAG = "name";
66 private static final String X_DMAAP_DR_META = "X-DMAAP-DR-META";
68 private static final String HOST = "54.45.33.2";
69 private static final String HTTPS_SCHEME = "https";
70 private static final int PORT = 1234;
71 private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
72 private static final String URI_SEPARATOR = "/";
73 private static final String PUBLISH_TOPIC = "publish";
74 private static final String DEFAULT_FEED_ID = "1";
75 private static final String FILE_CONTENT = "Just a string.";
77 private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
79 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
80 private ConsumerDmaapModel consumerDmaapModel;
82 private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class);
83 private CloseableHttpAsyncClient clientMock;
84 private HttpResponse responseMock = mock(HttpResponse.class);
85 private Future<HttpResponse> futureMock = mock(Future.class);
86 private StatusLine statusLine = mock(StatusLine.class);
87 private InputStream fileStream;
91 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
92 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
93 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
94 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin");
95 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin");
96 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
97 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
100 consumerDmaapModel = ImmutableConsumerDmaapModel.builder()
101 .productName("NrRadio")
102 .vendorName("Ericsson")
103 .lastEpochMicrosec("8745745764578")
104 .sourceName("oteNB5309")
105 .startEpochMicrosec("8745745764578")
106 .timeZoneOffset("UTC+05:00")
107 .name("A20161224.1030-1045.bin.gz")
108 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1145.bin.gz")
109 .internalLocation("target/A20161224.1030-1045.bin.gz")
111 .fileFormatType("org.3GPP.32.435#measCollec")
112 .fileFormatVersion("V10")
116 dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
117 dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
118 clientMock = mock(CloseableHttpAsyncClient.class);
119 dmaapProducerReactiveHttpClient.setWebClient(clientMock);
123 void getHttpResponse_Success() throws Exception {
124 mockWebClientDependantObject(true);
126 HttpPut httpPut = new HttpPut();
127 httpPut.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_OCTET_STREAM_CONTENT_TYPE);
129 URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT)
130 .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build();
131 httpPut.setURI(expectedUri);
133 JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
134 metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
135 metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
136 httpPut.addHeader(X_DMAAP_DR_META, metaData.toString());
138 String plainCreds = "dradmin" + ":" + "dradmin";
139 byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
140 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
141 String base64Creds = new String(base64CredsBytes);
142 httpPut.addHeader("Authorization", "Basic " + base64Creds);
145 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
146 .expectNext(HttpStatus.OK).verifyComplete();
148 verify(fileSystemResourceMock).setPath(Paths.get("target/" + FILE_NAME));
149 InputStream fileInputStream = fileSystemResourceMock.getInputStream();
150 httpPut.setEntity(new ByteArrayEntity(IOUtils.toByteArray(fileInputStream)));
154 void getHttpResponse_Fail() throws Exception {
155 mockWebClientDependantObject(false);
156 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
161 private void mockWebClientDependantObject(boolean success)
162 throws IOException, InterruptedException, ExecutionException {
163 fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
164 when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
166 when(clientMock.execute(any(HttpPut.class), any())).thenReturn(futureMock);
167 when(futureMock.get()).thenReturn(responseMock);
168 when(responseMock.getStatusLine()).thenReturn(statusLine);
169 when(statusLine.getStatusCode()).thenReturn(HttpUtils.SC_OK);
171 when(clientMock.execute(any(HttpPut.class), any())).thenReturn(futureMock);
172 when(futureMock.get()).thenThrow(new InterruptedException());