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.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;
34 import java.io.ByteArrayInputStream;
35 import java.io.InputStream;
37 import java.util.HashMap;
39 import org.apache.http.HttpEntity;
40 import org.apache.http.HttpResponse;
41 import org.apache.http.StatusLine;
42 import org.apache.http.client.methods.HttpGet;
43 import org.apache.http.client.methods.HttpUriRequest;
44 import org.junit.jupiter.api.BeforeAll;
45 import org.junit.jupiter.api.Test;
46 import org.mockito.ArgumentCaptor;
47 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
48 import org.onap.dcaegen2.collectors.datafile.configuration.PublisherConfiguration;
49 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
50 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
51 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerHttpClient;
53 public class PublishedCheckerTest {
54 private static final String PUBLISH_URL = "https://54.45.33.2:1234/";
55 private static final String EMPTY_CONTENT = "[]";
56 private static final String SOURCE_NAME = "oteNB5309";
57 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
58 private static final String LOCAL_FILE_NAME = SOURCE_NAME + "_" + FILE_NAME;
59 private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
60 private static final String LOG_URI = "https://localhost:3907/feedlog/1";
62 private static final Map<String, String> CONTEXT_MAP = new HashMap<>();
64 private static PublisherConfiguration publisherConfigurationMock = mock(PublisherConfiguration.class);
65 private static AppConfig appConfigMock;
66 private DmaapProducerHttpClient httpClientMock = mock(DmaapProducerHttpClient.class);
68 private PublishedChecker publishedCheckerUnderTestSpy;
72 private static void setUp() throws DatafileTaskException {
73 when(publisherConfigurationMock.publishUrl()).thenReturn(PUBLISH_URL);
75 appConfigMock = mock(AppConfig.class);
76 when(appConfigMock.getPublisherConfiguration(CHANGE_IDENTIFIER)).thenReturn(publisherConfigurationMock);
80 public void executeWhenNotPublished_returnsFalse() throws Exception {
81 prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, null);
84 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
86 assertFalse(isPublished);
88 ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
89 verify(httpClientMock).addUserCredentialsToHead(any(HttpUriRequest.class));
90 verify(httpClientMock).getDmaapProducerResponseWithCustomTimeout(requestCaptor.capture(), any(), any());
91 verifyNoMoreInteractions(httpClientMock);
93 HttpUriRequest getRequest = requestCaptor.getValue();
94 assertTrue(getRequest instanceof HttpGet);
95 URI actualUri = getRequest.getURI();
96 // https://localhost:3907/feedlog/1?type=pub&filename=oteNB5309_A20161224.1030-1045.bin.gz
97 String expUri = LOG_URI + "?type=pub&filename=" + LOCAL_FILE_NAME;
98 assertEquals(expUri, actualUri.toString());
102 public void executeWhenDataRouterReturnsNok_returnsFalse() throws Exception {
103 prepareMocksForTests(HttpUtils.SC_BAD_REQUEST, EMPTY_CONTENT, null);
105 boolean isPublished =
106 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
108 assertFalse(isPublished);
112 public void executeWhenPublished_returnsTrue() throws Exception {
113 prepareMocksForTests(HttpUtils.SC_OK, "[" + LOCAL_FILE_NAME + "]", null);
115 boolean isPublished =
116 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
118 assertTrue(isPublished);
122 public void executeWhenErrorInDataRouter_returnsFalse() throws Exception {
123 prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, new DatafileTaskException(""));
125 boolean isPublished =
126 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
128 assertFalse(isPublished);
131 final void prepareMocksForTests(int responseCode, String content, Exception exception) throws Exception {
132 publishedCheckerUnderTestSpy = spy(new PublishedChecker(appConfigMock));
134 doReturn(publisherConfigurationMock).when(publishedCheckerUnderTestSpy).resolveConfiguration(CHANGE_IDENTIFIER);
135 doReturn(LOG_URI).when(publisherConfigurationMock).logUrl();
136 doReturn(httpClientMock).when(publishedCheckerUnderTestSpy).resolveClient(publisherConfigurationMock);
138 HttpResponse httpResponseMock = mock(HttpResponse.class);
139 if (exception == null) {
140 when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
141 .thenReturn(httpResponseMock);
143 when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
144 .thenThrow(exception);
146 HttpEntity httpEntityMock = mock(HttpEntity.class);
147 StatusLine statusLineMock = mock(StatusLine.class);
148 when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
149 when(statusLineMock.getStatusCode()).thenReturn(responseCode);
150 when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
151 InputStream stream = new ByteArrayInputStream(content.getBytes());
152 when(httpEntityMock.getContent()).thenReturn(stream);