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;
44 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
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.springframework.http.HttpHeaders;
51 import org.springframework.web.util.DefaultUriBuilderFactory;
53 import reactor.test.StepVerifier;
56 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
57 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
59 class DmaapProducerReactiveHttpClientTest {
61 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
62 private static final String INTERNAL_LOCATION_JSON_TAG = "internalLocation";
63 private static final String NAME_JSON_TAG = "name";
64 private static final String X_ATT_DR_META = "X-ATT-DR-META";
66 private static final String HOST = "54.45.33.2";
67 private static final String HTTPS_SCHEME = "https";
68 private static final int PORT = 1234;
69 private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
70 private static final String URI_SEPARATOR = "/";
71 private static final String PUBLISH_TOPIC = "publish";
72 private static final String DEFAULT_FEED_ID = "1";
73 private static final String FILE_CONTENT = "Just a string.";
75 private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
77 private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
78 private ConsumerDmaapModel consumerDmaapModel;
80 private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class);
81 private CloseableHttpAsyncClient clientMock;
82 private HttpResponse responseMock = mock(HttpResponse.class);
83 private Future<HttpResponse> futureMock = mock(Future.class);
84 private StatusLine statusLine = mock(StatusLine.class);
85 private InputStream fileStream;
89 when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
90 when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
91 when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
92 when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin");
93 when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin");
94 when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
95 when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
98 consumerDmaapModel = ImmutableConsumerDmaapModel.builder()
99 .productName("NrRadio")
100 .vendorName("Ericsson")
101 .lastEpochMicrosec("8745745764578")
102 .sourceName("oteNB5309")
103 .startEpochMicrosec("8745745764578")
104 .timeZoneOffset("UTC+05:00")
105 .name("A20161224.1030-1045.bin.gz")
106 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1145.bin.gz")
107 .internalLocation("target/A20161224.1030-1045.bin.gz")
109 .fileFormatType("org.3GPP.32.435#measCollec")
110 .fileFormatVersion("V10")
114 dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
115 dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
116 clientMock = mock(CloseableHttpAsyncClient.class);
117 dmaapProducerReactiveHttpClient.setWebClient(clientMock);
121 void getHttpResponse_Success() throws Exception {
122 mockWebClientDependantObject(true);
124 URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT)
125 .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build();
127 HttpPut httpPut = new HttpPut();
128 httpPut.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_OCTET_STREAM_CONTENT_TYPE);
130 JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
131 metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
132 metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
133 httpPut.addHeader(X_ATT_DR_META, metaData.toString());
134 httpPut.setURI(expectedUri);
136 String plainCreds = "dradmin" + ":" + "dradmin";
137 byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
138 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
139 String base64Creds = new String(base64CredsBytes);
140 httpPut.addHeader("Authorization", "Basic " + base64Creds);
143 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
144 .expectNext(responseMock.toString()).verifyComplete();
146 verify(fileSystemResourceMock).setPath("target/" + FILE_NAME);
147 InputStream fileInputStream = fileSystemResourceMock.getInputStream();
148 httpPut.setEntity(new ByteArrayEntity(IOUtils.toByteArray(fileInputStream)));
152 void getHttpResponse_Fail() throws Exception {
153 mockWebClientDependantObject(false);
154 StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
158 private void mockWebClientDependantObject(boolean success)
159 throws IOException, InterruptedException, ExecutionException {
160 fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
161 when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
163 when(clientMock.execute(any(HttpPut.class), any())).thenReturn(futureMock);
164 when(futureMock.get()).thenReturn(responseMock);
165 when(responseMock.getStatusLine()).thenReturn(statusLine);
166 when(statusLine.getStatusCode()).thenReturn(HttpUtils.SC_OK);
168 when(clientMock.execute(any(HttpPut.class), any())).thenReturn(futureMock);
169 when(futureMock.get()).thenThrow(new InterruptedException());