d5707c22f83a098c9da4c33dbf08e9287a3b47bc
[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 shouldNotGetHttpRequest_whenCredencialsAreNotProvided() {
111
112         httpDmaapConsumer.updateCredentials(null, null);
113
114         List<String> output =  httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
115
116         assertTrue(output.isEmpty());
117         verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
118     }
119
120     @Test
121     public void shouldNotBeSuccessful_whenHttpResponseIsOtherThanOk() throws Exception {
122
123         when(statusLine.getStatusCode()).thenReturn(FORBIDDEN.getStatusCode());
124
125         List<String> output =  httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
126
127         assertTrue(output.isEmpty());
128         verify(httpClient).execute(any(HttpGet.class));
129         verify(httpResponse).getStatusLine();
130         verify(httpResponse).getEntity();
131         verify(httpResponse).close();
132         verify(statusLine).getStatusCode();
133         verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
134     }
135
136     @Test
137     public void shouldNotBeSuccessful_whenRequestToOneOfUrlsCannotBeSent() throws Exception {
138
139         when(httpClient.execute(any(HttpGet.class))).thenThrow(new ClientProtocolException());
140
141         List<String> output =  httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
142
143         assertTrue(output.isEmpty());
144         verify(httpClient).execute(any(HttpGet.class));
145         verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
146
147
148         reset(httpClient);
149         when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
150         when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
151
152         output =  httpDmaapConsumer.fetch(TIMEOUT_MS, LIMIT);
153
154         assertFalse(output.isEmpty());
155         assertTrue(output.containsAll(OUTPUT_MSG));
156         verify(httpClient).execute(any(HttpGet.class));
157         verify(httpResponse).getStatusLine();
158         verify(httpResponse).getEntity();
159         verify(httpResponse).close();
160         verify(statusLine).getStatusCode();
161         verifyNoMoreInteractions(httpClient, httpResponse, statusLine, entity);
162     }
163
164 }