1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.net.MalformedURLException;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.HashSet;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.ws.rs.core.Response.ResponseBuilder;
36 import org.apache.http.HttpException;
37 import org.glassfish.jersey.internal.util.Base64;
38 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
39 import org.json.JSONException;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PowerMockIgnore;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
49 @RunWith(PowerMockRunner.class)
50 @PowerMockIgnore("org.apache.http.conn.ssl.*")
51 @PrepareForTest({ DmaapClientUtil.class })
52 public class MRBaseClientTest {
55 private MRBaseClient mrBaseClient;
56 private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
57 private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
60 public void setup() throws MalformedURLException {
61 mrBaseClient = new MRBaseClient(hosts, clientSignature);
62 PowerMockito.mockStatic(DmaapClientUtil.class);
66 public void testGet() throws JSONException, HttpException {
68 Response response = Mockito.mock(Response.class);
69 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
70 map.add("transactionid", "transactionid");
72 PowerMockito.when(response.getStatus()).thenReturn(200);
73 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
74 PowerMockito.when(response.getHeaders()).thenReturn(map);
77 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
78 .thenReturn(response);
80 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
86 public void testGet_403() throws JSONException, HttpException {
87 ResponseBuilder responseBuilder = Response.status(403);
89 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
92 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
93 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
99 public void testGet_basicauth() throws JSONException, HttpException {
101 Response response = Mockito.mock(Response.class);
102 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
103 map.add("transactionid", "transactionid");
105 PowerMockito.when(response.getStatus()).thenReturn(200);
106 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
107 PowerMockito.when(response.getHeaders()).thenReturn(map);
109 PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
110 Base64.encodeAsString("username:password"))).thenReturn(response);
112 mrBaseClient.get("/path", "username", "password", "HTTPAAF");
117 @Test(expected = HttpException.class)
118 public void testGet_error() throws JSONException, HttpException {
120 ResponseBuilder responseBuilder = Response.ok();
122 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
125 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
127 mrBaseClient.get("/path", null, null, "HTTPAUTH");
133 public void testGet_wrongjson() throws JSONException, HttpException {
135 Response response = Mockito.mock(Response.class);
136 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
137 map.add("transactionid", "transactionid");
139 PowerMockito.when(response.getStatus()).thenReturn(200);
140 PowerMockito.when(response.readEntity(String.class)).thenReturn("[[");
141 PowerMockito.when(response.getHeaders()).thenReturn(map);
144 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
145 .thenReturn(response);
147 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
152 public void testGetResponse() throws JSONException, HttpException {
154 Response response = Mockito.mock(Response.class);
155 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
156 map.add("transactionid", "transactionid");
158 PowerMockito.when(response.getStatus()).thenReturn(200);
159 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
160 PowerMockito.when(response.getHeaders()).thenReturn(map);
163 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
164 .thenReturn(response);
166 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
172 public void testGetResponse_aaf() throws JSONException, HttpException {
174 Response response = Mockito.mock(Response.class);
175 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
176 map.add("transactionid", "transactionid");
178 PowerMockito.when(response.getStatus()).thenReturn(200);
179 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
180 PowerMockito.when(response.getHeaders()).thenReturn(map);
182 PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
183 Base64.encodeAsString("username:password"))).thenReturn(response);
185 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
190 @Test(expected = HttpException.class)
191 public void testGetResponse_error() throws JSONException, HttpException {
193 ResponseBuilder responseBuilder = Response.ok();
195 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
198 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
200 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
205 public void testAuthResponse() throws JSONException, HttpException {
207 Response response = Mockito.mock(Response.class);
208 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
209 map.add("transactionid", "transactionid");
211 PowerMockito.when(response.getStatus()).thenReturn(200);
212 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
213 PowerMockito.when(response.getHeaders()).thenReturn(map);
216 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
217 .thenReturn(response);
219 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
224 @Test(expected = HttpException.class)
225 public void testAuthResponsee_error() throws JSONException, HttpException {
227 ResponseBuilder responseBuilder = Response.ok();
229 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
232 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
234 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
239 public void testPostAuth() throws JSONException, HttpException {
241 Response response = Mockito.mock(Response.class);
242 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
243 map.add("transactionid", "transactionid");
245 PowerMockito.when(response.getStatus()).thenReturn(200);
246 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
247 PowerMockito.when(response.getHeaders()).thenReturn(map);
250 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
251 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
252 .thenReturn(response);
254 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
255 .setData( new String("{\"test\":\"test\"}").getBytes())
256 .setContentType("application/json")
257 .setAuthKey("username")
258 .setAuthDate("password")
259 .setUsername("username")
260 .setPassword("password")
261 .setProtocolFlag("HTTPAUTH"));
266 @Test(expected = HttpException.class)
267 public void testPostAuth_error() throws JSONException, HttpException {
269 ResponseBuilder responseBuilder = Response.ok();
271 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
272 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
274 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
276 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
277 .setData( new String("{\"test\":\"test\"}").getBytes())
278 .setContentType("application/json")
283 .setProtocolFlag("HTTPAUTH"));
290 public void testGetNoAuthResponse() throws JSONException, HttpException {
292 Response response = Mockito.mock(Response.class);
293 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
294 map.add("transactionid", "transactionid");
296 PowerMockito.when(response.getStatus()).thenReturn(200);
297 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
298 PowerMockito.when(response.getHeaders()).thenReturn(map);
300 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
302 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
308 public void testPost() throws JSONException, HttpException {
310 Response response = Mockito.mock(Response.class);
311 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
312 map.add("transactionid", "transactionid");
314 PowerMockito.when(response.getStatus()).thenReturn(200);
315 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
316 PowerMockito.when(response.getHeaders()).thenReturn(map);
318 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
319 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
321 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
322 "password", "HTTPAUTH");
327 @Test(expected = HttpException.class)
328 public void testPost_error() throws JSONException, HttpException {
330 ResponseBuilder responseBuilder = Response.ok();
332 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
333 Base64.encodeAsString("username:password")))
335 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
337 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
343 public void testPostAuthwithResponse() throws JSONException, HttpException {
345 Response response = Mockito.mock(Response.class);
346 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
347 map.add("transactionid", "transactionid");
349 PowerMockito.when(response.getStatus()).thenReturn(200);
350 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
351 PowerMockito.when(response.getHeaders()).thenReturn(map);
354 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
355 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
356 .thenReturn(response);
358 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
359 "username", "password", "username", "password", "HTTPAUTH");
364 @Test(expected = HttpException.class)
365 public void testPostAuthwithResponse_error() throws JSONException, HttpException {
367 ResponseBuilder responseBuilder = Response.ok();
369 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
370 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
372 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
374 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
375 null, null, null, null, "HTTPAUTH");
381 public void testPostWithResponse() throws JSONException, HttpException {
383 Response response = Mockito.mock(Response.class);
384 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
385 map.add("transactionid", "transactionid");
387 PowerMockito.when(response.getStatus()).thenReturn(200);
388 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
389 PowerMockito.when(response.getHeaders()).thenReturn(map);
391 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
392 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
394 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
395 "username", "password", "HTTPAUTH");
400 @Test(expected = HttpException.class)
401 public void testPostWithResponse_error() throws JSONException, HttpException {
403 ResponseBuilder responseBuilder = Response.ok();
405 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
406 Base64.encodeAsString("username:password")))
408 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
410 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null,
416 public void testGetAuth() throws JSONException, HttpException {
418 Response response = Mockito.mock(Response.class);
419 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
420 map.add("transactionid", "transactionid");
422 PowerMockito.when(response.getStatus()).thenReturn(200);
423 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
424 PowerMockito.when(response.getHeaders()).thenReturn(map);
427 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
428 .thenReturn(response);
429 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
434 @Test(expected = HttpException.class)
435 public void testGetAuth_error() throws JSONException, HttpException {
437 ResponseBuilder responseBuilder = Response.ok();
439 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
440 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
442 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
444 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
450 public void testGetNoAuth() throws JSONException, HttpException {
452 Response response = Mockito.mock(Response.class);
453 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
454 map.add("transactionid", "transactionid");
456 PowerMockito.when(response.getStatus()).thenReturn(200);
457 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
458 PowerMockito.when(response.getHeaders()).thenReturn(map);
460 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
461 mrBaseClient.getNoAuth("/path");
468 public void testGetHTTPErrorResponseMessage() {
470 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
475 public void getGTTPErrorResponseCode() {
477 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");