Skip to content

Commit

Permalink
Update Jackson to 2.9.5 #189
Browse files Browse the repository at this point in the history
* InPlaceMapOverrider tests
* fixing PathSegment.fillMissingParents() .. (was it die to the upgrade or did we have this problem all along?)
  • Loading branch information
andrus committed Apr 7, 2018
1 parent 945699d commit 28cfebd
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 30 deletions.
@@ -1,14 +1,14 @@
package io.bootique.config.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.StreamSupport;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
* A helper class to navigate {@link JsonNode} objects.
*/
Expand Down Expand Up @@ -77,7 +77,7 @@ void fillMissingParents() {

void fillMissingNodes(String field, JsonNode child, JsonNodeFactory nodeFactory) {

if (node == null) {
if (node == null || node.isNull()) {
node = new ObjectNode(nodeFactory);
parent.fillMissingNodes(incomingPath, node, nodeFactory);
}
Expand Down
@@ -0,0 +1,60 @@
package io.bootique.config.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;

import java.util.Collections;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

public class InPlaceMapOverriderTest {

@Test
public void testApply_InPlace() {

Map<String, String> props = Collections.singletonMap("a", "50");
InPlaceMapOverrider overrider = new InPlaceMapOverrider(props, '.');

JsonNode node = YamlReader.read("a: 5");
JsonNode overridden = overrider.apply(node);
assertSame("Overriding must happen in place", node, overridden);
}

@Test
public void testApply() {

Map<String, String> props = Collections.singletonMap("a", "50");
InPlaceMapOverrider overrider = new InPlaceMapOverrider(props, '.');

JsonNode node = YamlReader.read("a: 5");
overrider.apply(node);

assertEquals(50, node.get("a").asInt());
}

@Test
public void testApply_Nested() {

Map<String, String> props = Collections.singletonMap("a.b", "50");
InPlaceMapOverrider overrider = new InPlaceMapOverrider(props, '.');

JsonNode node = YamlReader.read("a:\n b: 5");
overrider.apply(node);

assertEquals(50, node.get("a").get("b").asInt());
}

@Test
public void testApply_MissingRecreated() {

Map<String, String> props = Collections.singletonMap("a.b", "50");
InPlaceMapOverrider overrider = new InPlaceMapOverrider(props, '.');

JsonNode node = YamlReader.read("a:");
overrider.apply(node);

assertEquals(50, node.get("a").get("b").asInt());
}
}
@@ -1,37 +1,19 @@
package io.bootique.config.jackson;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Optional;

import io.bootique.config.jackson.PathSegment;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class PathSegmentTest {

private static JsonNode readYaml(String yaml) {

ByteArrayInputStream in = new ByteArrayInputStream(yaml.getBytes());

try {
YAMLParser parser = new YAMLFactory().createParser(in);
return new ObjectMapper().readTree(parser);
} catch (IOException e) {
throw new RuntimeException("Error reading config file", e);
}
}

@Test
public void testLastPathComponent_Root() {
JsonNode node = readYaml("a: b\nc: d");
JsonNode node = YamlReader.read("a: b\nc: d");
Optional<PathSegment> last = new PathSegment(node, "", '.').lastPathComponent();

assertNotNull(last);
Expand All @@ -42,7 +24,7 @@ public void testLastPathComponent_Root() {

@Test
public void testLastPathComponent() {
JsonNode node = readYaml("a: b\nc: d");
JsonNode node = YamlReader.read("a: b\nc: d");
Optional<PathSegment> last = new PathSegment(node, "a", '.').lastPathComponent();

assertNotNull(last);
Expand All @@ -52,7 +34,7 @@ public void testLastPathComponent() {

@Test
public void testLastPathComponent_Nested() {
JsonNode node = readYaml("a: b\nc:\n d: e");
JsonNode node = YamlReader.read("a: b\nc:\n d: e");
Optional<PathSegment> last = new PathSegment(node, "c.d", '.').lastPathComponent();

assertNotNull(last);
Expand Down
23 changes: 23 additions & 0 deletions bootique/src/test/java/io/bootique/config/jackson/YamlReader.java
@@ -0,0 +1,23 @@
package io.bootique.config.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;

import java.io.ByteArrayInputStream;
import java.io.IOException;

class YamlReader {
static JsonNode read(String yaml) {

ByteArrayInputStream in = new ByteArrayInputStream(yaml.getBytes());

try {
YAMLParser parser = new YAMLFactory().createParser(in);
return new ObjectMapper().readTree(parser);
} catch (IOException e) {
throw new RuntimeException("Error reading yaml: " + yaml, e);
}
}
}

0 comments on commit 28cfebd

Please sign in to comment.