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.Mockito.doReturn;
29 import static org.mockito.Mockito.reset;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.verifyNoMoreInteractions;
32 import static org.mockito.Mockito.when;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import org.apache.http.StatusLine;
37 import org.apache.http.client.ClientProtocolException;
38 import org.apache.http.client.methods.CloseableHttpResponse;
39 import org.apache.http.client.methods.HttpPost;
40 import org.apache.http.impl.client.CloseableHttpClient;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.mockito.Spy;
47 public class TestHttpDmaapProducerImpl {
49 private static final Collection<String> URLS = Arrays.asList("test.com", "test.org");
50 private static final String TOPIC_NAME = "Topic";
51 private static final String USERNAME = "username";
52 private static final String PASSWORD = "password";
53 private static final String PARTITION = "partition";
54 private static final String DATA = "data";
58 private HttpDmaapProducerImpl httpDmaapProducer = new HttpDmaapProducerImpl();
61 private CloseableHttpClient httpClient;
64 private CloseableHttpResponse httpResponse;
67 private StatusLine statusLine;
70 public void setUp() throws Exception {
71 httpDmaapProducer = new HttpDmaapProducerImpl(URLS, TOPIC_NAME);
72 httpDmaapProducer.updateCredentials(USERNAME, PASSWORD);
74 MockitoAnnotations.initMocks(this);
75 doReturn(httpClient).when(httpDmaapProducer).getClient();
76 when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse);
77 when(httpResponse.getStatusLine()).thenReturn(statusLine);
81 public void shouldPostHttpRequest() throws Exception {
83 when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
85 boolean successful = httpDmaapProducer.post(PARTITION, DATA);
87 assertTrue(successful);
88 verify(httpClient).execute(any(HttpPost.class));
89 verify(httpResponse).getStatusLine();
90 verify(httpResponse).close();
91 verify(statusLine).getStatusCode();
92 verifyNoMoreInteractions(httpClient, httpResponse, statusLine);
96 public void shouldNotPostHttpRequest_whenCredencialsAreNotProvided() {
98 httpDmaapProducer.updateCredentials(null, null);
100 boolean successful = httpDmaapProducer.post(PARTITION, DATA);
102 assertFalse(successful);
103 verifyNoMoreInteractions(httpClient, httpResponse, statusLine);
107 public void shouldNotBeSuccessful_whenHttpResponseIsOtherThanOk() throws Exception {
109 when(statusLine.getStatusCode()).thenReturn(FORBIDDEN.getStatusCode());
111 boolean successful = httpDmaapProducer.post(PARTITION, DATA);
113 assertFalse(successful);
114 verify(httpClient).execute(any(HttpPost.class));
115 verify(httpResponse).getStatusLine();
116 verify(httpResponse).close();
117 verify(statusLine).getStatusCode();
118 verifyNoMoreInteractions(httpClient, httpResponse, statusLine);
122 public void shouldNotBeSuccessful_whenRequestToOneOfUrlsCannotBeSent() throws Exception {
124 when(httpClient.execute(any(HttpPost.class))).thenThrow(new ClientProtocolException());
126 boolean successful = httpDmaapProducer.post(PARTITION, DATA);
128 assertFalse(successful);
129 verify(httpClient).execute(any(HttpPost.class));
130 verifyNoMoreInteractions(httpClient, httpResponse, statusLine);
134 when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse);
135 when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
137 successful = httpDmaapProducer.post(PARTITION, DATA);
139 assertTrue(successful);
140 verify(httpClient).execute(any(HttpPost.class));
141 verify(httpResponse).getStatusLine();
142 verify(httpResponse).close();
143 verify(statusLine).getStatusCode();
144 verifyNoMoreInteractions(httpClient, httpResponse, statusLine);