Search in sources :

Example 11 with ConfigDescription

use of org.eclipse.smarthome.config.core.ConfigDescription in project smarthome by eclipse.

the class ConfigDescriptionsTest method ConfigDescriptionsShouldLoadProperly.

@Test
public void ConfigDescriptionsShouldLoadProperly() throws Exception {
    int initialNumberOfConfigDescriptions = configDescriptionRegistry.getConfigDescriptions().size();
    // install test bundle
    Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(bundle, is(notNullValue()));
    Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions();
    assertThat(configDescriptions.size(), is(initialNumberOfConfigDescriptions + 3));
    URI bridgeURI = new URI("thing-type:hue:bridge");
    ConfigDescription bridgeConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(bridgeURI)).findFirst().get();
    assertThat(bridgeConfigDescription, is(notNullValue()));
    Collection<ConfigDescriptionParameter> parameters = bridgeConfigDescription.getParameters();
    assertThat(parameters.size(), is(2));
    ConfigDescriptionParameter ipParameter = parameters.stream().filter(it -> it.getName().equals("ip")).findFirst().get();
    assertThat(ipParameter, is(notNullValue()));
    assertThat(ipParameter.getType(), is(Type.TEXT));
    assertThat(ipParameter.getContext(), is("network-address"));
    assertThat(ipParameter.getLabel(), is("Network Address"));
    assertThat(ipParameter.getDescription(), is("Network address of the hue bridge."));
    assertThat(ipParameter.isRequired(), is(true));
    ConfigDescriptionParameter userNameParameter = parameters.stream().filter(it -> it.getName().equals("username")).findFirst().get();
    assertThat(userNameParameter, is(notNullValue()));
    assertThat(userNameParameter.isAdvanced(), is(true));
    assertThat(userNameParameter.getType(), is(Type.TEXT));
    assertThat(userNameParameter.getContext(), is("password"));
    assertThat(userNameParameter.getLabel(), is("Username"));
    assertThat(userNameParameter.getDescription(), is("Name of a registered hue bridge user, that allows to access the API."));
    URI colorURI = new URI("channel-type:hue:color");
    ConfigDescription colorConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(colorURI)).findFirst().get();
    assertThat(colorConfigDescription, is(notNullValue()));
    parameters = colorConfigDescription.getParameters();
    assertThat(parameters.size(), is(1));
    ConfigDescriptionParameter lastDimValueParameter = parameters.stream().filter(it -> it.getName().equals("lastDimValue")).findFirst().get();
    assertThat(lastDimValueParameter, is(notNullValue()));
    assertThat(lastDimValueParameter.getType(), is(Type.BOOLEAN));
    Collection<ConfigDescriptionParameterGroup> groups = bridgeConfigDescription.getParameterGroups();
    assertThat(groups.size(), is(2));
    ConfigDescriptionParameterGroup group1 = groups.stream().filter(it -> it.getName().equals("group1")).findFirst().get();
    assertThat(group1, is(notNullValue()));
    assertThat(group1.getContext(), is("Group1-context"));
    assertThat(group1.getLabel(), is("Group Label 1"));
    assertThat(group1.getDescription(), is("Group description 1"));
    // uninstall test bundle
    bundle.uninstall();
    assertThat(bundle.getState(), is(Bundle.UNINSTALLED));
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) Bundle(org.osgi.framework.Bundle) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URI(java.net.URI) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 12 with ConfigDescription

use of org.eclipse.smarthome.config.core.ConfigDescription in project smarthome by eclipse.

the class ConfigDescriptionsTest method parametersWithOptionsAndFiltersShouldLoadProperly.

@Test
public void parametersWithOptionsAndFiltersShouldLoadProperly() throws Exception {
    int initialNumberOfConfigDescriptions = configDescriptionRegistry.getConfigDescriptions().size();
    // install test bundle
    Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(bundle, is(notNullValue()));
    Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions();
    assertThat(configDescriptions.size(), is(initialNumberOfConfigDescriptions + 3));
    URI dummyURI = new URI("thing-type:hue:dummy");
    ConfigDescription bridgeConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(dummyURI)).findFirst().get();
    assertThat(bridgeConfigDescription, is(notNullValue()));
    Collection<ConfigDescriptionParameter> parameters = bridgeConfigDescription.getParameters();
    assertThat(parameters.size(), is(2));
    ConfigDescriptionParameter unitParameter = parameters.stream().filter(it -> it.getName().equals("unit")).findFirst().get();
    assertThat(unitParameter, is(notNullValue()));
    assertThat(join(unitParameter.getOptions(), ","), is("ParameterOption [value=\"us\", label=\"US\"],ParameterOption [value=\"metric\", label=\"Metric\"]"));
    ConfigDescriptionParameter lightParameter = parameters.stream().filter(it -> it.getName().equals("color-alarming-light")).findFirst().get();
    assertThat(lightParameter, is(notNullValue()));
    assertThat(join(lightParameter.getFilterCriteria(), ","), is("FilterCriteria [name=\"tags\", value=\"alarm, light\"],FilterCriteria [name=\"type\", value=\"color\"],FilterCriteria [name=\"binding-id\", value=\"hue\"]"));
}
Also used : Bundle(org.osgi.framework.Bundle) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URI(java.net.URI) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 13 with ConfigDescription

use of org.eclipse.smarthome.config.core.ConfigDescription in project smarthome by eclipse.

the class ConfigI18nLocalizationService method getLocalizedConfigDescription.

/**
 * Localize a config description.
 *
 * @param bundle the bundle the i18n resources are located
 * @param configDescription the config description that should be localized
 * @param locale the locale it should be localized to
 * @return a localized configuration description on success, a non-localized one on error (e.g. no translation is
 *         found).
 */
public ConfigDescription getLocalizedConfigDescription(final Bundle bundle, final ConfigDescription configDescription, @Nullable final Locale locale) {
    final List<ConfigDescriptionParameter> localizedConfigDescriptionParameters = new ArrayList<>(configDescription.getParameters().size());
    // Loop through all the configuration parameters and localize them
    for (final ConfigDescriptionParameter configDescriptionParameter : configDescription.getParameters()) {
        final ConfigDescriptionParameter localizedConfigDescriptionParameter = getLocalizedConfigDescriptionParameter(bundle, configDescription, configDescriptionParameter, locale);
        localizedConfigDescriptionParameters.add(localizedConfigDescriptionParameter);
    }
    final List<ConfigDescriptionParameterGroup> localizedConfigDescriptionGroups = new ArrayList<>(configDescription.getParameterGroups().size());
    // Loop through all the configuration groups and localize them
    for (final ConfigDescriptionParameterGroup configDescriptionParameterGroup : configDescription.getParameterGroups()) {
        final ConfigDescriptionParameterGroup localizedConfigDescriptionGroup = getLocalizedConfigDescriptionGroup(bundle, configDescription, configDescriptionParameterGroup, locale);
        localizedConfigDescriptionGroups.add(localizedConfigDescriptionGroup);
    }
    return new ConfigDescription(configDescription.getUID(), localizedConfigDescriptionParameters, localizedConfigDescriptionGroups);
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 14 with ConfigDescription

use of org.eclipse.smarthome.config.core.ConfigDescription in project smarthome by eclipse.

the class ConfigDescriptionValidatorImpl method validate.

/**
 * Validates the given configuration parameters against the given configuration description having the given URI.
 *
 * @param configurationParameters the configuration parameters to be validated
 * @param configDescriptionURI the URI of the configuration description against which the configuration parameters
 *            are to be validated
 *
 * @throws ConfigValidationException if one or more configuration parameters do not match with the configuration
 *             description having the given URI
 * @throws NullPointerException if given config description URI or configuration parameters are null
 */
@Override
@SuppressWarnings("unchecked")
public void validate(Map<String, Object> configurationParameters, URI configDescriptionURI) {
    Objects.requireNonNull(configurationParameters, "Configuration parameters must not be null");
    Objects.requireNonNull(configDescriptionURI, "Config description URI must not be null");
    ConfigDescription configDescription = getConfigDescription(configDescriptionURI);
    if (configDescription == null) {
        logger.warn("Skipping config description validation because no config description found for URI '{}'", configDescriptionURI);
        return;
    }
    Map<String, ConfigDescriptionParameter> map = configDescription.toParametersMap();
    Collection<ConfigValidationMessage> configDescriptionValidationMessages = new ArrayList<>();
    for (String key : configurationParameters.keySet()) {
        ConfigDescriptionParameter configDescriptionParameter = map.get(key);
        if (configDescriptionParameter != null) {
            // If the parameter supports multiple selection, then it may be provided as an array
            if (configDescriptionParameter.isMultiple() && configurationParameters.get(key) instanceof List) {
                // Perform validation on each value in the list separately
                for (Object value : (List<Object>) configurationParameters.get(key)) {
                    ConfigValidationMessage message = validateParameter(configDescriptionParameter, value);
                    if (message != null) {
                        configDescriptionValidationMessages.add(message);
                    }
                }
            } else {
                ConfigValidationMessage message = validateParameter(configDescriptionParameter, configurationParameters.get(key));
                if (message != null) {
                    configDescriptionValidationMessages.add(message);
                }
            }
        }
    }
    if (!configDescriptionValidationMessages.isEmpty()) {
        throw new ConfigValidationException(bundleContext.getBundle(), translationProvider, configDescriptionValidationMessages);
    }
}
Also used : ConfigValidationMessage(org.eclipse.smarthome.config.core.validation.ConfigValidationMessage) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ArrayList(java.util.ArrayList) List(java.util.List) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException)

Example 15 with ConfigDescription

use of org.eclipse.smarthome.config.core.ConfigDescription in project smarthome by eclipse.

the class PersistentInbox method getConfigDescParams.

private List<ConfigDescriptionParameter> getConfigDescParams(ThingType thingType) {
    if (thingType != null && thingType.getConfigDescriptionURI() != null) {
        URI descURI = thingType.getConfigDescriptionURI();
        ConfigDescription desc = configDescRegistry.getConfigDescription(descURI);
        if (desc != null) {
            return desc.getParameters();
        }
    }
    return Collections.emptyList();
}
Also used : ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) URI(java.net.URI)

Aggregations

ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)22 URI (java.net.URI)10 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)9 ArrayList (java.util.ArrayList)5 List (java.util.List)3 ConfigDescriptionParameterGroup (org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup)3 BindingInfo (org.eclipse.smarthome.core.binding.BindingInfo)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Test (org.junit.Test)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 URISyntaxException (java.net.URISyntaxException)2 Locale (java.util.Locale)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 ConfigDescriptionDTO (org.eclipse.smarthome.config.core.dto.ConfigDescriptionDTO)2 ConfigDescriptionParameterDTO (org.eclipse.smarthome.config.core.dto.ConfigDescriptionParameterDTO)2 ConfigDescriptionParameterGroupDTO (org.eclipse.smarthome.config.core.dto.ConfigDescriptionParameterGroupDTO)2 NodeIterator (org.eclipse.smarthome.config.xml.util.NodeIterator)2 ThingType (org.eclipse.smarthome.core.thing.type.ThingType)2