2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * =============================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.appc.adapter.messaging.dmaap.http;
23 import static javax.ws.rs.core.Response.Status.FORBIDDEN;
24 import static javax.ws.rs.core.Response.Status.OK;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.same;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.reset;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoMoreInteractions;
33 import static org.mockito.Mockito.when;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.List;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.StatusLine;
40 import org.apache.http.client.ClientProtocolException;
41 import org.apache.http.client.methods.CloseableHttpResponse;
42 import org.apache.http.client.methods.HttpGet;
43 import org.apache.http.impl.client.CloseableHttpClient;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.Spy;
50 public class TestHttpDmaapConsumerImpl {
52 private static final Collection<String> URLS = Arrays.asList("test.com", "test.org");
53 private static final Collection<String> OUTPUT_MSG = Arrays.asList("FirstMessage", "SecondMessage");
54 private static final String TOPIC_NAME = "Topic";
55 private static final String CONSUMER_NAME = "Consumer";
56 private static final String CONSUMER_ID = "Id";
57 private static final String FILTER = "filter";
58 private static final String USERNAME = "username";
59 private static final String PASSWORD = "password";
60 private static final String MESSAGE_BODY = "[FirstMessage, SecondMessage]";
61 private static final int TIMEOUT_MS = 1000;
62 private static final int LIMIT = 1000;
65 private HttpDmaapConsumerImpl httpDmaapConsumer;
68 private CloseableHttpClient httpClient;
71 private CloseableHttpResponse httpResponse;
74 private StatusLine statusLine;
77 private HttpEntity entity;
80 public void setUp() throws Exception {
81 httpDmaapConsumer = new HttpDmaapConsumerImpl(URLS, TOPIC_NAME, CONSUMER_NAME, CONSUMER_ID, FILTER);
82 httpDmaapConsumer.updateCredentials(USERNAME, PASSWORD);
84 MockitoAnnotations.initMocks(this);
85 doReturn(httpClient).when(httpDmaapConsumer).getClient();
86 when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
87 when(httpResponse.getStatusLine()).thenReturn(statusLine);
88 when(httpResponse.getEntity()).thenReturn(entity);
89 doReturn(MESSAGE_BODY).when(httpDmaapConsumer).entityToString(same(entity));
93 public void shouldGetHttpRequest() throws Exception {
95 when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
97 List<String> output = httpDmaapConsumer.fetch();
99 assertFalse(output.isEmpty());
100 assertTrue(output.containsAll(OUTPUT_MSG));
101 verify(httpClient).execute(any(HttpGet.class));
102 verify(httpResponse).getStatusLine();
103 verify(httpResponse).getEntity();
104 verify(httpResponse).close();
105 verify(statusLine).getStatusCode();
106 verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
110 public void shouldNotBeSuccessful_whenHttpResponseIsOtherThanOk() throws Exception {
112 when(statusLine.getStatusCode()).thenReturn(FORBIDDEN.getStatusCode());
114 List<String> output = httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
116 assertTrue(output.isEmpty());
117 verify(httpClient).execute(any(HttpGet.class));
118 verify(httpResponse).getStatusLine();
119 verify(httpResponse).getEntity();
120 verify(httpResponse).close();
121 verify(statusLine).getStatusCode();
122 verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
126 public void shouldNotBeSuccessful_whenRequestToOneOfUrlsCannotBeSent() throws Exception {
128 when(httpClient.execute(any(HttpGet.class))).thenThrow(new ClientProtocolException());
130 List<String> output = httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
132 assertTrue(output.isEmpty());
133 verify(httpClient).execute(any(HttpGet.class));
134 verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
138 when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
139 when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
141 output = httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
143 assertFalse(output.isEmpty());
144 assertTrue(output.containsAll(OUTPUT_MSG));
145 verify(httpClient).execute(any(HttpGet.class));
146 verify(httpResponse).getStatusLine();
147 verify(httpResponse).getEntity();
148 verify(httpResponse).close();
149 verify(statusLine).getStatusCode();
150 verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);