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 com.att.nsa.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.Response;
33 import javax.ws.rs.core.Response.ResponseBuilder;
35 import org.apache.http.HttpException;
36 import org.glassfish.jersey.internal.util.Base64;
37 import org.json.JSONException;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
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 ResponseBuilder responseBuilder = Response.ok();
70 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
73 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
75 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
81 public void testGet_403() throws JSONException, HttpException {
82 ResponseBuilder responseBuilder = Response.status(403);
84 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
87 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
88 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
94 public void testGet_basicauth() throws JSONException, HttpException {
96 ResponseBuilder responseBuilder = Response.ok();
98 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
99 Base64.encodeAsString("username:password")))
101 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
103 mrBaseClient.get("/path", "username", "password", "HTTPAAF");
108 @Test(expected = HttpException.class)
109 public void testGet_error() throws JSONException, HttpException {
111 ResponseBuilder responseBuilder = Response.ok();
113 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
116 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
118 mrBaseClient.get("/path", null, null, "HTTPAUTH");
124 public void testGet_wrongjson() throws JSONException, HttpException {
126 ResponseBuilder responseBuilder = Response.ok();
128 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
130 .thenReturn(responseBuilder.header("transactionid", "transactionid").entity("[[").build());
132 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
137 public void testGetResponse() throws JSONException, HttpException {
139 ResponseBuilder responseBuilder = Response.ok();
141 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
144 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
146 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
152 public void testGetResponse_aaf() throws JSONException, HttpException {
154 ResponseBuilder responseBuilder = Response.ok();
156 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
157 Base64.encodeAsString("username:password")))
159 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
161 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
166 @Test(expected = HttpException.class)
167 public void testGetResponse_error() throws JSONException, HttpException {
169 ResponseBuilder responseBuilder = Response.ok();
171 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
174 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
176 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
181 public void testAuthResponse() throws JSONException, HttpException {
183 ResponseBuilder responseBuilder = Response.ok();
185 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
188 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
190 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
195 @Test(expected = HttpException.class)
196 public void testAuthResponsee_error() throws JSONException, HttpException {
198 ResponseBuilder responseBuilder = Response.ok();
200 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
203 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
205 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
210 public void testPostAuth() throws JSONException, HttpException {
212 ResponseBuilder responseBuilder = Response.ok();
214 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
215 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
217 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
219 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
220 "password", "username", "password", "HTTPAUTH");
225 @Test(expected = HttpException.class)
226 public void testPostAuth_error() throws JSONException, HttpException {
228 ResponseBuilder responseBuilder = Response.ok();
230 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
231 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
233 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
235 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
236 null, null, "HTTPAUTH");
242 public void testGetNoAuthResponse() throws JSONException, HttpException {
244 ResponseBuilder responseBuilder = Response.ok();
245 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(
246 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
248 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
254 public void testPost() throws JSONException, HttpException {
256 ResponseBuilder responseBuilder = Response.ok();
258 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
259 Base64.encodeAsString("username:password")))
261 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
263 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
264 "password", "HTTPAUTH");
269 @Test(expected = HttpException.class)
270 public void testPost_error() throws JSONException, HttpException {
272 ResponseBuilder responseBuilder = Response.ok();
274 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
275 Base64.encodeAsString("username:password")))
277 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
279 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
285 public void testPostAuthwithResponse() throws JSONException, HttpException {
287 ResponseBuilder responseBuilder = Response.ok();
289 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
290 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
292 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
294 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
295 "username", "password", "username", "password", "HTTPAUTH");
300 @Test(expected = HttpException.class)
301 public void testPostAuthwithResponse_error() throws JSONException, HttpException {
303 ResponseBuilder responseBuilder = Response.ok();
305 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
306 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
308 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
310 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
311 null, null, null, null, "HTTPAUTH");
317 public void testPostWithResponse() throws JSONException, HttpException {
319 ResponseBuilder responseBuilder = Response.ok();
321 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
322 Base64.encodeAsString("username:password")))
324 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
326 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
327 "username", "password", "HTTPAUTH");
332 @Test(expected = HttpException.class)
333 public void testPostWithResponse_error() throws JSONException, HttpException {
335 ResponseBuilder responseBuilder = Response.ok();
337 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
338 Base64.encodeAsString("username:password")))
340 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
342 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null,
348 public void testGetAuth() throws JSONException, HttpException {
350 ResponseBuilder responseBuilder = Response.ok();
352 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
355 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
356 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
361 @Test(expected = HttpException.class)
362 public void testGetAuth_error() throws JSONException, HttpException {
364 ResponseBuilder responseBuilder = Response.ok();
366 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
367 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
369 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
371 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
377 public void testGetNoAuth() throws JSONException, HttpException {
379 ResponseBuilder responseBuilder = Response.ok();
380 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(
381 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
382 mrBaseClient.getNoAuth("/path", "username", "password", "HTTPAUTH");
387 @Test(expected = HttpException.class)
388 public void testGetNoAuth_error() throws JSONException, HttpException {
390 ResponseBuilder responseBuilder = Response.ok();
391 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(
392 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
393 mrBaseClient.getNoAuth("/path", null, null, "HTTPAUTH");
399 public void testGetHTTPErrorResponseMessage() {
401 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
406 public void getGTTPErrorResponseCode() {
408 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");