package brooklyn.entity.webapp;

import java.util.Map;

import org.testng.Assert;
import org.testng.annotations.Test;

import brooklyn.entity.Entity;
import brooklyn.entity.basic.BasicConfigurableEntityFactory;
import brooklyn.entity.basic.ConfigurableEntityFactory;
import brooklyn.entity.webapp.ElasticJavaWebAppService.ElasticJavaWebAppServiceAwareLocation;
import brooklyn.location.basic.SimulatedLocation;
import brooklyn.test.entity.TestApplication;
import brooklyn.test.entity.TestApplicationImpl;
import brooklyn.test.entity.TestEntityImpl;
import brooklyn.util.collections.MutableMap;

import com.google.common.collect.ImmutableList;

public class ElasticCustomLocationTest {

    public static class MockWebServiceLocation extends SimulatedLocation implements ElasticJavaWebAppServiceAwareLocation {
        public MockWebServiceLocation() {
        }
        
        @Override
        public ConfigurableEntityFactory<ElasticJavaWebAppService> newWebClusterFactory() {
            return new BasicConfigurableEntityFactory(MockWebService.class);
        }
    }
    
    public static class MockWebService extends TestEntityImpl implements ElasticJavaWebAppService {
        public MockWebService() {
        } 
        // TODO Used by asicConfigurableEntityFactory.newEntity2, via MockWebServiceLocation.newWebClusterFactory
        public MockWebService(Map flags, Entity parent) {
            super(flags, parent);
        } 
    }
    
    @Test
    public void testElasticClusterCreatesTestEntity() {
        MockWebServiceLocation l = new MockWebServiceLocation();
        TestApplication app = new TestApplicationImpl();
        app.setConfig(MockWebService.ROOT_WAR, "WAR0");
        app.setConfig(MockWebService.NAMED_WARS, ImmutableList.of("ignore://WARn"));
        
        ElasticJavaWebAppService svc = 
            new ElasticJavaWebAppService.Factory().newFactoryForLocation(l).newEntity(MutableMap.of("war", "WAR1"), app);
        Assert.assertTrue(svc instanceof MockWebService, "expected MockWebService, got "+svc);
        //check config has been set correctly, where overridden, and where inherited
        Assert.assertEquals(svc.getConfig(MockWebService.ROOT_WAR), "WAR1");
        Assert.assertEquals(svc.getConfig(MockWebService.NAMED_WARS), ImmutableList.of("ignore://WARn"));
    }
    
}

@@ -109,44 +109,180 @@
 ons.
-Test;%0A%0Aimport brooklyn.entity.Entity
+AfterMethod;%0Aimport org.testng.annotations.BeforeMethod;%0Aimport org.testng.annotations.Test;%0A%0Aimport brooklyn.entity.Entity;%0Aimport brooklyn.entity.basic.ApplicationBuilder
 ;%0Aim
@@ -388,32 +388,71 @@
 eEntityFactory;%0A
+import brooklyn.entity.basic.Entities;%0A
 import brooklyn.
@@ -628,57 +628,8 @@
 on;%0A
-import brooklyn.test.entity.TestApplicationImpl;%0A
 impo
@@ -1562,28 +1562,377 @@
    %7D %0A    %7D%0A
+%0A
     
+private TestApplication app;%0A    %0A    @BeforeMethod(alwaysRun=true)%0A    public void setUp() throws Exception %7B%0A        app = ApplicationBuilder.newManagedApp(TestApplication.class);%0A    %7D%0A%0A    @AfterMethod(alwaysRun=true)%0A    public void tearDown() throws Exception %7B%0A        if (app != null) Entities.destroyAll(app.getManagementContext());%0A    %7D%0A
 %0A    @Test%0A 
@@ -2055,65 +2055,8 @@
 ();%0A
-        TestApplication app = new TestApplicationImpl();%0A
     
@@ -2363,16 +2363,55 @@
 , app);%0A
+        Entities.manage(svc);%0A        %0A
         

Fix ElasticCustomLocationTest