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;
40 import org.apache.http.HttpEntity;
41 import org.apache.http.HttpResponse;
42 import org.apache.http.StatusLine;
43 import org.apache.http.client.methods.HttpGet;
44 import org.apache.http.client.methods.HttpUriRequest;
45 import org.junit.jupiter.api.BeforeAll;
46 import org.junit.jupiter.api.Test;
47 import org.mockito.ArgumentCaptor;
48 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
49 import org.onap.dcaegen2.collectors.datafile.configuration.PublisherConfiguration;
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;
54 public class PublishedCheckerTest {
55 private static final String PUBLISH_URL = "https://54.45.33.2:1234/";
56 private static final String EMPTY_CONTENT = "[]";
57 private static final String SOURCE_NAME = "oteNB5309";
58 private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
59 private static final String LOCAL_FILE_NAME = SOURCE_NAME + "_" + FILE_NAME;
60 private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
61 private static final String LOG_URI = "https://localhost:3907/feedlog/1";
63 private static final Map<String, String> CONTEXT_MAP = new HashMap<>();
65 private static PublisherConfiguration publisherConfigurationMock = mock(PublisherConfiguration.class);
66 private static AppConfig appConfigMock;
67 private DmaapProducerHttpClient httpClientMock = mock(DmaapProducerHttpClient.class);
69 private PublishedChecker publishedCheckerUnderTestSpy;
73 public static void setUp() throws DatafileTaskException {
74 when(publisherConfigurationMock.publishUrl()).thenReturn(PUBLISH_URL);
76 appConfigMock = mock(AppConfig.class);
77 when(appConfigMock.getPublisherConfiguration(CHANGE_IDENTIFIER)).thenReturn(publisherConfigurationMock);
81 public void executeWhenNotPublished_returnsFalse() throws Exception {
82 prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, null);
85 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
87 assertFalse(isPublished);
89 ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
90 verify(httpClientMock).addUserCredentialsToHead(any(HttpUriRequest.class));
91 verify(httpClientMock).getDmaapProducerResponseWithCustomTimeout(requestCaptor.capture(), any(), any());
92 verifyNoMoreInteractions(httpClientMock);
94 HttpUriRequest getRequest = requestCaptor.getValue();
95 assertTrue(getRequest instanceof HttpGet);
96 URI actualUri = getRequest.getURI();
97 // https://localhost:3907/feedlog/1?type=pub&filename=oteNB5309_A20161224.1030-1045.bin.gz
98 String expUri = LOG_URI + "?type=pub&filename=" + LOCAL_FILE_NAME;
99 assertEquals(expUri, actualUri.toString());
103 public void executeWhenDataRouterReturnsNok_returnsFalse() throws Exception {
104 prepareMocksForTests(HttpUtils.SC_BAD_REQUEST, EMPTY_CONTENT, null);
106 boolean isPublished =
107 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
109 assertFalse(isPublished);
113 public void executeWhenPublished_returnsTrue() throws Exception {
114 prepareMocksForTests(HttpUtils.SC_OK, "[" + LOCAL_FILE_NAME + "]", null);
116 boolean isPublished =
117 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
119 assertTrue(isPublished);
123 public void executeWhenErrorInDataRouter_returnsFalse() throws Exception {
124 prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, new DatafileTaskException(""));
126 boolean isPublished =
127 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
129 assertFalse(isPublished);
132 final void prepareMocksForTests(int responseCode, String content, Exception exception) throws Exception {
133 publishedCheckerUnderTestSpy = spy(new PublishedChecker(appConfigMock));
135 doReturn(publisherConfigurationMock).when(publishedCheckerUnderTestSpy).resolveConfiguration(CHANGE_IDENTIFIER);
136 doReturn(LOG_URI).when(publisherConfigurationMock).logUrl();
137 doReturn(httpClientMock).when(publishedCheckerUnderTestSpy).resolveClient(publisherConfigurationMock);
139 HttpResponse httpResponseMock = mock(HttpResponse.class);
140 if (exception == null) {
141 when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
142 .thenReturn(httpResponseMock);
144 when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
145 .thenThrow(exception);
147 HttpEntity httpEntityMock = mock(HttpEntity.class);
148 StatusLine statusLineMock = mock(StatusLine.class);
149 when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
150 when(statusLineMock.getStatusCode()).thenReturn(responseCode);
151 when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
152 InputStream stream = new ByteArrayInputStream(content.getBytes());
153 when(httpEntityMock.getContent()).thenReturn(stream);