From commits-return-26494-archive-asf-public=cust-asf.ponee.io@geode.apache.org Tue Apr 10 15:31:35 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5790918064C for ; Tue, 10 Apr 2018 15:31:35 +0200 (CEST) Received: (qmail 9821 invoked by uid 500); 10 Apr 2018 13:31:34 -0000 Mailing-List: contact commits-help@geode.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.apache.org Delivered-To: mailing list commits@geode.apache.org Received: (qmail 9808 invoked by uid 99); 10 Apr 2018 13:31:34 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Apr 2018 13:31:34 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id D1BB685087; Tue, 10 Apr 2018 13:31:33 +0000 (UTC) Date: Tue, 10 Apr 2018 13:31:33 +0000 To: "commits@geode.apache.org" Subject: [geode] branch develop updated: GEODE-5009: Add unit tests for GfJsonObject (#1761) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <152336709335.29357.14367837901936671578@gitbox.apache.org> From: jensdeppe@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: geode X-Git-Refname: refs/heads/develop X-Git-Reftype: branch X-Git-Oldrev: 65a19d0ccfd14a4ad02607ebacc83926b4c5b252 X-Git-Newrev: 24840e2e9b3efd7e9e6382067e4b080d1456361b X-Git-Rev: 24840e2e9b3efd7e9e6382067e4b080d1456361b X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated This is an automated email from the ASF dual-hosted git repository. jensdeppe pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode.git The following commit(s) were added to refs/heads/develop by this push: new 24840e2 GEODE-5009: Add unit tests for GfJsonObject (#1761) 24840e2 is described below commit 24840e2e9b3efd7e9e6382067e4b080d1456361b Author: Jens Deppe AuthorDate: Tue Apr 10 06:31:29 2018 -0700 GEODE-5009: Add unit tests for GfJsonObject (#1761) --- .../management/internal/cli/json/GfJsonObject.java | 15 --- .../internal/cli/json/GfJsonObjectTest.java | 148 +++++++++++++++++++++ 2 files changed, 148 insertions(+), 15 deletions(-) diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/json/GfJsonObject.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/json/GfJsonObject.java index 3326118..c8f3cb5 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/json/GfJsonObject.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/json/GfJsonObject.java @@ -106,21 +106,6 @@ public class GfJsonObject { return this; } - public GfJsonObject accumulateAsJSONObject(String key, Object value) throws GfJsonException { - JSONObject val = new JSONObject(value); - try { - if (jsonObject.has(key)) { - jsonObject.append(key, val); - } else { - // first time always add JSONArray for accumulate - for convenience - jsonObject.put(key, new JSONArray().put(val)); - } - } catch (JSONException e) { - throw new GfJsonException(e.getMessage()); - } - return this; - } - /** * * @param key diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/json/GfJsonObjectTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/json/GfJsonObjectTest.java new file mode 100644 index 0000000..b4bd1d1 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/json/GfJsonObjectTest.java @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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. + */ + +package org.apache.geode.management.internal.cli.json; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.geode.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class GfJsonObjectTest { + + private GfJsonObject gfJsonObject; + + public static class Simple { + private int key; + private String value; + + public int getKey() { + return key; + } + + public String getValue() { + return value; + } + + public Simple(int key, String value) { + this.key = key; + this.value = value; + } + } + + @Before + public void setup() { + gfJsonObject = new GfJsonObject(); + } + + @Test + public void emptyObject() { + assertThat(gfJsonObject.toString()).isEqualTo("{}"); + } + + @Test + public void addObject() { + gfJsonObject = new GfJsonObject(new Simple(1, "a")); + + assertThat(gfJsonObject.getString("key")).isEqualTo("1"); + assertThat(gfJsonObject.getString("value")).isEqualTo("a"); + } + + @Test + public void addRawObject() throws Exception { + gfJsonObject = new GfJsonObject("{\"key\":1}"); + assertThat(gfJsonObject.getString("key")).isEqualTo("1"); + } + + @Test + public void accumulatePrimitives() throws Exception { + gfJsonObject.accumulate("string", "value1"); + gfJsonObject.accumulate("string", "value2"); + + assertThat(gfJsonObject.getString("string")).isEqualTo("[\"value1\",\"value2\"]"); + } + + @Test + public void putAndGetPrimitives() throws Exception { + gfJsonObject.put("string", "value1"); + gfJsonObject.put("int", Integer.MAX_VALUE); + gfJsonObject.put("boolean", true); + gfJsonObject.put("double", Double.MAX_VALUE); + gfJsonObject.put("long", Long.MAX_VALUE); + + assertThat(gfJsonObject.getString("string")).isEqualTo("value1"); + + assertThat(gfJsonObject.getString("int")).isEqualTo(Integer.toString(Integer.MAX_VALUE)); + assertThat(gfJsonObject.getInt("int")).isEqualTo(Integer.MAX_VALUE); + + assertThat(gfJsonObject.getString("boolean")).isEqualTo("true"); + assertThat(gfJsonObject.getBoolean("boolean")).isEqualTo(true); + + assertThat(gfJsonObject.getString("double")).isEqualTo(Double.toString(Double.MAX_VALUE)); + assertThat(gfJsonObject.getDouble("double")).isEqualTo(Double.MAX_VALUE); + + assertThat(gfJsonObject.getString("long")).isEqualTo(Long.toString(Long.MAX_VALUE)); + assertThat(gfJsonObject.getDouble("long")).isEqualTo(Long.MAX_VALUE); + } + + @Test + public void appendCreatesAndAddsToArray() throws Exception { + gfJsonObject.append("array", 1); + gfJsonObject.append("array", 2); + + assertThat(gfJsonObject.getString("array")).isEqualTo("[1,2]"); + } + + @Test + public void cannotAppendToExistingKey() throws Exception { + gfJsonObject.put("wat", 1); + assertThatThrownBy(() -> gfJsonObject.append("wat", 2)).isInstanceOf(GfJsonException.class); + } + + @Test + public void canGetGfJsonObject() throws Exception { + GfJsonObject sub = new GfJsonObject(); + sub.put("foo", "bar"); + gfJsonObject.put("sub", sub); + + assertThat(gfJsonObject.getJSONObject("sub").toString()).isEqualTo("{\"foo\":\"bar\"}"); + } + + @Test + public void canGetGfJsonArray() throws Exception { + gfJsonObject.append("array", 1); + gfJsonObject.append("array", "a"); + + assertThat(gfJsonObject.getJSONArray("array").toString()).isEqualTo("[1,\"a\"]"); + } + + @Test + public void canGetNames() throws Exception { + gfJsonObject.put("string", "value1"); + gfJsonObject.put("int", Integer.MAX_VALUE); + gfJsonObject.put("boolean", true); + gfJsonObject.put("double", Double.MAX_VALUE); + gfJsonObject.put("long", Long.MAX_VALUE); + + assertThat(gfJsonObject.names().size()).isEqualTo(5); + assertThat(gfJsonObject.names().toString()) + .isEqualTo("[\"string\",\"int\",\"boolean\",\"double\",\"long\"]"); + } +} -- To stop receiving notification emails like this one, please contact jensdeppe@apache.org.