You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2016/11/08 20:58:38 UTC

[2/5] brooklyn-server git commit: Code tidy for tests

Code tidy for tests


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/a5b34a72
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/a5b34a72
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/a5b34a72

Branch: refs/heads/master
Commit: a5b34a722aa40bd74e0c5dbdf0c078689a2cd853
Parents: e56de02
Author: Aled Sage <al...@gmail.com>
Authored: Tue Nov 8 15:29:28 2016 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Nov 8 15:35:08 2016 +0000

----------------------------------------------------------------------
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 144 ++++++++-----------
 .../core/mgmt/rebind/RebindOptions.java         |  10 ++
 .../core/mgmt/rebind/RebindTestFixture.java     |   5 +-
 .../test/framework/TestHttpCallTest.java        |  39 ++---
 4 files changed, 95 insertions(+), 103 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a5b34a72/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index a1147db..64362d2 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -18,9 +18,9 @@
  */
 package org.apache.brooklyn.camp.brooklyn;
 
-import java.io.File;
+import static org.testng.Assert.assertNotNull;
+
 import java.util.List;
-import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -29,9 +29,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.brooklyn.api.entity.Application;
 import org.apache.brooklyn.api.entity.Entity;
-import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.api.mgmt.ha.MementoCopyMode;
 import org.apache.brooklyn.api.mgmt.rebind.mementos.BrooklynMementoRawData;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
@@ -43,13 +41,10 @@ import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.entity.EntityAsserts;
 import org.apache.brooklyn.core.entity.EntityInternal;
-import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
 import org.apache.brooklyn.core.mgmt.persist.BrooklynPersistenceUtils;
-import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.entity.group.DynamicCluster;
-import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.slf4j.Logger;
@@ -61,43 +56,26 @@ import org.testng.annotations.Test;
 
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
-import com.google.common.io.Files;
 
 @Test
-public class DslAndRebindYamlTest extends AbstractYamlTest {
+public class DslAndRebindYamlTest extends AbstractYamlRebindTest {
 
     private static final Logger log = LoggerFactory.getLogger(DslAndRebindYamlTest.class);
 
-    protected ClassLoader classLoader = getClass().getClassLoader();
-    protected File mementoDir;
-    protected Set<ManagementContext> mgmtContexts = MutableSet.of();
     protected ExecutorService executor;
 
-    @Override
-    protected LocalManagementContext newTestManagementContext() {
-        if (mementoDir != null) throw new IllegalStateException("already created mgmt context");
-        mementoDir = Files.createTempDir();
-        mementoDir.deleteOnExit();
-        LocalManagementContext mgmt = RebindTestUtils.newPersistingManagementContext(mementoDir, classLoader, 1);
-        mgmtContexts.add(mgmt);
-        return mgmt;
-    }
-
     @BeforeMethod(alwaysRun = true)
     @Override
     public void setUp() throws Exception {
-    	super.setUp();
+        super.setUp();
         executor = Executors.newSingleThreadExecutor();
     }
     
     @AfterMethod(alwaysRun = true)
     @Override
     public void tearDown() throws Exception {
-    	if (executor != null) executor.shutdownNow();
-        for (ManagementContext mgmt : mgmtContexts) Entities.destroyAll(mgmt);
+        if (executor != null) executor.shutdownNow();
         super.tearDown();
-        mementoDir = null;
-        mgmtContexts.clear();
     }
 
     @Override
@@ -105,14 +83,14 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         return log;
     }
 
-    protected Application rebind(Application app) throws Exception {
-        RebindTestUtils.stopPersistence(app);
-        Application result = RebindTestUtils.rebind(mementoDir, getClass().getClassLoader());
-        mgmtContexts.add(result.getManagementContext());
-        return result;
+    @SuppressWarnings("unchecked")
+    protected <T extends Entity> T rebind(T entity) throws Exception {
+        rebind();
+        Entity result = mgmt().getEntityManager().getEntity(entity.getId());
+        assertNotNull(result, "no entity found after rebind with id " + entity.getId());
+        return (T) result;
     }
 
-
     protected Entity setupAndCheckTestEntityInBasicYamlWith(String... extras) throws Exception {
         Entity app = createAndStartApplication(loadYaml("test-entity-basic-template.yaml", extras));
         waitForApplicationTasks(app);
@@ -139,16 +117,16 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     }
 
     protected <T> Future<T> getConfigInTaskAsync(final Entity entity, final ConfigKey<T> key) {
-	    // Wait for the attribute to be ready in a new Task
-	    Callable<T> configGetter = new Callable<T>() {
-	        @Override
-	        public T call() throws Exception {
-	            T s = getConfigInTask(entity, key);
-	            getLogger().info("getConfig {}={}", key, s);
-	            return s;
-	        }
-	    };
-	    return executor.submit(configGetter);
+        // Wait for the attribute to be ready in a new Task
+        Callable<T> configGetter = new Callable<T>() {
+            @Override
+            public T call() throws Exception {
+                T s = getConfigInTask(entity, key);
+                getLogger().info("getConfig {}={}", key, s);
+                return s;
+            }
+        };
+        return executor.submit(configGetter);
     }
 
     @Test
@@ -163,8 +141,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         Entity testEntity = entityWithAttributeWhenReady();
         ((EntityInternal) testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
         
-        Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+        Entity e2 = rebind(testEntity);
 
         Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar");
     }
@@ -173,8 +150,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     public void testDslAttributeWhenReadyWhenNotYetResolved() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
         
-        Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+        Entity e2 = rebind(testEntity);
 
         // Wait for the attribute to be ready in a new Task
         Future<String> stringFuture = getConfigInTaskAsync(e2, TestEntity.CONF_NAME);
@@ -190,25 +166,24 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
 
     @Test
     public void testDslAttributeWhenReadyPersistedAsDeferredSupplier() throws Exception {
-    	doDslAttributeWhenReadyPersistedAsDeferredSupplier(false);
+        doDslAttributeWhenReadyPersistedAsDeferredSupplier(false);
     }
     
     @Test
     public void testDslAttributeWhenReadyPersistedWithoutLeakingResolvedValue() throws Exception {
-    	doDslAttributeWhenReadyPersistedAsDeferredSupplier(true);
+        doDslAttributeWhenReadyPersistedAsDeferredSupplier(true);
     }
     
     protected void doDslAttributeWhenReadyPersistedAsDeferredSupplier(boolean resolvedBeforeRebind) throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
         
         if (resolvedBeforeRebind) {
-        	testEntity.sensors().set(Sensors.newStringSensor("foo"), "bar");
-        	Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_NAME), "bar");
+            testEntity.sensors().set(Sensors.newStringSensor("foo"), "bar");
+            Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_NAME), "bar");
         }
         
         // Persist and rebind
-        Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+        Entity e2 = rebind(testEntity);
 
         Maybe<Object> maybe = ((EntityInternal) e2).config().getLocalRaw(TestEntity.CONF_NAME);
         Assert.assertTrue(maybe.isPresentAndNonNull());
@@ -217,7 +192,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"x\").attributeWhenReady(\"foo\")");
 
         // Assert the persisted state itself is as expected, and not too big
-        BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(app2.getManagementContext(), MementoCopyMode.LOCAL);
+        BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(mgmt(), MementoCopyMode.LOCAL);
         String persistedStateForE2 = raw.getEntities().get(e2.getId());
         Matcher matcher = Pattern.compile(".*\\<test.confName\\>(.*)\\<\\/test.confName\\>.*", Pattern.DOTALL)
                 .matcher(persistedStateForE2);
@@ -244,12 +219,12 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
 
     @Test
     public void testDslAttributeWhenReadyInEntitySpecWhenNotYetResolved() throws Exception {
-    	doDslAttributeWhenReadyInEntitySpec(false);
+        doDslAttributeWhenReadyInEntitySpec(false);
     }
     
     @Test
     public void testDslAttributeWhenReadyInEntitySpecWhenAlreadyResolved() throws Exception {
-    	doDslAttributeWhenReadyInEntitySpec(true);
+        doDslAttributeWhenReadyInEntitySpec(true);
     }
     
     protected void doDslAttributeWhenReadyInEntitySpec(boolean resolvedBeforeRebind) throws Exception {
@@ -275,11 +250,10 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         }
 
         // Persist and rebind
-        Application app2 = rebind(cluster.getApplication());
-        DynamicCluster cluster2 = (DynamicCluster) Iterables.getOnlyElement(app2.getApplication().getChildren());
+        DynamicCluster cluster2 = rebind(cluster);
 
         // Assert the persisted state itself is as expected, and not too big
-        BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(app2.getManagementContext(), MementoCopyMode.LOCAL);
+        BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(mgmt(), MementoCopyMode.LOCAL);
         String persistedStateForE2 = raw.getEntities().get(cluster2.getId());
         String expectedTag = "org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslComponent_-AttributeWhenReady";
         Matcher matcher = Pattern.compile(".*\\<"+expectedTag+"\\>(.*)\\<\\/"+expectedTag+"\\>.*", Pattern.DOTALL)
@@ -294,38 +268,38 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         
         // Both the existing and the new member should have the DeferredSupplier config
         for (Entity member : Iterables.filter(cluster2.getChildren(), TestEntity.class)) {
-	        Maybe<Object> maybe = ((EntityInternal)member).config().getLocalRaw(TestEntity.CONF_NAME);
-	        Assert.assertTrue(maybe.isPresentAndNonNull());
-	        BrooklynDslDeferredSupplier<?> deferredSupplier = (BrooklynDslDeferredSupplier<?>) maybe.get();
-	        Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"test-cluster\").attributeWhenReady(\"sensor\")");
+            Maybe<Object> maybe = ((EntityInternal)member).config().getLocalRaw(TestEntity.CONF_NAME);
+            Assert.assertTrue(maybe.isPresentAndNonNull());
+            BrooklynDslDeferredSupplier<?> deferredSupplier = (BrooklynDslDeferredSupplier<?>) maybe.get();
+            Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"test-cluster\").attributeWhenReady(\"sensor\")");
         }
         
         if (resolvedBeforeRebind) {
             // All members should resolve their config
             for (Entity member : Iterables.filter(cluster2.getChildren(), TestEntity.class)) {
-		        String val = getConfigInTask(member, TestEntity.CONF_NAME);
-		        Assert.assertEquals(val, "bar");
+                String val = getConfigInTask(member, TestEntity.CONF_NAME);
+                Assert.assertEquals(val, "bar");
             }
         } else {
-        	List<Future<String>> futures = Lists.newArrayList();
-        	
+            List<Future<String>> futures = Lists.newArrayList();
+            
             // All members should have unresolved values
             for (Entity member : Iterables.filter(cluster2.getChildren(), TestEntity.class)) {
-		        // Wait for the attribute to be ready in a new Task
-		        Future<String> stringFuture = getConfigInTaskAsync(member, TestEntity.CONF_NAME);
-		        futures.add(stringFuture);
-		        
-		        // Check that the Task is still waiting for attribute to be ready
-		        Thread.sleep(100);
-		        Assert.assertFalse(stringFuture.isDone());
+                // Wait for the attribute to be ready in a new Task
+                Future<String> stringFuture = getConfigInTaskAsync(member, TestEntity.CONF_NAME);
+                futures.add(stringFuture);
+                
+                // Check that the Task is still waiting for attribute to be ready
+                Thread.sleep(100);
+                Assert.assertFalse(stringFuture.isDone());
             }
             
             // After setting the sensor, all those values should now resolve
-	        cluster2.sensors().set(Sensors.newStringSensor("sensor"), "bar");
-	        
-	        for (Future<String> future : futures) {
-		        String s = future.get(10, TimeUnit.SECONDS); // Timeout just for sanity
-		        Assert.assertEquals(s, "bar");
+            cluster2.sensors().set(Sensors.newStringSensor("sensor"), "bar");
+            
+            for (Future<String> future : futures) {
+                String s = future.get(10, TimeUnit.SECONDS); // Timeout just for sanity
+                Assert.assertEquals(s, "bar");
             }
         }
     }
@@ -347,8 +321,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         Sensor<?> s;
         s = inTask ? getConfigInTask(testEntity, configKey) : testEntity.getConfig(configKey);
         Assert.assertEquals(s, expectedSensor);
-        Application app2 = rebind(testEntity.getApplication());
-        Entity te2 = Iterables.getOnlyElement(app2.getChildren());
+        Entity te2 = rebind(testEntity);
         s = inTask ? getConfigInTask(te2, configKey) : te2.getConfig(configKey);
         Assert.assertEquals(s, expectedSensor);
     }
@@ -356,6 +329,8 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     @Test
     public void testDslSensorFromClass() throws Exception {
         doTestOnEntityWithSensor(entityWithSensorFromClass(), Attributes.SERVICE_UP);
+        switchOriginalToNewManagementContext();
+        
         // without context it can still find it
         doTestOnEntityWithSensor(entityWithSensorFromClass(), Attributes.SERVICE_UP, false);
     }
@@ -406,8 +381,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     @Test
     public void testDslConfigFromRootRebind() throws Exception {
         Entity testEntity = entityWithConfigFromRoot();
-        Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+        Entity e2 = rebind(testEntity);
 
         Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar");
     }
@@ -431,8 +405,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     @Test
     public void testDslFormatStringRebind() throws Exception {
         Entity testEntity = entityWithFormatString();
-        Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+        Entity e2 = rebind(testEntity);
 
         Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "hello world");
     }
@@ -444,7 +417,6 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
                 "    test.confName: $brooklyn:formatString(\"hello %s\", \"world\")");
     }
 
-
     /*
         - type: org.apache.brooklyn.enricher.stock.Transformer
           brooklyn.config:

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a5b34a72/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindOptions.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindOptions.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindOptions.java
index e3c32db..bfa65d4 100644
--- a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindOptions.java
+++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindOptions.java
@@ -30,6 +30,8 @@ import org.apache.brooklyn.api.mgmt.rebind.mementos.BrooklynMementoPersister;
 import org.apache.brooklyn.core.mgmt.persist.PersistenceObjectStore;
 
 import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
 
 /**
  * See {@link RebindTestFixture#rebind(RebindOptions)} and {@link RebindTestUtils#rebind(RebindOptions)}.
@@ -117,6 +119,14 @@ public class RebindOptions {
         this.applicationChooserOnRebind = val;
         return this;
     }
+    public RebindOptions applicationChooserOnRebind(final Predicate<? super Application> val) {
+        Function<Collection<Application>, Application> funcVal = new Function<Collection<Application>, Application>() {
+            @Override public Application apply(Collection<Application> input) {
+                return Iterables.find(input, val);
+            }
+        };
+        return applicationChooserOnRebind(funcVal);
+    }
     public RebindOptions additionalProperties(Map<?, ?> additionalProperties) {
         this.additionalProperties = additionalProperties;
         return this;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a5b34a72/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
index 0e2c9fb..ec8aae9 100644
--- a/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
+++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/rebind/RebindTestFixture.java
@@ -29,6 +29,7 @@ import java.util.concurrent.Callable;
 import org.apache.brooklyn.api.catalog.BrooklynCatalog;
 import org.apache.brooklyn.api.catalog.CatalogItem;
 import org.apache.brooklyn.api.entity.Application;
+import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.api.mgmt.ha.HighAvailabilityMode;
@@ -38,6 +39,7 @@ import org.apache.brooklyn.api.mgmt.rebind.mementos.BrooklynMementoManifest;
 import org.apache.brooklyn.core.catalog.internal.CatalogUtils;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.entity.EntityFunctions;
+import org.apache.brooklyn.core.entity.EntityPredicates;
 import org.apache.brooklyn.core.entity.StartableApplication;
 import org.apache.brooklyn.core.entity.trait.Startable;
 import org.apache.brooklyn.core.internal.BrooklynProperties;
@@ -293,8 +295,9 @@ public abstract class RebindTestFixture<T extends StartableApplication> {
         if (options.mementoDir == null) options.mementoDir(mementoDir);
         if (options.origManagementContext == null) options.origManagementContext(origManagementContext);
         if (options.newManagementContext == null) options.newManagementContext(createNewManagementContext(options.mementoDir, options.additionalProperties));
+        if (options.applicationChooserOnRebind == null && origApp != null) options.applicationChooserOnRebind(EntityPredicates.idEqualTo(origApp.getId()));
         
-        RebindTestUtils.stopPersistence(origApp);
+        RebindTestUtils.stopPersistence(options.origManagementContext);
         
         newManagementContext = options.newManagementContext;
         newApp = (T) RebindTestUtils.rebind(options);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a5b34a72/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
index 975e65a..cb824ce 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
@@ -18,37 +18,37 @@
  */
 package org.apache.brooklyn.test.framework;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.LocationSpec;
-import org.apache.brooklyn.api.mgmt.ManagementContext;
-import org.apache.brooklyn.core.test.entity.TestApplication;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation;
 import org.apache.brooklyn.test.http.TestHttpRequestHandler;
 import org.apache.brooklyn.test.http.TestHttpServer;
 import org.apache.brooklyn.util.text.Identifiers;
 import org.apache.brooklyn.util.time.Duration;
 import org.apache.http.HttpStatus;
+import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-
-public class TestHttpCallTest {
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 
+public class TestHttpCallTest extends BrooklynAppUnitTestSupport {
 
     private TestHttpServer server;
-    private TestApplication app;
-    private ManagementContext managementContext;
     private LocalhostMachineProvisioningLocation loc;
     private String testId;
 
     @BeforeMethod(alwaysRun = true)
-    public void setup() {
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
         testId = Identifiers.makeRandomId(8);
         server = new TestHttpServer()
                 .handler("/201", new TestHttpRequestHandler()
@@ -62,12 +62,19 @@ public class TestHttpCallTest {
                         .response("{\"a\":\"b\",\"c\":\"d\",\"e\":123,\"g\":false}")
                         .code(200 + Identifiers.randomInt(99)))
                 .start();
-        app = TestApplication.Factory.newManagedInstanceForTests();
-        managementContext = app.getManagementContext();
-        loc = managementContext.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)
+        loc = mgmt.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)
                 .configure("name", testId));
     }
 
+    @AfterMethod(alwaysRun=true)
+    @Override
+    public void tearDown() throws Exception {
+        try {
+            super.tearDown();
+        } finally {
+            if (server != null) server.stop();
+        }
+    }
 
     @Test(groups = "Integration")
     public void testHttpBodyAssertions() {