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