]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt.ruuvigateway] Re-enable itests after core change (#15018)
authorHolger Friedrich <holgerfriedrich@users.noreply.github.com>
Mon, 17 Jul 2023 19:31:35 +0000 (21:31 +0200)
committerGitHub <noreply@github.com>
Mon, 17 Jul 2023 19:31:35 +0000 (21:31 +0200)
Also-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
itests/org.openhab.binding.mqtt.ruuvigateway.tests/itest.bndrun
itests/org.openhab.binding.mqtt.ruuvigateway.tests/src/main/java/org/openhab/binding/mqtt/ruuvigateway/RuuviGatewayTest.java
itests/pom.xml

index 29d6efc723f0ea5d743cec70cc50020e075f7256..067883504f580cf1d2df3d835cd7506ff27afb90 100644 (file)
@@ -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
index 3e1602309647a67d01c70994740ce4c8a71b60ab..b815cd962ba28f3fe81ef44faab83e99fad9d672 100644 (file)
@@ -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<String, String> CHANNEL_TO_ITEM_TYPE = new HashMap<>();
     static {
@@ -132,6 +143,27 @@ public class RuuviGatewayTest extends MqttOSGiTest {
     private @NonNullByDefault({}) RuuviGatewayDiscoveryService ruuviDiscoveryService;
     private Set<Thing> 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));
index f404ec305a94ebe3f83cc2292b4786756a94f7a1..b8831d14724930ce63790d7b0b7ec4af8cd1cdad 100644 (file)
@@ -28,9 +28,7 @@
     <module>org.openhab.binding.modbus.tests</module>
     <module>org.openhab.binding.mqtt.homeassistant.tests</module>
     <module>org.openhab.binding.mqtt.homie.tests</module>
-    <!-- MQTT ruuvigateway tests disabled until fixed (CoreItemFactory + NumberItem)
-      <module>org.openhab.binding.mqtt.ruuvigateway.tests</module>
-    -->
+    <module>org.openhab.binding.mqtt.ruuvigateway.tests</module>
     <module>org.openhab.binding.nest.tests</module>
     <module>org.openhab.binding.ntp.tests</module>
     <module>org.openhab.binding.systeminfo.tests</module>