From bc1cad19ab499422b902bf8a2ee2c9900c03e906 Mon Sep 17 00:00:00 2001 From: su622b Date: Fri, 6 Apr 2018 13:19:14 -0400 Subject: Fixes for response parsing errors Issue-ID: DMAAP-396 Change-Id: I2d8fa85dfe5246d76cf4e2066f4c5e6859c4440f Signed-off-by: su622b --- pom.xml | 2 +- .../com/att/nsa/mr/client/impl/MRBaseClient.java | 12 +- .../att/nsa/mr/client/impl/MRBaseClientTest.java | 203 ++++++++++++++------- version.properties | 2 +- 4 files changed, 144 insertions(+), 75 deletions(-) diff --git a/pom.xml b/pom.xml index f2b7d58..4110471 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ org.onap.dmaap.messagerouter.dmaapclient dmaapClient jar - 1.1.3-SNAPSHOT + 1.1.4-SNAPSHOT dmaap-messagerouter-dmaapclient Client library for MR event routing API https://github.com/att/dmaap-framework diff --git a/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java b/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java index 0cffa7d..b526fed 100644 --- a/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java +++ b/src/main/java/com/att/nsa/mr/client/impl/MRBaseClient.java @@ -122,7 +122,7 @@ public class MRBaseClient extends HttpClient implements MRClient { response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding); - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { throw new HttpException( @@ -154,7 +154,7 @@ public class MRBaseClient extends HttpClient implements MRClient { Response response=null; target = DmaapClientUtil.getTarget(path, username, password); response = DmaapClientUtil.postResponsewtCambriaAuth(target, authKey, authDate, data, contentType); - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { @@ -208,7 +208,7 @@ public class MRBaseClient extends HttpClient implements MRClient { fLog.info("TransactionId : " + transactionid); } - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { throw new HttpException( @@ -262,7 +262,7 @@ public class MRBaseClient extends HttpClient implements MRClient { fLog.info("TransactionId : " + transactionid); } - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } else { throw new HttpException( @@ -285,7 +285,7 @@ public class MRBaseClient extends HttpClient implements MRClient { fLog.info("TransactionId : " + transactionid); } - responseData = (String)response.getEntity(); + responseData = (String)response.readEntity(String.class); return responseData; } @@ -325,7 +325,7 @@ public class MRBaseClient extends HttpClient implements MRClient { jsonObject.put("status", response.getStatus()); return jsonObject; } - String responseData = (String)response.getEntity(); + String responseData = (String)response.readEntity(String.class); JSONTokener jsonTokener = new JSONTokener(responseData); JSONObject jsonObject = null; diff --git a/src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java b/src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java index 1c291b9..b05cc7f 100644 --- a/src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java +++ b/src/test/java/com/att/nsa/mr/client/impl/MRBaseClientTest.java @@ -29,17 +29,20 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import org.apache.http.HttpException; import org.glassfish.jersey.internal.util.Base64; +import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap; import org.json.JSONException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -51,26 +54,31 @@ import org.powermock.modules.junit4.PowerMockRunner; @PrepareForTest({ DmaapClientUtil.class }) public class MRBaseClientTest { - //@InjectMocks + // @InjectMocks private MRBaseClient mrBaseClient; private Collection hosts = new HashSet<>(Arrays.asList("localhost:8080")); private String clientSignature = "topic" + "::" + "cg" + "::" + "cid"; @Before public void setup() throws MalformedURLException { - mrBaseClient=new MRBaseClient(hosts, clientSignature); + mrBaseClient = new MRBaseClient(hosts, clientSignature); PowerMockito.mockStatic(DmaapClientUtil.class); } @Test public void testGet() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", - "password")) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when( + DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) + .thenReturn(response); mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); assertTrue(true); @@ -93,12 +101,16 @@ public class MRBaseClientTest { @Test public void testGet_basicauth() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), - Base64.encodeAsString("username:password"))) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))).thenReturn(response); mrBaseClient.get("/path", "username", "password", "HTTPAAF"); assertTrue(true); @@ -123,11 +135,17 @@ public class MRBaseClientTest { @Test public void testGet_wrongjson() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", - "password")) - .thenReturn(responseBuilder.header("transactionid", "transactionid").entity("[[").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("[["); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when( + DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) + .thenReturn(response); mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); assertTrue(true); @@ -136,12 +154,17 @@ public class MRBaseClientTest { @Test public void testGetResponse() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", - "password")) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when( + DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) + .thenReturn(response); mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH"); assertTrue(true); @@ -151,12 +174,16 @@ public class MRBaseClientTest { @Test public void testGetResponse_aaf() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), - Base64.encodeAsString("username:password"))) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))).thenReturn(response); mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF"); assertTrue(true); @@ -180,12 +207,17 @@ public class MRBaseClientTest { @Test public void testAuthResponse() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", - "password")) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when( + DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) + .thenReturn(response); mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH"); assertTrue(true); @@ -209,12 +241,18 @@ public class MRBaseClientTest { @Test public void testPostAuth() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + PowerMockito .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + .thenReturn(response); mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username", "password", "username", "password", "HTTPAUTH"); @@ -241,9 +279,15 @@ public class MRBaseClientTest { @Test public void testGetNoAuthResponse() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response); mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH"); assertTrue(true); @@ -253,12 +297,16 @@ public class MRBaseClientTest { @Test public void testPost() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), - Base64.encodeAsString("username:password"))) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))).thenReturn(response); mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username", "password", "HTTPAUTH"); @@ -284,12 +332,18 @@ public class MRBaseClientTest { @Test public void testPostAuthwithResponse() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + PowerMockito .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + .thenReturn(response); mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username", "password", "username", "password", "HTTPAUTH"); @@ -316,12 +370,16 @@ public class MRBaseClientTest { @Test public void testPostWithResponse() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), - Base64.encodeAsString("username:password"))) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Base64.encodeAsString("username:password"))).thenReturn(response); mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username", "password", "HTTPAUTH"); @@ -347,12 +405,17 @@ public class MRBaseClientTest { @Test public void testGetAuth() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", - "password")) - .thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when( + DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) + .thenReturn(response); mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH"); assertTrue(true); @@ -376,9 +439,15 @@ public class MRBaseClientTest { @Test public void testGetNoAuth() throws JSONException, HttpException { - ResponseBuilder responseBuilder = Response.ok(); - PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn( - responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); + Response response = Mockito.mock(Response.class); + MultivaluedMap map = new StringKeyIgnoreCaseMultivaluedMap<>(); + map.add("transactionid", "transactionid"); + + PowerMockito.when(response.getStatus()).thenReturn(200); + PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + PowerMockito.when(response.getHeaders()).thenReturn(map); + + PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response); mrBaseClient.getNoAuth("/path", "username", "password", "HTTPAUTH"); assertTrue(true); diff --git a/version.properties b/version.properties index 05dc9e5..e7654d1 100644 --- a/version.properties +++ b/version.properties @@ -27,7 +27,7 @@ major=1 minor=1 -patch=3 +patch=4 base_version=${major}.${minor}.${patch} -- cgit 1.2.3-korg