You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by bl...@apache.org on 2022/08/17 23:28:20 UTC

[iceberg] branch master updated: Core: Add tests for JsonUtil, reduce duplication (#5526)

This is an automated email from the ASF dual-hosted git repository.

blue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new a0cb091085 Core: Add tests for JsonUtil, reduce duplication (#5526)
a0cb091085 is described below

commit a0cb0910853f0fbbafdb600da58cd67add91b724
Author: Eduard Tudenhöfner <et...@gmail.com>
AuthorDate: Thu Aug 18 01:28:14 2022 +0200

    Core: Add tests for JsonUtil, reduce duplication (#5526)
---
 .../java/org/apache/iceberg/util/JsonUtil.java     |  27 +--
 .../java/org/apache/iceberg/util/TestJsonUtil.java | 192 +++++++++++++++++++++
 2 files changed, 197 insertions(+), 22 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/util/JsonUtil.java b/core/src/main/java/org/apache/iceberg/util/JsonUtil.java
index a3e39fee5c..5d39c0cea6 100644
--- a/core/src/main/java/org/apache/iceberg/util/JsonUtil.java
+++ b/core/src/main/java/org/apache/iceberg/util/JsonUtil.java
@@ -108,7 +108,7 @@ public class JsonUtil {
     Preconditions.checkArgument(node.has(property), "Cannot parse missing int: %s", property);
     JsonNode pNode = node.get(property);
     Preconditions.checkArgument(
-        pNode != null && !pNode.isNull() && pNode.isNumber(),
+        pNode != null && !pNode.isNull() && pNode.isIntegralNumber() && pNode.canConvertToInt(),
         "Cannot parse to an integer value: %s: %s",
         property,
         pNode);
@@ -119,33 +119,21 @@ public class JsonUtil {
     if (!node.hasNonNull(property)) {
       return null;
     }
-    JsonNode pNode = node.get(property);
-    Preconditions.checkArgument(
-        pNode != null && !pNode.isNull() && pNode.isIntegralNumber() && pNode.canConvertToInt(),
-        "Cannot parse to an integer value: %s: %s",
-        property,
-        pNode);
-    return pNode.asInt();
+    return getInt(property, node);
   }
 
   public static Long getLongOrNull(String property, JsonNode node) {
     if (!node.hasNonNull(property)) {
       return null;
     }
-    JsonNode pNode = node.get(property);
-    Preconditions.checkArgument(
-        pNode != null && !pNode.isNull() && pNode.isIntegralNumber() && pNode.canConvertToLong(),
-        "Cannot parse to a long value: %s: %s",
-        property,
-        pNode);
-    return pNode.asLong();
+    return getLong(property, node);
   }
 
   public static long getLong(String property, JsonNode node) {
     Preconditions.checkArgument(node.has(property), "Cannot parse missing long: %s", property);
     JsonNode pNode = node.get(property);
     Preconditions.checkArgument(
-        pNode != null && !pNode.isNull() && pNode.isNumber(),
+        pNode != null && !pNode.isNull() && pNode.isIntegralNumber() && pNode.canConvertToLong(),
         "Cannot parse to a long value: %s: %s",
         property,
         pNode);
@@ -182,12 +170,7 @@ public class JsonUtil {
     if (pNode != null && pNode.isNull()) {
       return null;
     }
-    Preconditions.checkArgument(
-        pNode != null && pNode.isTextual(),
-        "Cannot parse from non-string value: %s: %s",
-        property,
-        pNode);
-    return pNode.asText();
+    return getString(property, node);
   }
 
   public static Map<String, String> getStringMap(String property, JsonNode node) {
diff --git a/core/src/test/java/org/apache/iceberg/util/TestJsonUtil.java b/core/src/test/java/org/apache/iceberg/util/TestJsonUtil.java
new file mode 100644
index 0000000000..6fc6c6a79b
--- /dev/null
+++ b/core/src/test/java/org/apache/iceberg/util/TestJsonUtil.java
@@ -0,0 +1,192 @@
+/*
+ * 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.iceberg.util;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.assertj.core.api.Assertions;
+import org.junit.Test;
+
+public class TestJsonUtil {
+
+  @Test
+  public void get() throws JsonProcessingException {
+    Assertions.assertThatThrownBy(() -> JsonUtil.get("x", JsonUtil.mapper().readTree("{}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing field: x");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.get("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing field: x");
+
+    Assertions.assertThat(JsonUtil.get("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")).asText())
+        .isEqualTo("23");
+  }
+
+  @Test
+  public void getInt() throws JsonProcessingException {
+    Assertions.assertThatThrownBy(() -> JsonUtil.getInt("x", JsonUtil.mapper().readTree("{}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing int: x");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getInt("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to an integer value: x: null");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getInt("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to an integer value: x: \"23\"");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getInt("x", JsonUtil.mapper().readTree("{\"x\": 23.0}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to an integer value: x: 23.0");
+
+    Assertions.assertThat(JsonUtil.getInt("x", JsonUtil.mapper().readTree("{\"x\": 23}")))
+        .isEqualTo(23);
+  }
+
+  @Test
+  public void getIntOrNull() throws JsonProcessingException {
+    Assertions.assertThat(JsonUtil.getIntOrNull("x", JsonUtil.mapper().readTree("{}"))).isNull();
+    Assertions.assertThat(JsonUtil.getIntOrNull("x", JsonUtil.mapper().readTree("{\"x\": 23}")))
+        .isEqualTo(23);
+    Assertions.assertThat(JsonUtil.getIntOrNull("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isNull();
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getIntOrNull("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to an integer value: x: \"23\"");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getIntOrNull("x", JsonUtil.mapper().readTree("{\"x\": 23.0}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to an integer value: x: 23.0");
+  }
+
+  @Test
+  public void getLong() throws JsonProcessingException {
+    Assertions.assertThatThrownBy(() -> JsonUtil.getLong("x", JsonUtil.mapper().readTree("{}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing long: x");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getLong("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a long value: x: null");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getLong("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a long value: x: \"23\"");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getLong("x", JsonUtil.mapper().readTree("{\"x\": 23.0}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a long value: x: 23.0");
+
+    Assertions.assertThat(JsonUtil.getLong("x", JsonUtil.mapper().readTree("{\"x\": 23}")))
+        .isEqualTo(23);
+  }
+
+  @Test
+  public void getLongOrNull() throws JsonProcessingException {
+    Assertions.assertThat(JsonUtil.getLongOrNull("x", JsonUtil.mapper().readTree("{}"))).isNull();
+    Assertions.assertThat(JsonUtil.getLongOrNull("x", JsonUtil.mapper().readTree("{\"x\": 23}")))
+        .isEqualTo(23);
+    Assertions.assertThat(JsonUtil.getLongOrNull("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isNull();
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getLongOrNull("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a long value: x: \"23\"");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getLongOrNull("x", JsonUtil.mapper().readTree("{\"x\": 23.0}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a long value: x: 23.0");
+  }
+
+  @Test
+  public void getString() throws JsonProcessingException {
+    Assertions.assertThatThrownBy(() -> JsonUtil.getString("x", JsonUtil.mapper().readTree("{}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing string: x");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getString("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a string value: x: null");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getString("x", JsonUtil.mapper().readTree("{\"x\": 23}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a string value: x: 23");
+
+    Assertions.assertThat(JsonUtil.getString("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")))
+        .isEqualTo("23");
+  }
+
+  @Test
+  public void getStringOrNull() throws JsonProcessingException {
+    Assertions.assertThat(JsonUtil.getStringOrNull("x", JsonUtil.mapper().readTree("{}"))).isNull();
+    Assertions.assertThat(
+            JsonUtil.getStringOrNull("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")))
+        .isEqualTo("23");
+    Assertions.assertThat(
+            JsonUtil.getStringOrNull("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isNull();
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getStringOrNull("x", JsonUtil.mapper().readTree("{\"x\": 23}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a string value: x: 23");
+  }
+
+  @Test
+  public void getBool() throws JsonProcessingException {
+    Assertions.assertThatThrownBy(() -> JsonUtil.getBool("x", JsonUtil.mapper().readTree("{}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing boolean: x");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getBool("x", JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a boolean value: x: null");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getBool("x", JsonUtil.mapper().readTree("{\"x\": \"23\"}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a boolean value: x: \"23\"");
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getBool("x", JsonUtil.mapper().readTree("{\"x\": \"true\"}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse to a boolean value: x: \"true\"");
+
+    Assertions.assertThat(JsonUtil.getBool("x", JsonUtil.mapper().readTree("{\"x\": true}")))
+        .isTrue();
+    Assertions.assertThat(JsonUtil.getBool("x", JsonUtil.mapper().readTree("{\"x\": false}")))
+        .isFalse();
+  }
+}