Search in sources :

Example 1 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-addons by openhab.

the class MieleHandlerFactoryTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    registerVolatileStorageService();
    thingRegistry = getService(ThingRegistry.class, ThingRegistry.class);
    assertNotNull(thingRegistry, "Thing registry is missing");
    // Ensure the MieleWebservice is not initialized.
    MieleHandlerFactory factory = getService(ThingHandlerFactory.class, MieleHandlerFactory.class);
    assertNotNull(factory);
    // Assume an access token has already been stored
    AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
    accessTokenResponse.setAccessToken(ACCESS_TOKEN);
    OAuthClientService oAuthClientService = mock(OAuthClientService.class);
    when(oAuthClientService.getAccessTokenResponse()).thenReturn(accessTokenResponse);
    OAuthFactory oAuthFactory = mock(OAuthFactory.class);
    when(oAuthFactory.getOAuthClientService(MieleCloudBindingIntegrationTestConstants.EMAIL)).thenReturn(oAuthClientService);
    OpenHabOAuthTokenRefresher tokenRefresher = getService(OAuthTokenRefresher.class, OpenHabOAuthTokenRefresher.class);
    assertNotNull(tokenRefresher);
    setPrivate(Objects.requireNonNull(tokenRefresher), "oauthFactory", oAuthFactory);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) OpenHabOAuthTokenRefresher(org.openhab.binding.mielecloud.internal.auth.OpenHabOAuthTokenRefresher) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) ThingRegistry(org.openhab.core.thing.ThingRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-addons by openhab.

the class NeeoDeviceDefinitions method save.

/**
 * Saves the current definitions to the {@link #file}. Any {@link IOException} will be logged and ignored.
 */
public void save() {
    logger.debug("Saving devices to {}", file.toPath());
    try {
        // ensure full path exists
        file.getParentFile().mkdirs();
        final List<NeeoDevice> devices = new ArrayList<>();
        // filter for only things that are still valid
        final ThingRegistry thingRegistry = context.getThingRegistry();
        for (NeeoDevice device : uidToDevice.values()) {
            if (NeeoConstants.NEEOIO_BINDING_ID.equalsIgnoreCase(device.getUid().getBindingId())) {
                devices.add(device);
            } else {
                if (thingRegistry.get(device.getUid().asThingUID()) != null) {
                    devices.add(device);
                }
            }
        }
        final String json = gson.toJson(devices);
        final byte[] contents = json.getBytes(StandardCharsets.UTF_8);
        Files.write(file.toPath(), contents);
    } catch (IOException e) {
        logger.debug("IOException writing {}: {}", file.toPath(), e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) NeeoDevice(org.openhab.io.neeo.internal.models.NeeoDevice) ThingRegistry(org.openhab.core.thing.ThingRegistry)

Example 3 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-addons by openhab.

the class CreateBridgeServletTest method whenBridgeReconfigurationFailsDueToMissingBridgeHandlerThenAWarningIsShownOnTheSuccessPage.

@Test
public void whenBridgeReconfigurationFailsDueToMissingBridgeHandlerThenAWarningIsShownOnTheSuccessPage() throws Exception {
    // given:
    MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
    assertNotNull(configService);
    CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
    assertNotNull(createBridgeServlet);
    Inbox inbox = mock(Inbox.class);
    when(inbox.add(any())).thenReturn(false);
    setPrivate(Objects.requireNonNull(createBridgeServlet), "inbox", inbox);
    Thing bridge = mock(Thing.class);
    when(bridge.getHandler()).thenReturn(null);
    ThingRegistry thingRegistry = mock(ThingRegistry.class);
    when(thingRegistry.get(any())).thenReturn(bridge);
    setPrivate(Objects.requireNonNull(createBridgeServlet), "thingRegistry", thingRegistry);
    // when:
    Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?" + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&" + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
    // then:
    assertTrue(website.contains("Pairing successful!"));
    assertTrue(website.contains("Could not auto reconfigure the bridge. Bridge thing or thing handler is not available. Please try the configuration flow again."));
}
Also used : Website(org.openhab.binding.mielecloud.internal.util.Website) MieleCloudConfigService(org.openhab.binding.mielecloud.internal.config.MieleCloudConfigService) Inbox(org.openhab.core.config.discovery.inbox.Inbox) Thing(org.openhab.core.thing.Thing) ThingRegistry(org.openhab.core.thing.ThingRegistry) AbstractConfigFlowTest(org.openhab.binding.mielecloud.internal.util.AbstractConfigFlowTest) Test(org.junit.jupiter.api.Test)

Example 4 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-addons by openhab.

the class SysteminfoOSGiTest method setUp.

@BeforeEach
public void setUp() {
    VolatileStorageService volatileStorageService = new VolatileStorageService();
    registerService(volatileStorageService);
    // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
    mockedSystemInfo = mock(SysteminfoInterface.class);
    when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
    when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
    when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
    when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
    when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
    systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
    SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
    // the external OSHI library
    if (oshiSystemInfo != null) {
        systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
    }
    systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
    managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));
    thingRegistry = getService(ThingRegistry.class);
    assertThat(thingRegistry, is(notNullValue()));
    itemRegistry = getService(ItemRegistry.class);
    assertThat(itemRegistry, is(notNullValue()));
}
Also used : ThingProvider(org.openhab.core.thing.ThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) StringType(org.openhab.core.library.types.StringType) SysteminfoInterface(org.openhab.binding.systeminfo.internal.model.SysteminfoInterface) DecimalType(org.openhab.core.library.types.DecimalType) SysteminfoHandlerFactory(org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) VolatileStorageService(org.openhab.core.test.storage.VolatileStorageService) ItemRegistry(org.openhab.core.items.ItemRegistry) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) ThingRegistry(org.openhab.core.thing.ThingRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-addons by openhab.

the class GenericWemoOSGiTest method setUpServices.

protected void setUpServices() throws IOException {
    WemoUtil.serviceAvailableFunction = (host, port) -> true;
    // StorageService is required from the ManagedThingProvider
    VolatileStorageService volatileStorageService = new VolatileStorageService();
    registerService(volatileStorageService);
    // Mock the UPnP Service, that is required from the UPnP IO Service
    mockUpnpService = new MockUpnpService(false, true);
    mockUpnpService.startup();
    registerService(mockUpnpService, UpnpService.class.getName());
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));
    thingRegistry = getService(ThingRegistry.class);
    assertThat(thingRegistry, is(notNullValue()));
    // UPnP IO Service is required from the WemoDiscoveryService and WemoHandlerFactory
    upnpIOService = getService(UpnpIOService.class);
    assertThat(upnpIOService, is(notNullValue()));
    mockCaller = Mockito.spy(new WemoHttpCall());
    WemoHttpCallFactory wemoHttpCallFactory = () -> mockCaller;
    registerService(wemoHttpCallFactory, WemoHttpCallFactory.class.getName());
    ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), any(Locale.class))).thenReturn(ChannelTypeBuilder.state(DEFAULT_CHANNEL_TYPE_UID, "label", CoreItemFactory.SWITCH).build());
    registerService(channelTypeProvider);
}
Also used : MockUpnpService(org.jupnp.mock.MockUpnpService) UpnpService(org.jupnp.UpnpService) WemoHttpCallFactory(org.openhab.binding.wemo.internal.WemoHttpCallFactory) Locale(java.util.Locale) MockUpnpService(org.jupnp.mock.MockUpnpService) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) WemoHttpCall(org.openhab.binding.wemo.internal.http.WemoHttpCall) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) VolatileStorageService(org.openhab.core.test.storage.VolatileStorageService) UpnpIOService(org.openhab.core.io.transport.upnp.UpnpIOService) ThingRegistry(org.openhab.core.thing.ThingRegistry)

Aggregations

ThingRegistry (org.openhab.core.thing.ThingRegistry)29 Thing (org.openhab.core.thing.Thing)18 Test (org.junit.jupiter.api.Test)17 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)14 ThingUID (org.openhab.core.thing.ThingUID)13 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)11 Configuration (org.openhab.core.config.core.Configuration)9 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ThingHandler (org.openhab.core.thing.binding.ThingHandler)7 Nullable (org.eclipse.jdt.annotation.Nullable)6 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)6 Bridge (org.openhab.core.thing.Bridge)5 ThingProvider (org.openhab.core.thing.ThingProvider)5 ItemChannelLink (org.openhab.core.thing.link.ItemChannelLink)5 Item (org.openhab.core.items.Item)4 NumberItem (org.openhab.core.library.items.NumberItem)4 VolatileStorageService (org.openhab.core.test.storage.VolatileStorageService)4 Locale (java.util.Locale)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3