Yamls { public static YamlExtract getTextOfYamlAtPath(String yaml, Object ...path) { YamlExtract result = new YamlExtract(); if (yaml==null) return result; try { int pathIndex = 0; result.yaml = yaml; result.focus = newYaml().compose(new StringReader(yaml)); findTextOfYamlAtPath(result, pathIndex, path); return result; } catch (NoSuchMethodError e) { throw new IllegalStateException("Class version error. This can happen if using a TestNG plugin in your IDE " + "which is an older version, dragging in an older version of SnakeYAML which does not support Mark.getIndex.", e); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.debug("Unable to find element in yaml (setting in result): "+e); result.error = e; return result; } }  @SuppressWarnings({ "unchecked", "rawtypes" }) static T getAs(Object x, Class<T> type); @Beta static Object getAt(String yaml, List<String> path); @Beta @SuppressWarnings("unchecked") static Object getAtPreParsed(Object current, List<String> path); @SuppressWarnings("rawtypes") static void dump(int depth, Object r); @SuppressWarnings("unchecked") static Iterable<Object> parseAll(String yaml); @SuppressWarnings("unchecked") static Iterable<Object> parseAll(Reader yaml); static Object removeMultinameAttribute(Map<String,Object> obj, String ...equivalentNames); static Object getMultinameAttribute(Map<String,Object> obj, String ...equivalentNames); static YamlExtract getTextOfYamlAtPath(String yaml, Object ...path);  }
@Test public void testExtractMap() { String sample = "#before\nk1:\n- v1\nk2:\n # comment\n k21: v21\nk3: v3\n#after\n"; Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k1").withKeyIncluded(true).getMatchedYamlText(), sample.substring(0, sample.indexOf("k2"))); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k3").withKeyIncluded(true).getMatchedYamlText(), sample.substring(sample.indexOf("k3"))); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").getMatchedYamlText(), "# comment\nv21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").getMatchedYamlText(), "# comment\nv21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withKeyIncluded(true).getMatchedYamlText(), "# comment\nk21: v21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withKeyIncluded(true).withPrecedingCommentsIncluded(false).getMatchedYamlText(), "k21: v21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withPrecedingCommentsIncluded(false).getMatchedYamlText(), "v21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withOriginalIndentation(true).getMatchedYamlText(), " # comment\n v21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withKeyIncluded(true).withOriginalIndentation(true).getMatchedYamlText(), " # comment\n k21: v21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withKeyIncluded(true).withPrecedingCommentsIncluded(false).withOriginalIndentation(true).getMatchedYamlText(), " k21: v21"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withPrecedingCommentsIncluded(false).withOriginalIndentation(true).getMatchedYamlText(), " v21"); }
@Test public void testExtractInList() { String sample = "- a\n" + "- b: 2\n" + "- # c\n" + " c1:\n" + " 1\n" + " c2:\n" + " 2\n" + "-\n" + " - a # for a\n" + " # for b\n" + " - b\n"; Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 0).getMatchedYamlText(), "a"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 1, "b").getMatchedYamlText(), "2"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 3, 0).getMatchedYamlText(), "a" ); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 2).getMatchedYamlText(), "c1:\n 1\nc2:\n 2\n"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 2).withOriginalIndentation(true).getMatchedYamlText(), " c1:\n 1\n c2:\n 2\n"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 3, 0).withOriginalIndentation(true).getMatchedYamlText(), " a" ); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 3, 1).getMatchedYamlText(), "# for b\nb\n"); Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, 3, 1).withPrecedingCommentsIncluded(false).getMatchedYamlText(), "b\n"); }
@Test public void testExtractMapIgnoringPreviousComments() { String sample = "a: 1 # one\n" + "b: 2 # two"; Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "b").getMatchedYamlText(), "2 # two"); }
@Test public void testExtractMapWithOddWhitespace() { Assert.assertEquals(Yamls.getTextOfYamlAtPath("x: a\n bc", "x").getMatchedYamlText(), "a\n bc"); }
@Test public void testReplace() { Assert.assertEquals(Yamls.getTextOfYamlAtPath("x: a\n bc", "x").getFullYamlTextWithExtractReplaced("\nc: 1\nd: 2"), "x: \n c: 1\n d: 2"); }
@Test public void testExtractNoOOBE() { YamlExtract r1 = Yamls.getTextOfYamlAtPath( "items:\n- id: sample2\n itemType: location\n item:\n type: jclouds:aws-ec2\n brooklyn.config:\n key2: value2\n\n", "item"); r1.getMatchedYamlTextOrWarn(); try { r1.getMatchedYamlText(); Assert.fail(); } catch (UserFacingException e) { } }