5dbc908954a42206b9ca6dbb7046eaa0508a9c92
[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.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URI;
29
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
33 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
34 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.util.DefaultUriBuilderFactory;
39
40 import reactor.test.StepVerifier;
41
42 /**
43  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
44  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
45  */
46 class DmaapProducerReactiveHttpClientTest {
47
48     private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
49     private static final String LOCATION_JSON_TAG = "location";
50     private static final String NAME_JSON_TAG = "name";
51     private static final String X_ATT_DR_META = "X-ATT-DR-META";
52
53     private static final String HOST = "54.45.33.2";
54     private static final String HTTPS_SCHEME = "https";
55     private static final int PORT = 1234;
56     private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream";
57     private static final String URI_SEPARATOR = "/";
58     private static final String PUBLISH_TOPIC = "publish";
59     private static final String DEFAULT_FEED_ID = "1";
60     private static final String FILE_CONTENT = "Just a string.";
61
62     private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
63
64     private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
65     private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
66
67     private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class);
68     private IRestTemplate restTemplateMock = mock(IRestTemplate.class);
69     private ResponseEntity<String> responseEntityMock = mock(ResponseEntity.class);
70
71
72     @BeforeEach
73     void setUp() {
74         when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
75         when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
76         when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
77         when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin");
78         when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin");
79         when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE);
80         when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC);
81
82         dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
83         dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock);
84         dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock);
85     }
86
87     @Test
88     void getHttpResponse_Success() throws Exception {
89         mockWebClientDependantObject();
90
91         StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel))
92                 .expectNext(HttpStatus.OK.toString()).verifyComplete();
93
94         URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT)
95                 .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build();
96
97         verify(restTemplateMock)
98                 .exchange(eq(expectedUri), eq(HttpMethod.PUT), any(), eq(String.class));
99     }
100
101     private void mockWebClientDependantObject() throws IOException {
102         InputStream fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes());
103         when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream);
104
105         when(restTemplateMock.exchange(any(), any(), any(), any()))
106                 .thenReturn(responseEntityMock);
107         when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK);
108     }
109 }