Search in sources :

Example 1 with ConfigDescriptionProvider

use of org.openhab.core.config.core.ConfigDescriptionProvider in project openhab-core by openhab.

the class InboxOSGiTest method assertThatApproveAddsPropertiesOfDiscoveryResultWhichAreConfigDescriptionParametersAsThingConfigurationPropertiesAndPropertiesWhichAreNoConfigDescriptionParametersAsThingProperties.

@Test
@SuppressWarnings("null")
public void assertThatApproveAddsPropertiesOfDiscoveryResultWhichAreConfigDescriptionParametersAsThingConfigurationPropertiesAndPropertiesWhichAreNoConfigDescriptionParametersAsThingProperties() {
    final List<Object> services = new LinkedList<>();
    inbox.add(testDiscoveryResult);
    final ThingTypeProvider thingTypeProvider = new ThingTypeProvider() {

        @Override
        public Collection<ThingType> getThingTypes(@Nullable Locale locale) {
            return Set.of(testThingType);
        }

        @Override
        @Nullable
        public ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
            return thingTypeUID.equals(testThingType.getUID()) ? testThingType : null;
        }
    };
    services.add(registerService(thingTypeProvider));
    final ThingTypeRegistry thingTypeRegistry = getService(ThingTypeRegistry.class);
    assertNotNull(thingTypeRegistry);
    waitForAssert(() -> assertNotNull(thingTypeRegistry.getThingType(testThingType.getUID())));
    final ConfigDescriptionProvider configDescriptionProvider = new ConfigDescriptionProvider() {

        @Override
        public Collection<ConfigDescription> getConfigDescriptions(@Nullable Locale locale) {
            return Set.of(testConfigDescription);
        }

        @Override
        @Nullable
        public ConfigDescription getConfigDescription(URI uri, @Nullable Locale locale) {
            return uri.equals(testConfigDescription.getUID()) ? testConfigDescription : null;
        }
    };
    services.add(registerService(configDescriptionProvider));
    final ConfigDescriptionRegistry configDescriptionRegistry = getService(ConfigDescriptionRegistry.class);
    assertNotNull(configDescriptionRegistry);
    waitForAssert(() -> assertNotNull(configDescriptionRegistry.getConfigDescription(testConfigDescription.getUID())));
    Thing approvedThing = inbox.approve(testThing.getUID(), testThingLabel, null);
    Thing addedThing = registry.get(testThing.getUID());
    assertTrue(approvedThing.equals(addedThing));
    assertFalse(addedThing == null);
    for (String key : keysInConfigDescription) {
        Object thingConfItem = addedThing.getConfiguration().get(key);
        Object descResultParam = discoveryResultProperties.get(key);
        if (descResultParam instanceof Number) {
            descResultParam = new BigDecimal(descResultParam.toString());
        }
        assertFalse(thingConfItem == null);
        assertFalse(descResultParam == null);
        assertTrue(thingConfItem.equals(descResultParam));
    }
    for (String key : keysNotInConfigDescription) {
        String thingProperty = addedThing.getProperties().get(key);
        String descResultParam = String.valueOf(discoveryResultProperties.get(key));
        assertFalse(thingProperty == null);
        assertFalse(descResultParam == null);
        assertTrue(thingProperty.equals(descResultParam));
    }
    services.forEach(obj -> unregisterService(obj));
}
Also used : Locale(java.util.Locale) ConfigDescription(org.openhab.core.config.core.ConfigDescription) ThingType(org.openhab.core.thing.type.ThingType) URI(java.net.URI) LinkedList(java.util.LinkedList) BigDecimal(java.math.BigDecimal) ThingTypeRegistry(org.openhab.core.thing.type.ThingTypeRegistry) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ConfigDescriptionRegistry(org.openhab.core.config.core.ConfigDescriptionRegistry) ThingTypeProvider(org.openhab.core.thing.binding.ThingTypeProvider) Nullable(org.eclipse.jdt.annotation.Nullable) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with ConfigDescriptionProvider

use of org.openhab.core.config.core.ConfigDescriptionProvider in project openhab-core by openhab.

the class ChangeThingTypeOSGiTest method setup.

@BeforeEach
public void setup() throws URISyntaxException {
    registerVolatileStorageService();
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));
    configDescriptionRegistry = getService(ConfigDescriptionRegistry.class);
    assertThat(configDescriptionRegistry, is(notNullValue()));
    managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
    assertThat(managedItemChannelLinkProvider, is(notNullValue()));
    managedItemProvider = getService(ManagedItemProvider.class);
    assertThat(managedItemProvider, is(notNullValue()));
    ComponentContext componentContext = mock(ComponentContext.class);
    when(componentContext.getBundleContext()).thenReturn(bundleContext);
    thingHandlerFactory = new SampleThingHandlerFactory();
    thingHandlerFactory.activate(componentContext);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    Map<String, String> thingTypeGenericProperties = Map.ofEntries(entry(PROPERTY_ON_GENERIC_THING_TYPE, GENERIC_VALUE), entry(PROPERTY_ON_GENERIC_AND_SPECIFIC_THING_TYPE, GENERIC_VALUE));
    Map<String, String> thingTypeSpecificProperties = Map.ofEntries(entry(PROPERTY_ON_SPECIFIC_THING_TYPE, SPECIFIC_VALUE), entry(PROPERTY_ON_GENERIC_AND_SPECIFIC_THING_TYPE, SPECIFIC_VALUE));
    thingTypeGeneric = registerThingTypeAndConfigDescription(THING_TYPE_GENERIC_UID, thingTypeGenericProperties);
    thingTypeSpecific = registerThingTypeAndConfigDescription(THING_TYPE_SPECIFIC_UID, thingTypeSpecificProperties);
    ThingTypeProvider thingTypeProvider = mock(ThingTypeProvider.class);
    when(thingTypeProvider.getThingType(any(), any())).thenAnswer(invocation -> thingTypes.get(invocation.getArgument(0)));
    registerService(thingTypeProvider);
    ThingTypeRegistry thingTypeRegistry = mock(ThingTypeRegistry.class);
    when(thingTypeRegistry.getThingType(any(), any())).thenAnswer(invocation -> thingTypes.get(invocation.getArgument(0)));
    registerService(thingTypeRegistry);
    ConfigDescriptionProvider configDescriptionProvider = mock(ConfigDescriptionProvider.class);
    when(configDescriptionProvider.getConfigDescription(any(), any())).thenAnswer(invocation -> configDescriptions.get(invocation.getArgument(0)));
    registerService(configDescriptionProvider);
    ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelTypes(any())).thenReturn(channelTypes.values());
    when(channelTypeProvider.getChannelType(any(), any())).thenAnswer(invocation -> channelTypes.get(invocation.getArgument(0)));
    registerService(channelTypeProvider);
    ChannelGroupTypeProvider channelGroupTypeProvider = mock(ChannelGroupTypeProvider.class);
    when(channelGroupTypeProvider.getChannelGroupTypes(any())).thenReturn(channelGroupTypes.values());
    when(channelGroupTypeProvider.getChannelGroupType(any(), any())).thenAnswer(invocation -> channelGroupTypes.get(invocation.getArgument(0)));
    registerService(channelGroupTypeProvider);
    managedItemProvider.add(new StringItem(ITEM_GENERIC));
    managedItemProvider.add(new StringItem(ITEM_SPECIFIC));
    managedItemChannelLinkProvider.add(ITEM_CHANNEL_LINK_GENERIC);
    managedItemChannelLinkProvider.add(ITEM_CHANNEL_LINK_SPECIFIC);
}
Also used : ManagedItemProvider(org.openhab.core.items.ManagedItemProvider) ManagedItemChannelLinkProvider(org.openhab.core.thing.link.ManagedItemChannelLinkProvider) ComponentContext(org.osgi.service.component.ComponentContext) StringItem(org.openhab.core.library.items.StringItem) ChannelGroupTypeProvider(org.openhab.core.thing.type.ChannelGroupTypeProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeRegistry(org.openhab.core.thing.type.ThingTypeRegistry) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) ConfigDescriptionRegistry(org.openhab.core.config.core.ConfigDescriptionRegistry) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ConfigDescriptionProvider

use of org.openhab.core.config.core.ConfigDescriptionProvider in project openhab-core by openhab.

the class BindingBaseClassesOSGiTest method registerThingTypeAndConfigDescription.

private void registerThingTypeAndConfigDescription() {
    ThingType thingType = ThingTypeBuilder.instance(new ThingTypeUID(BINDING_ID, THING_TYPE_ID), "label").withConfigDescriptionURI(BINDING_CONFIG_URI).build();
    ConfigDescription configDescription = ConfigDescriptionBuilder.create(BINDING_CONFIG_URI).withParameter(ConfigDescriptionParameterBuilder.create("parameter", ConfigDescriptionParameter.Type.TEXT).withRequired(true).build()).build();
    ThingTypeProvider thingTypeProvider = mock(ThingTypeProvider.class);
    when(thingTypeProvider.getThingType(ArgumentMatchers.any(ThingTypeUID.class), ArgumentMatchers.any(Locale.class))).thenReturn(thingType);
    registerService(thingTypeProvider);
    ThingTypeRegistry thingTypeRegistry = mock(ThingTypeRegistry.class);
    when(thingTypeRegistry.getThingType(ArgumentMatchers.any(ThingTypeUID.class))).thenReturn(thingType);
    registerService(thingTypeRegistry);
    ConfigDescriptionProvider configDescriptionProvider = mock(ConfigDescriptionProvider.class);
    when(configDescriptionProvider.getConfigDescription(ArgumentMatchers.any(URI.class), ArgumentMatchers.nullable(Locale.class))).thenReturn(configDescription);
    registerService(configDescriptionProvider);
}
Also used : Locale(java.util.Locale) ThingTypeRegistry(org.openhab.core.thing.type.ThingTypeRegistry) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ConfigDescription(org.openhab.core.config.core.ConfigDescription) ThingType(org.openhab.core.thing.type.ThingType) URI(java.net.URI) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider)

Example 4 with ConfigDescriptionProvider

use of org.openhab.core.config.core.ConfigDescriptionProvider in project openhab-core by openhab.

the class ThingManagerOSGiTest method registerConfigDescriptionProvider.

private void registerConfigDescriptionProvider(boolean withRequiredParameter) {
    ConfigDescription configDescription = ConfigDescriptionBuilder.create(THING_CONFIG_URI).withParameter(ConfigDescriptionParameterBuilder.create("parameter", ConfigDescriptionParameter.Type.TEXT).withRequired(withRequiredParameter).build()).build();
    ConfigDescriptionProvider configDescriptionProvider = mock(ConfigDescriptionProvider.class);
    when(configDescriptionProvider.getConfigDescription(any(URI.class), nullable(Locale.class))).thenReturn(configDescription);
    registerService(configDescriptionProvider);
}
Also used : Locale(java.util.Locale) ConfigDescription(org.openhab.core.config.core.ConfigDescription) URI(java.net.URI) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider)

Example 5 with ConfigDescriptionProvider

use of org.openhab.core.config.core.ConfigDescriptionProvider in project openhab-core by openhab.

the class GenericThingProviderTest3 method setUp.

@BeforeEach
public void setUp() throws Exception {
    thingRegistry = getService(ThingRegistry.class);
    assertThat(thingRegistry, is(notNullValue()));
    modelRepository = getService(ModelRepository.class);
    assertThat(modelRepository, is(notNullValue()));
    modelRepository.removeModel(TESTMODEL_NAME);
    ComponentContext componentContextMock = mock(ComponentContext.class);
    when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
    // create a "dumb" thing handler that acts as if the XML config was not yet loaded
    dumbThingHandlerFactory = new DumbThingHandlerFactory(componentContextMock, true);
    Collection<Thing> things = thingRegistry.getAll();
    assertThat(things.size(), is(0));
    String model = // 
    "dumb:DUMB:boo \"Test Label\" @ \"Test Location\" [" + // 
    "    testConf=\"foo\"" + // 
    "]" + // 
    "{" + // 
    "    Switch : manual [ duration = \"5\" ]" + "}";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    registerService(dumbThingHandlerFactory, ThingHandlerFactory.class.getName());
    ConfigDescription configDescription = ConfigDescriptionBuilder.create(new URI("test:test")).withParameters(Stream.of(ConfigDescriptionParameterBuilder.create("testAdditional", ConfigDescriptionParameter.Type.TEXT).withRequired(false).withDefault("hello world").build(), ConfigDescriptionParameterBuilder.create("testConf", ConfigDescriptionParameter.Type.TEXT).withRequired(false).withDefault("bar").build()).collect(toList())).build();
    ConfigDescriptionProvider configDescriptionProvider = mock(ConfigDescriptionProvider.class);
    when(configDescriptionProvider.getConfigDescription(any(), nullable(Locale.class))).thenReturn(configDescription);
    registerService(configDescriptionProvider);
}
Also used : Locale(java.util.Locale) ComponentContext(org.osgi.service.component.ComponentContext) ConfigDescription(org.openhab.core.config.core.ConfigDescription) DumbThingHandlerFactory(org.openhab.core.model.thing.testsupport.hue.DumbThingHandlerFactory) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) DumbThingHandlerFactory(org.openhab.core.model.thing.testsupport.hue.DumbThingHandlerFactory) URI(java.net.URI) ThingRegistry(org.openhab.core.thing.ThingRegistry) ModelRepository(org.openhab.core.model.core.ModelRepository) ByteArrayInputStream(java.io.ByteArrayInputStream) Thing(org.openhab.core.thing.Thing) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ConfigDescriptionProvider (org.openhab.core.config.core.ConfigDescriptionProvider)7 URI (java.net.URI)5 Locale (java.util.Locale)5 ConfigDescription (org.openhab.core.config.core.ConfigDescription)5 ThingTypeRegistry (org.openhab.core.thing.type.ThingTypeRegistry)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 ConfigDescriptionRegistry (org.openhab.core.config.core.ConfigDescriptionRegistry)2 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)2 Thing (org.openhab.core.thing.Thing)2 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)2 ThingType (org.openhab.core.thing.type.ThingType)2 ComponentContext (org.osgi.service.component.ComponentContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 ConfigDescriptionParameter (org.openhab.core.config.core.ConfigDescriptionParameter)1 ManagedItemProvider (org.openhab.core.items.ManagedItemProvider)1 StringItem (org.openhab.core.library.items.StringItem)1