Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* InPlaceMapOverrider tests * fixing PathSegment.fillMissingParents() .. (was it die to the upgrade or did we have this problem all along?)
- Loading branch information
Showing
4 changed files
with
95 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
bootique/src/test/java/io/bootique/config/jackson/InPlaceMapOverriderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
bootique/src/test/java/io/bootique/config/jackson/YamlReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |