Repository: brooklyn-server Updated Branches: refs/heads/master f85674e55 -> 3ad3045c7
EntityInitializers.resolve config: return default val Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/f6a2ed15 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/f6a2ed15 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/f6a2ed15 Branch: refs/heads/master Commit: f6a2ed158400761a30b4c9f82e353f11b33e1934 Parents: 8d382ab Author: Aled Sage <aled.s...@gmail.com> Authored: Mon Jan 29 16:41:23 2018 +0000 Committer: Aled Sage <aled.s...@gmail.com> Committed: Mon Jan 29 16:41:23 2018 +0000 ---------------------------------------------------------------------- .../core/entity/EntityInitializers.java | 3 +- .../core/entity/EntityInitializersTest.java | 67 ++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f6a2ed15/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java index cffcced7..b712431 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java @@ -70,7 +70,8 @@ public class EntityInitializers { public static <T> T resolve(ConfigBag configBag, ConfigKey<T> key, ExecutionContext executionContext) { if (key instanceof ConfigKeySelfExtracting && executionContext != null) { ConfigKeySelfExtracting<T> ckse = ((ConfigKeySelfExtracting<T>) key); - return ckse.extractValue(configBag.getAllConfigAsConfigKeyMap(), executionContext); + T result = ckse.extractValue(configBag.getAllConfigAsConfigKeyMap(), executionContext); + return (result != null || configBag.containsKey(key.getName())) ? result : key.getDefaultValue(); } return configBag.get(key); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/f6a2ed15/core/src/test/java/org/apache/brooklyn/core/entity/EntityInitializersTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityInitializersTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntityInitializersTest.java new file mode 100644 index 0000000..579e93c --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityInitializersTest.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.core.entity; + +import static org.testng.Assert.assertEquals; + +import java.util.Map; + +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.util.core.config.ConfigBag; +import org.apache.brooklyn.util.core.task.DeferredSupplier; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; + +public class EntityInitializersTest extends BrooklynAppUnitTestSupport { + + private ConfigKey<String> keyWithDefault = ConfigKeys.newStringConfigKey("mykey", "mydescription", "mydefault"); + + @Test + public void testResolveSimple() throws Exception { + // Should return config val + assertEquals(resolve(ImmutableMap.of("mykey", "myval"), keyWithDefault), "myval"); + + // Should return explicit null val + assertEquals(resolve(MutableMap.of("mykey", null), keyWithDefault), null); + } + + @Test + public void testResolveDeferredSupplier() throws Exception { + DeferredSupplier<String> supplier = new DeferredSupplier<String>() { + @Override public String get() { + return "myval"; + } + }; + assertEquals(resolve(ImmutableMap.of("mykey", supplier), keyWithDefault), "myval"); + } + + @Test + public void testResolveUsesDefaultIfAbsent() throws Exception { + // No config value so should return default + assertEquals(resolve(ImmutableMap.of(), keyWithDefault), "mydefault"); + } + + private Object resolve(Map<?,?> config, ConfigKey<?> key) { + return EntityInitializers.resolve(ConfigBag.newInstance(config), key, app.getExecutionContext()); + } +}