Пример #1
0
 @Test
 public void testGetAs() throws Exception {
   MutableList<String> list = MutableList.of("x");
   assertEquals(Yamls.getAs(list.iterator(), List.class), list);
   assertEquals(Yamls.getAs(list.iterator(), Iterable.class), list);
   assertEquals(Yamls.getAs(list.iterator(), Iterator.class), list.iterator());
   assertEquals(Yamls.getAs(list.iterator(), String.class), "x");
 }
Пример #2
0
 @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 testGetSensorPredicateNoWhitelist() {
   Object rootElement = Iterables.getOnlyElement(Yamls.parseAll(PLANS_YAML));
   Predicate<String> predicate =
       BrooklynServiceInstanceBindingService.getSensorWhitelistPredicate(rootElement);
   assertNotNull(predicate);
   assertTrue(predicate.apply("foo.bar"));
   assertTrue(predicate.apply("bar.foo"));
 }
Пример #4
0
  @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"
        // TODO comments after on same line not yet included - would be nice to add
        //            "a # for a"
        );

    // out-dent
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, 2).getMatchedYamlText(), "c1:\n 1\nc2:\n 2\n");
    // don't outdent
    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"
        // as above, comments after not included
        //            "   a # for a"
        );

    // with preceding comments
    // TODO final item includes newline (and comments) after - this behaviour might change, it's
    // inconsistent,
    // but it means the final comments aren't lost
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, 3, 1).getMatchedYamlText(), "# for b\nb\n");

    // exclude preceding comments
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, 3, 1)
            .withPrecedingCommentsIncluded(false)
            .getMatchedYamlText(),
        "b\n");
  }
Пример #5
0
 /**
  * Returns a wrapped map, if the object is YAML which parses as a map; otherwise returns absent
  * capable of throwing an error with more details
  */
 @SuppressWarnings("unchecked")
 public static Maybe<Map<?, ?>> getAsYamlMap(Object planData) {
   if (!(planData instanceof String)) return Maybe.absent("not a string");
   Iterable<Object> result;
   try {
     result = Yamls.parseAll((String) planData);
   } catch (Exception e) {
     Exceptions.propagateIfFatal(e);
     return Maybe.absent(e);
   }
   Iterator<Object> ri = result.iterator();
   if (!ri.hasNext()) return Maybe.absent("YAML has no elements in it");
   Object r1 = ri.next();
   if (ri.hasNext()) return Maybe.absent("YAML has multiple elements in it");
   if (r1 instanceof Map) return (Maybe<Map<?, ?>>) (Maybe<?>) Maybe.of(r1);
   return Maybe.absent("YAML does not contain a map");
 }
Пример #6
0
  @Test
  public void testExtractNoOOBE() {
    // this might log a warning, as item not found, but won't throw
    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");

    // won't throw
    r1.getMatchedYamlTextOrWarn();
    // will throw
    try {
      r1.getMatchedYamlText();
      Assert.fail();
    } catch (UserFacingException e) {
      // expected, it should give a vaguely explanatory exception and no trace
    }
  }
Пример #7
0
  @Test
  public void testGetAt() throws Exception {
    // leaf of map
    assertEquals(Yamls.getAt("k1: v", ImmutableList.of("k1")), "v");
    assertEquals(Yamls.getAt("k1: {k2: v}", ImmutableList.of("k1", "k2")), "v");

    // get list
    assertEquals(
        Yamls.getAt("k1: [v1, v2]", ImmutableList.<String>of("k1")), ImmutableList.of("v1", "v2"));

    // get map
    assertEquals(Yamls.getAt("k1: v", ImmutableList.<String>of()), ImmutableMap.of("k1", "v"));
    assertEquals(Yamls.getAt("k1: {k2: v}", ImmutableList.of("k1")), ImmutableMap.of("k2", "v"));

    // get array index
    assertEquals(Yamls.getAt("k1: [v1, v2]", ImmutableList.<String>of("k1", "[0]")), "v1");
    assertEquals(Yamls.getAt("k1: [v1, v2]", ImmutableList.<String>of("k1", "[1]")), "v2");
  }
Пример #8
0
  @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")));

    // comments and no key, outdented - the default
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21").getMatchedYamlText(), "# comment\nv21");
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21").getMatchedYamlText(), "# comment\nv21");
    // comments and key
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21").withKeyIncluded(true).getMatchedYamlText(),
        "# comment\nk21: v21");
    // no comments
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21")
            .withKeyIncluded(true)
            .withPrecedingCommentsIncluded(false)
            .getMatchedYamlText(),
        "k21: v21");
    // no comments and no key
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21")
            .withPrecedingCommentsIncluded(false)
            .getMatchedYamlText(),
        "v21");

    // comments and no key, not outdented
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21")
            .withOriginalIndentation(true)
            .getMatchedYamlText(),
        "  # comment\n  v21");
    // comments and key
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21")
            .withKeyIncluded(true)
            .withOriginalIndentation(true)
            .getMatchedYamlText(),
        "  # comment\n  k21: v21");
    // no comments
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21")
            .withKeyIncluded(true)
            .withPrecedingCommentsIncluded(false)
            .withOriginalIndentation(true)
            .getMatchedYamlText(),
        "  k21: v21");
    // no comments and no key
    Assert.assertEquals(
        Yamls.getTextOfYamlAtPath(sample, "k2", "k21")
            .withPrecedingCommentsIncluded(false)
            .withOriginalIndentation(true)
            .getMatchedYamlText(),
        "  v21");
  }
Пример #9
0
 @Test
 public void testExtractMapWithOddWhitespace() {
   Assert.assertEquals(Yamls.getTextOfYamlAtPath("x: a\n bc", "x").getMatchedYamlText(), "a\n bc");
 }
Пример #10
0
 @Test
 public void testExtractMapIgnoringPreviousComments() {
   String sample = "a: 1 # one\n" + "b: 2 # two";
   Assert.assertEquals(Yamls.getTextOfYamlAtPath(sample, "b").getMatchedYamlText(), "2 # two");
 }