d5f6515014bb66c17ac2c95b2e207e661e4475ea
[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.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.HashMap;
40 import java.util.Map;
41
42 import org.apache.http.HttpEntity;
43 import org.apache.http.HttpResponse;
44 import org.apache.http.StatusLine;
45 import org.apache.http.client.methods.HttpGet;
46 import org.apache.http.client.methods.HttpUriRequest;
47 import org.junit.jupiter.api.BeforeAll;
48 import org.junit.jupiter.api.Test;
49 import org.mockito.ArgumentCaptor;
50 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
51 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
52 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
53 import org.onap.dcaegen2.collectors.datafile.service.producer.DmaapProducerHttpClient;
54 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
55 import org.springframework.web.util.DefaultUriBuilderFactory;
56 import org.springframework.web.util.UriBuilder;
57
58 /**
59  * @author <a href="mailto:maxime.bonneau@est.tech">Maxime Bonneau</a>
60  *
61  */
62 public class PublishedCheckerTest {
63     private static final String EMPTY_CONTENT = "[]";
64     private static final String FEEDLOG_TOPIC = "feedlog";
65     private static final String FEED_ID = "1";
66     private static final String HTTPS_SCHEME = "https";
67     private static final String HOST = "54.45.33.2";
68     private static final int PORT = 1234;
69     private static final String SOURCE_NAME = "oteNB5309";
70     private static final String FILE_NAME = "A20161224.1030-1045.bin.gz";
71     private static final String LOCAL_FILE_NAME = SOURCE_NAME + "_" + FILE_NAME;
72
73     private static final Map<String, String> CONTEXT_MAP = new HashMap<>();
74
75     private static DmaapPublisherConfiguration publisherConfigurationMock = mock(DmaapPublisherConfiguration.class);
76     private static AppConfig appConfigMock;
77     private DmaapProducerHttpClient httpClientMock = mock(DmaapProducerHttpClient.class);
78
79     private PublishedChecker publishedCheckerUnderTestSpy;
80
81     /**
82      * Sets up data for the tests.
83      */
84     @BeforeAll
85     public static void setUp() {
86         when(publisherConfigurationMock.dmaapHostName()).thenReturn(HOST);
87         when(publisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME);
88         when(publisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT);
89
90         appConfigMock = mock(AppConfig.class);
91         when(appConfigMock.getDmaapPublisherConfiguration()).thenReturn(publisherConfigurationMock);
92     }
93
94     @Test
95     public void executeWhenNotPublished_returnsFalse() throws Exception {
96         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, null);
97
98         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
99
100         assertFalse(isPublished);
101
102         ArgumentCaptor<HttpUriRequest> requestCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
103         verify(httpClientMock).getBaseUri();
104         verify(httpClientMock).addUserCredentialsToHead(any(HttpUriRequest.class));
105         verify(httpClientMock).getDmaapProducerResponseWithCustomTimeout(requestCaptor.capture(), any(), any());
106         verifyNoMoreInteractions(httpClientMock);
107
108         HttpUriRequest getRequest = requestCaptor.getValue();
109         assertTrue(getRequest instanceof HttpGet);
110         URI actualUri = getRequest.getURI();
111         assertEquals(HTTPS_SCHEME, actualUri.getScheme());
112         assertEquals(HOST, actualUri.getHost());
113         assertEquals(PORT, actualUri.getPort());
114         Path actualPath = Paths.get(actualUri.getPath());
115         assertTrue(FEEDLOG_TOPIC.equals(actualPath.getName(0).toString()));
116         assertTrue(FEED_ID.equals(actualPath.getName(1).toString()));
117         String actualQuery = actualUri.getQuery();
118         assertTrue(actualQuery.contains("type=pub"));
119         assertTrue(actualQuery.contains("filename=" + LOCAL_FILE_NAME));
120     }
121
122     @Test
123     public void executeWhenDataRouterReturnsNok_returnsFalse() throws Exception {
124         prepareMocksForTests(HttpUtils.SC_BAD_REQUEST, EMPTY_CONTENT, null);
125
126         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
127
128         assertFalse(isPublished);
129     }
130
131     @Test
132     public void executeWhenPublished_returnsTrue() throws Exception {
133         prepareMocksForTests(HttpUtils.SC_OK, "[" + LOCAL_FILE_NAME + "]", null);
134
135         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
136
137         assertTrue(isPublished);
138     }
139
140     @Test
141     public void executeWhenErrorInDataRouter_returnsFalse() throws Exception {
142         prepareMocksForTests(HttpUtils.SC_OK, EMPTY_CONTENT, new DatafileTaskException(""));
143
144         boolean isPublished = publishedCheckerUnderTestSpy.execute(LOCAL_FILE_NAME, CONTEXT_MAP);
145
146         assertFalse(isPublished);
147     }
148
149     final void prepareMocksForTests(int responseCode, String content, Exception exception) throws Exception {
150         publishedCheckerUnderTestSpy = spy(new PublishedChecker(appConfigMock));
151
152         doReturn(publisherConfigurationMock).when(publishedCheckerUnderTestSpy).resolveConfiguration();
153         doReturn(httpClientMock).when(publishedCheckerUnderTestSpy).resolveClient();
154
155         UriBuilder uriBuilder = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT);
156         when(httpClientMock.getBaseUri()).thenReturn(uriBuilder);
157
158         HttpResponse httpResponseMock = mock(HttpResponse.class);
159         if (exception == null) {
160             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
161                     .thenReturn(httpResponseMock);
162         } else {
163             when(httpClientMock.getDmaapProducerResponseWithCustomTimeout(any(HttpUriRequest.class), any(), any()))
164                     .thenThrow(exception);
165         }
166         HttpEntity httpEntityMock = mock(HttpEntity.class);
167         StatusLine statusLineMock = mock(StatusLine.class);
168         when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
169         when(statusLineMock.getStatusCode()).thenReturn(responseCode);
170         when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
171         InputStream stream = new ByteArrayInputStream(content.getBytes());
172         when(httpEntityMock.getContent()).thenReturn(stream);
173     }
174 }