a3d2e3afa50f978a34c75842d5aa8c0c4e2ed43c
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / http / TestHttpDmaapProducerImpl.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.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;
33
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;
46
47 public class TestHttpDmaapProducerImpl {
48
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";
55
56
57     @Spy
58     private HttpDmaapProducerImpl httpDmaapProducer = new HttpDmaapProducerImpl();
59
60     @Mock
61     private CloseableHttpClient httpClient;
62
63     @Mock
64     private CloseableHttpResponse httpResponse;
65
66     @Mock
67     private StatusLine statusLine;
68
69     @Before
70     public void setUp() throws Exception {
71         httpDmaapProducer = new HttpDmaapProducerImpl(URLS, TOPIC_NAME);
72         httpDmaapProducer.updateCredentials(USERNAME, PASSWORD);
73
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);
78     }
79
80     @Test
81     public void shouldPostHttpRequest() throws Exception {
82
83         when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
84
85         boolean successful = httpDmaapProducer.post(PARTITION, DATA);
86
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);
93     }
94
95     @Test
96     public void shouldNotPostHttpRequest_whenCredencialsAreNotProvided() {
97
98         httpDmaapProducer.updateCredentials(null, null);
99
100         boolean successful = httpDmaapProducer.post(PARTITION, DATA);
101
102         assertFalse(successful);
103         verifyNoMoreInteractions(httpClient, httpResponse, statusLine);
104     }
105
106     @Test
107     public void shouldNotBeSuccessful_whenHttpResponseIsOtherThanOk() throws Exception {
108
109         when(statusLine.getStatusCode()).thenReturn(FORBIDDEN.getStatusCode());
110
111         boolean successful = httpDmaapProducer.post(PARTITION, DATA);
112
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);
119     }
120
121     @Test
122     public void shouldNotBeSuccessful_whenRequestToOneOfUrlsCannotBeSent() throws Exception {
123
124         when(httpClient.execute(any(HttpPost.class))).thenThrow(new ClientProtocolException());
125
126         boolean successful = httpDmaapProducer.post(PARTITION, DATA);
127
128         assertFalse(successful);
129         verify(httpClient).execute(any(HttpPost.class));
130         verifyNoMoreInteractions(httpClient, httpResponse, statusLine);
131
132
133         reset(httpClient);
134         when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse);
135         when(statusLine.getStatusCode()).thenReturn(OK.getStatusCode());
136
137         successful = httpDmaapProducer.post(PARTITION, DATA);
138
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);
145     }
146 }