From f4f42b7b476cf806d361f89c6d66688fb86814bf Mon Sep 17 00:00:00 2001 From: gmunozfe Date: Wed, 13 Nov 2024 09:30:31 +0100 Subject: [PATCH] Add some edge cases in unit tests --- .../jackson/utils/CommonObjectModule.java | 11 ++--- .../jackson/utils/JsonObjectUtilsTest.java | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/CommonObjectModule.java b/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/CommonObjectModule.java index 9c14c09a21e..09cbe9371e5 100644 --- a/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/CommonObjectModule.java +++ b/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/CommonObjectModule.java @@ -27,7 +27,6 @@ import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.databind.node.ObjectNode; class CommonObjectModule extends SimpleModule { @@ -51,14 +50,12 @@ public File deserialize(JsonParser p, DeserializationContext ctxt) throws IOExce private static String fromNode(JsonParser p) throws IOException { JsonNode node = p.readValueAsTree(); + if (node.size() == 1) { + node = node.iterator().next(); + } if (node.isTextual()) { return node.asText(); - } else if (node.isObject()) { - return ((ObjectNode) node).iterator().next().asText(); - } else if (node.isArray()) { - return node.get(0).asText(); - } else { - throw new IOException("Cannot extract string from node " + node); } + throw new IOException(node + "should be a string or have exactly one property of type string"); } } diff --git a/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/JsonObjectUtilsTest.java b/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/JsonObjectUtilsTest.java index 0e3cf6705bb..4976954db11 100644 --- a/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/JsonObjectUtilsTest.java +++ b/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/JsonObjectUtilsTest.java @@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.node.BinaryNode; +import com.fasterxml.jackson.databind.node.BooleanNode; import com.fasterxml.jackson.databind.node.DoubleNode; import com.fasterxml.jackson.databind.node.FloatNode; import com.fasterxml.jackson.databind.node.IntNode; @@ -33,6 +34,7 @@ import com.fasterxml.jackson.databind.node.TextNode; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class JsonObjectUtilsTest { @@ -98,11 +100,53 @@ void testFile() { final String file = "/home/myhome/sample.txt"; final String additionalData = "Javierito"; assertThat(JsonObjectUtils.convertValue(ObjectMapperFactory.get().createObjectNode().put("file", file), File.class)).isEqualTo(new File(file)); + assertThat(JsonObjectUtils.convertValue(ObjectMapperFactory.get().createArrayNode().add(file), File.class)).isEqualTo(new File(file)); assertThat(JsonObjectUtils.convertValue(ObjectMapperFactory.get().createObjectNode().put("file", file).put("additionalData", additionalData), PseudoPOJO.class)) .isEqualTo(new PseudoPOJO(additionalData, new File(file))); assertThat(JsonObjectUtils.convertValue(new TextNode(file), File.class)).isEqualTo(new File(file)); } + @Test + void testFileNullInput() { + assertThat(JsonObjectUtils.convertValue(NullNode.getInstance(), File.class)).isNull(); + } + + @Test + void testFileEmptyPath() { + final String emptyPath = ""; + assertThat(JsonObjectUtils.convertValue(new TextNode(emptyPath), File.class)).isEqualTo(new File(emptyPath)); + } + + @Test + void testFileWithSpecialCharacters() { + final String pathWithSpecialChars = "/home/user/my file en espaƱol.txt"; + assertThat(JsonObjectUtils.convertValue(new TextNode(pathWithSpecialChars), File.class)) + .isEqualTo(new File(pathWithSpecialChars)); + } + + @Test + void testUnsupportedNodeType() { + final String errorMessage = "should be a string or have exactly one property of type string"; + assertThatThrownBy(() -> JsonObjectUtils.convertValue(BooleanNode.TRUE, URI.class)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(errorMessage); + assertThatThrownBy(() -> JsonObjectUtils.convertValue(new IntNode(1), URI.class)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(errorMessage); + assertThatThrownBy(() -> JsonObjectUtils.convertValue(ObjectMapperFactory.get().createArrayNode(), URI.class)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(errorMessage); + assertThatThrownBy(() -> JsonObjectUtils.convertValue(ObjectMapperFactory.get().createObjectNode(), URI.class)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(errorMessage); + assertThatThrownBy(() -> JsonObjectUtils.convertValue(ObjectMapperFactory.get().createObjectNode().put("name", 1), URI.class)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(errorMessage); + assertThatThrownBy(() -> JsonObjectUtils.convertValue(ObjectMapperFactory.get().createArrayNode().add("first.txt").add("second.txt"), URI.class)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(errorMessage); + } + private static record PseudoPOJO(String additionalData, File file) { }