Parameters test coverage
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/9b985431 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/9b985431 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/9b985431 Branch: refs/heads/master Commit: 9b985431e82bef78c09710093f0518f80121495c Parents: 3965638 Author: Svetoslav Neykov <[email protected]> Authored: Tue Nov 3 17:31:49 2015 +0200 Committer: Svetoslav Neykov <[email protected]> Committed: Thu Nov 5 15:23:38 2015 +0200 ---------------------------------------------------------------------- .../objs/BasicSpecParameterFromClassTest.java | 29 +- .../camp/brooklyn/AbstractYamlTest.java | 7 +- .../brooklyn/catalog/CatalogParametersTest.java | 318 +++++++++++++++++++ 3 files changed, 351 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9b985431/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromClassTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromClassTest.java b/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromClassTest.java index 2009c98..b0f71a0 100644 --- a/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromClassTest.java +++ b/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromClassTest.java @@ -24,15 +24,27 @@ import java.util.List; import org.apache.brooklyn.api.catalog.CatalogConfig; import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.ImplementedBy; +import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.api.objs.SpecParameter; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.AbstractEntity; +import org.apache.brooklyn.core.entity.BrooklynConfigKeys; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.base.Predicate; import com.google.common.reflect.TypeToken; public class BasicSpecParameterFromClassTest { + private ManagementContext mgmt; + @BeforeMethod(alwaysRun=true) + public void setUp() { + mgmt = LocalManagementContextForTests.newInstance(); + } + public interface SpecParameterTestEntity extends Entity { @CatalogConfig(label="String Key", priority=3) ConfigKey<String> STRING_KEY = ConfigKeys.newStringConfigKey("string_key"); @@ -47,15 +59,30 @@ public class BasicSpecParameterFromClassTest { ConfigKey<String> UNPINNNED_KEY = ConfigKeys.newStringConfigKey("unpinned_key"); } + @ImplementedBy(ConfigInImplParameterTestEntityImpl.class) + public static interface ConfigInImplParameterTestEntity extends Entity {} + public static class ConfigInImplParameterTestEntityImpl extends AbstractEntity implements ConfigInImplParameterTestEntity { + public static final ConfigKey<String> SUGGESTED_VERSION = BrooklynConfigKeys.SUGGESTED_VERSION; + } + @Test public void testFullDefinition() { - List<SpecParameter<?>> inputs = BasicSpecParameter.fromClass(SpecParameterTestEntity.class); + List<SpecParameter<?>> inputs = BasicSpecParameter.fromClass(mgmt, SpecParameterTestEntity.class); + assertEquals(inputs.size(), 4); assertInput(inputs.get(0), "Predicate Key", true, SpecParameterTestEntity.PREDICATE_KEY); assertInput(inputs.get(1), "Integer Key", true, SpecParameterTestEntity.INTEGER_KEY); assertInput(inputs.get(2), "String Key", true, SpecParameterTestEntity.STRING_KEY); assertInput(inputs.get(3), "unpinned_key", false, SpecParameterTestEntity.UNPINNNED_KEY); } + @Test + public void testConfigInImplVisible() { + List<SpecParameter<?>> inputs = BasicSpecParameter.fromClass(mgmt, ConfigInImplParameterTestEntity.class); + assertEquals(inputs.size(), 1); + ConfigKey<String> key = ConfigInImplParameterTestEntityImpl.SUGGESTED_VERSION; + assertInput(inputs.get(0), key.getName(), false, key); + } + private void assertInput(SpecParameter<?> input, String label, boolean pinned, ConfigKey<?> type) { assertEquals(input.getLabel(), label); assertEquals(input.isPinned(), pinned); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9b985431/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractYamlTest.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractYamlTest.java index 8a858cf..a45d272 100644 --- a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractYamlTest.java +++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractYamlTest.java @@ -22,6 +22,7 @@ import java.io.Reader; import java.io.StringReader; import java.util.Set; +import org.apache.brooklyn.api.catalog.BrooklynCatalog; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntitySpec; import org.apache.brooklyn.api.mgmt.ManagementContext; @@ -52,6 +53,7 @@ public abstract class AbstractYamlTest { protected static final String TEST_VERSION = "0.1.2"; private ManagementContext brooklynMgmt; + protected BrooklynCatalog catalog; protected BrooklynCampPlatform platform; protected BrooklynCampPlatformLauncherNoServer launcher; private boolean forceUpdate; @@ -73,6 +75,7 @@ public abstract class AbstractYamlTest { }; launcher.launch(); brooklynMgmt = launcher.getBrooklynMgmt(); + catalog = brooklynMgmt.getCatalog(); platform = launcher.getCampPlatform(); } @@ -168,11 +171,11 @@ public abstract class AbstractYamlTest { return LOG; } - private String joinLines(Iterable<String> catalogYaml) { + protected String joinLines(Iterable<String> catalogYaml) { return Joiner.on("\n").join(catalogYaml); } - private String joinLines(String[] catalogYaml) { + protected String joinLines(String... catalogYaml) { return Joiner.on("\n").join(catalogYaml); } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9b985431/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogParametersTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogParametersTest.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogParametersTest.java new file mode 100644 index 0000000..6407d4a --- /dev/null +++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogParametersTest.java @@ -0,0 +1,318 @@ +/* + * 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.catalog; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; + +import java.util.Iterator; +import java.util.List; + +import org.apache.brooklyn.api.catalog.CatalogItem; +import org.apache.brooklyn.api.entity.Application; +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.internal.AbstractBrooklynObjectSpec; +import org.apache.brooklyn.api.objs.BrooklynObject; +import org.apache.brooklyn.api.objs.SpecParameter; +import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.AbstractEntity; +import org.apache.brooklyn.core.location.AbstractLocation; +import org.apache.brooklyn.core.mgmt.EntityManagementUtils; +import org.apache.brooklyn.core.objs.BasicSpecParameter; +import org.apache.brooklyn.core.policy.AbstractPolicy; +import org.apache.brooklyn.entity.stock.BasicApplication; +import org.testng.SkipException; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; + +public class CatalogParametersTest extends AbstractYamlTest { + private static final String SYMBOLIC_NAME = "my.catalog.app.id.load"; + + private static final ConfigKey<String> SHARED_CONFIG = ConfigKeys.newStringConfigKey("sample.config"); + public static class ConfigEntityForTest extends AbstractEntity { + public static final ConfigKey<String> SAMPLE_CONFIG = SHARED_CONFIG; + } + public static class ConfigPolicyForTest extends AbstractPolicy { + public static final ConfigKey<String> SAMPLE_CONFIG = SHARED_CONFIG; + } + public static class ConfigLocationForTest extends AbstractLocation { + private static final long serialVersionUID = 1L; + public static final ConfigKey<String> SAMPLE_CONFIG = SHARED_CONFIG; + } + + @DataProvider(name="brooklynTypes") + public Object[][] brooklynTypes() { + return new Object[][] { + {ConfigEntityForTest.class}, + {ConfigPolicyForTest.class}, + {ConfigLocationForTest.class}}; + } + + @DataProvider(name="catalogTemplates") + public Object[][] catalogTemplates() { + return new Object[][] { + {joinLines( + "brooklyn.catalog:", + " id: " + SYMBOLIC_NAME, + " version: " + TEST_VERSION, + " item:", + " type: ${testClass}", + " brooklyn.parameters:", + " - simple")}, + {joinLines( + "brooklyn.catalog:", + " id: " + SYMBOLIC_NAME, + " version: " + TEST_VERSION, + " brooklyn.parameters:", + " - simple", + " item:", + " type: ${testClass}")} + }; + } + + @DataProvider(name="typesAndTemplates") + public Object[][] typesAndTemplates() { + // cartesian product of brooklynTypes X catalogTemplates + Object[][] brooklynTypes = brooklynTypes(); + Object[][] catalogTemplates = catalogTemplates(); + Object[][] arr = new Object[brooklynTypes.length * catalogTemplates.length][]; + for (int i = 0; i < catalogTemplates.length; i++) { + for (int j = 0; j < brooklynTypes.length; j++) { + Object[] item = new Object[2]; + item[0] = brooklynTypes[j][0]; + item[1] = catalogTemplates[i][0]; + arr[i*brooklynTypes.length + j] = item; + } + } + return arr; + } + + @Test(dataProvider = "typesAndTemplates") + public void testParameters(Class<? extends BrooklynObject> testClass, String template) { + addCatalogItems(template.replace("${testClass}", testClass.getName())); + + ConfigKey<String> SIMPLE_CONFIG = ConfigKeys.newStringConfigKey("simple"); + SpecParameter<String> SIMPLE_PARAM = new BasicSpecParameter<>("simple", true, SIMPLE_CONFIG); + CatalogItem<?, ?> item = catalog.getCatalogItem(SYMBOLIC_NAME, TEST_VERSION); + assertEquals(item.getParameters(), ImmutableList.of(SIMPLE_PARAM)); + @SuppressWarnings({"unchecked", "rawtypes"}) + AbstractBrooklynObjectSpec<?,?> spec = catalog.createSpec((CatalogItem)item); + assertEquals(ImmutableSet.copyOf(spec.getParameters()), ImmutableList.of(SIMPLE_PARAM)); + } + + @Test(dataProvider = "brooklynTypes") + public void testDefaultParameters(Class<? extends BrooklynObject> testClass) { + addCatalogItems( + "brooklyn.catalog:", + " id: " + SYMBOLIC_NAME, + " version: " + TEST_VERSION, + " item:", + " type: "+ testClass.getName()); + + CatalogItem<?, ?> item = catalog.getCatalogItem(SYMBOLIC_NAME, TEST_VERSION); + assertEquals(ImmutableSet.copyOf(item.getParameters()), ImmutableSet.copyOf(BasicSpecParameter.fromClass(mgmt(), testClass))); + @SuppressWarnings({"unchecked", "rawtypes"}) + AbstractBrooklynObjectSpec<?,?> spec = (AbstractBrooklynObjectSpec<?,?>) catalog.createSpec((CatalogItem)item); + assertEquals(ImmutableSet.copyOf(spec.getParameters()), ImmutableSet.copyOf(BasicSpecParameter.fromClass(mgmt(),testClass))); + } + + + @Test + public void testRootParametersUnwrapped() { + addCatalogItems( + "brooklyn.catalog:", + " id: " + SYMBOLIC_NAME, + " version: " + TEST_VERSION, + " item:", + " services:", + " - type: " + ConfigEntityForTest.class.getName(), + " brooklyn.parameters:", + " - simple"); + + CatalogItem<?, ?> item = catalog.getCatalogItem(SYMBOLIC_NAME, TEST_VERSION); + List<SpecParameter<?>> inputs = item.getParameters(); + assertEquals(inputs.size(), 1); + SpecParameter<?> firstInput = inputs.get(0); + assertEquals(firstInput.getLabel(), "simple"); + } + + @Test + public void testExplicitParametersInMetaOverride() { + addCatalogItems( + "brooklyn.catalog:", + " id: " + SYMBOLIC_NAME, + " version: " + TEST_VERSION, + " brooklyn.parameters:", + " - metaSimple", + " item:", + " type: " + ConfigEntityForTest.class.getName(), + " brooklyn.parameters:", + " - simple"); + + CatalogItem<?, ?> item = catalog.getCatalogItem(SYMBOLIC_NAME, TEST_VERSION); + List<SpecParameter<?>> inputs = item.getParameters(); + assertEquals(inputs.size(), 1); + SpecParameter<?> firstInput = inputs.get(0); + assertEquals(firstInput.getLabel(), "metaSimple"); + } + + @Test(dataProvider="brooklynTypes") + public void testDepentantCatalogsInheritParameters(Class<? extends BrooklynObject> type) { + if (type == ConfigLocationForTest.class) { + //TODO + throw new SkipException("Locations don't inherit parameters, should migrate to the type registry first"); + } + addCatalogItems( + "brooklyn.catalog:", + " version: " + TEST_VERSION, + " items:", + " - id: paramItem", + " item:", + " type: " + type.getName(), + " brooklyn.parameters:", + " - simple", + " - id: " + SYMBOLIC_NAME, + " item:", + " type: paramItem:" + TEST_VERSION); + + CatalogItem<?, ?> item = catalog.getCatalogItem(SYMBOLIC_NAME, TEST_VERSION); + List<SpecParameter<?>> inputs = item.getParameters(); + assertEquals(inputs.size(), 1); + SpecParameter<?> firstInput = inputs.get(0); + assertEquals(firstInput.getLabel(), "simple"); + } + + @Test(dataProvider="brooklynTypes") + public void testDepentantCatalogsOverrideParameters(Class<? extends BrooklynObject> type) { + addCatalogItems( + "brooklyn.catalog:", + " version: " + TEST_VERSION, + " items:", + " - id: paramItem", + " item:", + " type: " + type.getName(), + " brooklyn.parameters:", + " - simple", + " - id: " + SYMBOLIC_NAME, + " item:", + " type: paramItem:" + TEST_VERSION, + " brooklyn.parameters:", + " - override"); + + CatalogItem<?, ?> item = catalog.getCatalogItem(SYMBOLIC_NAME, TEST_VERSION); + List<SpecParameter<?>> inputs = item.getParameters(); + assertEquals(inputs.size(), 1); + SpecParameter<?> firstInput = inputs.get(0); + assertEquals(firstInput.getLabel(), "override"); + } + + @Test + public void testChildEntitiyHasParameters() { + addCatalogItems( + "brooklyn.catalog:", + " id: " + SYMBOLIC_NAME, + " version: " + TEST_VERSION, + " items:", + " - item:", + " type: " + ConfigEntityForTest.class.getName(), + " brooklyn.children:", + " - type: " + ConfigEntityForTest.class.getName(), + " brooklyn.parameters:", + " - simple"); + + CatalogItem<?, ?> item = catalog.getCatalogItem(SYMBOLIC_NAME, TEST_VERSION); + @SuppressWarnings({ "rawtypes", "unchecked"}) + EntitySpec<?> parentSpec = (EntitySpec<?>) catalog.createSpec((CatalogItem)item); + EntitySpec<?> spec = parentSpec.getChildren().get(0); + SpecParameter<?> firstInput = spec.getParameters().get(0); + assertEquals(firstInput.getLabel(), "simple"); + } + + @Test + public void testAppSpecInheritsCatalogParameters() { + addCatalogItems( + "brooklyn.catalog:", + " version: " + TEST_VERSION, + " items:", + " - id: " + SYMBOLIC_NAME, + " item:", + " type: " + BasicApplication.class.getName(), + " brooklyn.parameters:", + " - simple"); + + EntitySpec<? extends Application> spec = EntityManagementUtils.createEntitySpecForApplication(mgmt(), joinLines( + "services:", + "- type: " + ver(SYMBOLIC_NAME))); + List<SpecParameter<?>> params = spec.getParameters(); + assertEquals(params.size(), 1); + SpecParameter<?> firstInput = params.get(0); + assertEquals(firstInput.getLabel(), "simple"); + } + + @Test + public void testParametersCoercedOnSetAndReferences() throws Exception { + Integer testValue = Integer.valueOf(55); + addCatalogItems( + "brooklyn.catalog:", + " id: " + SYMBOLIC_NAME, + " version: " + TEST_VERSION, + " brooklyn.parameters:", + " - name: num", + " type: integer", + " item:", + " type: " + BasicApplication.class.getName(), + " brooklyn.children:", + " - type: " + ConfigEntityForTest.class.getName(), + " brooklyn.config:", + " refConfig: $brooklyn:scopeRoot().config(\"num\")", + " - type: " + ConfigEntityForTest.class.getName(), + " brooklyn.config:", + " refConfig: $brooklyn:config(\"num\")"); //inherited config + + Entity app = createAndStartApplication( + "services:", + "- type: " + BasicApplication.class.getName(), + " brooklyn.children:", + " - type: " + ver(SYMBOLIC_NAME), + " brooklyn.config:", + " num: \"" + testValue + "\""); + + Entity scopeRoot = Iterables.getOnlyElement(app.getChildren()); + + ConfigKey<Object> numKey = ConfigKeys.newConfigKey(Object.class, "num"); + assertEquals(scopeRoot.config().get(numKey), testValue); + + ConfigKey<Object> refConfigKey = ConfigKeys.newConfigKey(Object.class, "refConfig"); + + Iterator<Entity> childIter = scopeRoot.getChildren().iterator(); + Entity c1 = childIter.next(); + assertEquals(c1.config().get(refConfigKey), testValue); + Entity c2 = childIter.next(); + assertEquals(c2.config().get(refConfigKey), testValue); + assertFalse(childIter.hasNext()); + } + +}
