From e9ea495487c031e72d95675c28eb352290d74e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gr=C3=A4ff?= Date: Wed, 23 Aug 2017 00:01:33 +1000 Subject: [PATCH] core/ConfigUtil: Do not normalize objectClass, component.name, component.id configuration values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: David Gräff --- .../config/core/ConfigUtilTest.groovy | 45 --------------- .../smarthome/config/core/ConfigUtilTest.java | 57 +++++++++++++++++++ .../smarthome/config/core/ConfigUtil.java | 22 +++++-- 3 files changed, 74 insertions(+), 50 deletions(-) delete mode 100644 bundles/config/org.eclipse.smarthome.config.core.test/src/test/groovy/org/eclipse/smarthome/config/core/ConfigUtilTest.groovy create mode 100644 bundles/config/org.eclipse.smarthome.config.core.test/src/test/java/org/eclipse/smarthome/config/core/ConfigUtilTest.java diff --git a/bundles/config/org.eclipse.smarthome.config.core.test/src/test/groovy/org/eclipse/smarthome/config/core/ConfigUtilTest.groovy b/bundles/config/org.eclipse.smarthome.config.core.test/src/test/groovy/org/eclipse/smarthome/config/core/ConfigUtilTest.groovy deleted file mode 100644 index 5cc2757b18d..00000000000 --- a/bundles/config/org.eclipse.smarthome.config.core.test/src/test/groovy/org/eclipse/smarthome/config/core/ConfigUtilTest.groovy +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright (c) 2014-2017 by the respective copyright holders. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.smarthome.config.core - -import static org.hamcrest.CoreMatchers.* -import static org.junit.Assert.* -import static org.junit.matchers.JUnitMatchers.* - -import org.eclipse.smarthome.config.core.ConfigDescriptionParameter.Type -import org.junit.Test - - -/** - * - * @author Simon Kaufmann - initial contribution and API. - * - */ -class ConfigUtilTest { - - @Test - def void 'assert that the first config description wins for normalization'() { - ConfigDescription configDescriptionInteger = new ConfigDescription(new URI("thing:fooThing"), [ - new ConfigDescriptionParameter("foo", Type.INTEGER) - ]) - ConfigDescription configDescriptionString = new ConfigDescription(new URI("thingType:fooThing"), [ - new ConfigDescriptionParameter("foo", Type.TEXT) - ]) - - assertThat ConfigUtil.normalizeTypes(["foo":"1"], [configDescriptionInteger]).get("foo"), is(instanceOf(BigDecimal)) - assertThat ConfigUtil.normalizeTypes(["foo":"1"], [configDescriptionString]).get("foo"), is(instanceOf(String)) - assertThat ConfigUtil.normalizeTypes(["foo":"1"], [ - configDescriptionInteger, - configDescriptionString - ]).get("foo"), is(instanceOf(BigDecimal)) - assertThat ConfigUtil.normalizeTypes(["foo":"1"], [ - configDescriptionString, - configDescriptionInteger - ]).get("foo"), is(instanceOf(String)) - } -} diff --git a/bundles/config/org.eclipse.smarthome.config.core.test/src/test/java/org/eclipse/smarthome/config/core/ConfigUtilTest.java b/bundles/config/org.eclipse.smarthome.config.core.test/src/test/java/org/eclipse/smarthome/config/core/ConfigUtilTest.java new file mode 100644 index 00000000000..2e43d91e227 --- /dev/null +++ b/bundles/config/org.eclipse.smarthome.config.core.test/src/test/java/org/eclipse/smarthome/config/core/ConfigUtilTest.java @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2014-2017 by the respective copyright holders. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.smarthome.config.core; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + +import java.math.BigDecimal; +import java.net.*; +import java.util.*; + +import org.eclipse.smarthome.config.core.ConfigDescriptionParameter.Type; +import org.junit.Test; + +/** + * + * @author Simon Kaufmann - initial contribution and API. + * + */ +class ConfigUtilTest { + private Map m(String a, Object b) { + Map m = new HashMap<>(); + m.put(a, b); + return m; + } + + private static class L extends ArrayList { + L(T... args) { + for (T arg : args) { + add(arg); + } + } + }; + + @Test + public void firstDesciptionWinsForNormalization() throws URISyntaxException { + ConfigDescription configDescriptionInteger = new ConfigDescription(new URI("thing:fooThing"), + new L<>(new ConfigDescriptionParameter("foo", Type.INTEGER))); + + ConfigDescription configDescriptionString = new ConfigDescription(new URI("thingType:fooThing"), + new L<>(new ConfigDescriptionParameter("foo", Type.TEXT))); + + assertThat(ConfigUtil.normalizeTypes(m("foo", "1"), new L<>(configDescriptionInteger)).get("foo"), + is(instanceOf(BigDecimal.class))); + assertThat(ConfigUtil.normalizeTypes(m("foo", "1"), new L<>(configDescriptionString)).get("foo"), + is(instanceOf(String.class))); + assertThat(ConfigUtil.normalizeTypes(m("foo", "1"), new L<>(configDescriptionInteger, configDescriptionString)) + .get("foo"), is(instanceOf(BigDecimal.class))); + assertThat(ConfigUtil.normalizeTypes(m("foo", "1"), new L<>(configDescriptionString, configDescriptionInteger)) + .get("foo"), is(instanceOf(String.class))); + } +} diff --git a/bundles/config/org.eclipse.smarthome.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java b/bundles/config/org.eclipse.smarthome.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java index 78be8d1ae82..223499edc4f 100644 --- a/bundles/config/org.eclipse.smarthome.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java +++ b/bundles/config/org.eclipse.smarthome.config.core/src/main/java/org/eclipse/smarthome/config/core/ConfigUtil.java @@ -27,6 +27,14 @@ * @author Thomas Höfer - Minor changes for type normalization based on config description */ public class ConfigUtil { + /** + * We do not want to handle or try to normalize OSGi provided configuration parameters + * + * @param name The configuration parameter name + */ + private static boolean isOSGiConfigParameter(String name) { + return name.equals("objectClass") || name.equals("component.name") || name.equals("component.id"); + } /** * Normalizes the types to the ones allowed for configurations. @@ -35,11 +43,13 @@ public class ConfigUtil { * @return normalized configuration */ public static Map normalizeTypes(Map configuration) { - Map convertedConfiguration = new HashMap(configuration.size()); + Map convertedConfiguration = new HashMap<>(configuration.size()); for (Entry parameter : configuration.entrySet()) { String name = parameter.getKey(); Object value = parameter.getValue(); - convertedConfiguration.put(name, normalizeType(value)); + if (!isOSGiConfigParameter(name)) { + convertedConfiguration.put(name, normalizeType(value)); + } } return convertedConfiguration; } @@ -129,7 +139,7 @@ public static Map normalizeTypes(Map configurati return null; } - Map convertedConfiguration = new HashMap(configuration.size()); + Map convertedConfiguration = new HashMap<>(); Map configParams = new HashMap<>(); for (int i = configDescriptions.size() - 1; i >= 0; i--) { @@ -138,8 +148,10 @@ public static Map normalizeTypes(Map configurati for (Entry parameter : configuration.entrySet()) { String name = parameter.getKey(); Object value = parameter.getValue(); - ConfigDescriptionParameter configDescriptionParameter = configParams.get(name); - convertedConfiguration.put(name, normalizeType(value, configDescriptionParameter)); + if (!isOSGiConfigParameter(name)) { + ConfigDescriptionParameter configDescriptionParameter = configParams.get(name); + convertedConfiguration.put(name, normalizeType(value, configDescriptionParameter)); + } } return convertedConfiguration; }