Repository: brooklyn-server
Updated Branches:
  refs/heads/master e997d9abd -> 7f1846c0f


Fix order of provisioner check and add tests for LocationEntity


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

Branch: refs/heads/master
Commit: 70baabbd9b4b0a2b6c4af0bcf265d48f9b5aa125
Parents: 6615ddf
Author: Andrew Donald Kennedy <[email protected]>
Authored: Tue Nov 22 13:52:00 2016 +0000
Committer: Andrew Donald Kennedy <[email protected]>
Committed: Tue Nov 22 13:52:15 2016 +0000

----------------------------------------------------------------------
 .../entity/stock/LocationEntityImpl.java        |  16 +-
 .../entity/stock/LocationEntityTest.java        | 182 +++++++++++++++++++
 2 files changed, 190 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/70baabbd/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java 
b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
index fbfcdc5..eee147e 100644
--- 
a/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
+++ 
b/core/src/main/java/org/apache/brooklyn/entity/stock/LocationEntityImpl.java
@@ -79,17 +79,17 @@ public class LocationEntityImpl extends BasicStartableImpl 
implements LocationEn
             if (child == null && specMap.size() > 0) {
                 // Determine provisioning location
                 MachineProvisioningLocation<?> provisioner = null;
-                Maybe<? extends Location> location = 
Machines.findUniqueElement(locations, MachineLocation.class);
+                Maybe<? extends Location> location = 
Machines.findUniqueElement(locations, MachineProvisioningLocation.class);
                 if (location.isPresent()) {
-                    Location parent = location.get().getParent();
-                    while (parent != null && ! (parent instanceof 
MachineProvisioningLocation)) {
-                        parent = parent.getParent();
-                    }
-                    provisioner = (MachineProvisioningLocation<?>) parent;
+                    provisioner = (MachineProvisioningLocation<?>) 
location.get();
                 } else {
-                    location = Machines.findUniqueElement(locations, 
MachineProvisioningLocation.class);
+                    location = Machines.findUniqueElement(locations, 
MachineLocation.class);
                     if (location.isPresent()) {
-                        provisioner = (MachineProvisioningLocation<?>) 
location.get();
+                        Location parent = location.get().getParent();
+                        while (parent != null && ! (parent instanceof 
MachineProvisioningLocation)) {
+                            parent = parent.getParent();
+                        }
+                        provisioner = (MachineProvisioningLocation<?>) parent;
                     }
                 }
                 if (provisioner == null) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/70baabbd/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java 
b/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java
new file mode 100644
index 0000000..009841f
--- /dev/null
+++ 
b/core/src/test/java/org/apache/brooklyn/entity/stock/LocationEntityTest.java
@@ -0,0 +1,182 @@
+/*
+ * 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.entity.stock;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Map;
+
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.location.LocationSpec;
+import org.apache.brooklyn.core.entity.EntityAsserts;
+import org.apache.brooklyn.core.entity.trait.Startable;
+import org.apache.brooklyn.core.location.LocationConfigKeys;
+import org.apache.brooklyn.core.location.SimulatedLocation;
+import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+
+public class LocationEntityTest extends BrooklynAppUnitTestSupport {
+
+    private SimulatedLocation loc1, loc2, loc3;
+    private LocationEntity entity;
+
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        loc1 = 
mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class));
+        loc2 = 
mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class).configure(LocationConfigKeys.CLOUD_PROVIDER,
 "cloud"));
+        loc3 = 
mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class).configure(LocationConfigKeys.ISO_3166,
 ImmutableSet.of("UK", "US")));
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithType() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, 
EntitySpec<?>>builder()
+                        .put("OtherLocation", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .put("SimulatedLocation", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, 
"Simulated"))
+                        .put("default", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, 
"Simulated");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithDefault() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, 
EntitySpec<?>>builder()
+                        .put("default", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, 
"Default");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithWrongTypeAndDefault() 
throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, 
EntitySpec<?>>builder()
+                        .put("OtherLocation", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .put("default", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, 
"Default");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithWrongTypeAndNoDefault() 
throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, 
EntitySpec<?>>builder()
+                        .put("OtherLocation", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc1));
+
+        assertTrue(entity.getChildren().isEmpty());
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_ENTITY, null);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithProvider() throws Exception 
{
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, 
EntitySpec<?>>builder()
+                        .put("cloud", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Cloud"))
+                        .put("other", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Other"))
+                        .put("default", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc2));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_PROVIDER, "cloud");
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, "Cloud");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+    @Test
+    public void testLocationEntityConfigurationWithCountry() throws Exception {
+        Map<String, EntitySpec<?>> map = ImmutableMap.<String, 
EntitySpec<?>>builder()
+                        .put("UK", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "United 
Kingdom"))
+                        .put("DE", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "Germany"))
+                        .put("default", 
EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, "default"))
+                        .build();
+        entity = app.addChild(EntitySpec.create(LocationEntity.class)
+                .configure(LocationEntity.LOCATION_ENTITY_SPEC_MAP, map));
+        app.start(ImmutableList.of(loc3));
+
+        assertEquals(entity.getChildren().size(), 1);
+        Entity child = Iterables.getOnlyElement(entity.getChildren());
+        assertTrue(child instanceof TestEntity);
+
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_TYPE, "SimulatedLocation");
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_COUNTRY_CODES, ImmutableSet.of("UK", "US"));
+        EntityAsserts.assertAttributeEqualsEventually(entity, 
LocationEntity.LOCATION_ENTITY, child);
+        EntityAsserts.assertConfigEquals(child, TestEntity.CONF_NAME, "United 
Kingdom");
+        EntityAsserts.assertEntityHealthy(child);
+        EntityAsserts.assertEntityHealthy(entity);
+    }
+
+}

Reply via email to