Repository: brooklyn-server Updated Branches: refs/heads/master 284f110a8 -> f8eb654c7
Adds CleanOrphanedLocationsTest unit test Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/fa578d5c Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/fa578d5c Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/fa578d5c Branch: refs/heads/master Commit: fa578d5c2a5a0901028dd943864f6792967d91f9 Parents: 8f9d73e Author: Aled Sage <aled.s...@gmail.com> Authored: Mon Jul 18 22:46:32 2016 +0100 Committer: Ivana Yovcheva <ivana.yovch...@gmail.com> Committed: Tue Jul 19 10:48:30 2016 +0300 ---------------------------------------------------------------------- .../launcher/CleanOrphanedLocationsTest.java | 241 +++++++++++++++++++ 1 file changed, 241 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/fa578d5c/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsTest.java ---------------------------------------------------------------------- diff --git a/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsTest.java b/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsTest.java new file mode 100644 index 0000000..ff8515a --- /dev/null +++ b/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsTest.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.launcher; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Set; + +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.api.location.LocationSpec; +import org.apache.brooklyn.api.mgmt.rebind.mementos.BrooklynMementoRawData; +import org.apache.brooklyn.api.policy.PolicySpec; +import org.apache.brooklyn.api.sensor.EnricherSpec; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.enricher.AbstractEnricher; +import org.apache.brooklyn.core.feed.AbstractFeed; +import org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp; +import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils; +import org.apache.brooklyn.core.mgmt.rebind.transformer.impl.DeleteOrphanedLocationsTransformer; +import org.apache.brooklyn.core.policy.AbstractPolicy; +import org.apache.brooklyn.core.sensor.Sensors; +import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.testng.annotations.Test; + +import com.beust.jcommander.internal.Sets; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +public class CleanOrphanedLocationsTest extends RebindTestFixtureWithApp { + + @Test + public void testDeletesOrphanedLocations() throws Exception { + SshMachineLocation loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + SshMachineLocation loc2 = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + assertTransformDeletes(new Deletions().locations(loc.getId(), loc2.getId())); + } + + @Test + public void testDeletesOrphanedLocationThatLinksToReachable() throws Exception { + Location referantLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + Location refereeLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) + .configure(ConfigKeys.newConfigKey(Object.class, "myconfig"), referantLoc)); + origApp.addLocations(ImmutableList.of(referantLoc)); + assertTransformDeletes(new Deletions().locations(refereeLoc.getId())); + } + + @Test + public void testDeletesOrphanedSubGraph() throws Exception { + Location loc1 = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + Location loc2 = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) + .configure(ConfigKeys.newConfigKey(Object.class, "myconfig"), loc1)); + assertTransformDeletes(new Deletions().locations(loc1.getId(), loc2.getId())); + } + + @Test + public void testKeepsLocationsReferencedInEntityLocs() throws Exception { + SshMachineLocation loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + SshMachineLocation loc2 = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + origApp.addLocations(ImmutableList.of(loc, loc2)); + assertTransformIsNoop(); + } + + @Test + public void testKeepsLocationsReferencedInSensor() throws Exception { + SshMachineLocation loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + origApp.sensors().set(Sensors.newSensor(Object.class, "mysensor"), loc); + assertTransformIsNoop(); + } + + @Test + public void testKeepsLocationsReferencedInConfig() throws Exception { + SshMachineLocation loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + origApp.config().set(ConfigKeys.newConfigKey(Object.class, "myconfig"), loc); + assertTransformIsNoop(); + } + + @Test + public void testKeepsLocationsReferencedInComplexTypeSensor() throws Exception { + SshMachineLocation loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + SshMachineLocation loc2 = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + origApp.sensors().set(Sensors.newSensor(Object.class, "mysensor"), ImmutableMap.of("mykey", loc, loc2, "myval")); + assertTransformIsNoop(); + } + + @Test + public void testKeepsParentOfReachableLocation() throws Exception { + SshMachineLocation parentLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + SshMachineLocation childLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) + .parent((parentLoc))); + origApp.addLocations(ImmutableList.of(childLoc)); + assertTransformIsNoop(); + } + + @Test + public void testKeepsChildrenOfReachableLocation() throws Exception { + SshMachineLocation parentLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) + .parent((parentLoc))); + origApp.addLocations(ImmutableList.of(parentLoc)); + assertTransformIsNoop(); + } + + @Test + public void testKeepsReferencesOfReachableLocation() throws Exception { + Location referantLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + Location refereeLoc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) + .configure(ConfigKeys.newConfigKey(Object.class, "myconfig"), referantLoc)); + origApp.addLocations(ImmutableList.of(refereeLoc)); + assertTransformIsNoop(); + } + + @Test + public void testKeepsLocationsReferencedByEnricher() throws Exception { + Location loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + origApp.enrichers().add(EnricherSpec.create(MyEnricher.class) + .configure(ConfigKeys.newConfigKey(Object.class, "myconfig"), loc)); + assertTransformIsNoop(); + } + + @Test + public void testKeepsLocationsReferencedByPolicy() throws Exception { + Location loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + origApp.policies().add(PolicySpec.create(MyPolicy.class) + .configure(ConfigKeys.newConfigKey(Object.class, "myconfig"), loc)); + assertTransformIsNoop(); + } + + @Test + public void testKeepsLocationsReferencedByFeed() throws Exception { + Location loc = mgmt().getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)); + MyFeed feed = new MyFeed(); + feed.config().set(ConfigKeys.newConfigKey(Object.class, "myconfig"), loc); + origApp.feeds().add(feed); + assertTransformIsNoop(); + } + + private void assertTransformIsNoop() throws Exception { + BrooklynMementoRawData origData = getRawData(); + BrooklynMementoRawData transformedData = transformRawData(origData); + assertRawData(transformedData, origData); + } + + private void assertTransformDeletes(Deletions deletions) throws Exception { + BrooklynMementoRawData origData = getRawData(); + BrooklynMementoRawData transformedData = transformRawData(origData); + assertRawData(transformedData, origData, deletions); + } + + protected BrooklynMementoRawData getRawData() throws Exception { + RebindTestUtils.waitForPersisted(origApp); + return mgmt().getRebindManager().retrieveMementoRawData(); + } + + protected BrooklynMementoRawData transformRawData(BrooklynMementoRawData rawData) throws Exception { + DeleteOrphanedLocationsTransformer transformer = DeleteOrphanedLocationsTransformer.builder().build(); + return transformer.transform(rawData); + } + + protected void assertRawData(BrooklynMementoRawData actual, BrooklynMementoRawData expected) { + // asserting lots of times, to ensure we get a more specific error message! + assertEquals(actual.getCatalogItems().keySet(), expected.getCatalogItems().keySet()); + assertEquals(actual.getCatalogItems(), expected.getCatalogItems()); + assertEquals(actual.getEntities().keySet(), expected.getEntities().keySet()); + assertEquals(actual.getEntities(), expected.getEntities()); + assertEquals(actual.getLocations().keySet(), expected.getLocations().keySet()); + assertEquals(actual.getLocations(), expected.getLocations()); + assertEquals(actual.getEnrichers().keySet(), expected.getEnrichers().keySet()); + assertEquals(actual.getEnrichers(), expected.getEnrichers()); + assertEquals(actual.getPolicies().keySet(), expected.getPolicies().keySet()); + assertEquals(actual.getPolicies(), expected.getPolicies()); + assertEquals(actual.getFeeds().keySet(), expected.getFeeds().keySet()); + assertEquals(actual.getFeeds(), expected.getFeeds()); + assertTrue(EqualsBuilder.reflectionEquals(actual, expected)); + } + + protected void assertRawData(BrooklynMementoRawData actual, BrooklynMementoRawData orig, Deletions deletions) { + BrooklynMementoRawData expected = BrooklynMementoRawData.builder() + .catalogItems(orig.getCatalogItems()) + .entities(orig.getEntities()) + .locations(MutableMap.<String, String>builder().putAll(orig.getLocations()).removeAll(deletions.locations).build()) + .feeds(MutableMap.<String, String>builder().putAll(orig.getFeeds()).removeAll(deletions.feeds).build()) + .enrichers(MutableMap.<String, String>builder().putAll(orig.getEnrichers()).removeAll(deletions.enrichers).build()) + .policies(MutableMap.<String, String>builder().putAll(orig.getPolicies()).removeAll(deletions.policies).build()) + .build(); + assertRawData(actual, expected); + } + + @SuppressWarnings("unused") + private static class Deletions { + final Set<String> locations = Sets.newLinkedHashSet(); + final Set<String> feeds = Sets.newLinkedHashSet(); + final Set<String> enrichers = Sets.newLinkedHashSet(); + final Set<String> policies = Sets.newLinkedHashSet(); + + Deletions locations(String... vals) { + if (vals != null) locations.addAll(Arrays.asList(vals)); + return this; + } + Deletions feeds(String... vals) { + if (vals != null) feeds.addAll(Arrays.asList(vals)); + return this; + } + Deletions enrichers(String... vals) { + if (vals != null) enrichers.addAll(Arrays.asList(vals)); + return this; + } + Deletions policies(String... vals) { + if (vals != null) policies.addAll(Arrays.asList(vals)); + return this; + } + } + + public static class MyEnricher extends AbstractEnricher { + } + + public static class MyPolicy extends AbstractPolicy { + } + + public static class MyFeed extends AbstractFeed { + } +}