d8296e1c2665b14ab3e4c2fa2e24a0811fa4bd55
[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 INTERNAL_LOCATION_JSON_TAG = "internalLocation";
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         // @formatter:off
96         consumerDmaapModel = ImmutableConsumerDmaapModel.builder()
97                 .productName("NrRadio")
98                 .vendorName("Ericsson")
99                 .lastEpochMicrosec("8745745764578")
100                 .sourceName("oteNB5309")
101                 .startEpochMicrosec("8745745764578")
102                 .timeZoneOffset("UTC+05:00")
103                 .name("A20161224.1030-1045.bin.gz")
104                 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1145.bin.gz")
105                 .internalLocation("target/A20161224.1030-1045.bin.gz")
106                 .compression("gzip")
107                 .fileFormatType("org.3GPP.32.435#measCollec")
108                 .fileFormatVersion("V10")
109                 .build();
110         //formatter:on
111
112         dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
113         dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
114         dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock);
115     }
116
117     @Test
118     void getHttpResponse_Success() throws Exception {
119         mockWebClientDependantObject(true);
120
121         StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
122                 .expectNext(HttpStatus.OK.toString()).verifyComplete();
123
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();
126
127         HttpHeaders headers = new HttpHeaders();
128
129         headers.setContentType(MediaType.parseMediaType(APPLICATION_OCTET_STREAM_CONTENT_TYPE));
130
131         JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel));
132         metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
133         metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
134         headers.set(X_ATT_DR_META, metaData.toString());
135
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         headers.add("Authorization", "Basic " + base64Creds);
141
142         fileStream.reset();
143
144         HttpEntity<byte[]> requestEntity = new HttpEntity<>(IOUtils.toByteArray(fileStream), headers);
145         verify(fileSystemResourceMock).setPath("target/" + FILE_NAME);
146         verify(restTemplateMock).exchange(expectedUri, HttpMethod.PUT, requestEntity, String.class);
147         verifyNoMoreInteractions(restTemplateMock);
148     }
149
150     @Test
151     void getHttpResponse_Fail() throws Exception {
152         mockWebClientDependantObject(false);
153
154         StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
155                 .verifyComplete();
156     }
157
158     private void mockWebClientDependantObject(boolean success) throws IOException {
159         fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
160         when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
161
162         if (success) {
163             when(restTemplateMock.exchange(any(), any(), any(), any())).thenReturn(responseEntityMock);
164             when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK);
165         } else {
166             when(restTemplateMock.exchange(any(), any(), any(), any())).thenThrow(new RuntimeException());
167         }
168     }
169 }