ba424626a8a834b5e81501694254288a715d7cee
[dcaegen2/collectors/datafile.git] /
1 /*
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.service.producer;
18
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;
24
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.net.URI;
32 import java.nio.charset.StandardCharsets;
33
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.ConsumerDmaapModelForUnitTest;
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;
51
52 import reactor.test.StepVerifier;
53
54 /**
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>
57  */
58 class DmaapProducerReactiveHttpClientTest {
59
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";
64
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.";
73
74     private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
75
76     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
77     private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
78
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;
83
84
85     @BeforeEach
86     void setUp() {
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);
94
95         dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
96         dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
97         dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock);
98     }
99
100     @Test
101     void getHttpResponse_Success() throws Exception {
102         mockWebClientDependantObject();
103
104         StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
105                 .expectNext(HttpStatus.OK.toString()).verifyComplete();
106
107         URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT)
108                 .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build();
109
110         HttpHeaders headers = new HttpHeaders();
111
112         headers.setContentType(MediaType.parseMediaType(APPLICATION_OCTET_STREAM_CONTENT_TYPE));
113
114         JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
115         metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
116         metaData.getAsJsonObject().remove(LOCATION_JSON_TAG);
117         headers.set(X_ATT_DR_META, metaData.toString());
118
119         String plainCreds = "dradmin" + ":" + "dradmin";
120         byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
121         byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
122         String base64Creds = new String(base64CredsBytes);
123         headers.add("Authorization", "Basic " + base64Creds);
124
125         fileStream.reset();
126
127         HttpEntity<byte[]> requestEntity = new HttpEntity<>(IOUtils.toByteArray(fileStream), headers);
128         verify(fileSystemResourceMock).setPath("target/" + FILE_NAME);
129         verify(restTemplateMock).exchange(expectedUri, HttpMethod.PUT, requestEntity, String.class);
130         verifyNoMoreInteractions(restTemplateMock);
131     }
132
133     private void mockWebClientDependantObject() throws IOException {
134         fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
135         when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
136
137         when(restTemplateMock.exchange(any(), any(), any(), any())).thenReturn(responseEntityMock);
138         when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK);
139     }
140 }