44755814fa2bda60e8be4c1e421ea685900bc140
[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.util.HashMap;
38 import java.util.Map;
39
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;
53
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";
62
63     private static final Map<String, String> CONTEXT_MAP = new HashMap<>();
64
65     private static PublisherConfiguration publisherConfigurationMock = mock(PublisherConfiguration.class);
66     private static AppConfig appConfigMock;
67     private DmaapProducerHttpClient httpClientMock = mock(DmaapProducerHttpClient.class);
68
69     private PublishedChecker publishedCheckerUnderTestSpy;
70
71
72     @BeforeAll
73     public static void setUp() throws DatafileTaskException {
74         when(publisherConfigurationMock.publishUrl()).thenReturn(PUBLISH_URL);
75
76         appConfigMock = mock(AppConfig.class);
77         when(appConfigMock.getPublisherConfiguration(CHANGE_IDENTIFIER)).thenReturn(publisherConfigurationMock);
78     }
79
80     @Test
81     public void executeWhenNotPublished_returnsFalse() throws Exception {
82         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, null);
83
84         boolean isPublished =
85                 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
86
87         assertFalse(isPublished);
88
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);
93
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());
100     }
101
102     @Test
103     public void executeWhenDataRouterReturnsNok_returnsFalse() throws Exception {
104         prepareMocksForTests(HttpUtils.SC_BAD_REQUEST, EMPTY_CONTENT, null);
105
106         boolean isPublished =
107                 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
108
109         assertFalse(isPublished);
110     }
111
112     @Test
113     public void executeWhenPublished_returnsTrue() throws Exception {
114         prepareMocksForTests(HttpUtils.SC_OK, "[" + LOCAL_FILE_NAME + "]", null);
115
116         boolean isPublished =
117                 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
118
119         assertTrue(isPublished);
120     }
121
122     @Test
123     public void executeWhenErrorInDataRouter_returnsFalse() throws Exception {
124         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, new DatafileTaskException(""));
125
126         boolean isPublished =
127                 publishedCheckerUnderTestSpy.isFilePublished(LOCAL_FILE_NAME, CHANGE_IDENTIFIER, CONTEXT_MAP);
128
129         assertFalse(isPublished);
130     }
131
132     final void prepareMocksForTests(int responseCode, String content, Exception exception) throws Exception {
133         publishedCheckerUnderTestSpy = spy(new PublishedChecker(appConfigMock));
134
135         doReturn(publisherConfigurationMock).when(publishedCheckerUnderTestSpy).resolveConfiguration(CHANGE_IDENTIFIER);
136         doReturn(LOG_URI).when(publisherConfigurationMock).logUrl();
137         doReturn(httpClientMock).when(publishedCheckerUnderTestSpy).resolveClient(publisherConfigurationMock);
138
139         HttpResponse httpResponseMock = mock(HttpResponse.class);
140         if (exception == null) {
141             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
142                     .thenReturn(httpResponseMock);
143         } else {
144             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
145                     .thenThrow(exception);
146         }
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);
154     }
155 }