diff --git a/itests/org.openhab.binding.mqtt.ruuvigateway.tests/itest.bndrun b/itests/org.openhab.binding.mqtt.ruuvigateway.tests/itest.bndrun index 29d6efc72..067883504 100644 --- a/itests/org.openhab.binding.mqtt.ruuvigateway.tests/itest.bndrun +++ b/itests/org.openhab.binding.mqtt.ruuvigateway.tests/itest.bndrun @@ -104,4 +104,10 @@ Import-Package: \ io.netty.resolver;version='[4.1.92,4.1.93)',\ io.netty.transport;version='[4.1.92,4.1.93)',\ io.netty.transport-native-unix-common;version='[4.1.92,4.1.93)',\ - org.osgi.service.cm;version='[1.6.0,1.6.1)' \ No newline at end of file + jakarta.ws.rs-api;version='[2.1.6,2.1.7)',\ + net.bytebuddy.byte-buddy;version='[1.12.19,1.12.20)',\ + net.bytebuddy.byte-buddy-agent;version='[1.12.19,1.12.20)',\ + org.mockito.mockito-core;version='[4.11.0,4.11.1)',\ + org.objenesis;version='[3.3.0,3.3.1)',\ + org.osgi.service.cm;version='[1.6.0,1.6.1)',\ + org.mockito.junit-jupiter;version='[4.11.0,4.11.1)' \ No newline at end of file diff --git a/itests/org.openhab.binding.mqtt.ruuvigateway.tests/src/main/java/org/openhab/binding/mqtt/ruuvigateway/RuuviGatewayTest.java b/itests/org.openhab.binding.mqtt.ruuvigateway.tests/src/main/java/org/openhab/binding/mqtt/ruuvigateway/RuuviGatewayTest.java index 3e1602309..b815cd962 100644 --- a/itests/org.openhab.binding.mqtt.ruuvigateway.tests/src/main/java/org/openhab/binding/mqtt/ruuvigateway/RuuviGatewayTest.java +++ b/itests/org.openhab.binding.mqtt.ruuvigateway.tests/src/main/java/org/openhab/binding/mqtt/ruuvigateway/RuuviGatewayTest.java @@ -15,7 +15,9 @@ package org.openhab.binding.mqtt.ruuvigateway; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; import static org.openhab.binding.mqtt.ruuvigateway.internal.RuuviGatewayBindingConstants.*; +import static org.openhab.core.library.unit.MetricPrefix.HECTO; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -50,15 +52,21 @@ import org.eclipse.jdt.annotation.Nullable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService; import org.openhab.binding.mqtt.ruuvigateway.internal.RuuviGatewayBindingConstants; import org.openhab.binding.mqtt.ruuvigateway.internal.discovery.RuuviGatewayDiscoveryService; import org.openhab.binding.mqtt.ruuvigateway.internal.handler.RuuviTagHandler; import org.openhab.core.config.core.Configuration; import org.openhab.core.config.discovery.DiscoveryResult; +import org.openhab.core.i18n.UnitProvider; import org.openhab.core.io.transport.mqtt.MqttBrokerConnection; import org.openhab.core.io.transport.mqtt.MqttConnectionObserver; import org.openhab.core.io.transport.mqtt.MqttConnectionState; @@ -94,7 +102,10 @@ import org.openhab.core.types.UnDefType; * @author Sami Salonen - Adapted and extended to Ruuvi Gateway tests */ @NonNullByDefault +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) public class RuuviGatewayTest extends MqttOSGiTest { + protected @Mock @NonNullByDefault({}) UnitProvider mockedUnitProvider; private static final String BASE_TOPIC_RUUVI = "ruuvi"; private static final Map CHANNEL_TO_ITEM_TYPE = new HashMap<>(); static { @@ -132,6 +143,27 @@ public class RuuviGatewayTest extends MqttOSGiTest { private @NonNullByDefault({}) RuuviGatewayDiscoveryService ruuviDiscoveryService; private Set things = new HashSet<>(); + @BeforeEach + public void setup() { + when(mockedUnitProvider.getUnit(any())).then(i -> { + Class clazz = i.getArgument(0); + if (Temperature.class.equals(clazz)) { + return SIUnits.CELSIUS; + } else if (Acceleration.class.equals(clazz)) { + return Units.METRE_PER_SQUARE_SECOND; + } else if (Dimensionless.class.equals(clazz)) { + return Units.ONE; + } else if (ElectricPotential.class.equals(clazz)) { + return Units.VOLT; + } else if (Pressure.class.equals(clazz)) { + return HECTO(SIUnits.PASCAL); + } else if (Power.class.equals(clazz)) { + return Units.WATT; + } + return null; + }); + } + private Bridge createMqttBrokerBridge() { Configuration configuration = new Configuration(); configuration.put("host", "127.0.0.1"); @@ -208,7 +240,7 @@ public class RuuviGatewayTest extends MqttOSGiTest { private String linkChannelToAutogeneratedItem(ChannelUID channelUID) { String itemName = itemName(channelUID); String itemType = CHANNEL_TO_ITEM_TYPE.get(channelUID.getId()); - GenericItem item = new CoreItemFactory().createItem(itemType, itemName); + GenericItem item = new CoreItemFactory(mockedUnitProvider).createItem(itemType, itemName); assertNotNull(item, itemType); itemProvider.add(item); itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID)); diff --git a/itests/pom.xml b/itests/pom.xml index f404ec305..b8831d147 100644 --- a/itests/pom.xml +++ b/itests/pom.xml @@ -28,9 +28,7 @@ org.openhab.binding.modbus.tests org.openhab.binding.mqtt.homeassistant.tests org.openhab.binding.mqtt.homie.tests - + org.openhab.binding.mqtt.ruuvigateway.tests org.openhab.binding.nest.tests org.openhab.binding.ntp.tests org.openhab.binding.systeminfo.tests