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.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.util.concurrent.ExecutionException;
33 import java.util.concurrent.Future;
35 import org.apache.commons.codec.binary.Base64;
36 import org.apache.commons.io.IOUtils;
37 import org.apache.http.HttpResponse;
38 import org.apache.http.StatusLine;
39 import org.apache.http.client.methods.HttpPut;
40 import org.apache.http.entity.ByteArrayEntity;
41 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
42 import org.junit.jupiter.api.BeforeEach;
43 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.web.util.DefaultUriBuilderFactory;
54 import reactor.test.StepVerifier;
57 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
58 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
60 class DmaapProducerReactiveHttpClientTest {
62 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
63 private static final String INTERNAL_LOCATION_JSON_TAG = "internalLocation";
64 private static final String NAME_JSON_TAG = "name";
65 private static final String X_ATT_DR_META = "X-ATT-DR-META";
67 private static final String HOST = "54.45.33.2";
68 private static final String HTTPS_SCHEME = "https";
69 private static final int PORT = 1234;
70 private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
71 private static final String URI_SEPARATOR = "/";
72 private static final String PUBLISH_TOPIC = "publish";
73 private static final String DEFAULT_FEED_ID = "1";
74 private static final String FILE_CONTENT = "Just a string.";
76 private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
78 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
79 private ConsumerDmaapModel consumerDmaapModel;
81 private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class);
82 private CloseableHttpAsyncClient clientMock;
83 private HttpResponse responseMock = mock(HttpResponse.class);
84 private Future<HttpResponse> futureMock = mock(Future.class);
85 private StatusLine statusLine = mock(StatusLine.class);
86 private InputStream fileStream;
90 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
91 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
92 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
93 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin");
94 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin");
95 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
96 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
99 consumerDmaapModel = ImmutableConsumerDmaapModel.builder()
100 .productName("NrRadio")
101 .vendorName("Ericsson")
102 .lastEpochMicrosec("8745745764578")
103 .sourceName("oteNB5309")
104 .startEpochMicrosec("8745745764578")
105 .timeZoneOffset("UTC+05:00")
106 .name("A20161224.1030-1045.bin.gz")
107 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1145.bin.gz")
108 .internalLocation("target/A20161224.1030-1045.bin.gz")
110 .fileFormatType("org.3GPP.32.435#measCollec")
111 .fileFormatVersion("V10")
115 dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
116 dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
117 clientMock = mock(CloseableHttpAsyncClient.class);
118 dmaapProducerReactiveHttpClient.setWebClient(clientMock);
122 void getHttpResponse_Success() throws Exception {
123 mockWebClientDependantObject(true);
125 URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT)
126 .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build();
128 HttpPut httpPut = new HttpPut();
129 httpPut.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_OCTET_STREAM_CONTENT_TYPE);
131 JsonElement metaData = new JsonParser().parse(new CommonFunctions().createJsonBody(consumerDmaapModel));
132 metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
133 metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
134 httpPut.addHeader(X_ATT_DR_META, metaData.toString());
135 httpPut.setURI(expectedUri);
137 String plainCreds = "dradmin" + ":" + "dradmin";
138 byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
139 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
140 String base64Creds = new String(base64CredsBytes);
141 httpPut.addHeader("Authorization", "Basic " + base64Creds);
144 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
145 .expectNext(responseMock.toString()).verifyComplete();
147 verify(fileSystemResourceMock).setPath("target/" + FILE_NAME);
148 InputStream fileInputStream = fileSystemResourceMock.getInputStream();
149 httpPut.setEntity(new ByteArrayEntity(IOUtils.toByteArray(fileInputStream)));
153 void getHttpResponse_Fail() throws Exception {
154 mockWebClientDependantObject(false);
155 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
159 private void mockWebClientDependantObject(boolean success)
160 throws IOException, InterruptedException, ExecutionException {
161 fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
162 when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
164 when(clientMock.execute(any(HttpPut.class), any())).thenReturn(futureMock);
165 when(futureMock.get()).thenReturn(responseMock);
166 when(responseMock.getStatusLine()).thenReturn(statusLine);
167 when(statusLine.getStatusCode()).thenReturn(HttpUtils.SC_OK);
169 when(clientMock.execute(any(HttpPut.class), any())).thenReturn(futureMock);
170 when(futureMock.get()).thenThrow(new InterruptedException());