Add external-config unit tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/b0d321cc Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/b0d321cc Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/b0d321cc Branch: refs/heads/master Commit: b0d321cc37f172fdf531fa28ce1f569053eedd80 Parents: 52647af Author: Aled Sage <[email protected]> Authored: Mon Sep 14 16:11:50 2015 +0100 Committer: Aled Sage <[email protected]> Committed: Mon Sep 14 16:42:44 2015 +0100 ---------------------------------------------------------------------- .../ExternalConfigSupplierRegistryTest.java | 72 +++++++++++++ .../camp/brooklyn/ExternalConfigYamlTest.java | 107 +++++++++++++++++++ 2 files changed, 179 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b0d321cc/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistryTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistryTest.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistryTest.java new file mode 100644 index 0000000..1ce0299 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/ExternalConfigSupplierRegistryTest.java @@ -0,0 +1,72 @@ +/* + * 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.mgmt.internal; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.fail; + +import java.util.Map; + +import org.apache.brooklyn.core.config.external.ExternalConfigSupplier; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; + +public class ExternalConfigSupplierRegistryTest extends BrooklynAppUnitTestSupport { + + @Test + public void testLooksUpSupplier() throws Exception { + MyExternalConfigSupplier supplier1 = new MyExternalConfigSupplier(ImmutableMap.of("mykey", "myval1")); + mgmt.getExternalConfigProviderRegistry().addProvider("myprovider1", supplier1); + assertEquals(mgmt.getExternalConfigProviderRegistry().getConfig("myprovider1", "mykey"), "myval1"); + assertNull(mgmt.getExternalConfigProviderRegistry().getConfig("myprovider1", "wrongkey")); + + MyExternalConfigSupplier supplier2 = new MyExternalConfigSupplier(ImmutableMap.of("mykey", "myval2")); + mgmt.getExternalConfigProviderRegistry().addProvider("myprovider2", supplier2); + assertEquals(mgmt.getExternalConfigProviderRegistry().getConfig("myprovider2", "mykey"), "myval2"); + } + + @Test + public void testExceptionIfSupplierDoesNotExist() throws Exception { + try { + assertNull(mgmt.getExternalConfigProviderRegistry().getConfig("wrongprovider", "mykey")); + fail(); + } catch (IllegalArgumentException e) { + if (!e.toString().contains("No provider found with name")) throw e; + } + } + + private static class MyExternalConfigSupplier implements ExternalConfigSupplier { + private final Map<String, String> conf; + + public MyExternalConfigSupplier(Map<String, String> conf) { + this.conf = conf; + } + + @Override public String getName() { + return "myprovider"; + } + + @Override public String get(String key) { + return conf.get(key); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b0d321cc/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java new file mode 100644 index 0000000..274056c --- /dev/null +++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ExternalConfigYamlTest.java @@ -0,0 +1,107 @@ +/* + * 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.camp.brooklyn; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + +import java.io.StringReader; +import java.util.Map; + +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.config.external.AbstractExternalConfigSupplier; +import org.apache.brooklyn.core.internal.BrooklynProperties; +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.apache.brooklyn.core.test.entity.TestApplication; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +import com.google.common.base.Joiner; + +@Test +public class ExternalConfigYamlTest extends AbstractYamlTest { + private static final Logger log = LoggerFactory.getLogger(ExternalConfigYamlTest.class); + + @Override + protected LocalManagementContext newTestManagementContext() { + BrooklynProperties props = BrooklynProperties.Factory.newEmpty(); + props.put("brooklyn.external.myprovider", MyExternalConfigSupplier.class.getName()); + props.put("brooklyn.external.myprovider.mykey", "myval"); + + return LocalManagementContextForTests.builder(true) + .useProperties(props) + .build(); + } + + @Test + public void testExternalisedConfigReferencedFromYaml() throws Exception { + ConfigKey<String> MY_CONFIG_KEY = ConfigKeys.newStringConfigKey("my.config.key"); + + String yaml = Joiner.on("\n").join( + "services:", + "- serviceType: org.apache.brooklyn.core.test.entity.TestApplication", + " brooklyn.config:", + " my.config.key: $brooklyn:external(\"myprovider\", \"mykey\")"); + + TestApplication app = (TestApplication) createAndStartApplication(new StringReader(yaml)); + waitForApplicationTasks(app); + + assertEquals(app.getConfig(MY_CONFIG_KEY), "myval"); + } + + @Test + public void testWhenExternalisedConfigSupplierDoesNotExist() throws Exception { + BrooklynProperties props = BrooklynProperties.Factory.newEmpty(); + props.put("brooklyn.external.myprovider", "wrong.classname.DoesNotExist"); + + try { + LocalManagementContextForTests.builder(true) + .useProperties(props) + .build(); + fail(); + } catch (Exception e) { + if (Exceptions.getFirstThrowableOfType(e, ClassNotFoundException.class) == null) { + throw e; + } + } + } + + @Override + protected Logger getLogger() { + return log; + } + + public static class MyExternalConfigSupplier extends AbstractExternalConfigSupplier { + private final Map<String, String> conf; + + public MyExternalConfigSupplier(ManagementContext mgmt, String name, Map<String, String> conf) { + super(mgmt, name); + this.conf = conf; + } + + @Override public String get(String key) { + return conf.get(key); + } + } +}
