### Question:
Expander { public static String expand(String textWithMacros, Cluster cluster, Set<Node> nodes, Node node) throws SyntaxException, IncompleteClusterException { int pos = 0; StringBuilder builder = nodes == null ? null : new StringBuilder(); boolean expansionHappened = false; while (pos < textWithMacros.length()) { int pos1 = findNextPercent(textWithMacros, pos); if (pos1 >= 0) { int pos2 = findNextPercent(textWithMacros, pos1 + 1); if (pos2 >= 0) { if (builder != null) { builder.append(textWithMacros, pos, pos1); } String macro = textWithMacros.substring(pos1 + 1, pos2); Expression expression = new Parser(macro).parse(); if (builder != null) { String expansion = expression.evaluate(cluster, nodes, node); if (expansion != null) { builder.append(expansion); expansionHappened = true; } } pos = pos2 + 1; continue; } } if (builder != null && expansionHappened) { builder.append(textWithMacros, pos, textWithMacros.length()); } break; } return expansionHappened ? builder.toString() : textWithMacros; }  static void validate(String textWithMacros); static String expand(String textWithMacros, Cluster cluster, Set<Node> nodes, Node node); static JsonElement expand(JsonElement json, @Nullable java.util.List<String> path, Cluster cluster,
                                   Set<Node> nodes, Node node);  }### Answer:
@Test public void testExpansion() throws SyntaxException, IncompleteClusterException { Assert.assertEquals( "service one: rab:2181,oof:2181,eno:2181", Expander.expand("service one: %join(map(host.service.svc1,'$:2181'),',')%", cluster, clusterNodes, node1)); }
@Test public void testExpansionCreatesNewJson() throws SyntaxException, IncompleteClusterException { JsonObject input = new JsonObject(); input.addProperty("host", "%host.service.svc1%"); JsonObject output = Expander.expand(input, null, cluster, clusterNodes, node2).getAsJsonObject(); Assert.assertEquals("rab,oof,eno", output.get("host").getAsString()); Assert.assertEquals("%host.service.svc1%", input.get("host").getAsString()); }
@Test public void testJsonExpansion() throws SyntaxException, IncompleteClusterException { JsonElement json = new Gson().fromJson(jsonIn, JsonElement.class); JsonElement json1 = Expander.expand(json, ImmutableList.of("defaults", "config"), cluster, clusterNodes, node2); Assert.assertNotSame(json, json1); Assert.assertTrue(json1.toString().contains("hdfs: Assert.assertTrue(json1.toString().contains("quorum\":\"oof:2181,owt:2181\"")); Assert.assertTrue(json1.toString().contains("server.1\":\"oof:2888:3888\"")); Assert.assertTrue(json1.toString().contains("server.2\":\"rab:2888:3888\"")); Assert.assertTrue(json1.toString().contains("server.3\":\"eno:2888:3888\"")); Assert.assertTrue(json1.toString().contains("myid\":\"2\"")); Assert.assertTrue(json1.toString().contains("quorum.size\":\"3\"")); Assert.assertTrue(json1.toString().contains("email\":\"" + cluster.getAccount().getUserId() + "@company.net\"")); Assert.assertTrue(json1.toString().contains("dummyvar\":\"%instance.self.service.svc2%\"")); }
@Test public void testJsonExpansionSkipsUnexpandableMacros() throws SyntaxException, IncompleteClusterException { JsonObject input = new Gson().fromJson(jsonIn, JsonObject.class); input.addProperty("invalid-cluster-macro", "%host.service.svc4%"); JsonElement expanded = Expander.expand(input, null, cluster, clusterNodes, node1); Assert.assertEquals("%host.service.svc4%", expanded.getAsJsonObject().get("invalid-cluster-macro").getAsString()); }