CompoundTransformer { public static Builder builder() { return new Builder(); } protected  CompoundTransformer(Builder builder); static Builder builder(); BrooklynMementoRawData transform(BrooklynMementoPersister reader, RebindExceptionHandler exceptionHandler); BrooklynMementoRawData transform(BrooklynMementoRawData rawData); static final CompoundTransformer NOOP; }
@Test public void testXmlReplaceItemText() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .xmlReplaceItem("Tag1/text()[.='foo']", "bar") .build(); assertSingleXmlTransformation(transformer, "<Tag1>foo</Tag1>", "<Tag1>bar</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag1>baz</Tag1>", "<Tag1>baz</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag2>foo</Tag2>", "<Tag2>foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag0><Tag1>foo</Tag1><Tag2/></Tag0>", "<Tag0><Tag1>bar</Tag1><Tag2/></Tag0>"); assertSingleXmlTransformation(transformer, "<Tag1 attr=\"value\">foo</Tag1>", "<Tag1 attr=\"value\">bar</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag1>foo<Tag2/></Tag1>", "<Tag1>bar<Tag2/></Tag1>"); }
@Test public void testXmlReplaceItemTree() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .xmlReplaceItem("Tag1[text()='foo']", "<Tag1>bar</Tag1>") .build(); assertSingleXmlTransformation(transformer, "<Tag1>foo</Tag1>", "<Tag1>bar</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag1>baz</Tag1>", "<Tag1>baz</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag2>foo</Tag2>", "<Tag2>foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag0><Tag1>foo</Tag1><Tag2/></Tag0>", "<Tag0><Tag1>bar</Tag1><Tag2/></Tag0>"); assertSingleXmlTransformation(transformer, "<Tag1 attr=\"value\">foo</Tag1>", "<Tag1>bar</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag1>foo<Tag2/></Tag1>", "<Tag1>bar</Tag1>"); }
@Test public void testXmlReplaceItemAttribute() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .xmlReplaceItem("Tag1/@attr[.='foo']", "<xsl:attribute name=\"attr\">bar</xsl:attribute>") .build(); assertSingleXmlTransformation(transformer, "<Tag1 attr=\"foo\">foo</Tag1>", "<Tag1 attr=\"bar\">foo</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag1 attr=\"baz\">foo</Tag1>", "<Tag1 attr=\"baz\">foo</Tag1>"); }
@Test public void testXmlRenameTag() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .xmlRenameTag("Tag1[text()='foo']", "Tag2") .build(); assertSingleXmlTransformation(transformer, "<Tag1>foo</Tag1>", "<Tag2>foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag1>baz</Tag1>", "<Tag1>baz</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag2>foo</Tag2>", "<Tag2>foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag0><Tag1>foo</Tag1><Tag2/></Tag0>", "<Tag0><Tag2>foo</Tag2><Tag2/></Tag0>"); assertSingleXmlTransformation(transformer, "<Tag1 attr=\"value\">foo</Tag1>", "<Tag2 attr=\"value\">foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag1>foo<Tag2/></Tag1>", "<Tag2>foo<Tag2/></Tag2>"); }
@Test public void testXmlReplaceItemActuallyAlsoRenamingTag() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .xmlReplaceItem("Tag1[text()='foo']", "<Tag2><xsl:apply-templates select=\"@*|node()\" /></Tag2>") .build(); assertSingleXmlTransformation(transformer, "<Tag1>foo</Tag1>", "<Tag2>foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag1>baz</Tag1>", "<Tag1>baz</Tag1>"); assertSingleXmlTransformation(transformer, "<Tag2>foo</Tag2>", "<Tag2>foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag0><Tag1>foo</Tag1><Tag2/></Tag0>", "<Tag0><Tag2>foo</Tag2><Tag2/></Tag0>"); assertSingleXmlTransformation(transformer, "<Tag1 attr=\"value\">foo</Tag1>", "<Tag2 attr=\"value\">foo</Tag2>"); assertSingleXmlTransformation(transformer, "<Tag1>foo<Tag2/></Tag1>", "<Tag2>foo<Tag2/></Tag2>"); }
@Test public void testNoopTransformation() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .build(); newApp = transformAndRebind(transformer); assertEquals(newApp.getId(), origApp.getId()); assertEquals(origManagementContext.getManagementPlaneIdMaybe(), newManagementContext.getManagementPlaneIdMaybe()); }
@Test public void testRenameClass() throws Exception { ConfigKey<Object> CONF1 = new BasicConfigKey<Object>(Object.class, "test.conf1"); origApp.config().set(CONF1, new OrigType("myfieldval")); CompoundTransformer transformer = CompoundTransformer.builder() .renameClassTag(OrigType.class.getName(), RenamedType.class.getName()) .build(); newApp = transformAndRebind(transformer); Object newConfVal = newApp.getConfig(CONF1); assertEquals(((RenamedType)newConfVal).myfield, "myfieldval"); }
@Test public void testRenameAnonymousInnerClass() throws Exception { ConfigKey<Object> CONF1 = new BasicConfigKey<Object>(Object.class, "test.conf1"); Predicate<Entity> origPredicate = idEqualTo(origApp.getId()); origApp.config().set(CONF1, origPredicate); CompoundTransformer transformer = CompoundTransformer.builder() .renameClassTag(origPredicate.getClass().getName(), RenamedIdEqualToPredicate.class.getName()) .renameField(RenamedIdEqualToPredicate.class.getName(), "val$paramVal", "val") .build(); newApp = transformAndRebind(transformer); RenamedIdEqualToPredicate newPredicate = (RenamedIdEqualToPredicate) newApp.getConfig(CONF1); assertTrue(newPredicate.apply(newApp)); }
@Test public void testRenameTypeInXml() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .renameType("mytype.Before", "mytype.After") .build(); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <nested>"+NEWLINE+ " <type myattrib3=\"myval3\">doesNotMatch</type>"+NEWLINE+ " <type myattrib4=\"myval4\">partial.mytype.Before</type>"+NEWLINE+ " <type myattrib5=\"myval5\">mytype.Before</type>"+NEWLINE+ " </nested>"+NEWLINE+ " <id>myid</id>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.After</type>"+NEWLINE+ " <nested>"+NEWLINE+ " <type myattrib3=\"myval3\">doesNotMatch</type>"+NEWLINE+ " <type myattrib4=\"myval4\">partial.mytype.Before</type>"+NEWLINE+ " <type myattrib5=\"myval5\">mytype.After</type>"+NEWLINE+ " </nested>"+NEWLINE+ " <id>myid</id>"+NEWLINE+ "</entity>"; assertSingleXmlTransformation(transformer, input, expected); }
@Test public void testRenameFieldInXml() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .renameField("MyClass", "myFieldBefore", "myFieldAfter") .build(); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClass>"+NEWLINE+ " <myFieldBefore class=\"string\">myfieldval</myFieldBefore>"+NEWLINE+ " </MyClass>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " <test.conf2>"+NEWLINE+ " <MyOtherClass>"+NEWLINE+ " <myFieldBefore class=\"string\">myfieldval</myFieldBefore>"+NEWLINE+ " </MyOtherClass>"+NEWLINE+ " </test.conf2>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClass>"+NEWLINE+ " <myFieldAfter class=\"string\">myfieldval</myFieldAfter>"+NEWLINE+ " </MyClass>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " <test.conf2>"+NEWLINE+ " <MyOtherClass>"+NEWLINE+ " <myFieldBefore class=\"string\">myfieldval</myFieldBefore>"+NEWLINE+ " </MyOtherClass>"+NEWLINE+ " </test.conf2>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; assertSingleXmlTransformation(transformer, input, expected); }
@Test public void testRenameClassTagInXml() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .renameClassTag("MyClassBefore", "MyClassAfter") .build(); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClassBefore>"+NEWLINE+ " </MyClassBefore>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClassAfter>"+NEWLINE+ " </MyClassAfter>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; assertSingleXmlTransformation(transformer, input, expected); }
@Test public void testRenameClassInXml() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .renameClass("MyClassBefore", "MyClassAfter") .build(); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">MyClassBefore</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1 class=\"MyClassBefore\">"+NEWLINE+ " <MyClassBefore>"+NEWLINE+ " </MyClassBefore>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">MyClassAfter</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1 class=\"MyClassAfter\">"+NEWLINE+ " <MyClassAfter>"+NEWLINE+ " </MyClassAfter>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; assertXmlTransformation(transformer, input, expected); }
@Test public void testChangeCatalogItemIdExplicitVersionInXml() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .changeCatalogItemId("foo", "1.0", "bar", "2.0") .build(); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <catalogItemId>foo:1.0</catalogItemId>"+NEWLINE+ " <config>ignore</config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <catalogItemId>bar:2.0</catalogItemId>"+NEWLINE+ " <config>ignore</config>"+NEWLINE+ "</entity>"; assertSingleXmlTransformation(transformer, input, expected); }
@Test public void testChangeCatalogItemIdExplicitVersionNonMatchInXml() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .changeCatalogItemId("foo", "1.0", "bar", "2.0") .build(); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <catalogItemId>foo:1.1</catalogItemId>"+NEWLINE+ " <config>ignore</config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <catalogItemId>foo:1.1</catalogItemId>"+NEWLINE+ " <config>ignore</config>"+NEWLINE+ "</entity>"; assertSingleXmlTransformation(transformer, input, expected); }
@Test public void testChangeCatalogItemIdAnyVersionInXml() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .changeCatalogItemId("foo", "bar", "2.0") .build(); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <catalogItemId>foo:1.2</catalogItemId>"+NEWLINE+ " <config>ignore</config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <catalogItemId>bar:2.0</catalogItemId>"+NEWLINE+ " <config>ignore</config>"+NEWLINE+ "</entity>"; assertSingleXmlTransformation(transformer, input, expected); }
@Test public void testDeleteCatalogItem() throws Exception { CompoundTransformer transformer = CompoundTransformer.builder() .deletion(BrooklynObjectType.CATALOG_ITEM, ImmutableList.of("foo:1.2")) .build(); String yaml = Joiner.on("\n").join( "brooklyn.catalog:", " items:", " - id: foo", " version: 1.2", " itemType: template", " item:", " services:", " - type: "+BasicApplication.class.getName(), " - id: bar", " version: 1.2", " itemType: template", " item:", " services:", " - type: "+BasicApplication.class.getName()); mgmt().getCatalog().addItems(yaml); CatalogItem<?, ?> origItem = mgmt().getCatalog().getCatalogItem("foo", "1.2"); assertNotNull(origItem); transformAndRebind(transformer); assertNull(mgmt().getCatalog().getCatalogItem("foo", "1.2")); assertNotNull(mgmt().getCatalog().getCatalogItem("bar", "1.2")); }