[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 <github@maindrain.net>
This commit is contained in:
Wouter Born 2022-04-08 14:32:32 +02:00 committed by GitHub
parent 9bb68a7415
commit 972149fb3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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);
}