From cca365206063aef585f2bf549e6c051baf67fcf7 Mon Sep 17 00:00:00 2001 From: Kyle Stewart Date: Fri, 31 Aug 2018 15:13:42 +0100 Subject: Add Tests to LOGJSONObjectTest Change-Id: I3443adf93cc36920a5b87093337909172e49c59c Signed-off-by: Kyle Stewart Issue-ID: DMAAP-101 --- .../datarouter/provisioning/RouteServletTest.java | 3 +- .../provisioning/utils/LOGJSONObjectTest.java | 43 +++++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) (limited to 'datarouter-prov/src/test/java') diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java index 34421f52..f1082ba3 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/RouteServletTest.java @@ -273,6 +273,7 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_PUT_And_Is_Not_Authorized() throws Exception { + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); routeServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @@ -291,8 +292,8 @@ public class RouteServletTest extends DrServletTestBase @Test public void Given_Request_Is_HTTP_POST_And_Is_Not_Authorized() throws Exception { - routeServlet.doPost(request, response); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true); + routeServlet.doPost(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java index 7cfe68f9..4dd1b471 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java @@ -26,6 +26,7 @@ package org.onap.dmaap.datarouter.provisioning.utils; import java.io.CharArrayWriter; import java.io.Writer; import org.json.JSONArray; +import org.json.JSONTokener; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,6 +49,39 @@ public class LOGJSONObjectTest { logJO = new LOGJSONObject(map); } + @Test + public void Construct_JSONObject_From_A_Subset_Of_Values_From_Another_JSONObject() + throws Exception { + Map map = new HashMap<>(); + map.put("key1", "value1"); + map.put("key2", "value2"); + map.put("key3", "value3"); + LOGJSONObject ljo= new LOGJSONObject(map); + String[] sA = {"key1", "key3"}; + LOGJSONObject logJObject = new LOGJSONObject(ljo, sA); + assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}")); + } + + @Test + public void Construct_JSONObject_From_A_JSONTokener() + throws Exception { + JSONTokener x = new JSONTokener("{\"key1\":\"value1\",\"key3\":\"value3\"}"); + LOGJSONObject logJObject = new LOGJSONObject(x); + assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}")); + } + + @Test + public void Construct_JSONObject_From_A_Bean_Object_And_Populate_From_Its_Getters_And_Setters() + throws Exception { + Map map = new HashMap<>(); + map.put("key1", "value1"); + map.put("key2", "value2"); + map.put("key3", "value3"); + Object bean = map; + LOGJSONObject logJObject = new LOGJSONObject(bean); + assertThat(logJObject.toString(), is("{\"empty\":false}")); + } + @Test public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray() throws Exception { @@ -171,14 +205,11 @@ public class LOGJSONObjectTest { public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() throws Exception { LOGJSONObject logJObj = new LOGJSONObject(); - logJObj.put("name", "stub_name"); + logJObj.put("name1", "elyk"); String[] sArray = new String[logJObj.length()]; - sArray[0] = "name"; - Map map = new HashMap<>(); - map.put("key", sArray); - LOGJSONObject logJObject = new LOGJSONObject(map); + sArray[0] = "name1"; + LOGJSONObject logJObject = new LOGJSONObject(); - String s = "key"; assertThat(logJObject.getNames(logJObj), is(sArray)); } -- cgit 1.2.3-korg