]> git.basschouten.com Git - openhab-addons.git/commitdiff
[modbus] Make tests more stable (#12584)
authorWouter Born <github@maindrain.net>
Fri, 8 Apr 2022 12:32:32 +0000 (14:32 +0200)
committerGitHub <noreply@github.com>
Fri, 8 Apr 2022 12:32:32 +0000 (14:32 +0200)
This fixes the issue of the integration tests sometimes failing because channels have not yet been linked causing item state to remain null.

Signed-off-by: Wouter Born <github@maindrain.net>
itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/AbstractModbusOSGiTest.java

index 97a7e6ce4c22c9367daeb4a1b0106b2dd9b0d560..8cbf2d712c32d4aa93e27862838aed99da2d37b5 100644 (file)
@@ -60,8 +60,10 @@ import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingProvider;
 import org.openhab.core.thing.binding.ThingHandler;
 import org.openhab.core.thing.binding.ThingHandlerFactory;
+import org.openhab.core.thing.link.AbstractLink;
 import org.openhab.core.thing.link.ItemChannelLink;
 import org.openhab.core.thing.link.ItemChannelLinkProvider;
+import org.openhab.core.thing.link.ItemChannelLinkRegistry;
 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
 import org.openhab.core.thing.type.ChannelTypeUID;
 import org.openhab.core.transform.TransformationService;
@@ -108,7 +110,7 @@ public abstract class AbstractModbusOSGiTest extends JavaOSGiTest {
             ItemStateEvent stateEvent = (ItemStateEvent) event;
             logger.trace("Captured event: {} of type {}. Payload: {}", event,
                     stateEvent.getItemState().getClass().getSimpleName(), event.getPayload());
-            stateUpdates.computeIfAbsent(stateEvent.getItemName(), (item) -> new ArrayList<>())
+            stateUpdates.computeIfAbsent(stateEvent.getItemName(), item -> new ArrayList<>())
                     .add(stateEvent.getItemState());
         }
     }
@@ -121,6 +123,7 @@ public abstract class AbstractModbusOSGiTest extends JavaOSGiTest {
     protected @NonNullByDefault({}) ManagedItemProvider itemProvider;
     protected @NonNullByDefault({}) ManagedItemChannelLinkProvider itemChannelLinkProvider;
     protected @NonNullByDefault({}) ItemRegistry itemRegistry;
+    protected @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry;
     protected @NonNullByDefault({}) CoreItemFactory coreItemFactory;
 
     private Set<Item> addedItems = new HashSet<>();
@@ -130,10 +133,6 @@ public abstract class AbstractModbusOSGiTest extends JavaOSGiTest {
 
     protected @Mock @NonNullByDefault({}) ModbusCommunicationInterface comms;
 
-    public AbstractModbusOSGiTest() {
-        super();
-    }
-
     /**
      * Before each test, configure mocked services
      */
@@ -154,6 +153,8 @@ public abstract class AbstractModbusOSGiTest extends JavaOSGiTest {
         assertThat("Could not get ManagedItemChannelLinkProvider", itemChannelLinkProvider, is(notNullValue()));
         itemRegistry = getService(ItemRegistry.class);
         assertThat("Could not get ItemRegistry", itemRegistry, is(notNullValue()));
+        itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class);
+        assertThat("Could not get ItemChannelLinkRegistry", itemChannelLinkRegistry, is(notNullValue()));
 
         coreItemFactory = new CoreItemFactory();
 
@@ -218,6 +219,8 @@ public abstract class AbstractModbusOSGiTest extends JavaOSGiTest {
         ItemChannelLink link = new ItemChannelLink(itemName, channelUID);
         assertThat(addedLinks.contains(link), not(equalTo(true)));
         itemChannelLinkProvider.add(link);
+        waitForAssert(() -> assertThat(itemChannelLinkRegistry.get(AbstractLink.getIDFor(itemName, channelUID)),
+                is(notNullValue())));
         addedLinks.add(link);
     }