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/10/11 14:52:16 UTC

[2/3] brooklyn-server git commit: BROOKLYN-360: jclouds extensions config coerced from string

BROOKLYN-360: jclouds extensions config coerced from string

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

Branch: refs/heads/master
Commit: a676b3134f40ac9498ec7c8aafe1b3bcafd6c331
Parents: 9b7a0e6
Author: Aled Sage <al...@gmail.com>
Authored: Tue Oct 11 12:25:33 2016 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Oct 11 12:25:33 2016 +0100

----------------------------------------------------------------------
 .../brooklyn/core/config/MapConfigKey.java      | 13 +++-
 .../brooklyn/core/entity/EntityConfigTest.java  | 26 ++++++++
 .../JcloudsLocationExtensionStubbedTest.java    | 67 ++++++++++++++++++++
 3 files changed, 104 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a676b313/core/src/main/java/org/apache/brooklyn/core/config/MapConfigKey.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/config/MapConfigKey.java b/core/src/main/java/org/apache/brooklyn/core/config/MapConfigKey.java
index b27ba12..a0b9bd7 100644
--- a/core/src/main/java/org/apache/brooklyn/core/config/MapConfigKey.java
+++ b/core/src/main/java/org/apache/brooklyn/core/config/MapConfigKey.java
@@ -32,6 +32,8 @@ import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.internal.AbstractStructuredConfigKey;
 import org.apache.brooklyn.util.collections.Jsonya;
 import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -183,8 +185,15 @@ public class MapConfigKey<V> extends AbstractStructuredConfigKey<Map<String,V>,M
             return ((StructuredModification)value).applyToKeyInMap(this, target);
         if (value instanceof Map.Entry)
             return applyEntryValueToMap((Map.Entry)value, target);
-        if (!(value instanceof Map)) 
-            throw new IllegalArgumentException("Cannot set non-map entries "+value+" on "+this);
+        if (!(value instanceof Map)) {
+            Maybe<Map> coercedValue = TypeCoercions.tryCoerce(value, Map.class);
+            if (coercedValue.isPresent()) {
+                log.trace("Coerced value for {} from type {} to map", this, value.getClass().getName());
+                value = coercedValue.get();
+            } else {
+                throw new IllegalArgumentException("Cannot set non-map entries on "+this+", given type "+value.getClass().getName()+", value "+value);
+            }
+        }
         
         Map result = new MutableMap();
         for (Object entry: ((Map)value).entrySet()) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a676b313/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
index 8e7603b..c25e6d2 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
@@ -209,6 +209,32 @@ public class EntityConfigTest extends BrooklynAppUnitTestSupport {
         assertEquals(entity.config().getNonBlocking(TestEntity.CONF_MAP_THING.subKey("mysub")).get(), "myval");
     }
     
+    @Test
+    public void testGetConfigMapWithCoercedStringToMap() throws Exception {
+        TestEntity entity = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+                .configure(TestEntity.CONF_MAP_THING.getName(), "{mysub: myval}"));
+        assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "myval"));
+        
+        TestEntity entity2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+                .configure(TestEntity.CONF_MAP_THING.getName(), "{mysub: {sub2: myval}}"));
+        assertEquals(entity2.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", ImmutableMap.of("sub2", "myval")));
+    }
+    
+    @Test
+    public void testGetConfigMapWithSubValueAsStringNotCoerced() throws Exception {
+        TestEntity entity = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+                .configure(TestEntity.CONF_MAP_THING, ImmutableMap.of("mysub", "{a: b}")));
+        assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "{a: b}"));
+        
+        TestEntity entity2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+                .configure(TestEntity.CONF_MAP_THING.subKey("mysub"), "{a: b}"));
+        assertEquals(entity2.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "{a: b}"));
+        
+        TestEntity entity3 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
+                .configure(TestEntity.CONF_MAP_THING.getName(), ImmutableMap.of("mysub", "{a: b}")));
+        assertEquals(entity3.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "{a: b}"));
+    }
+    
     // TODO This now fails because the task has been cancelled, in entity.config().get().
     // But it used to pass (e.g. with commit 56fcc1632ea4f5ac7f4136a7e04fabf501337540).
     // It failed after the rename of CONF_MAP_THING_OBJ to CONF_MAP_OBJ_THING, which 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a676b313/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsLocationExtensionStubbedTest.java
----------------------------------------------------------------------
diff --git a/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsLocationExtensionStubbedTest.java b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsLocationExtensionStubbedTest.java
new file mode 100644
index 0000000..c192297
--- /dev/null
+++ b/locations/jclouds/src/test/java/org/apache/brooklyn/location/jclouds/JcloudsLocationExtensionStubbedTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.location.jclouds;
+
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+import org.apache.brooklyn.api.location.MachineLocation;
+import org.apache.brooklyn.core.internal.BrooklynProperties;
+import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
+import org.apache.brooklyn.util.executor.HttpExecutorFactory;
+import org.apache.brooklyn.util.executor.HttpExecutorFactoryImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+public class JcloudsLocationExtensionStubbedTest extends AbstractJcloudsStubbedUnitTest {
+
+    @SuppressWarnings("unused")
+    private static final Logger log = LoggerFactory.getLogger(JcloudsLocationExtensionStubbedTest.class);
+    
+    @Override
+    protected LocalManagementContext newManagementContext() {
+        LocalManagementContext result = super.newManagementContext();
+        BrooklynProperties brooklynProperties = result.getBrooklynProperties();
+        brooklynProperties.put("brooklyn.location.named.jclouds-with-extension", super.getLocationSpec());
+        brooklynProperties.put("brooklyn.location.named.jclouds-with-extension.extensions", 
+                "{ "+HttpExecutorFactory.class.getName()+": "+MyHttpExecutorFactory.class.getName()+" }");
+       return result;
+    }
+
+    @Override
+    protected String getLocationSpec() {
+        return "jclouds-with-extension";
+    }
+
+    @Test
+    public void testHasExtension() throws Exception {
+        initNodeCreatorAndJcloudsLocation(newVanillaNodeCreator(), ImmutableMap.of());
+        MachineLocation machine = jcloudsLocation.obtain();
+        
+        HttpExecutorFactory extension = machine.getExtension(HttpExecutorFactory.class);
+        assertNotNull(extension);
+        assertTrue(extension instanceof MyHttpExecutorFactory, "extension="+extension);
+    }
+    
+    public static class MyHttpExecutorFactory extends HttpExecutorFactoryImpl {
+    }
+}