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