From 972149fb3fc2dd95a1cdd31657aabf391c5e616d Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Fri, 8 Apr 2022 14:32:32 +0200 Subject: [PATCH] [modbus] Make tests more stable (#12584) 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 --- .../modbus/tests/AbstractModbusOSGiTest.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/AbstractModbusOSGiTest.java b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/AbstractModbusOSGiTest.java index 97a7e6ce4c2..8cbf2d712c3 100644 --- a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/AbstractModbusOSGiTest.java +++ b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/AbstractModbusOSGiTest.java @@ -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 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); }