diff options
Diffstat (limited to 'datarouter-prov/src/test')
4 files changed, 806 insertions, 0 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java index 0a9632eb..0babdc47 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/StatisticsServletTest.java @@ -105,6 +105,8 @@ public class StatisticsServletTest extends DrServletTestBase { when(response.getOutputStream()).thenReturn(outStream); statisticsServlet = PowerMockito.mock(StatisticsServlet.class); PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString()); + PowerMockito.doCallRealMethod().when(statisticsServlet, "buildMapFromRequest", anyObject()); + PowerMockito.doCallRealMethod().when(statisticsServlet, "getTimeFromParam", anyString()); doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); doCallRealMethod().when(statisticsServlet).doGet(request, response); when(statisticsServlet.getFeedIdsByGroupId(anyInt())).thenReturn(new StringBuffer("1")); @@ -116,10 +118,13 @@ public class StatisticsServletTest extends DrServletTestBase { public void Given_Request_Is_HTTP_GET_With_GroupId_And_FeedId_Parameters_Then_Request_Succeeds() throws Exception { when(request.getParameter("feedid")).thenReturn("1"); + when(request.getParameter("statusCode")).thenReturn("500"); ServletOutputStream outStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(outStream); statisticsServlet = PowerMockito.mock(StatisticsServlet.class); PowerMockito.doReturn(null).when(statisticsServlet, "getRecordsForSQL", anyString()); + PowerMockito.doCallRealMethod().when(statisticsServlet, "buildMapFromRequest", anyObject()); + PowerMockito.doCallRealMethod().when(statisticsServlet, "getTimeFromParam", anyString()); doNothing().when(statisticsServlet).rsToCSV(anyObject(), anyObject()); doCallRealMethod().when(statisticsServlet).doGet(request, response); doCallRealMethod().when(statisticsServlet).queryGeneretor(anyObject()); diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRouteTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRouteTest.java new file mode 100644 index 00000000..ad259a59 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/IngressRouteTest.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.dmaap.datarouter.provisioning.beans; + +import org.apache.commons.lang3.reflect.FieldUtils; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.Mockito.mock; + +@RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Feed") +public class IngressRouteTest { + + private IngressRoute ingressRoute; + + @Before + public void setUp() throws IllegalAccessException{ + PowerMockito.mockStatic(Feed.class); + Feed feed = mock(Feed.class); + PowerMockito.when(Feed.getFeedById(1)).thenReturn(feed); + Map<String, Integer> map = new HashMap<>(); + FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true); + } + + @Test + public void Validate_IngressRoute_Constructors_Create_Same_Object() { + List<String> nodes = new ArrayList<>(); + nodes.add("node.datarouternew.com"); + ingressRoute = new IngressRoute(1, 1, "user1", "172.100.0.0/25", nodes); + JSONObject ingressRouteJson = createIngressRouteJson(); + Assert.assertEquals(ingressRoute, new IngressRoute(ingressRouteJson)); + } + + @Test + public void Validate_AsJsonObject_Returns_Same_Values() { + List<String> nodes = new ArrayList<>(); + nodes.add("node.datarouternew.com"); + ingressRoute = new IngressRoute(1, 1, "user1", "172.100.0.0/25", nodes); + JSONObject ingressRouteJson = createIngressRouteJson(); + Assert.assertEquals(ingressRoute.asJSONObject().toString(), ingressRouteJson.toString()); + } + + private JSONObject createIngressRouteJson() { + JSONObject ingressRouteJson = new JSONObject(); + ingressRouteJson.put("seq", 1); + ingressRouteJson.put("feedid", 1); + ingressRouteJson.put("user", "user1"); + ingressRouteJson.put("subnet", "172.100.0.0/25"); + JSONArray nodes = new JSONArray(); + nodes.put("node.datarouternew.com"); + ingressRouteJson.put("node", nodes); + return ingressRouteJson; + } + +} diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DRRouteCLITest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DRRouteCLITest.java new file mode 100755 index 00000000..dc8d4c79 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DRRouteCLITest.java @@ -0,0 +1,215 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.dmaap.datarouter.provisioning.utils; + +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.impl.client.AbstractHttpClient; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +public class DRRouteCLITest { + + @Mock + private AbstractHttpClient httpClient; + + @Mock + private CloseableHttpResponse httpResponse; + + @Mock + private HttpEntity httpEntity; + + @Mock + private StatusLine statusLine; + + private DRRouteCLI drRouteCLI; + + @Before + public void setUp() throws IllegalAccessException{ + drRouteCLI = mock(DRRouteCLI.class); + doCallRealMethod().when(drRouteCLI).runCommand(anyObject()); + FieldUtils.writeField(drRouteCLI, "server", "prov.datarouternew.com", true); + } + + @Test + public void Given_Add_Egress_Then_RunCommand_Returns_True() throws Exception{ + mockHttpClientForRestCall(); + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "node.datarouternew.com"})); + } + + @Test + public void Given_Add_Network_Then_RunCommand_Returns_True() throws Exception{ + mockHttpClientForRestCall(); + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com", "172.100.0.1"})); + } + + @Test + public void Given_Add_Egress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{ + mockHttpClientForRestCall(); + Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "egress", "1", "user1", "172.100.0.0", "node.datarouternew.com"})); + } + + @Test + public void Given_Error_On_Post_Rest_Call_RunCommand_Returns_False() throws Exception{ + mockErrorResponseFromRestCall(); + Assert.assertFalse(drRouteCLI.runCommand(new String[]{"add", "network", "prov.datarouternew.com", "node.datarouternew.com"})); + } + + @Test + public void Given_Delete_Ingress_Then_RunCommand_Returns_True() throws Exception{ + mockHttpClientForRestCall(); + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "ingress", "1", "user1", "172.100.0.0"})); + } + + @Test + public void Given_Delete_Egress_Then_RunCommand_Returns_True() throws Exception{ + mockHttpClientForRestCall(); + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "egress", "1"})); + } + + @Test + public void Given_Delete_Network_Then_RunCommand_Returns_True() throws Exception{ + mockHttpClientForRestCall(); + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"})); + } + + @Test + public void Given_Delete_Ingress_With_Incorrect_Args_Then_RunCommand_Returns_False() throws Exception{ + mockHttpClientForRestCall(); + Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "ingress", "prov.datarouternew.com", "node.datarouternew.com"})); + } + + @Test + public void Given_Error_On_Delete_Rest_Call_RunCommand_Returns_False() throws Exception{ + mockErrorResponseFromRestCall(); + Assert.assertFalse(drRouteCLI.runCommand(new String[]{"del", "network", "prov.datarouternew.com", "node.datarouternew.com"})); + } + + @Test + public void Given_List_Args_Then_RunCommand_Returns_True() throws Exception{ + mockHttpClientForGetRequest(); + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"})); + } + + @Test + public void Given_Error_On_Get_Rest_Call_RunCommand_Returns_True() throws Exception{ + mockErrorResponseFromRestCall(); + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"list"})); + } + + @Test + public void Given_Width_Arg_Then_RunCommand_Returns_True() { + Assert.assertTrue(drRouteCLI.runCommand(new String[]{"width", "130"})); + } + + @Test + public void Given_Usage_Arg_Then_RunCommand_Returns_False() { + Assert.assertFalse(drRouteCLI.runCommand(new String[]{"usage"})); + } + + private void mockHttpClientForRestCall() throws Exception{ + when(httpResponse.getEntity()).thenReturn(httpEntity); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpClient.execute(anyObject())).thenReturn(httpResponse); + FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true); + } + + private void mockHttpClientForGetRequest() throws Exception{ + mockResponseFromGet(); + when(httpResponse.getEntity()).thenReturn(httpEntity); + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpClient.execute(anyObject())).thenReturn(httpResponse); + FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true); + } + + private void mockResponseFromGet() throws IOException { + JSONObject response = new JSONObject(); + response.put("ingress", addIngressObject()); + response.put("egress", addEgressObject()); + response.put("routing", addRoutingObject()); + InputStream in = new ByteArrayInputStream(response.toString().getBytes()); + when(httpEntity.getContent()).thenReturn(in); + } + + private JSONArray addRoutingObject() { + JSONArray routing = new JSONArray(); + JSONObject route = new JSONObject(); + route.put("from", "prov.datarouternew.com"); + route.put("to", "node.datarouternew.com"); + route.put("via", "172.100.0.1"); + routing.put(route); + return routing; + } + + private JSONObject addEgressObject() { + JSONObject egress = new JSONObject(); + egress.put("1", "node.datarouternew.com"); + egress.put("2", "172.0.0.1"); + return egress; + } + + private JSONArray addIngressObject() { + JSONArray ingresses = new JSONArray(); + JSONObject ingress = new JSONObject(); + ingress.put("seq", 21); + ingress.put("feedid", 1); + ingress.put("user", "user1"); + ingress.put("subnet", "172.0.0.0"); + JSONArray nodes = new JSONArray(); + nodes.put("node.datarouternew.com"); + nodes.put("172.0.0.1"); + ingress.put("node", nodes); + ingresses.put(ingress); + return ingresses; + } + + private void mockErrorResponseFromRestCall() throws Exception{ + InputStream in = new ByteArrayInputStream("<pre> Server Not Found </pre>".getBytes()); + when(httpEntity.getContent()).thenReturn(in); + when(httpResponse.getEntity()).thenReturn(httpEntity); + when(statusLine.getStatusCode()).thenReturn(400); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpClient.execute(anyObject())).thenReturn(httpResponse); + FieldUtils.writeField(drRouteCLI, "httpclient", httpClient, true); + } +} 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 new file mode 100755 index 00000000..7cfe68f9 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java @@ -0,0 +1,498 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ + +package org.onap.dmaap.datarouter.provisioning.utils; + +import java.io.CharArrayWriter; +import java.io.Writer; +import org.json.JSONArray; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; +import java.util.HashMap; +import java.util.Map; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + + +@RunWith(PowerMockRunner.class) +public class LOGJSONObjectTest { + + private static LOGJSONObject logJO; + + @Before + public void setUp() throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", null); + logJO = new LOGJSONObject(map); + } + + @Test + public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray() + throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", 3); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + logJObject.accumulate(s, null); + assertThat(logJObject.get("key").toString(), is("[3,null]")); + } + + @Test + public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair() + throws Exception { + String s = "key"; + logJO.accumulate(s, null); + assertThat(logJO.has("key"), is(false)); + } + + @Test + public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value() + throws Exception { + String s = "key"; + double d = 2.0; + logJO.append(s, d); + assertThat(logJO.getJSONArray("key").get(0), is(2.0)); + } + + + @Test + public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null() + throws Exception { + double d = 2.0; + assertThat(logJO.doubleToString(d), is("2")); + } + + + @Test + public void Given_Method_Is_GetBoolean_And_Value_Is_False_Return_False() + throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", false); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getBoolean(s), is(false)); + } + + @Test + public void Given_Method_Is_GetBoolean_And_Value_Is_True_Return_True() + throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", true); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getBoolean(s), is(true)); + } + + @Test + public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value() + throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", 2.0); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getDouble(s), is(2.0)); + } + + @Test + public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value() + throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", 3); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getInt(s), is(3)); + } + + @Test + public void Given_Method_Is_GetJSONArray_And_Value_Is_A_JSONArray_Return_Value() + throws Exception { + JSONArray jA = new JSONArray(); + Map<String, Object> map = new HashMap<>(); + map.put("key", jA); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getJSONArray(s), is(jA)); + } + + @Test + public void Given_Method_Is_GetJSONObject_And_Value_Is_A_JSONObject_Return_Value() + throws Exception { + LOGJSONObject logJObj = new LOGJSONObject(); + logJObj.put("stub_key", 1); + Map<String, Object> map = new HashMap<>(); + map.put("key", logJObj); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getJSONObject(s), is(logJObj)); + } + + @Test + public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() + throws Exception { + long l = 5; + Map<String, Object> map = new HashMap<>(); + map.put("key", l); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getLong(s), is(5L)); + } + + @Test + public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() + throws Exception { + LOGJSONObject logJObj = new LOGJSONObject(); + logJObj.put("name", "stub_name"); + String[] sArray = new String[logJObj.length()]; + sArray[0] = "name"; + Map<String, Object> map = new HashMap<>(); + map.put("key", sArray); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getNames(logJObj), is(sArray)); + } + + @Test + public void Given_Method_Is_GetString_And_Value_Is_A_String_Return_Value() + throws Exception { + String val = "value"; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.getString(s), is("value")); + } + + @Test + public void Given_Method_Is_Increment_And_Value_Is_Null_Put_Defualt_Value() + throws Exception { + Map<String, Object> mapResult = new HashMap<>(); + mapResult.put("key", 1); + LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); + + String val = null; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + logJObject.increment(s); + assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); + } + + @Test + public void Given_Method_Is_Increment_And_Value_Is_An_Int_Put_Value_Plus_One() + throws Exception { + Map<String, Object> mapResult = new HashMap<>(); + mapResult.put("key", 3); + LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); + + int val = 2; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + logJObject.increment(s); + assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); + } + + @Test + public void Given_Method_Is_Increment_And_Value_Is_A_Long_Put_Value_Plus_One() + throws Exception { + Map<String, Object> mapResult = new HashMap<>(); + mapResult.put("key", 4L); + LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); + + long val = 3; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + logJObject.increment(s); + assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); + } + + @Test + public void Given_Method_Is_Increment_And_Value_Is_A_Double_Put_Value_Plus_One() + throws Exception { + Map<String, Object> mapResult = new HashMap<>(); + mapResult.put("key", 5.0); + LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); + + double val = 4.0; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + logJObject.increment(s); + assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); + } + + @Test + public void Given_Method_Is_Increment_And_Value_Is_A_Float_Put_Value_Plus_One() + throws Exception { + Map<String, Object> mapResult = new HashMap<>(); + mapResult.put("key", 5.0); + LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); + + float val = 4.0f; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + logJObject.increment(s); + assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); + } + + @Test + public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() + throws Exception { + JSONArray ja = new JSONArray(); + ja.put("key"); + + String val = "value"; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.names().get(0), is(ja.get(0))); + } + + @Test + public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() + throws Exception { + Number num = 3.0; + + String val = "value"; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.numberToString(num), is("3")); + } + + @Test + public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() + throws Exception { + boolean val = true; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optBoolean(s, false), is(true)); + } + + @Test + public void Given_Method_Is_OptBoolean_And_Value_is_Not_Boolean_Return_Default_Value() + throws Exception { + String val = "not_boolean"; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optBoolean(s, false), is(false)); + } + + @Test + public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() + throws Exception { + double val = 2.0; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optDouble(s, 0.0), is(2.0)); + } + + @Test + public void Given_Method_Is_OptDouble_And_Value_is_Not_Double_Return_Default_Value() + throws Exception { + String val = "not_double"; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optDouble(s, 0.0), is(0.0)); + } + + @Test + public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() + throws Exception { + int val = 1; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optInt(s, 0), is(1)); + } + + @Test + public void Given_Method_Is_OptInt_And_Value_Is_Null_Return_Default_Value() + throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", null); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optInt(s, 0), is(0)); + } + + @Test + public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() + throws Exception { + long val = 4; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optLong(s, 0), is(4L)); + } + + @Test + public void Given_Method_Is_OptLong_And_Value_is_Not_Long_Return_Default_Value() + throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("key", null); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optLong(s, 0), is(0L)); + } + + @Test + public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() + throws Exception { + String val = "value"; + Map<String, Object> map = new HashMap<>(); + map.put("key", val); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.optString(s, "default_value"), is("value")); + } + + @Test + public void Given_Method_Is_putOnce_And_KeyValuePair_Does_Not_Exist_In_logJObject_Put_KeyValuePair_Into_logJObject() + throws Exception { + String val = "value"; + Map<String, Object> map = new HashMap<>(); + LOGJSONObject logJObject = new LOGJSONObject(map); + + String s = "key"; + assertThat(logJObject.putOnce(s, val).get("key"), is("value")); + } + + @Test + public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() + throws Exception { + String val = "312"; + Map<String, Object> map = new HashMap<>(); + LOGJSONObject logJObject = new LOGJSONObject(map); + + assertThat(logJObject.stringToValue(val), is(312)); + } + + @Test + public void Given_Method_Is_ToJSONArray_And_KeyValue_Exists_Return_Value_Array() + throws Exception { + JSONArray names = new JSONArray(); + Map<String, Object> map = new HashMap<>(); + map.put("name", "value"); + names.put("name"); + LOGJSONObject logJObject = new LOGJSONObject(map); + + assertThat(logJObject.toJSONArray(names).get(0), is("value")); + } + + @Test + public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() + throws Exception { + JSONArray val = new JSONArray(); + Map<String, Object> map = new HashMap<>(); + map.put("key", "value"); + val.put("value"); + LOGJSONObject logJObject = new LOGJSONObject(map); + + assertThat(logJObject.valueToString(val), is("[\"value\"]")); + } + + @Test + public void Given_Method_Is_writeValue_And_Value_IS_Not_Null_Return_Writer_With_Value() + throws Exception { + Writer writer = new CharArrayWriter(); + String val = "value"; + Map<String, Object> map = new HashMap<>(); + map.put("key", "value"); + LOGJSONObject logJObject = new LOGJSONObject(map); + + assertThat(logJObject.writeValue(writer, val, 3, 1).toString(), is("\"value\"")); + } + + @Test + public void Given_Method_Is_write_And_Length_Of_logJObject_Is_One_Write_Value_With_Indent() + throws Exception { + Writer writer = new CharArrayWriter(); + String val = "value"; + Map<String, Object> map = new HashMap<>(); + map.put("key", "value"); + LOGJSONObject logJObject = new LOGJSONObject(map); + + assertThat(logJObject.write(writer, 3, 1).toString(), is("{\"key\": \"value\"}")); + } + + @Test + public void Given_Method_Is_write_And_Length_Of_logJObject_Is_Not_One_Or_Zero_Write_Value_With_New_Indent() + throws Exception { + Writer writer = new CharArrayWriter(); + String val = "value"; + Map<String, Object> map = new HashMap<>(); + map.put("key", "value"); + map.put("key1", "value1"); + LOGJSONObject logJObject = new LOGJSONObject(map); + + assertThat(logJObject.write(writer, 3, 1).toString(), is("{\n \"key1\": \"value1\",\n \"key\": \"value\"\n }")); + } +} |