Possible fix for event listener
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / http / TestHttpDmaapConsumerImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.appc.adapter.messaging.dmaap.http;
22
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;
34
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;
49
50 public class TestHttpDmaapConsumerImpl {
51
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;
63
64     @Spy
65     private HttpDmaapConsumerImpl httpDmaapConsumer;
66
67     @Mock
68     private CloseableHttpClient httpClient;
69
70     @Mock
71     private CloseableHttpResponse httpResponse;
72
73     @Mock
74     private StatusLine statusLine;
75
76     @Mock
77     private HttpEntity entity;
78
79     @Before
80     public void setUp() throws Exception {
81         httpDmaapConsumer = new HttpDmaapConsumerImpl(URLS, TOPIC_NAME, CONSUMER_NAME, CONSUMER_ID, FILTER);
82         httpDmaapConsumer.updateCredentials(USERNAME, PASSWORD);
83
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));
90     }
91
92     @Test
93     public void shouldGetHttpRequest() throws Exception {
94
95         when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
96
97         List<String> output =  httpDmaapConsumer.fetch();
98
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);
107     }
108
109     @Test
110     public void shouldNotBeSuccessful_whenHttpResponseIsOtherThanOk() throws Exception {
111
112         when(statusLine.getStatusCode()).thenReturn(FORBIDDEN.getStatusCode());
113
114         List<String> output =  httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
115
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);
123     }
124
125     @Test
126     public void shouldNotBeSuccessful_whenRequestToOneOfUrlsCannotBeSent() throws Exception {
127
128         when(httpClient.execute(any(HttpGet.class))).thenThrow(new ClientProtocolException());
129
130         List<String> output =  httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
131
132         assertTrue(output.isEmpty());
133         verify(httpClient).execute(any(HttpGet.class));
134         verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
135
136
137         reset(httpClient);
138         when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
139         when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
140
141         output =  httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
142
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);
151     }
152
153 }