87c99f88854280e10079cd7492721e182a4106e0
[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.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;
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;
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         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();
98
99         dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
100         dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
101         dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock);
102     }
103
104     @Test
105     void getHttpResponse_Success() throws Exception {
106         mockWebClientDependantObject(true);
107
108         StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
109                 .expectNext(HttpStatus.OK.toString()).verifyComplete();
110
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();
113
114         HttpHeaders headers = new HttpHeaders();
115
116         headers.setContentType(MediaType.parseMediaType(APPLICATION_OCTET_STREAM_CONTENT_TYPE));
117
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());
122
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);
128
129         fileStream.reset();
130
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);
135     }
136
137     @Test
138     void getHttpResponse_Fail() throws Exception {
139         mockWebClientDependantObject(false);
140
141         StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
142                 .verifyComplete();
143     }
144
145     private void mockWebClientDependantObject(boolean success) throws IOException {
146         fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
147         when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
148
149         if (success) {
150             when(restTemplateMock.exchange(any(), any(), any(), any())).thenReturn(responseEntityMock);
151             when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK);
152         } else {
153             when(restTemplateMock.exchange(any(), any(), any(), any())).thenThrow(new RuntimeException());
154         }
155     }
156 }