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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.collectors.datafile.tasks;
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;
35 import java.io.ByteArrayInputStream;
36 import java.io.InputStream;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.HashMap;
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;
60 * @author <a href="mailto:maxime.bonneau@est.tech">Maxime Bonneau</a>
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;
74 private static final Map<String, String> CONTEXT_MAP = new HashMap<>();
76 private static DmaapPublisherConfiguration publisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
77 private static AppConfig appConfigMock;
78 private DmaapProducerReactiveHttpClient httpClientMock = mock(DmaapProducerReactiveHttpClient.class);
80 private PublishedChecker publishedCheckerUnderTestSpy;
83 * Sets up data for the tests.
86 public static void setUp() {
87 when(publisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
88 when(publisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
89 when(publisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
91 appConfigMock = mock(AppConfig.class);
92 when(appConfigMock.getDmaapPublisherConfiguration()).thenReturn(publisherConfigurationMock);
96 public void executeWhenNotPublished_returnsFalse() throws Exception {
97 prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, null);
99 boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
101 assertFalse(isPublished);
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);
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));
124 public void executeWhenDataRouterReturnsNok_returnsFalse() throws Exception {
125 prepareMocksForTests(HttpUtils.SC_BAD_REQUEST, EMPTY_CONTENT, null);
127 boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
129 assertFalse(isPublished);
133 public void executeWhenPublished_returnsTrue() throws Exception {
134 prepareMocksForTests(HttpUtils.SC_OK, "[" + LOCAL_FILE_NAME + "]", null);
136 boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
138 assertTrue(isPublished);
142 public void executeWhenErrorInDataRouter_returnsFalse() throws Exception {
143 prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, new DatafileTaskException(""));
145 boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
147 assertFalse(isPublished);
150 final void prepareMocksForTests(int responseCode, String content, Exception exception) throws Exception {
151 publishedCheckerUnderTestSpy = spy(new PublishedChecker(appConfigMock));
153 doReturn(publisherConfigurationMock).when(publishedCheckerUnderTestSpy).resolveConfiguration();
154 doReturn(httpClientMock).when(publishedCheckerUnderTestSpy).resolveClient();
156 UriBuilder uriBuilder = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT);
157 when(httpClientMock.getBaseUri()).thenReturn(uriBuilder);
159 HttpResponse httpResponseMock = mock(HttpResponse.class);
160 if (exception == null) {
161 when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), anyInt(), any()))
162 .thenReturn(httpResponseMock);
164 when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), anyInt(), any()))
165 .thenThrow(exception);
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);