diff options
author | micdzied <michal.1.dziedzic@nokia.com> | 2019-07-04 14:01:31 +0200 |
---|---|---|
committer | micdzied <michal.1.dziedzic@nokia.com> | 2019-07-04 14:01:31 +0200 |
commit | 5d8ec67db97bdb61b0c07c46afb3a58a02661274 (patch) | |
tree | 45d79130517d85d69fc3a08a146574ac9317ee58 /src/test | |
parent | 3815da092efa0a1047b276fba2ce01ad3cb2a0ab (diff) |
code improvement5.0.2-ONAPelalto
Change-Id: Idc402a05d24c89e5cafc3a4d4221ec2158876f0a
Issue-ID: DMAAP-1229
Signed-off-by: micdzied <michal.1.dziedzic@nokia.com>
Diffstat (limited to 'src/test')
3 files changed, 105 insertions, 104 deletions
diff --git a/src/test/java/org/onap/dmaap/mr/client/impl/DMaapClientUtilTest.java b/src/test/java/org/onap/dmaap/mr/client/impl/DMaapClientUtilTest.java index 21c3b6c..a845572 100644 --- a/src/test/java/org/onap/dmaap/mr/client/impl/DMaapClientUtilTest.java +++ b/src/test/java/org/onap/dmaap/mr/client/impl/DMaapClientUtilTest.java @@ -22,45 +22,42 @@ package org.onap.dmaap.mr.client.impl; import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.client.Invocation; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; - import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; public class DMaapClientUtilTest { - - Builder builder; - - Response response; - - WebTarget target; - + @Mock + Response response; + @Mock + Builder builder; + @Mock + WebTarget target; + @Before public void setup(){ - Mockito.mock(HttpServletRequest.class); - builder = Mockito.mock(Invocation.Builder.class); - response = Mockito.mock(Response.class); - target = Mockito.mock(WebTarget.class); - + MockitoAnnotations.initMocks(this); } @Test - public void testGetTarget() throws IOException{ + public void testGetTarget() { WebTarget actual = DmaapClientUtil.getTarget("testpath"); assertEquals("testpath", actual.getUri().getPath()); } @Test - public void testGetTargetWithParams() throws IOException{ + public void testGetTargetWithParams() { WebTarget actual = DmaapClientUtil.getTarget("testpath", "testuser", "testpassword"); assertEquals("testpath", actual.getUri().getPath()); @@ -74,8 +71,10 @@ public class DMaapClientUtilTest { Mockito.when(builder.get()).thenReturn(response); Response actual = DmaapClientUtil.getResponsewtCambriaAuth(target, "testuser", "testpassword"); - + assertEquals(response, actual); + verify(target).request(); + verify(builder, times(2)).header((String) any(), any()); } diff --git a/src/test/java/org/onap/dmaap/mr/client/impl/MRBaseClientTest.java b/src/test/java/org/onap/dmaap/mr/client/impl/MRBaseClientTest.java index 38c7f8a..a02691c 100644 --- a/src/test/java/org/onap/dmaap/mr/client/impl/MRBaseClientTest.java +++ b/src/test/java/org/onap/dmaap/mr/client/impl/MRBaseClientTest.java @@ -23,6 +23,8 @@ package org.onap.dmaap.mr.client.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.verify; import java.net.MalformedURLException; import java.util.Arrays; @@ -37,6 +39,7 @@ 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.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -69,30 +72,32 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when( + Mockito.when( DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn(response); - mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); - assertTrue(true); - + JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); + assertEquals(200, result.getInt("status")); + assertEquals("test", result.getString("test")); + verify(response, atLeast(1)).getStatus(); + verify(response).readEntity(String.class); + verify(response).getHeaders(); } @Test public void testGet_403() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.status(403); - PowerMockito + Mockito .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn( responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); - mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); - assertTrue(true); - + JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); + assertEquals(403, result.getInt("status")); } @Test @@ -102,15 +107,18 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), Base64.encodeAsString("username:password"))).thenReturn(response); - mrBaseClient.get("/path", "username", "password", "HTTPAAF"); - assertTrue(true); + JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF"); + assertEquals(200, result.getInt("status")); + verify(response, atLeast(1)).getStatus(); + verify(response).readEntity(String.class); + verify(response).getHeaders(); } @@ -118,15 +126,12 @@ public class MRBaseClientTest { public void testGet_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito - .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", + Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn( responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); mrBaseClient.get("/path", null, null, "HTTPAUTH"); - assertTrue(true); - } @Test @@ -136,16 +141,18 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("[["); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when( + Mockito.when( DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn(response); mrBaseClient.get("/path", "username", "password", "HTTPAUTH"); - assertTrue(true); + verify(response, atLeast(1)).getStatus(); + verify(response).readEntity(String.class); + verify(response).getHeaders(); } @Test @@ -155,11 +162,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when( + Mockito.when( DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn(response); @@ -175,11 +182,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), Base64.encodeAsString("username:password"))).thenReturn(response); mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF"); @@ -191,14 +198,13 @@ public class MRBaseClientTest { public void testGetResponse_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito + Mockito .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn( responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build()); mrBaseClient.getResponse("/path", null, null, "HTTPAUTH"); - } @Test @@ -208,11 +214,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when( + Mockito.when( DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn(response); @@ -225,7 +231,7 @@ public class MRBaseClientTest { public void testAuthResponsee_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito + Mockito .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn( @@ -242,11 +248,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito + Mockito .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) .thenReturn(response); @@ -267,7 +273,7 @@ public class MRBaseClientTest { public void testPostAuth_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito + Mockito .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) .thenReturn( @@ -281,9 +287,6 @@ public class MRBaseClientTest { .setUsername(null) .setPassword(null) .setProtocolFlag("HTTPAUTH")); - - assertTrue(true); - } @Test @@ -293,11 +296,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response); + Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response); mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH"); assertTrue(true); @@ -311,16 +314,18 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response); mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username", "password", "HTTPAUTH"); - assertTrue(true); + verify(response, atLeast(1)).getStatus(); + verify(response).readEntity(String.class); + verify(response).getHeaders(); } @@ -328,7 +333,7 @@ public class MRBaseClientTest { public void testPost_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito + Mockito .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), Base64.encodeAsString("username:password"))) .thenReturn( @@ -346,11 +351,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito + Mockito .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) .thenReturn(response); @@ -365,7 +370,7 @@ public class MRBaseClientTest { public void testPostAuthwithResponse_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito + Mockito .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) .thenReturn( @@ -384,11 +389,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), + Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response); mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", @@ -401,7 +406,7 @@ public class MRBaseClientTest { public void testPostWithResponse_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito + Mockito .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"), Base64.encodeAsString("username:password"))) .thenReturn( @@ -419,11 +424,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when( + Mockito.when( DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password")) .thenReturn(response); mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH"); @@ -435,7 +440,7 @@ public class MRBaseClientTest { public void testGetAuth_error() throws JSONException, HttpException { ResponseBuilder responseBuilder = Response.ok(); - PowerMockito + Mockito .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password", new String("{\"test\":\"test\"}").getBytes(), "application/json")) .thenReturn( @@ -453,11 +458,11 @@ public class MRBaseClientTest { MultivaluedMap<String, Object> 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); + Mockito.when(response.getStatus()).thenReturn(200); + Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}"); + Mockito.when(response.getHeaders()).thenReturn(map); - PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response); + Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response); mrBaseClient.getNoAuth("/path"); assertTrue(true); @@ -466,16 +471,13 @@ public class MRBaseClientTest { @Test public void testGetHTTPErrorResponseMessage() { - - assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest"); + assertEquals("testtest", mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>")); } @Test public void getGTTPErrorResponseCode() { - - assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest"); - + assertEquals("500", mrBaseClient.getHTTPErrorResponseCode("<title>500</title>")); } } diff --git a/src/test/java/org/onap/dmaap/mr/tools/ValidatorUtilTest.java b/src/test/java/org/onap/dmaap/mr/tools/ValidatorUtilTest.java index 1306a30..baa5440 100644 --- a/src/test/java/org/onap/dmaap/mr/tools/ValidatorUtilTest.java +++ b/src/test/java/org/onap/dmaap/mr/tools/ValidatorUtilTest.java @@ -41,7 +41,7 @@ public class ValidatorUtilTest { } catch(IllegalArgumentException e) { assertEquals(e.getMessage(), "Servicename is needed"); } - + } @Test |