3e3c2ed6d552f6ff8715874c8dac4556619abfff
[dcaegen2/collectors/datafile.git] /
1 /*-
2 * ============LICENSE_START=======================================================
3 *  Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
19 */
20
21 package org.onap.dcaegen2.collectors.datafile.tasks;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoMoreInteractions;
33 import static org.mockito.Mockito.when;
34
35 import java.io.ByteArrayInputStream;
36 import java.io.InputStream;
37 import java.net.URI;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import org.apache.http.HttpEntity;
44 import org.apache.http.HttpResponse;
45 import org.apache.http.StatusLine;
46 import org.apache.http.client.methods.HttpGet;
47 import org.apache.http.client.methods.HttpUriRequest;
48 import org.junit.jupiter.api.BeforeAll;
49 import org.junit.jupiter.api.Test;
50 import org.mockito.ArgumentCaptor;
51 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
52 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
53 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
54 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerReactiveHttpClient;
55 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
56 import org.springframework.web.util.DefaultUriBuilderFactory;
57 import org.springframework.web.util.UriBuilder;
58
59 /**
60  * @author <a href="mailto:maxime.bonneau@est.tech">Maxime Bonneau</a>
61  *
62  */
63 public class PublishedCheckerTest {
64     private static final String EMPTY_CONTENT = "[]";
65     private static final String FEEDLOG_TOPIC = "feedlog";
66     private static final String FEED_ID = "1";
67     private static final String HTTPS_SCHEME = "https";
68     private static final String HOST = "54.45.33.2";
69     private static final int PORT = 1234;
70     private static final String SOURCE_NAME = "oteNB5309";
71     private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
72     private static final String LOCAL_FILE_NAME = SOURCE_NAME + "_" + FILE_NAME;
73
74     private static final Map<String, String> CONTEXT_MAP = new HashMap<>();
75
76     private static DmaapPublisherConfiguration publisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
77     private static AppConfig appConfigMock;
78     private DmaapProducerReactiveHttpClient httpClientMock = mock(DmaapProducerReactiveHttpClient.class);
79
80     private PublishedChecker publishedCheckerUnderTestSpy;
81
82     /**
83      * Sets up data for the tests.
84      */
85     @BeforeAll
86     public static void setUp() {
87         when(publisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
88         when(publisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
89         when(publisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
90
91         appConfigMock = mock(AppConfig.class);
92         when(appConfigMock.getDmaapPublisherConfiguration()).thenReturn(publisherConfigurationMock);
93     }
94
95     @Test
96     public void executeWhenNotPublished_returnsFalse() throws Exception {
97         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, null);
98
99         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
100
101         assertFalse(isPublished);
102
103         ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
104         verify(httpClientMock).getBaseUri();
105         verify(httpClientMock).addUserCredentialsToHead(any(HttpUriRequest.class));
106         verify(httpClientMock).getDmaapProducerResponseWithCustomTimeout(requestCaptor.capture(), anyInt(), any());
107         verifyNoMoreInteractions(httpClientMock);
108
109         HttpUriRequest getRequest = requestCaptor.getValue();
110         assertTrue(getRequest instanceof HttpGet);
111         URI actualUri = getRequest.getURI();
112         assertEquals(HTTPS_SCHEME, actualUri.getScheme());
113         assertEquals(HOST, actualUri.getHost());
114         assertEquals(PORT, actualUri.getPort());
115         Path actualPath = Paths.get(actualUri.getPath());
116         assertTrue(FEEDLOG_TOPIC.equals(actualPath.getName(0).toString()));
117         assertTrue(FEED_ID.equals(actualPath.getName(1).toString()));
118         String actualQuery = actualUri.getQuery();
119         assertTrue(actualQuery.contains("type=pub"));
120         assertTrue(actualQuery.contains("filename=" + LOCAL_FILE_NAME));
121     }
122
123     @Test
124     public void executeWhenDataRouterReturnsNok_returnsFalse() throws Exception {
125         prepareMocksForTests(HttpUtils.SC_BAD_REQUEST, EMPTY_CONTENT, null);
126
127         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
128
129         assertFalse(isPublished);
130     }
131
132     @Test
133     public void executeWhenPublished_returnsTrue() throws Exception {
134         prepareMocksForTests(HttpUtils.SC_OK, "[" + LOCAL_FILE_NAME + "]", null);
135
136         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
137
138         assertTrue(isPublished);
139     }
140
141     @Test
142     public void executeWhenErrorInDataRouter_returnsFalse() throws Exception {
143         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, new DatafileTaskException(""));
144
145         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
146
147         assertFalse(isPublished);
148     }
149
150     final void prepareMocksForTests(int responseCode, String content, Exception exception) throws Exception {
151         publishedCheckerUnderTestSpy = spy(new PublishedChecker(appConfigMock));
152
153         doReturn(publisherConfigurationMock).when(publishedCheckerUnderTestSpy).resolveConfiguration();
154         doReturn(httpClientMock).when(publishedCheckerUnderTestSpy).resolveClient();
155
156         UriBuilder uriBuilder = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT);
157         when(httpClientMock.getBaseUri()).thenReturn(uriBuilder);
158
159         HttpResponse httpResponseMock = mock(HttpResponse.class);
160         if (exception == null) {
161             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), anyInt(), any()))
162                     .thenReturn(httpResponseMock);
163         } else {
164             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), anyInt(), any()))
165                     .thenThrow(exception);
166         }
167         HttpEntity httpEntityMock = mock(HttpEntity.class);
168         StatusLine statusLineMock = mock(StatusLine.class);
169         when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
170         when(statusLineMock.getStatusCode()).thenReturn(responseCode);
171         when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
172         InputStream stream = new ByteArrayInputStream(content.getBytes());
173         when(httpEntityMock.getContent()).thenReturn(stream);
174     }
175 }