]> git.basschouten.com Git - openhab-addons.git/blob
9050416cdd21f861b0c2e3ca0c7b98b1408c7adb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.util;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.MIELE_CLOUD_ACCOUNT_LABEL;
17
18 import java.util.Map;
19 import java.util.Objects;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
25 import org.openhab.binding.mielecloud.internal.handler.MieleBridgeHandler;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.config.discovery.inbox.Inbox;
28 import org.openhab.core.test.java.JavaOSGiTest;
29 import org.openhab.core.test.storage.VolatileStorageService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ManagedThingProvider;
32 import org.openhab.core.thing.ThingRegistry;
33 import org.openhab.core.thing.binding.builder.BridgeBuilder;
34
35 /**
36  * Parent class for openHAB OSGi tests offering helper methods for common interactions with the openHAB runtime and its
37  * services.
38  *
39  * @author Björn Lange - Initial contribution
40  */
41 @NonNullByDefault
42 public abstract class OpenHabOsgiTest extends JavaOSGiTest {
43     @Nullable
44     private Inbox inbox;
45     @Nullable
46     private ThingRegistry thingRegistry;
47
48     protected Inbox getInbox() {
49         assertNotNull(inbox);
50         return Objects.requireNonNull(inbox);
51     }
52
53     protected ThingRegistry getThingRegistry() {
54         assertNotNull(thingRegistry);
55         return Objects.requireNonNull(thingRegistry);
56     }
57
58     @BeforeEach
59     public void setUpEshOsgiTest() {
60         registerVolatileStorageService();
61         inbox = getService(Inbox.class);
62         setUpThingRegistry();
63     }
64
65     private void setUpThingRegistry() {
66         thingRegistry = getService(ThingRegistry.class, ThingRegistry.class);
67         assertNotNull(thingRegistry, "Thing registry is missing");
68     }
69
70     /**
71      * Sets up a {@link Bridge} with an attached {@link MieleBridgeHandler} and registers it with the
72      * {@link ManagedThingProvider} and {@link ThingRegistry}.
73      */
74     public void setUpBridge() {
75         ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
76         assertNotNull(managedThingProvider);
77
78         Bridge bridge = BridgeBuilder
79                 .create(MieleCloudBindingConstants.THING_TYPE_BRIDGE,
80                         MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)
81                 .withConfiguration(new Configuration(Map.of(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
82                         MieleCloudBindingIntegrationTestConstants.EMAIL)))
83                 .withLabel(MIELE_CLOUD_ACCOUNT_LABEL).build();
84         assertNotNull(bridge);
85
86         managedThingProvider.add(bridge);
87
88         waitForAssert(() -> {
89             assertNotNull(bridge.getHandler());
90             assertTrue(bridge.getHandler() instanceof MieleBridgeHandler, "Handler type is wrong");
91         });
92     }
93
94     /**
95      * Registers a volatile storage service.
96      */
97     @Override
98     protected void registerVolatileStorageService() {
99         registerService(new VolatileStorageService());
100     }
101 }