/** * Construct configuration the root data tree. * * @param root data tree * @throws Exception */ public YamlConfiguration(Yaml yaml) throws IOException { this.yaml = yaml; root = yaml.dataTree(); }
@Override public String originalConfig() throws IOException { return yaml.original(); } }
@Override public String toString() { try { return yaml.formatted(); } catch (Exception e) { return "Unable to read " + yaml.toString(); } }
@Test public void mapInclude() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, PARENT_INCLUDE_YAML), mapping); String expected = getRaw(PARENT_INCLUDE_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertTrue(dataTree.containsKey("collectionreader")); List<Object> annotators = (List<Object>) dataTree.get("annotators"); assertEquals(2, annotators.size()); assertEquals( "uk.gov.dstl.baleen.testing.DummyAnnotator", ((Map<String, Object>) annotators.get(1)).get("class")); assertEquals( "uk.gov.dstl.baleen.testing.DummyResourceAnnotator", ((Map<String, Object>) annotators.get(0)).get("class")); }
@Test public void simpleInclude() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, PARENT_YAML), mapping); String expected = getRaw(PARENT_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertTrue(dataTree.containsKey("collectionreader")); List<Object> annotators = (List<Object>) dataTree.get("annotators"); assertEquals(2, annotators.size()); assertEquals( "uk.gov.dstl.baleen.testing.DummyAnnotator", ((Map<String, Object>) annotators.get(1)).get("class")); assertEquals( "uk.gov.dstl.baleen.testing.DummyResourceAnnotator", ((Map<String, Object>) annotators.get(0)).get("class")); }
@Test public void nestedInclude() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, GRAND_PARENT_YAML), mapping); String expected = getRaw(GRAND_PARENT_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertTrue(dataTree.containsKey("collectionreader")); List<Object> annotators = (List<Object>) dataTree.get("annotators"); assertEquals(4, annotators.size()); assertEquals( "uk.gov.dstl.baleen.testing.DummyResourceAnnotator", ((Map<String, Object>) annotators.get(0)).get("class")); assertEquals("uk.gov.dstl.baleen.testing.DummyAnnotator1", annotators.get(1)); assertEquals( "uk.gov.dstl.baleen.testing.DummyAnnotator2", ((Map<String, Object>) annotators.get(2)).get("class")); assertEquals(6, ((Map<String, Object>) annotators.get(2)).get("example.count")); assertEquals("uk.gov.dstl.baleen.testing.DummyAnnotator4", annotators.get(3)); }
/** * Construct configuration the root data tree. * * @param root data tree * @throws Exception */ public YamlConfiguration(Yaml yaml) throws IOException { this.yaml = yaml; root = yaml.dataTree(); }
@Override public String originalConfig() throws IOException { return yaml.original(); } }
@Override public String toString() { try { return yaml.formatted(); } catch (Exception e) { return "Unable to read " + yaml.toString(); } }
@Test public void noIncludes() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, YAMLCONFIGURATION_YAML), mapping); String expected = getRaw(YAMLCONFIGURATION_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertNotNull(dataTree); assertTrue(dataTree.containsKey("example")); assertTrue(dataTree.containsKey("collectionreader")); assertTrue(dataTree.containsKey("annotators")); assertTrue(dataTree.containsKey("consumers")); assertTrue(dataTree.get("example") instanceof Map); }
@SuppressWarnings("unchecked") private Object getIncludedList(List<String> values) throws IOException { List<Object> included = new ArrayList<>(); for (String include : values) { Object data = mapping.apply(include).dataTree(); if (data instanceof List) { included.addAll((List<Object>) data); } else { included.add(data); } } return included; }
@Override protected String getSource() throws IOException { return yaml.original(); } }
@SuppressWarnings("unchecked") private Object getIncludedList(List<String> values) throws IOException { List<Object> included = new ArrayList<>(); for (String include : values) { Object data = mapping.apply(include).dataTree(); if (data instanceof List) { included.addAll((List<Object>) data); } else { included.add(data); } } return included; }
@Override protected String getSource() throws IOException { return yaml.original(); } }
@Override public Object dataTree() throws IOException { return processObject(yaml.dataTree()); }
@Override protected String getSource() throws IOException { StringBuilder sb = new StringBuilder(); for (Yaml yaml : yamls) { sb.append(yaml.original()); sb.append("\n\n"); } return sb.toString(); } }
@Override public Object dataTree() throws IOException { return processObject(yaml.dataTree()); }
@Override protected String getSource() throws IOException { StringBuilder sb = new StringBuilder(); for (Yaml yaml : yamls) { sb.append(yaml.original()); sb.append("\n\n"); } return sb.toString(); } }
@Override @SuppressWarnings("unchecked") public Object dataTree() throws IOException { Map<String, Object> dataTree = new LinkedHashMap<>(); for (Yaml yaml : yamls) { Object object = yaml.dataTree(); if (object instanceof Map<?, ?>) { Map<String, Object> subTree = (Map<String, Object>) object; for (Entry<String, Object> e : subTree.entrySet()) { mergeWithDataTree(dataTree, e); } } } return dataTree; }
@Override @SuppressWarnings("unchecked") public Object dataTree() throws IOException { Map<String, Object> dataTree = new LinkedHashMap<>(); for (Yaml yaml : yamls) { Object object = yaml.dataTree(); if (object instanceof Map<?, ?>) { Map<String, Object> subTree = (Map<String, Object>) object; for (Entry<String, Object> e : subTree.entrySet()) { mergeWithDataTree(dataTree, e); } } } return dataTree; }