83643637c873303b07c2fa5f40227b03a54c16d6
[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.Mockito.doReturn;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.verifyNoMoreInteractions;
32 import static org.mockito.Mockito.when;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.InputStream;
36 import java.net.URI;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.HashMap;
40 import java.util.Map;
41 import org.apache.http.HttpEntity;
42 import org.apache.http.HttpResponse;
43 import org.apache.http.StatusLine;
44 import org.apache.http.client.methods.HttpGet;
45 import org.apache.http.client.methods.HttpUriRequest;
46 import org.junit.jupiter.api.BeforeAll;
47 import org.junit.jupiter.api.Test;
48 import org.mockito.ArgumentCaptor;
49 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
50 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
51 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
52 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerHttpClient;
53 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
54 import org.springframework.web.util.DefaultUriBuilderFactory;
55 import org.springframework.web.util.UriBuilder;
56
57 public class PublishedCheckerTest {
58     private static final String EMPTY_CONTENT = "[]";
59     private static final String FEEDLOG_TOPIC = "feedlog";
60     private static final String FEED_ID = "1";
61     private static final String HTTPS_SCHEME = "https";
62     private static final String HOST = "54.45.33.2";
63     private static final int PORT = 1234;
64     private static final String SOURCE_NAME = "oteNB5309";
65     private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
66     private static final String LOCAL_FILE_NAME = SOURCE_NAME + "_" + FILE_NAME;
67
68     private static final Map<String, String> CONTEXT_MAP = new HashMap<>();
69
70     private static DmaapPublisherConfiguration publisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
71     private static AppConfig appConfigMock;
72     private DmaapProducerHttpClient httpClientMock = mock(DmaapProducerHttpClient.class);
73
74     private PublishedChecker publishedCheckerUnderTestSpy;
75
76     /**
77      * Sets up data for the tests.
78      */
79     @BeforeAll
80     public static void setUp() {
81         when(publisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
82         when(publisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
83         when(publisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
84
85         appConfigMock = mock(AppConfig.class);
86         when(appConfigMock.getDmaapPublisherConfiguration()).thenReturn(publisherConfigurationMock);
87     }
88
89     @Test
90     public void executeWhenNotPublished_returnsFalse() throws Exception {
91         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, null);
92
93         boolean isPublished = publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CONTEXT_MAP);
94
95         assertFalse(isPublished);
96
97         ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
98         verify(httpClientMock).getBaseUri();
99         verify(httpClientMock).addUserCredentialsToHead(any(HttpUriRequest.class));
100         verify(httpClientMock).getDmaapProducerResponseWithCustomTimeout(requestCaptor.capture(), any(), any());
101         verifyNoMoreInteractions(httpClientMock);
102
103         HttpUriRequest getRequest = requestCaptor.getValue();
104         assertTrue(getRequest instanceof HttpGet);
105         URI actualUri = getRequest.getURI();
106         assertEquals(HTTPS_SCHEME, actualUri.getScheme());
107         assertEquals(HOST, actualUri.getHost());
108         assertEquals(PORT, actualUri.getPort());
109         Path actualPath = Paths.get(actualUri.getPath());
110         assertTrue(FEEDLOG_TOPIC.equals(actualPath.getName(0).toString()));
111         assertTrue(FEED_ID.equals(actualPath.getName(1).toString()));
112         String actualQuery = actualUri.getQuery();
113         assertTrue(actualQuery.contains("type=pub"));
114         assertTrue(actualQuery.contains("filename=" + LOCAL_FILE_NAME));
115     }
116
117     @Test
118     public void executeWhenDataRouterReturnsNok_returnsFalse() throws Exception {
119         prepareMocksForTests(HttpUtils.SC_BAD_REQUEST, EMPTY_CONTENT, null);
120
121         boolean isPublished = publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CONTEXT_MAP);
122
123         assertFalse(isPublished);
124     }
125
126     @Test
127     public void executeWhenPublished_returnsTrue() throws Exception {
128         prepareMocksForTests(HttpUtils.SC_OK, "[" + LOCAL_FILE_NAME + "]", null);
129
130         boolean isPublished = publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CONTEXT_MAP);
131
132         assertTrue(isPublished);
133     }
134
135     @Test
136     public void executeWhenErrorInDataRouter_returnsFalse() throws Exception {
137         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, new DatafileTaskException(""));
138
139         boolean isPublished = publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CONTEXT_MAP);
140
141         assertFalse(isPublished);
142     }
143
144     final void prepareMocksForTests(int responseCode, String content, Exception exception) throws Exception {
145         publishedCheckerUnderTestSpy = spy(new PublishedChecker(appConfigMock));
146
147         doReturn(publisherConfigurationMock).when(publishedCheckerUnderTestSpy).resolveConfiguration();
148         doReturn(httpClientMock).when(publishedCheckerUnderTestSpy).resolveClient();
149
150         UriBuilder uriBuilder = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT);
151         when(httpClientMock.getBaseUri()).thenReturn(uriBuilder);
152
153         HttpResponse httpResponseMock = mock(HttpResponse.class);
154         if (exception == null) {
155             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
156                     .thenReturn(httpResponseMock);
157         } else {
158             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
159                     .thenThrow(exception);
160         }
161         HttpEntity httpEntityMock = mock(HttpEntity.class);
162         StatusLine statusLineMock = mock(StatusLine.class);
163         when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
164         when(statusLineMock.getStatusCode()).thenReturn(responseCode);
165         when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
166         InputStream stream = new ByteArrayInputStream(content.getBytes());
167         when(httpEntityMock.getContent()).thenReturn(stream);
168     }
169 }