Skip to content

Commit

Permalink
Add some edge cases in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gmunozfe authored and fjtirado committed Nov 13, 2024
1 parent 2b5dc82 commit f4f42b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
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;
import com.fasterxml.jackson.databind.node.NullNode;
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 {

Expand Down Expand Up @@ -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) {
}

Expand Down

0 comments on commit f4f42b7

Please sign in to comment.