]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt] Fix most SAT findings (#12492)
authorWouter Born <github@maindrain.net>
Sat, 19 Mar 2022 08:27:41 +0000 (09:27 +0100)
committerGitHub <noreply@github.com>
Sat, 19 Mar 2022 08:27:41 +0000 (09:27 +0100)
Signed-off-by: Wouter Born <github@maindrain.net>
60 files changed:
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/AbstractMQTTThingHandler.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/mapping/ColorMode.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/mapping/SubscribeFieldToMQTTtopic.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/utils/FutureCollector.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/ColorValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/DateTimeValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/ImageValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/LocationValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/OnOffValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/OpenCloseValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/RollershutterValue.java
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/TextValue.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/ChannelStateTests.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/ChannelStateTransformationTests.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/internal/handler/GenericThingHandlerTests.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/internal/handler/ThingChannelConstants.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/mapping/MqttTopicClassMapperTests.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/mapping/SubscribeFieldToMQTTtopicTests.java
bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/ComponentFactory.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/config/ChannelConfigurationTypeAdapterFactory.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/exception/ConfigurationException.java
bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/exception/UnsupportedComponentException.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/HaIDTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponentTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AlarmControlPanelTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/BinarySensorTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/CameraTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/ClimateTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/CoverTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/FanTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/HAConfigurationTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/LightTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/LockTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SensorTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SwitchTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/VacuumTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscoveryTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/handler/HomeAssistantThingHandlerTests.java
bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/generic/internal/MqttThingHandlerFactory.java
bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/homie300/Device.java
bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/homie300/DeviceAttributes.java
bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/homie300/Node.java
bundles/org.openhab.binding.mqtt.homie/src/main/java/org/openhab/binding/mqtt/homie/internal/homie300/NodeAttributes.java
bundles/org.openhab.binding.mqtt.homie/src/test/java/org/openhab/binding/mqtt/homie/generic/internal/mapping/HomieChildMapTests.java
bundles/org.openhab.binding.mqtt.homie/src/test/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandlerTests.java
bundles/org.openhab.binding.mqtt.homie/src/test/java/org/openhab/binding/mqtt/homie/internal/handler/ThingChannelConstants.java
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/handler/BrokerHandler.java
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/internal/MqttBrokerHandlerFactory.java
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/internal/MqttThingID.java
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/internal/ssl/Pin.java
bundles/org.openhab.binding.mqtt/src/main/java/org/openhab/binding/mqtt/internal/ssl/PinType.java
bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/BrokerHandlerEx.java
bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/BrokerHandlerTest.java
bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/MqttBrokerConnectionEx.java
bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/handler/MqttConnectionObserverEx.java
bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/internal/MQTTTopicDiscoveryServiceTest.java
bundles/org.openhab.binding.mqtt/src/test/java/org/openhab/binding/mqtt/internal/ssl/PinningSSLContextProviderTest.java

index e7ff415f4ff9102dccc067a8a4093d0e4beba007..b9f932c6d24b00fcbfd88ffc44cd111a068b8d0e 100644 (file)
@@ -23,7 +23,6 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.mqtt.generic.values.OnOffValue;
@@ -327,8 +326,8 @@ public abstract class AbstractMQTTThingHandler extends BaseThingHandler
     }
 
     @Override
-    public void removeAvailabilityTopic(@NonNull String availability_topic) {
-        availabilityStates.computeIfPresent(availability_topic, (topic, state) -> {
+    public void removeAvailabilityTopic(String availabilityTopic) {
+        availabilityStates.computeIfPresent(availabilityTopic, (topic, state) -> {
             if (connection != null && state != null) {
                 state.stop();
             }
index 131cdbeeffaeecfd93d9af4b279ebbf828d8d56a..db2d60c3345c6d8a32d64d98b9f78ae5c0d276fb 100644 (file)
  */
 package org.openhab.binding.mqtt.generic.mapping;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * Color modes supported by the binding.
  *
  * @author Aitor Iturrioz - Initial contribution
  */
+@NonNullByDefault
 public enum ColorMode {
     HSB,
     RGB,
index ca1d386c8bb5611f71b85dedd79104b8618c30e9..de416e07cb8a9d96fb6a31f0a772e64ea05169e8 100644 (file)
@@ -81,13 +81,13 @@ public class SubscribeFieldToMQTTtopic implements MqttMessageSubscriber {
         String typeName = type.getSimpleName();
         if (value instanceof BigDecimal && !type.equals(BigDecimal.class)) {
             BigDecimal bdValue = (BigDecimal) value;
-            if (type.equals(Float.class) || typeName.equals("float")) {
+            if (type.equals(Float.class) || "float".equals(typeName)) {
                 result = bdValue.floatValue();
-            } else if (type.equals(Double.class) || typeName.equals("double")) {
+            } else if (type.equals(Double.class) || "double".equals(typeName)) {
                 result = bdValue.doubleValue();
-            } else if (type.equals(Long.class) || typeName.equals("long")) {
+            } else if (type.equals(Long.class) || "long".equals(typeName)) {
                 result = bdValue.longValue();
-            } else if (type.equals(Integer.class) || typeName.equals("int")) {
+            } else if (type.equals(Integer.class) || "int".equals(typeName)) {
                 result = bdValue.intValue();
             }
         } else
@@ -95,17 +95,17 @@ public class SubscribeFieldToMQTTtopic implements MqttMessageSubscriber {
         // primitive types
         if (value instanceof String && !type.equals(String.class)) {
             String bdValue = (String) value;
-            if (type.equals(Float.class) || typeName.equals("float")) {
+            if (type.equals(Float.class) || "float".equals(typeName)) {
                 result = Float.valueOf(bdValue);
-            } else if (type.equals(Double.class) || typeName.equals("double")) {
+            } else if (type.equals(Double.class) || "double".equals(typeName)) {
                 result = Double.valueOf(bdValue);
-            } else if (type.equals(Long.class) || typeName.equals("long")) {
+            } else if (type.equals(Long.class) || "long".equals(typeName)) {
                 result = Long.valueOf(bdValue);
             } else if (type.equals(BigDecimal.class)) {
                 result = new BigDecimal(bdValue);
-            } else if (type.equals(Integer.class) || typeName.equals("int")) {
+            } else if (type.equals(Integer.class) || "int".equals(typeName)) {
                 result = Integer.valueOf(bdValue);
-            } else if (type.equals(Boolean.class) || typeName.equals("boolean")) {
+            } else if (type.equals(Boolean.class) || "boolean".equals(typeName)) {
                 result = Boolean.valueOf(bdValue);
             } else if (type.isEnum()) {
                 @SuppressWarnings({ "rawtypes", "unchecked" })
index c63aa2acf345203672186fe83d20891662dbf6c0..c3e0513ddd6587cd6f5b7a47905dce4a0e35e233 100644 (file)
@@ -18,21 +18,24 @@ import java.util.concurrent.CompletableFuture;
 import java.util.function.Supplier;
 import java.util.stream.Collector;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
 /**
  * Collector to combine a stream of CompletableFutures.
  *
  * @author Jochen Klein - Initial contribution
  *
  */
+@NonNullByDefault
 public class FutureCollector {
 
-    public static <X> Collector<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<Void>> allOf() {
-        return Collector.<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<Void>> of(
+    public static <X> Collector<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<@Nullable Void>> allOf() {
+        return Collector.<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<@Nullable Void>> of(
                 (Supplier<Set<CompletableFuture<X>>>) HashSet::new, Set::add, (left, right) -> {
                     left.addAll(right);
                     return left;
-                }, a -> {
-                    return CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()]));
-                }, Collector.Characteristics.UNORDERED);
+                }, a -> CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()])),
+                Collector.Characteristics.UNORDERED);
     }
 }
index f846b111f014480c15ba073afe3057060bf5d623..df8ac688809645ed1e8838d01506761a5788eac5 100644 (file)
@@ -13,9 +13,8 @@
 package org.openhab.binding.mqtt.generic.values;
 
 import java.math.BigDecimal;
+import java.util.List;
 import java.util.Locale;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
 
 import javax.ws.rs.NotSupportedException;
 
@@ -63,8 +62,7 @@ public class ColorValue extends Value {
      * @param onBrightness When receiving a ON command, the brightness percentage is set to this value
      */
     public ColorValue(ColorMode colorMode, @Nullable String onValue, @Nullable String offValue, int onBrightness) {
-        super(CoreItemFactory.COLOR,
-                Stream.of(OnOffType.class, PercentType.class, StringType.class).collect(Collectors.toList()));
+        super(CoreItemFactory.COLOR, List.of(OnOffType.class, PercentType.class, StringType.class));
 
         if (onBrightness > 100) {
             throw new IllegalArgumentException("Brightness parameter must be <= 100");
@@ -112,8 +110,8 @@ public class ColorValue extends Value {
                                 Integer.parseInt(split[2]));
                         break;
                     case XYY:
-                        HSBType temp_state = HSBType.fromXY(Float.parseFloat(split[0]), Float.parseFloat(split[1]));
-                        state = new HSBType(temp_state.getHue(), temp_state.getSaturation(), new PercentType(split[2]));
+                        HSBType tempState = HSBType.fromXY(Float.parseFloat(split[0]), Float.parseFloat(split[1]));
+                        state = new HSBType(tempState.getHue(), tempState.getSaturation(), new PercentType(split[2]));
                         break;
                     default:
                         logger.warn("Non supported color mode");
@@ -146,21 +144,21 @@ public class ColorValue extends Value {
             }
         }
 
-        HSBType hsb_state = (HSBType) state;
+        HSBType hsbState = (HSBType) state;
 
         switch (this.colorMode) {
             case HSB:
-                return String.format(formatPattern, hsb_state.getHue().intValue(), hsb_state.getSaturation().intValue(),
-                        hsb_state.getBrightness().intValue());
+                return String.format(formatPattern, hsbState.getHue().intValue(), hsbState.getSaturation().intValue(),
+                        hsbState.getBrightness().intValue());
             case RGB:
-                PercentType[] rgb = hsb_state.toRGB();
+                PercentType[] rgb = hsbState.toRGB();
                 return String.format(formatPattern, rgb[0].toBigDecimal().multiply(factor).intValue(),
                         rgb[1].toBigDecimal().multiply(factor).intValue(),
                         rgb[2].toBigDecimal().multiply(factor).intValue());
             case XYY:
-                PercentType[] xyY = hsb_state.toXY();
+                PercentType[] xyY = hsbState.toXY();
                 return String.format(Locale.ROOT, formatPattern, xyY[0].floatValue() / 100.0f,
-                        xyY[1].floatValue() / 100.0f, hsb_state.getBrightness().floatValue());
+                        xyY[1].floatValue() / 100.0f, hsbState.getBrightness().floatValue());
             default:
                 throw new NotSupportedException(String.format("Non supported color mode: {}", this.colorMode));
         }
index 3fcbb37d728ad68422bfc0f02dff42507f0048fc..916f8174ec2de97e14b5ac20dba38186bf9758be 100644 (file)
@@ -13,8 +13,7 @@
 package org.openhab.binding.mqtt.generic.values;
 
 import java.time.format.DateTimeFormatter;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -32,7 +31,7 @@ import org.openhab.core.types.UnDefType;
 @NonNullByDefault
 public class DateTimeValue extends Value {
     public DateTimeValue() {
-        super(CoreItemFactory.DATETIME, Stream.of(DateTimeType.class, StringType.class).collect(Collectors.toList()));
+        super(CoreItemFactory.DATETIME, List.of(DateTimeType.class, StringType.class));
     }
 
     @Override
index 0294f4f5271ef5c16fa2b638e23a2d6e46f56160..2eb81f3aca543916b624eb78f92d1ab873dadb63 100644 (file)
@@ -12,7 +12,7 @@
  */
 package org.openhab.binding.mqtt.generic.values;
 
-import java.util.Collections;
+import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.library.CoreItemFactory;
@@ -26,7 +26,7 @@ import org.openhab.core.types.Command;
 @NonNullByDefault
 public class ImageValue extends Value {
     public ImageValue() {
-        super(CoreItemFactory.IMAGE, Collections.emptyList());
+        super(CoreItemFactory.IMAGE, List.of());
     }
 
     @Override
index 7186dbbe16f07ff19829a8f41d4aca3c7eff3ab2..322dff58ec754ee4e3a9fc7ae4a70ce1a73d1ddb 100644 (file)
 package org.openhab.binding.mqtt.generic.values;
 
 import java.math.BigDecimal;
+import java.util.List;
 import java.util.Locale;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
 
-import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.library.CoreItemFactory;
@@ -33,11 +31,11 @@ import org.openhab.core.types.Command;
 @NonNullByDefault
 public class LocationValue extends Value {
     public LocationValue() {
-        super(CoreItemFactory.LOCATION, Stream.of(PointType.class, StringType.class).collect(Collectors.toList()));
+        super(CoreItemFactory.LOCATION, List.of(PointType.class, StringType.class));
     }
 
     @Override
-    public @NonNull String getMQTTpublishValue(@Nullable String pattern) {
+    public String getMQTTpublishValue(@Nullable String pattern) {
         String formatPattern = pattern;
         PointType point = ((PointType) state);
 
index de6f5c6f28ca1d2099713e0750b94fab44acb94f..26e352dce06528f6fd53fd36d253b63e1379fd6c 100644 (file)
@@ -13,8 +13,7 @@
 package org.openhab.binding.mqtt.generic.values;
 
 import java.math.BigDecimal;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import java.util.List;
 
 import javax.measure.Unit;
 
@@ -53,8 +52,7 @@ public class NumberValue extends Value {
 
     public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
             @Nullable Unit<?> unit) {
-        super(CoreItemFactory.NUMBER, Stream.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class)
-                .collect(Collectors.toList()));
+        super(CoreItemFactory.NUMBER, List.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class));
         this.min = min;
         this.max = max;
         this.step = step == null ? BigDecimal.ONE : step;
index 67db43f76107755c0e73d915af20d4bcf76fb022..d56eb1d66925c58a5d1f9ba5d2683c8ecae2baa6 100644 (file)
@@ -12,8 +12,7 @@
  */
 package org.openhab.binding.mqtt.generic.values;
 
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -65,7 +64,7 @@ public class OnOffValue extends Value {
      */
     public OnOffValue(@Nullable String onState, @Nullable String offState, @Nullable String onCommand,
             @Nullable String offCommand) {
-        super(CoreItemFactory.SWITCH, Stream.of(OnOffType.class, StringType.class).collect(Collectors.toList()));
+        super(CoreItemFactory.SWITCH, List.of(OnOffType.class, StringType.class));
         this.onState = onState == null ? OnOffType.ON.name() : onState;
         this.offState = offState == null ? OnOffType.OFF.name() : offState;
         this.onCommand = onCommand == null ? OnOffType.ON.name() : onCommand;
index 1bc873dc2d4382cef89a5da2ec496ae86e0f9f1c..5565476477382213366bd5730a2c872ba138bb54 100644 (file)
@@ -12,8 +12,7 @@
  */
 package org.openhab.binding.mqtt.generic.values;
 
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -36,7 +35,7 @@ public class OpenCloseValue extends Value {
      * Creates a contact Open/Close type.
      */
     public OpenCloseValue() {
-        super(CoreItemFactory.CONTACT, Stream.of(OpenClosedType.class, StringType.class).collect(Collectors.toList()));
+        super(CoreItemFactory.CONTACT, List.of(OpenClosedType.class, StringType.class));
         this.openString = OpenClosedType.OPEN.name();
         this.closeString = OpenClosedType.CLOSED.name();
     }
@@ -48,7 +47,7 @@ public class OpenCloseValue extends Value {
      * @param closeValue The OFF value string. This will be compared to MQTT messages.
      */
     public OpenCloseValue(@Nullable String openValue, @Nullable String closeValue) {
-        super(CoreItemFactory.CONTACT, Stream.of(OpenClosedType.class, StringType.class).collect(Collectors.toList()));
+        super(CoreItemFactory.CONTACT, List.of(OpenClosedType.class, StringType.class));
         this.openString = openValue == null ? OpenClosedType.OPEN.name() : openValue;
         this.closeString = closeValue == null ? OpenClosedType.CLOSED.name() : closeValue;
     }
index a8461e0a7653f0038bc36790106561270798021b..f9a537d4499acd993740b8247954664634409f41 100644 (file)
@@ -14,8 +14,7 @@ package org.openhab.binding.mqtt.generic.values;
 
 import java.math.BigDecimal;
 import java.math.MathContext;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -58,8 +57,8 @@ public class PercentageValue extends Value {
 
     public PercentageValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
             @Nullable String onValue, @Nullable String offValue) {
-        super(CoreItemFactory.DIMMER, Stream.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class,
-                OnOffType.class, UpDownType.class, StringType.class).collect(Collectors.toList()));
+        super(CoreItemFactory.DIMMER, List.of(DecimalType.class, QuantityType.class, IncreaseDecreaseType.class,
+                OnOffType.class, UpDownType.class, StringType.class));
         this.onValue = onValue;
         this.offValue = offValue;
         this.min = min == null ? BigDecimal.ZERO : min;
index f3fc46f3a88a275e3c09d0e50ed3894ba58193b1..b8f37e05ce88bbff734e0c14ea0157e1b5aafa89 100644 (file)
@@ -12,8 +12,7 @@
  */
 package org.openhab.binding.mqtt.generic.values;
 
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import java.util.List;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -51,8 +50,7 @@ public class RollershutterValue extends Value {
      */
     public RollershutterValue(@Nullable String upString, @Nullable String downString, @Nullable String stopString) {
         super(CoreItemFactory.ROLLERSHUTTER,
-                Stream.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class)
-                        .collect(Collectors.toList()));
+                List.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class));
         this.upString = upString;
         this.downString = downString;
         this.stopString = stopString == null ? StopMoveType.STOP.name() : stopString;
index f0f5e83c75f889b7d0c1663e17ca140c9fb15947..c184c5b575487b2699b421f0e0ce8b4af0423825 100644 (file)
@@ -14,7 +14,7 @@ package org.openhab.binding.mqtt.generic.values;
 
 import static java.util.function.Predicate.not;
 
-import java.util.Collections;
+import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -45,7 +45,7 @@ public class TextValue extends Value {
      *            will be allowed.
      */
     public TextValue(String[] states) {
-        super(CoreItemFactory.STRING, Collections.singletonList(StringType.class));
+        super(CoreItemFactory.STRING, List.of(StringType.class));
         Set<String> s = Stream.of(states).filter(not(String::isBlank)).collect(Collectors.toSet());
         if (!s.isEmpty()) {
             this.states = s;
@@ -55,7 +55,7 @@ public class TextValue extends Value {
     }
 
     public TextValue() {
-        super(CoreItemFactory.STRING, Collections.singletonList(StringType.class));
+        super(CoreItemFactory.STRING, List.of(StringType.class));
         this.states = null;
     }
 
index 75a582261e9acef2fb073a6aba32fa0f824408d2..c3b458dbf850a4fdded25e7a73b7804959d86b14 100644 (file)
@@ -24,12 +24,11 @@ import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Arrays;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
@@ -62,25 +61,26 @@ import org.openhab.core.thing.ChannelUID;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class ChannelStateTests {
 
-    private @Mock MqttBrokerConnection connection;
-    private @Mock ChannelStateUpdateListener channelStateUpdateListener;
-    private @Mock ChannelUID channelUID;
-    private @Spy TextValue textValue;
+    private @Mock @NonNullByDefault({}) MqttBrokerConnection connectionMock;
+    private @Mock @NonNullByDefault({}) ChannelStateUpdateListener channelStateUpdateListenerMock;
+    private @Mock @NonNullByDefault({}) ChannelUID channelUIDMock;
+    private @Spy @NonNullByDefault({}) TextValue textValue;
 
-    private ScheduledExecutorService scheduler;
+    private @NonNullByDefault({}) ScheduledExecutorService scheduler;
 
     private ChannelConfig config = ChannelConfigBuilder.create("state", "command").build();
 
     @BeforeEach
     public void setUp() {
-        CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
+        CompletableFuture<@Nullable Void> voidFutureComplete = new CompletableFuture<>();
         voidFutureComplete.complete(null);
-        doReturn(voidFutureComplete).when(connection).unsubscribeAll();
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(),
+        doReturn(voidFutureComplete).when(connectionMock).unsubscribeAll();
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).subscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).unsubscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).publish(any(), any(), anyInt(),
                 anyBoolean());
 
         scheduler = new ScheduledThreadPoolExecutor(1);
@@ -92,74 +92,74 @@ public class ChannelStateTests {
     }
 
     @Test
-    public void noInteractionTimeoutTest() throws InterruptedException, ExecutionException, TimeoutException {
-        ChannelState c = spy(new ChannelState(config, channelUID, textValue, channelStateUpdateListener));
-        c.start(connection, scheduler, 50).get(100, TimeUnit.MILLISECONDS);
-        verify(connection).subscribe(eq("state"), eq(c));
+    public void noInteractionTimeoutTest() throws Exception {
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, textValue, channelStateUpdateListenerMock));
+        c.start(connectionMock, scheduler, 50).get(100, TimeUnit.MILLISECONDS);
+        verify(connectionMock).subscribe(eq("state"), eq(c));
         c.stop().get();
-        verify(connection).unsubscribe(eq("state"), eq(c));
+        verify(connectionMock).unsubscribe(eq("state"), eq(c));
     }
 
     @Test
-    public void publishFormatTest() throws InterruptedException, ExecutionException, TimeoutException {
-        ChannelState c = spy(new ChannelState(config, channelUID, textValue, channelStateUpdateListener));
+    public void publishFormatTest() throws Exception {
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, textValue, channelStateUpdateListenerMock));
 
-        c.start(connection, scheduler, 0).get(50, TimeUnit.MILLISECONDS);
-        verify(connection).subscribe(eq("state"), eq(c));
+        c.start(connectionMock, scheduler, 0).get(50, TimeUnit.MILLISECONDS);
+        verify(connectionMock).subscribe(eq("state"), eq(c));
 
         c.publishValue(new StringType("UPDATE")).get();
-        verify(connection).publish(eq("command"), argThat(p -> Arrays.equals(p, "UPDATE".getBytes())), anyInt(),
+        verify(connectionMock).publish(eq("command"), argThat(p -> Arrays.equals(p, "UPDATE".getBytes())), anyInt(),
                 eq(false));
 
         c.config.formatBeforePublish = "prefix%s";
         c.publishValue(new StringType("UPDATE")).get();
-        verify(connection).publish(eq("command"), argThat(p -> Arrays.equals(p, "prefixUPDATE".getBytes())), anyInt(),
-                eq(false));
+        verify(connectionMock).publish(eq("command"), argThat(p -> Arrays.equals(p, "prefixUPDATE".getBytes())),
+                anyInt(), eq(false));
 
         c.config.formatBeforePublish = "%1$s-%1$s";
         c.publishValue(new StringType("UPDATE")).get();
-        verify(connection).publish(eq("command"), argThat(p -> Arrays.equals(p, "UPDATE-UPDATE".getBytes())), anyInt(),
-                eq(false));
+        verify(connectionMock).publish(eq("command"), argThat(p -> Arrays.equals(p, "UPDATE-UPDATE".getBytes())),
+                anyInt(), eq(false));
 
         c.config.formatBeforePublish = "%s";
         c.config.retained = true;
         c.publishValue(new StringType("UPDATE")).get();
-        verify(connection).publish(eq("command"), any(), anyInt(), eq(true));
+        verify(connectionMock).publish(eq("command"), any(), anyInt(), eq(true));
 
         c.stop().get();
-        verify(connection).unsubscribe(eq("state"), eq(c));
+        verify(connectionMock).unsubscribe(eq("state"), eq(c));
     }
 
     @Test
-    public void receiveWildcardTest() throws InterruptedException, ExecutionException, TimeoutException {
+    public void receiveWildcardTest() throws Exception {
         ChannelState c = spy(new ChannelState(ChannelConfigBuilder.create("state/+/topic", "command").build(),
-                channelUID, textValue, channelStateUpdateListener));
+                channelUIDMock, textValue, channelStateUpdateListenerMock));
 
-        CompletableFuture<@Nullable Void> future = c.start(connection, scheduler, 100);
+        CompletableFuture<@Nullable Void> future = c.start(connectionMock, scheduler, 100);
         c.processMessage("state/bla/topic", "A TEST".getBytes());
         future.get(300, TimeUnit.MILLISECONDS);
 
         assertThat(textValue.getChannelState().toString(), is("A TEST"));
-        verify(channelStateUpdateListener).updateChannelState(eq(channelUID), any());
+        verify(channelStateUpdateListenerMock).updateChannelState(eq(channelUIDMock), any());
     }
 
     @Test
-    public void receiveStringTest() throws InterruptedException, ExecutionException, TimeoutException {
-        ChannelState c = spy(new ChannelState(config, channelUID, textValue, channelStateUpdateListener));
+    public void receiveStringTest() throws Exception {
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, textValue, channelStateUpdateListenerMock));
 
-        CompletableFuture<@Nullable Void> future = c.start(connection, scheduler, 100);
+        CompletableFuture<@Nullable Void> future = c.start(connectionMock, scheduler, 100);
         c.processMessage("state", "A TEST".getBytes());
         future.get(300, TimeUnit.MILLISECONDS);
 
         assertThat(textValue.getChannelState().toString(), is("A TEST"));
-        verify(channelStateUpdateListener).updateChannelState(eq(channelUID), any());
+        verify(channelStateUpdateListenerMock).updateChannelState(eq(channelUIDMock), any());
     }
 
     @Test
     public void receiveDecimalTest() {
         NumberValue value = new NumberValue(null, null, new BigDecimal(10), null);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "15".getBytes());
         assertThat(value.getChannelState().toString(), is("15"));
@@ -170,14 +170,14 @@ public class ChannelStateTests {
         c.processMessage("state", "DECREASE".getBytes());
         assertThat(value.getChannelState().toString(), is("15"));
 
-        verify(channelStateUpdateListener, times(3)).updateChannelState(eq(channelUID), any());
+        verify(channelStateUpdateListenerMock, times(3)).updateChannelState(eq(channelUIDMock), any());
     }
 
     @Test
     public void receiveDecimalFractionalTest() {
         NumberValue value = new NumberValue(null, null, new BigDecimal(10.5), null);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "5.5".getBytes());
         assertThat(value.getChannelState().toString(), is("5.5"));
@@ -189,8 +189,8 @@ public class ChannelStateTests {
     @Test
     public void receiveDecimalUnitTest() {
         NumberValue value = new NumberValue(null, null, new BigDecimal(10), Units.WATT);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "15".getBytes());
         assertThat(value.getChannelState().toString(), is("15 W"));
@@ -201,27 +201,27 @@ public class ChannelStateTests {
         c.processMessage("state", "DECREASE".getBytes());
         assertThat(value.getChannelState().toString(), is("15 W"));
 
-        verify(channelStateUpdateListener, times(3)).updateChannelState(eq(channelUID), any());
+        verify(channelStateUpdateListenerMock, times(3)).updateChannelState(eq(channelUIDMock), any());
     }
 
     @Test
     public void receiveDecimalAsPercentageUnitTest() {
         NumberValue value = new NumberValue(null, null, new BigDecimal(10), Units.PERCENT);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "63.7".getBytes());
         assertThat(value.getChannelState().toString(), is("63.7 %"));
 
-        verify(channelStateUpdateListener, times(1)).updateChannelState(eq(channelUID), any());
+        verify(channelStateUpdateListenerMock, times(1)).updateChannelState(eq(channelUIDMock), any());
     }
 
     @Test
     public void receivePercentageTest() {
         PercentageValue value = new PercentageValue(new BigDecimal(-100), new BigDecimal(100), new BigDecimal(10), null,
                 null);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "-100".getBytes()); // 0%
         assertThat(value.getChannelState().toString(), is("0"));
@@ -241,8 +241,8 @@ public class ChannelStateTests {
     @Test
     public void receiveRGBColorTest() {
         ColorValue value = new ColorValue(ColorMode.RGB, "FON", "FOFF", 10);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "ON".getBytes()); // Normal on state
         assertThat(value.getChannelState().toString(), is("0,0,10"));
@@ -268,8 +268,8 @@ public class ChannelStateTests {
     @Test
     public void receiveHSBColorTest() {
         ColorValue value = new ColorValue(ColorMode.HSB, "FON", "FOFF", 10);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "ON".getBytes()); // Normal on state
         assertThat(value.getChannelState().toString(), is("0,0,10"));
@@ -291,8 +291,8 @@ public class ChannelStateTests {
     @Test
     public void receiveXYYColorTest() {
         ColorValue value = new ColorValue(ColorMode.XYY, "FON", "FOFF", 10);
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "ON".getBytes()); // Normal on state
         assertThat(value.getChannelState().toString(), is("0,0,10"));
@@ -317,8 +317,8 @@ public class ChannelStateTests {
     @Test
     public void receiveLocationTest() {
         LocationValue value = new LocationValue();
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         c.processMessage("state", "46.833974, 7.108433".getBytes());
         assertThat(value.getChannelState().toString(), is("46.833974,7.108433"));
@@ -328,8 +328,8 @@ public class ChannelStateTests {
     @Test
     public void receiveDateTimeTest() {
         DateTimeValue value = new DateTimeValue();
-        ChannelState subject = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        subject.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState subject = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        subject.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
         ZonedDateTime zd = ZonedDateTime.now();
         String datetime = zd.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
@@ -345,10 +345,10 @@ public class ChannelStateTests {
     @Test
     public void receiveImageTest() {
         ImageValue value = new ImageValue();
-        ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
-        c.start(connection, mock(ScheduledExecutorService.class), 100);
+        ChannelState c = spy(new ChannelState(config, channelUIDMock, value, channelStateUpdateListenerMock));
+        c.start(connectionMock, mock(ScheduledExecutorService.class), 100);
 
-        byte[] payload = new byte[] { (byte) 0xFF, (byte) 0xD8, 0x01, 0x02, (byte) 0xFF, (byte) 0xD9 };
+        byte[] payload = { (byte) 0xFF, (byte) 0xD8, 0x01, 0x02, (byte) 0xFF, (byte) 0xD9 };
         c.processMessage("state", payload);
         assertThat(value.getChannelState(), is(instanceOf(RawType.class)));
         assertThat(((RawType) value.getChannelState()).getMimeType(), is("image/jpeg"));
index 0b56767429181ffe6165da4746c5e2c714c769bd..8b1af9f861e771dade1756d43f2f62a26350bfa9 100644 (file)
@@ -20,8 +20,8 @@ import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelCons
 
 import java.util.concurrent.CompletableFuture;
 
-import javax.naming.ConfigurationException;
-
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -33,7 +33,6 @@ import org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler
 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
 import org.openhab.core.config.core.Configuration;
 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
-import org.openhab.core.io.transport.mqtt.MqttException;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
@@ -49,43 +48,44 @@ import org.openhab.core.transform.TransformationService;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class ChannelStateTransformationTests {
 
-    private @Mock TransformationService jsonPathService;
-    private @Mock TransformationServiceProvider transformationServiceProvider;
-    private @Mock ThingHandlerCallback callback;
-    private @Mock Thing thing;
-    private @Mock AbstractBrokerHandler bridgeHandler;
-    private @Mock MqttBrokerConnection connection;
+    private @Mock @NonNullByDefault({}) TransformationService jsonPathServiceMock;
+    private @Mock @NonNullByDefault({}) TransformationServiceProvider transformationServiceProviderMock;
+    private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
+    private @Mock @NonNullByDefault({}) Thing thingMock;
+    private @Mock @NonNullByDefault({}) AbstractBrokerHandler bridgeHandlerMock;
+    private @Mock @NonNullByDefault({}) MqttBrokerConnection connectionMock;
 
-    private GenericMQTTThingHandler thingHandler;
+    private @NonNullByDefault({}) GenericMQTTThingHandler thingHandler;
 
     @BeforeEach
-    public void setUp() throws ConfigurationException, MqttException {
+    public void setUp() throws Exception {
         ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
 
         // Mock the thing: We need the thingUID and the bridgeUID
-        when(thing.getUID()).thenReturn(testGenericThing);
-        when(thing.getChannels()).thenReturn(thingChannelListWithJson);
-        when(thing.getStatusInfo()).thenReturn(thingStatus);
-        when(thing.getConfiguration()).thenReturn(new Configuration());
+        when(thingMock.getUID()).thenReturn(TEST_GENERIC_THING);
+        when(thingMock.getChannels()).thenReturn(THING_CHANNEL_LIST_WITH_JSON);
+        when(thingMock.getStatusInfo()).thenReturn(thingStatus);
+        when(thingMock.getConfiguration()).thenReturn(new Configuration());
 
         // Return the mocked connection object if the bridge handler is asked for it
-        when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
+        when(bridgeHandlerMock.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connectionMock));
 
-        CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
+        CompletableFuture<@Nullable Void> voidFutureComplete = new CompletableFuture<>();
         voidFutureComplete.complete(null);
-        doReturn(voidFutureComplete).when(connection).unsubscribeAll();
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
+        doReturn(voidFutureComplete).when(connectionMock).unsubscribeAll();
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).subscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).unsubscribe(any(), any());
 
-        thingHandler = spy(new GenericMQTTThingHandler(thing, mock(MqttChannelStateDescriptionProvider.class),
-                transformationServiceProvider, 1500));
-        when(transformationServiceProvider.getTransformationService(anyString())).thenReturn(jsonPathService);
+        thingHandler = spy(new GenericMQTTThingHandler(thingMock, mock(MqttChannelStateDescriptionProvider.class),
+                transformationServiceProviderMock, 1500));
+        when(transformationServiceProviderMock.getTransformationService(anyString())).thenReturn(jsonPathServiceMock);
 
-        thingHandler.setCallback(callback);
+        thingHandler.setCallback(callbackMock);
         // Return the bridge handler if the thing handler asks for it
-        doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
+        doReturn(bridgeHandlerMock).when(thingHandler).getBridgeHandler();
 
         // We are by default online
         doReturn(thingStatus).when(thingHandler).getBridgeStatus();
@@ -93,31 +93,31 @@ public class ChannelStateTransformationTests {
 
     @SuppressWarnings("null")
     @Test
-    public void initialize() throws MqttException {
-        when(thing.getChannels()).thenReturn(thingChannelListWithJson);
+    public void initialize() throws Exception {
+        when(thingMock.getChannels()).thenReturn(THING_CHANNEL_LIST_WITH_JSON);
 
         thingHandler.initialize();
-        ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
-        assertThat(channelConfig.transformationsIn.get(0).pattern, is(jsonPathPattern));
+        ChannelState channelConfig = thingHandler.getChannelState(TEXT_CHANNEL_UID);
+        assertThat(channelConfig.transformationsIn.get(0).pattern, is(JSON_PATH_PATTERN));
     }
 
     @SuppressWarnings("null")
     @Test
     public void processMessageWithJSONPath() throws Exception {
-        when(jsonPathService.transform(jsonPathPattern, jsonPathJSON)).thenReturn("23.2");
+        when(jsonPathServiceMock.transform(JSON_PATH_PATTERN, JSON_PATH_JSON)).thenReturn("23.2");
 
         thingHandler.initialize();
-        ChannelState channelConfig = thingHandler.getChannelState(textChannelUID);
+        ChannelState channelConfig = thingHandler.getChannelState(TEXT_CHANNEL_UID);
         channelConfig.setChannelStateUpdateListener(thingHandler);
 
         ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);
 
-        byte payload[] = jsonPathJSON.getBytes();
-        assertThat(transformation.pattern, is(jsonPathPattern));
+        byte payload[] = JSON_PATH_JSON.getBytes();
+        assertThat(transformation.pattern, is(JSON_PATH_PATTERN));
         // Test process message
         channelConfig.processMessage(channelConfig.getStateTopic(), payload);
 
-        verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "23.2".equals(arg.toString())));
+        verify(callbackMock).stateUpdated(eq(TEXT_CHANNEL_UID), argThat(arg -> "23.2".equals(arg.toString())));
         assertThat(channelConfig.getCache().getChannelState().toString(), is("23.2"));
     }
 }
index 0621138b97874f0e2de73d3dc613b9b22c6aa60c..bcfa89e1c09aa62e2f62e6a1efd27a0e7c0caa98 100644 (file)
@@ -21,6 +21,8 @@ import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelCons
 
 import java.util.concurrent.CompletableFuture;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -57,42 +59,43 @@ import org.openhab.core.types.RefreshType;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class GenericThingHandlerTests {
 
-    private @Mock ThingHandlerCallback callback;
-    private @Mock Thing thing;
-    private @Mock AbstractBrokerHandler bridgeHandler;
-    private @Mock MqttBrokerConnection connection;
+    private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
+    private @Mock @NonNullByDefault({}) Thing thingMock;
+    private @Mock @NonNullByDefault({}) AbstractBrokerHandler bridgeHandlerMock;
+    private @Mock @NonNullByDefault({}) MqttBrokerConnection connectionMock;
 
-    private GenericMQTTThingHandler thingHandler;
+    private @NonNullByDefault({}) GenericMQTTThingHandler thingHandler;
 
     @BeforeEach
     public void setUp() {
         ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
 
         // Mock the thing: We need the thingUID and the bridgeUID
-        when(thing.getUID()).thenReturn(testGenericThing);
-        when(thing.getChannels()).thenReturn(thingChannelList);
-        when(thing.getStatusInfo()).thenReturn(thingStatus);
-        when(thing.getConfiguration()).thenReturn(new Configuration());
+        when(thingMock.getUID()).thenReturn(TEST_GENERIC_THING);
+        when(thingMock.getChannels()).thenReturn(THING_CHANNEL_LIST);
+        when(thingMock.getStatusInfo()).thenReturn(thingStatus);
+        when(thingMock.getConfiguration()).thenReturn(new Configuration());
 
         // Return the mocked connection object if the bridge handler is asked for it
-        when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
+        when(bridgeHandlerMock.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connectionMock));
 
-        CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
+        CompletableFuture<@Nullable Void> voidFutureComplete = new CompletableFuture<>();
         voidFutureComplete.complete(null);
-        doReturn(voidFutureComplete).when(connection).unsubscribeAll();
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(),
+        doReturn(voidFutureComplete).when(connectionMock).unsubscribeAll();
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).subscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).unsubscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).publish(any(), any(), anyInt(),
                 anyBoolean());
 
-        thingHandler = spy(new GenericMQTTThingHandler(thing, mock(MqttChannelStateDescriptionProvider.class),
+        thingHandler = spy(new GenericMQTTThingHandler(thingMock, mock(MqttChannelStateDescriptionProvider.class),
                 mock(TransformationServiceProvider.class), 1500));
-        thingHandler.setCallback(callback);
+        thingHandler.setCallback(callbackMock);
 
         // Return the bridge handler if the thing handler asks for it
-        doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
+        doReturn(bridgeHandlerMock).when(thingHandler).getBridgeHandler();
 
         // The broker connection bridge is by default online
         doReturn(thingStatus).when(thingHandler).getBridgeStatus();
@@ -102,8 +105,8 @@ public class GenericThingHandlerTests {
     public void initializeWithUnknownThingUID() {
         ChannelConfig config = textConfiguration().as(ChannelConfig.class);
         assertThrows(IllegalArgumentException.class,
-                () -> thingHandler.createChannelState(config, new ChannelUID(testGenericThing, "test"),
-                        ValueFactory.createValueState(config, unknownChannel.getId())));
+                () -> thingHandler.createChannelState(config, new ChannelUID(TEST_GENERIC_THING, "test"),
+                        ValueFactory.createValueState(config, UNKNOWN_CHANNEL.getId())));
     }
 
     @Test
@@ -111,16 +114,16 @@ public class GenericThingHandlerTests {
         thingHandler.initialize();
         verify(thingHandler).bridgeStatusChanged(any());
         verify(thingHandler).start(any());
-        assertThat(thingHandler.getConnection(), is(connection));
+        assertThat(thingHandler.getConnection(), is(connectionMock));
 
-        ChannelState channelConfig = thingHandler.channelStateByChannelUID.get(textChannelUID);
+        ChannelState channelConfig = thingHandler.channelStateByChannelUID.get(TEXT_CHANNEL_UID);
         assertThat(channelConfig.getStateTopic(), is("test/state"));
         assertThat(channelConfig.getCommandTopic(), is("test/command"));
 
-        verify(connection).subscribe(eq(channelConfig.getStateTopic()), eq(channelConfig));
+        verify(connectionMock).subscribe(eq(channelConfig.getStateTopic()), eq(channelConfig));
 
-        verify(callback).statusUpdated(eq(thing), argThat((arg) -> arg.getStatus().equals(ThingStatus.ONLINE)
-                && arg.getStatusDetail().equals(ThingStatusDetail.NONE)));
+        verify(callbackMock).statusUpdated(eq(thingMock), argThat(arg -> ThingStatus.ONLINE.equals(arg.getStatus())
+                && ThingStatusDetail.NONE.equals(arg.getStatusDetail())));
     }
 
     @Test
@@ -135,24 +138,24 @@ public class GenericThingHandlerTests {
         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
         thingHandler.initialize();
 
-        ThingHandlerHelper.setConnection(thingHandler, connection);
+        ThingHandlerHelper.setConnection(thingHandler, connectionMock);
 
-        thingHandler.handleCommand(textChannelUID, RefreshType.REFRESH);
-        verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "DEMOVALUE".equals(arg.toString())));
+        thingHandler.handleCommand(TEXT_CHANNEL_UID, RefreshType.REFRESH);
+        verify(callbackMock).stateUpdated(eq(TEXT_CHANNEL_UID), argThat(arg -> "DEMOVALUE".equals(arg.toString())));
     }
 
     @Test
     public void handleCommandUpdateString() {
         TextValue value = spy(new TextValue());
         ChannelState channelConfig = spy(
-                new ChannelState(ChannelConfigBuilder.create("stateTopic", "commandTopic").build(), textChannelUID,
+                new ChannelState(ChannelConfigBuilder.create("stateTopic", "commandTopic").build(), TEXT_CHANNEL_UID,
                         value, thingHandler));
         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
         thingHandler.initialize();
-        ThingHandlerHelper.setConnection(thingHandler, connection);
+        ThingHandlerHelper.setConnection(thingHandler, connectionMock);
 
         StringType updateValue = new StringType("UPDATE");
-        thingHandler.handleCommand(textChannelUID, updateValue);
+        thingHandler.handleCommand(TEXT_CHANNEL_UID, updateValue);
         verify(value).update(eq(updateValue));
         assertThat(channelConfig.getCache().getChannelState().toString(), is("UPDATE"));
     }
@@ -161,14 +164,14 @@ public class GenericThingHandlerTests {
     public void handleCommandUpdateBoolean() {
         OnOffValue value = spy(new OnOffValue("ON", "OFF"));
         ChannelState channelConfig = spy(
-                new ChannelState(ChannelConfigBuilder.create("stateTopic", "commandTopic").build(), textChannelUID,
+                new ChannelState(ChannelConfigBuilder.create("stateTopic", "commandTopic").build(), TEXT_CHANNEL_UID,
                         value, thingHandler));
         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
         thingHandler.initialize();
-        ThingHandlerHelper.setConnection(thingHandler, connection);
+        ThingHandlerHelper.setConnection(thingHandler, connectionMock);
 
         StringType updateValue = new StringType("ON");
-        thingHandler.handleCommand(textChannelUID, updateValue);
+        thingHandler.handleCommand(TEXT_CHANNEL_UID, updateValue);
 
         verify(value).update(eq(updateValue));
         assertThat(channelConfig.getCache().getChannelState(), is(OnOffType.ON));
@@ -178,7 +181,7 @@ public class GenericThingHandlerTests {
     public void processMessage() {
         TextValue textValue = new TextValue();
         ChannelState channelConfig = spy(
-                new ChannelState(ChannelConfigBuilder.create("test/state", "test/state/set").build(), textChannelUID,
+                new ChannelState(ChannelConfigBuilder.create("test/state", "test/state/set").build(), TEXT_CHANNEL_UID,
                         textValue, thingHandler));
         doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
         thingHandler.initialize();
@@ -186,10 +189,10 @@ public class GenericThingHandlerTests {
         // Test process message
         channelConfig.processMessage("test/state", payload);
 
-        verify(callback, atLeastOnce()).statusUpdated(eq(thing),
-                argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE)));
+        verify(callbackMock, atLeastOnce()).statusUpdated(eq(thingMock),
+                argThat(arg -> ThingStatus.ONLINE.equals(arg.getStatus())));
 
-        verify(callback).stateUpdated(eq(textChannelUID), argThat(arg -> "UPDATE".equals(arg.toString())));
+        verify(callbackMock).stateUpdated(eq(TEXT_CHANNEL_UID), argThat(arg -> "UPDATE".equals(arg.toString())));
         assertThat(textValue.getChannelState().toString(), is("UPDATE"));
     }
 
@@ -197,11 +200,11 @@ public class GenericThingHandlerTests {
     public void handleBridgeStatusChange() {
         Configuration config = new Configuration();
         config.put("availabilityTopic", "test/LWT");
-        when(thing.getConfiguration()).thenReturn(config);
+        when(thingMock.getConfiguration()).thenReturn(config);
         thingHandler.initialize();
         thingHandler
                 .bridgeStatusChanged(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, null));
         thingHandler.bridgeStatusChanged(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null));
-        verify(connection, times(2)).subscribe(eq("test/LWT"), any());
+        verify(connectionMock, times(2)).subscribe(eq("test/LWT"), any());
     }
 }
index 69fce0358c8e8a5015dc29c2d6e1b582caa1ae38..00c358a596a560cea4660ff97e90be8a7deb2bc6 100644 (file)
@@ -37,23 +37,23 @@ import org.openhab.core.thing.type.ChannelTypeUID;
 @NonNullByDefault
 public class ThingChannelConstants {
     // Common ThingUID and ChannelUIDs
-    public static final ThingUID testGenericThing = new ThingUID(GENERIC_MQTT_THING, "genericthing");
+    public static final ThingUID TEST_GENERIC_THING = new ThingUID(GENERIC_MQTT_THING, "genericthing");
 
-    public static final ChannelTypeUID textChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.STRING);
-    public static final ChannelTypeUID textWithJsonChannel = new ChannelTypeUID(BINDING_ID,
+    public static final ChannelTypeUID TEXT_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.STRING);
+    public static final ChannelTypeUID TEXT_WITH_JSON_CHANNEL = new ChannelTypeUID(BINDING_ID,
             MqttBindingConstants.STRING);
-    public static final ChannelTypeUID onoffChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.SWITCH);
-    public static final ChannelTypeUID numberChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.NUMBER);
-    public static final ChannelTypeUID percentageChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.DIMMER);
-    public static final ChannelTypeUID unknownChannel = new ChannelTypeUID(BINDING_ID, "unknown");
+    public static final ChannelTypeUID ON_OFF_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.SWITCH);
+    public static final ChannelTypeUID NUMBER_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.NUMBER);
+    public static final ChannelTypeUID PERCENTAGE_CHANNEL = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.DIMMER);
+    public static final ChannelTypeUID UNKNOWN_CHANNEL = new ChannelTypeUID(BINDING_ID, "unknown");
 
-    public static final ChannelUID textChannelUID = new ChannelUID(testGenericThing, "mytext");
+    public static final ChannelUID TEXT_CHANNEL_UID = new ChannelUID(TEST_GENERIC_THING, "mytext");
 
-    public static final String jsonPathJSON = "{ \"device\": { \"status\": { \"temperature\": 23.2 }}}";
-    public static final String jsonPathPattern = "$.device.status.temperature";
+    public static final String JSON_PATH_JSON = "{ \"device\": { \"status\": { \"temperature\": 23.2 }}}";
+    public static final String JSON_PATH_PATTERN = "$.device.status.temperature";
 
-    public static final List<Channel> thingChannelList = new ArrayList<>();
-    public static final List<Channel> thingChannelListWithJson = new ArrayList<>();
+    public static final List<Channel> THING_CHANNEL_LIST = new ArrayList<>();
+    public static final List<Channel> THING_CHANNEL_LIST_WITH_JSON = new ArrayList<>();
 
     /**
      * Create a channel with exact the parameters we need for the tests
@@ -65,20 +65,21 @@ public class ThingChannelConstants {
      * @return
      */
     public static Channel cb(String id, String acceptedType, Configuration config, ChannelTypeUID channelTypeUID) {
-        return ChannelBuilder.create(new ChannelUID(testGenericThing, id), acceptedType).withConfiguration(config)
+        return ChannelBuilder.create(new ChannelUID(TEST_GENERIC_THING, id), acceptedType).withConfiguration(config)
                 .withType(channelTypeUID).build();
     }
 
     static {
-        thingChannelList.add(cb("mytext", "TextItemType", textConfiguration(), textChannel));
-        thingChannelList.add(cb("onoff", "OnOffType", onoffConfiguration(), onoffChannel));
-        thingChannelList.add(cb("num", "NumberType", numberConfiguration(), numberChannel));
-        thingChannelList.add(cb("percent", "NumberType", percentageConfiguration(), percentageChannel));
-
-        thingChannelListWithJson.add(cb("mytext", "TextItemType", textConfigurationWithJson(), textWithJsonChannel));
-        thingChannelListWithJson.add(cb("onoff", "OnOffType", onoffConfiguration(), onoffChannel));
-        thingChannelListWithJson.add(cb("num", "NumberType", numberConfiguration(), numberChannel));
-        thingChannelListWithJson.add(cb("percent", "NumberType", percentageConfiguration(), percentageChannel));
+        THING_CHANNEL_LIST.add(cb("mytext", "TextItemType", textConfiguration(), TEXT_CHANNEL));
+        THING_CHANNEL_LIST.add(cb("onoff", "OnOffType", onoffConfiguration(), ON_OFF_CHANNEL));
+        THING_CHANNEL_LIST.add(cb("num", "NumberType", numberConfiguration(), NUMBER_CHANNEL));
+        THING_CHANNEL_LIST.add(cb("percent", "NumberType", percentageConfiguration(), PERCENTAGE_CHANNEL));
+
+        THING_CHANNEL_LIST_WITH_JSON
+                .add(cb("mytext", "TextItemType", textConfigurationWithJson(), TEXT_WITH_JSON_CHANNEL));
+        THING_CHANNEL_LIST_WITH_JSON.add(cb("onoff", "OnOffType", onoffConfiguration(), ON_OFF_CHANNEL));
+        THING_CHANNEL_LIST_WITH_JSON.add(cb("num", "NumberType", numberConfiguration(), NUMBER_CHANNEL));
+        THING_CHANNEL_LIST_WITH_JSON.add(cb("percent", "NumberType", percentageConfiguration(), PERCENTAGE_CHANNEL));
     }
 
     static Configuration textConfiguration() {
@@ -92,7 +93,7 @@ public class ThingChannelConstants {
         Map<String, Object> data = new HashMap<>();
         data.put("stateTopic", "test/state");
         data.put("commandTopic", "test/command");
-        data.put("transformationPattern", "JSONPATH:" + jsonPathPattern);
+        data.put("transformationPattern", "JSONPATH:" + JSON_PATH_PATTERN);
         return new Configuration(data);
     }
 
index 8b3b51c60af905f02ac44b349dfce75781344c58..029c8739e093c1f3d36a46b545bf59b5ae9825ca 100644 (file)
@@ -29,7 +29,7 @@ import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.stream.Stream;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -60,6 +60,7 @@ import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class MqttTopicClassMapperTests {
     @Retention(RetentionPolicy.RUNTIME)
     @Target({ FIELD })
@@ -72,15 +73,15 @@ public class MqttTopicClassMapperTests {
         public transient String ignoreTransient = "";
         public final String ignoreFinal = "";
 
-        public @TestValue("string") String aString;
-        public @TestValue("false") Boolean aBoolean;
-        public @TestValue("10") Long aLong;
-        public @TestValue("10") Integer aInteger;
-        public @TestValue("10") BigDecimal aDecimal;
+        public @TestValue("string") @Nullable String aString;
+        public @TestValue("false") @Nullable Boolean aBoolean;
+        public @TestValue("10") @Nullable Long aLong;
+        public @TestValue("10") @Nullable Integer aInteger;
+        public @TestValue("10") @Nullable BigDecimal aDecimal;
 
-        public @TestValue("10") @TopicPrefix("a") int Int = 24;
+        public @TestValue("10") @TopicPrefix("a") int aInt = 24;
         public @TestValue("false") boolean aBool = true;
-        public @TestValue("abc,def") @MQTTvalueTransform(splitCharacter = ",") String[] properties;
+        public @TestValue("abc,def") @MQTTvalueTransform(splitCharacter = ",") String @Nullable [] properties;
 
         public enum ReadyState {
             unknown,
@@ -99,22 +100,16 @@ public class MqttTopicClassMapperTests {
         public @TestValue("integer") @MQTTvalueTransform(suffix = "_") DataTypeEnum datatype = DataTypeEnum.unknown;
 
         @Override
-        public @NonNull Object getFieldsOf() {
+        public Object getFieldsOf() {
             return this;
         }
     }
 
-    @Mock
-    MqttBrokerConnection connection;
+    private @Mock @NonNullByDefault({}) MqttBrokerConnection connectionMock;
+    private @Mock @NonNullByDefault({}) ScheduledExecutorService executorMock;
+    private @Mock @NonNullByDefault({}) AttributeChanged fieldChangedObserverMock;
+    private @Spy Object countInjectedFields = new Object();
 
-    @Mock
-    ScheduledExecutorService executor;
-
-    @Mock
-    AttributeChanged fieldChangedObserver;
-
-    @Spy
-    Object countInjectedFields = new Object();
     int injectedFields = 0;
 
     // A completed future is returned for a subscribe call to the attributes
@@ -122,8 +117,8 @@ public class MqttTopicClassMapperTests {
 
     @BeforeEach
     public void setUp() {
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).subscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).unsubscribe(any(), any());
         injectedFields = (int) Stream.of(countInjectedFields.getClass().getDeclaredFields())
                 .filter(AbstractMqttAttributeClass::filterField).count();
     }
@@ -148,8 +143,8 @@ public class MqttTopicClassMapperTests {
                 anyBoolean());
 
         // Subscribe now to all fields
-        CompletableFuture<Void> future = attributes.subscribeAndReceive(connection, executor, "homie/device123", null,
-                10);
+        CompletableFuture<@Nullable Void> future = attributes.subscribeAndReceive(connectionMock, executorMock,
+                "homie/device123", null, 10);
         assertThat(future.isDone(), is(true));
         assertThat(attributes.subscriptions.size(), is(10 + injectedFields));
     }
@@ -157,17 +152,17 @@ public class MqttTopicClassMapperTests {
     // TODO timeout
     @SuppressWarnings({ "null", "unused" })
     @Test
-    public void subscribeAndReceive() throws IllegalArgumentException, IllegalAccessException {
+    public void subscribeAndReceive() throws Exception {
         final Attributes attributes = spy(new Attributes());
 
         doAnswer(this::createSubscriberAnswer).when(attributes).createSubscriber(any(), any(), anyString(),
                 anyBoolean());
 
-        verify(connection, times(0)).subscribe(anyString(), any());
+        verify(connectionMock, times(0)).subscribe(anyString(), any());
 
         // Subscribe now to all fields
-        CompletableFuture<Void> future = attributes.subscribeAndReceive(connection, executor, "homie/device123",
-                fieldChangedObserver, 10);
+        CompletableFuture<@Nullable Void> future = attributes.subscribeAndReceive(connectionMock, executorMock,
+                "homie/device123", fieldChangedObserverMock, 10);
         assertThat(future.isDone(), is(true));
 
         // We expect 10 subscriptions now
@@ -190,7 +185,7 @@ public class MqttTopicClassMapperTests {
 
             // Simulate a received MQTT value and use the annotation data as input.
             f.processMessage(f.topic, annotation.value().getBytes());
-            verify(fieldChangedObserver, times(++loopCounter)).attributeChanged(any(), any(), any(), any(),
+            verify(fieldChangedObserverMock, times(++loopCounter)).attributeChanged(any(), any(), any(), any(),
                     anyBoolean());
 
             // Check each value if the assignment worked
@@ -213,23 +208,23 @@ public class MqttTopicClassMapperTests {
     }
 
     @Test
-    public void ignoresInvalidEnum() throws IllegalArgumentException, IllegalAccessException {
+    public void ignoresInvalidEnum() throws Exception {
         final Attributes attributes = spy(new Attributes());
 
         doAnswer(this::createSubscriberAnswer).when(attributes).createSubscriber(any(), any(), anyString(),
                 anyBoolean());
 
-        verify(connection, times(0)).subscribe(anyString(), any());
+        verify(connectionMock, times(0)).subscribe(anyString(), any());
 
         // Subscribe now to all fields
-        CompletableFuture<Void> future = attributes.subscribeAndReceive(connection, executor, "homie/device123",
-                fieldChangedObserver, 10);
+        CompletableFuture<@Nullable Void> future = attributes.subscribeAndReceive(connectionMock, executorMock,
+                "homie/device123", fieldChangedObserverMock, 10);
         assertThat(future.isDone(), is(true));
 
         SubscribeFieldToMQTTtopic field = attributes.subscriptions.stream().filter(f -> f.field.getName() == "state")
                 .findFirst().get();
         field.processMessage(field.topic, "garbage".getBytes());
-        verify(fieldChangedObserver, times(0)).attributeChanged(any(), any(), any(), any(), anyBoolean());
+        verify(fieldChangedObserverMock, times(0)).attributeChanged(any(), any(), any(), any(), anyBoolean());
         assertThat(attributes.state.toString(), is("unknown"));
     }
 }
index baad5b6ef5c549772fb8db69bb62462489dec79a..e2e742bd859f8cc1de365f480355fcf13120ada7 100644 (file)
@@ -31,7 +31,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -50,6 +50,7 @@ import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class SubscribeFieldToMQTTtopicTests {
     @Retention(RetentionPolicy.RUNTIME)
     @Target({ FIELD })
@@ -64,15 +65,15 @@ public class SubscribeFieldToMQTTtopicTests {
         @SuppressWarnings("unused")
         public final String ignoreFinal = "";
 
-        public @TestValue("string") String aString;
-        public @TestValue("false") Boolean aBoolean;
-        public @TestValue("10") Long aLong;
-        public @TestValue("10") Integer aInteger;
-        public @TestValue("10") BigDecimal aDecimal;
+        public @TestValue("string") @Nullable String aString;
+        public @TestValue("false") @Nullable Boolean aBoolean;
+        public @TestValue("10") @Nullable Long aLong;
+        public @TestValue("10") @Nullable Integer aInteger;
+        public @TestValue("10") @Nullable BigDecimal aDecimal;
 
-        public @TestValue("10") @TopicPrefix("a") int Int = 24;
+        public @TestValue("10") @TopicPrefix("a") int aInt = 24;
         public @TestValue("false") boolean aBool = true;
-        public @TestValue("abc,def") @MQTTvalueTransform(splitCharacter = ",") String[] properties;
+        public @TestValue("abc,def") @MQTTvalueTransform(splitCharacter = ",") String @Nullable [] properties;
 
         public enum ReadyState {
             unknown,
@@ -91,53 +92,44 @@ public class SubscribeFieldToMQTTtopicTests {
         public @TestValue("integer") @MQTTvalueTransform(suffix = "_") DataTypeEnum datatype = DataTypeEnum.unknown;
 
         @Override
-        public @NonNull Object getFieldsOf() {
+        public Object getFieldsOf() {
             return this;
         }
     }
 
     Attributes attributes = new Attributes();
 
-    @Mock
-    MqttBrokerConnection connection;
-
-    @Mock
-    SubscribeFieldToMQTTtopic fieldSubscriber;
-
-    @Mock
-    FieldChanged fieldChanged;
+    private @Mock @NonNullByDefault({}) MqttBrokerConnection connectionMock;
+    private @Mock @NonNullByDefault({}) FieldChanged fieldChangedMock;
 
     @BeforeEach
     public void setUp() {
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).subscribe(any(), any());
     }
 
     @Test
-    public void TimeoutIfNoMessageReceive()
-            throws InterruptedException, NoSuchFieldException, ExecutionException, TimeoutException {
-        final Field field = Attributes.class.getField("Int");
+    public void timeoutIfNoMessageReceive() throws Exception {
+        final Field field = Attributes.class.getField("aInt");
         ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
 
-        SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, fieldChanged,
+        SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, fieldChangedMock,
                 "homie/device123", false);
         assertThrows(TimeoutException.class,
-                () -> subscriber.subscribeAndReceive(connection, 1000).get(50, TimeUnit.MILLISECONDS));
+                () -> subscriber.subscribeAndReceive(connectionMock, 1000).get(50, TimeUnit.MILLISECONDS));
     }
 
     @Test
-    public void MandatoryMissing()
-            throws InterruptedException, NoSuchFieldException, ExecutionException, TimeoutException {
-        final Field field = Attributes.class.getField("Int");
+    public void mandatoryMissing() throws Exception {
+        final Field field = Attributes.class.getField("aInt");
         ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
 
-        SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, fieldChanged,
+        SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, fieldChangedMock,
                 "homie/device123", true);
-        assertThrows(ExecutionException.class, () -> subscriber.subscribeAndReceive(connection, 50).get());
+        assertThrows(ExecutionException.class, () -> subscriber.subscribeAndReceive(connectionMock, 50).get());
     }
 
     @Test
-    public void MessageReceive()
-            throws InterruptedException, NoSuchFieldException, ExecutionException, TimeoutException {
+    public void messageReceive() throws Exception {
         final FieldChanged changed = (field, value) -> {
             try {
                 field.set(attributes.getFieldsOf(), value);
@@ -145,17 +137,17 @@ public class SubscribeFieldToMQTTtopicTests {
                 fail(e.getMessage());
             }
         };
-        final Field field = Attributes.class.getField("Int");
+        final Field field = Attributes.class.getField("aInt");
         ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
 
         SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, changed,
                 "homie/device123", false);
-        CompletableFuture<@Nullable Void> future = subscriber.subscribeAndReceive(connection, 1000);
+        CompletableFuture<@Nullable Void> future = subscriber.subscribeAndReceive(connectionMock, 1000);
 
         // Simulate a received MQTT message
         subscriber.processMessage("ignored", "10".getBytes());
         // No timeout should happen
         future.get(50, TimeUnit.MILLISECONDS);
-        assertThat(attributes.Int, is(10));
+        assertThat(attributes.aInt, is(10));
     }
 }
index eb39ed24351aaac92b949202600b3062dd39b277..8a407c3ad476a4e8e6a162284774c5d776207e62 100644 (file)
@@ -17,7 +17,9 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.*;
 
 import java.math.BigDecimal;
+import java.util.Objects;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
 import org.openhab.core.library.types.DecimalType;
@@ -44,9 +46,10 @@ import org.openhab.core.types.TypeParser;
  *
  * @author David Graeff - Initial contribution
  */
+@NonNullByDefault
 public class ValueTests {
-    Command p(Value v, String str) {
-        return TypeParser.parseCommand(v.getSupportedCommandTypes(), str);
+    private Command p(Value v, String str) {
+        return Objects.requireNonNull(TypeParser.parseCommand(v.getSupportedCommandTypes(), str));
     }
 
     @Test
index b2f40de9c7c1ff66245702b02ef0258ada768879..cb755ac0fef7c676d457794f3d0719cbaf4c7ee4 100644 (file)
@@ -24,8 +24,6 @@ import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChanne
 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
 import org.openhab.binding.mqtt.homeassistant.internal.exception.UnsupportedComponentException;
 import org.openhab.core.thing.ThingUID;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import com.google.gson.Gson;
 
@@ -37,8 +35,6 @@ import com.google.gson.Gson;
  */
 @NonNullByDefault
 public class ComponentFactory {
-    private static final Logger LOGGER = LoggerFactory.getLogger(ComponentFactory.class);
-
     /**
      * Create a HA MQTT component. The configuration JSon string is required.
      *
index 862601a6d3587e288f93ea53757029a71fa23f4d..b1418c2d13eb29400ceceebcdf108e2b1882e957 100644 (file)
@@ -154,7 +154,7 @@ public class ChannelConfigurationTypeAdapterFactory implements TypeAdapterFactor
 
             field.set(config, newValue);
         } catch (IllegalArgumentException | IllegalAccessException e) {
-            throw new RuntimeException(e);
+            throw new IllegalStateException(e);
         }
     }
 }
index cf59a8c1a29175593ea4fc768f18062b3403678e..111ca895c049daf2b33f2d4d4f3aa998fd15bf50 100644 (file)
  */
 package org.openhab.binding.mqtt.homeassistant.internal.exception;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * Exception class for errors in HomeAssistant components configurations
  *
  * @author Anton Kharuzhy - Initial contribution
  */
+@NonNullByDefault
 public class ConfigurationException extends RuntimeException {
     public ConfigurationException(String message) {
         super(message);
index 3d3941e97c86cccc80fd89bf4727672f58d07521..27341e9e7aa3a680119101f87d4058ef0a8199f6 100644 (file)
  */
 package org.openhab.binding.mqtt.homeassistant.internal.exception;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * Exception class for unsupported components
  *
  * @author Anton Kharuzhy - Initial contribution
  */
+@NonNullByDefault
 public class UnsupportedComponentException extends ConfigurationException {
     public UnsupportedComponentException(String message) {
         super(message);
index d01acd83e3c77793e0b9948fb27140bf7212e16d..e7dc1a136e14c9925da3734637b6f7f1df8b7ffb 100644 (file)
@@ -19,12 +19,14 @@ import static org.hamcrest.core.IsIterableContaining.hasItem;
 import java.util.Collection;
 import java.util.Collections;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.core.config.core.Configuration;
 
 /**
  * @author Jochen Klein - Initial contribution
  */
+@NonNullByDefault
 public class HaIDTests {
 
     @Test
index 1e128ef875aa703e12f5d2359543fb53fe741e4d..1484868e83a0a9e0043215af89baf73d978bd191 100644 (file)
@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
 
 import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -33,7 +34,6 @@ import java.util.concurrent.TimeUnit;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.hamcrest.CoreMatchers;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.mockito.Mock;
@@ -56,12 +56,13 @@ import org.openhab.core.types.State;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings({ "ConstantConditions" })
+@NonNullByDefault
 public abstract class AbstractComponentTests extends AbstractHomeAssistantTests {
-    private final static int SUBSCRIBE_TIMEOUT = 10000;
-    private final static int ATTRIBUTE_RECEIVE_TIMEOUT = 2000;
+    private static final int SUBSCRIBE_TIMEOUT = 10000;
+    private static final int ATTRIBUTE_RECEIVE_TIMEOUT = 2000;
 
-    private @Mock ThingHandlerCallback callback;
-    private LatchThingHandler thingHandler;
+    private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
+    private @NonNullByDefault({}) LatchThingHandler thingHandler;
 
     @BeforeEach
     public void setupThingHandler() {
@@ -70,12 +71,12 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
         config.put(HandlerConfiguration.PROPERTY_BASETOPIC, HandlerConfiguration.DEFAULT_BASETOPIC);
         config.put(HandlerConfiguration.PROPERTY_TOPICS, getConfigTopics());
 
-        when(callback.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
+        when(callbackMock.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
 
         thingHandler = new LatchThingHandler(haThing, channelTypeProvider, transformationServiceProvider,
                 SUBSCRIBE_TIMEOUT, ATTRIBUTE_RECEIVE_TIMEOUT);
         thingHandler.setConnection(bridgeConnection);
-        thingHandler.setCallback(callback);
+        thingHandler.setCallback(callbackMock);
         thingHandler = spy(thingHandler);
 
         thingHandler.initialize();
@@ -124,9 +125,7 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
         } catch (InterruptedException e) {
             assertThat(e.getMessage(), false);
         }
-        var component = thingHandler.getDiscoveredComponent();
-        assertThat(component, CoreMatchers.notNullValue());
-        return component;
+        return Objects.requireNonNull(thingHandler.getDiscoveredComponent());
     }
 
     /**
@@ -141,7 +140,7 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
      */
     protected static void assertChannel(AbstractComponent<@NonNull ? extends AbstractChannelConfiguration> component,
             String channelId, String stateTopic, String commandTopic, String label, Class<? extends Value> valueClass) {
-        var stateChannel = component.getChannel(channelId);
+        var stateChannel = Objects.requireNonNull(component.getChannel(channelId));
         assertChannel(stateChannel, stateTopic, commandTopic, label, valueClass);
     }
 
@@ -236,7 +235,6 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
         return false;
     }
 
-    @NonNullByDefault
     protected static class LatchThingHandler extends HomeAssistantThingHandler {
         private @Nullable CountDownLatch latch;
         private @Nullable AbstractComponent<@NonNull ? extends AbstractChannelConfiguration> discoveredComponent;
@@ -247,6 +245,7 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests
             super(thing, channelTypeProvider, transformationServiceProvider, subscribeTimeout, attributeReceiveTimeout);
         }
 
+        @Override
         public void componentDiscovered(HaID homeAssistantTopicID, AbstractComponent<@NonNull ?> component) {
             accept(List.of(component));
             discoveredComponent = component;
index 6d6db678a09e58252f6b5b4da27542871be30f1b..9e1456267f496649dda53a670749d1ddec3fb763 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.TextValue;
 import org.openhab.core.library.types.StringType;
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.StringType;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ConstantConditions")
+@NonNullByDefault
 public class AlarmControlPanelTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "alarm_control_panel/0x0000000000000000_alarm_control_panel_zigbee2mqtt";
 
@@ -89,6 +91,7 @@ public class AlarmControlPanelTests extends AbstractComponentTests {
         assertPublished("zigbee2mqtt/alarm/set/state", "ARM_HOME_");
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index e8a0277a31c6050d63432dec4956be9bbf20df6f..fcb5444129e250949e62b99e1316da127b29347f 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.OnOffValue;
 import org.openhab.core.library.types.OnOffType;
@@ -27,6 +28,7 @@ import org.openhab.core.types.UnDefType;
  *
  * @author Anton Kharuzhy - Initial contribution
  */
+@NonNullByDefault
 public class BinarySensorTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "binary_sensor/0x0000000000000000_binary_sensor_zigbee2mqtt";
 
@@ -148,6 +150,7 @@ public class BinarySensorTests extends AbstractComponentTests {
         waitForAssert(() -> assertState(component, BinarySensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index 3cb77eac8a1b55d24470d74da38f3dea0fdcfce0..53e2956afbe0e470b7b0dc41ce3f0f105e61e25a 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.ImageValue;
 import org.openhab.core.library.types.RawType;
@@ -26,6 +27,7 @@ import org.openhab.core.library.types.RawType;
  *
  * @author Anton Kharuzhy - Initial contribution
  */
+@NonNullByDefault
 public class CameraTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "camera/0x0000000000000000_camera_zigbee2mqtt";
 
@@ -63,6 +65,7 @@ public class CameraTests extends AbstractComponentTests {
         assertState(component, Camera.CAMERA_CHANNEL_ID, new RawType(imageBytes, "image/png"));
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index 9234b0810c8c9fd3fef355500085f57a1a549404..1f39a197d8bbe611722fb61736d95de8897383f3 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.NumberValue;
 import org.openhab.binding.mqtt.generic.values.OnOffValue;
@@ -34,6 +35,7 @@ import org.openhab.core.library.unit.SIUnits;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ConstantConditions")
+@NonNullByDefault
 public class ClimateTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "climate/0x847127fffe11dd6a_climate_zigbee2mqtt";
 
index 910cb78130491f524e929ed9a274948d27822f63..0bfeee34a5405f2ba90b3c33d9439e65d89171f5 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.RollershutterValue;
 import org.openhab.core.library.types.PercentType;
@@ -28,6 +29,7 @@ import org.openhab.core.library.types.StopMoveType;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ConstantConditions")
+@NonNullByDefault
 public class CoverTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "cover/0x0000000000000000_cover_zigbee2mqtt";
 
@@ -82,6 +84,7 @@ public class CoverTests extends AbstractComponentTests {
         assertPublished("zigbee2mqtt/cover/set/state", "STOP_", 2);
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index 43052835879c161e79117444093df91602395725..a0390dad9ba28fa5ac23bac9794be540112cd11a 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.OnOffValue;
 import org.openhab.core.library.types.OnOffType;
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.OnOffType;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ALL")
+@NonNullByDefault
 public class FanTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "fan/0x0000000000000000_fan_zigbee2mqtt";
 
@@ -78,6 +80,7 @@ public class FanTests extends AbstractComponentTests {
         assertPublished("zigbee2mqtt/fan/set/state", "ON_");
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index 9d3a405d2001d0a4d16699e55bd5c443ee18d9ab..ef518cff1312c2dcedb9c61d09fb89cc57ade1c7 100644 (file)
@@ -19,11 +19,12 @@ import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.UncheckedIOException;
 import java.math.BigDecimal;
 import java.util.Arrays;
 import java.util.List;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.homeassistant.internal.config.ChannelConfigurationTypeAdapterFactory;
 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
@@ -36,6 +37,7 @@ import com.google.gson.GsonBuilder;
 /**
  * @author Jochen Klein - Initial contribution
  */
+@NonNullByDefault
 public class HAConfigurationTests {
 
     private Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ChannelConfigurationTypeAdapterFactory())
@@ -53,7 +55,7 @@ public class HAConfigurationTests {
             }
             return result.toString();
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new UncheckedIOException(e);
         }
     }
 
@@ -79,7 +81,7 @@ public class HAConfigurationTests {
         if (device != null) {
             assertThat(device.getIdentifiers(), contains("H"));
             assertThat(device.getConnections(), is(notNullValue()));
-            List<@NonNull Connection> connections = device.getConnections();
+            List<Connection> connections = device.getConnections();
             if (connections != null) {
                 assertThat(connections.get(0).getType(), is("I1"));
                 assertThat(connections.get(0).getIdentifier(), is("I2"));
index d21876aa1685cfaabc6bbcd5d1e1130c894b5149..219b6c34b991976bba75f33ae6dee14e713ca909 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.ColorValue;
 import org.openhab.core.library.types.HSBType;
@@ -28,6 +29,7 @@ import org.openhab.core.library.types.OnOffType;
  *
  * @author Anton Kharuzhy - Initial contribution
  */
+@NonNullByDefault
 public class LightTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "light/0x0000000000000000_light_zigbee2mqtt";
 
@@ -85,6 +87,7 @@ public class LightTests extends AbstractComponentTests {
         assertPublished("zigbee2mqtt/light/set/state", "0,0,0");
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index c0efaae9a4fe1735f96cba608f1d4c1087137bc2..0a8d4de2eff1e3fd0d985c1c6ecaab7b353d453d 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.OnOffValue;
 import org.openhab.core.library.types.OnOffType;
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.OnOffType;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ALL")
+@NonNullByDefault
 public class LockTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "lock/0x0000000000000000_lock_zigbee2mqtt";
 
index eba2a730c399812b264f0f36083bacddd7e34495..feffc40f1869781af5963b0bac348267590b6388 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.NumberValue;
 import org.openhab.core.library.types.QuantityType;
@@ -29,6 +30,7 @@ import org.openhab.core.types.UnDefType;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ConstantConditions")
+@NonNullByDefault
 public class SensorTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
 
index 40a5a6b672282bdc796fa812bb99fa0b06137008..28738436fdff8aed7bc7ee84fcea39e23bb8f9ce 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.OnOffValue;
 import org.openhab.core.library.types.OnOffType;
@@ -27,6 +28,7 @@ import org.openhab.core.library.types.OnOffType;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ConstantConditions")
+@NonNullByDefault
 public class SwitchTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt";
 
@@ -117,6 +119,7 @@ public class SwitchTests extends AbstractComponentTests {
         assertPublished("zigbee2mqtt/th1/set/auto_lock", "AUTO");
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index 9ff969e3e2b7d5b1febc1651ed35becaef9ce91d..2b0a0f5f2023185aea41162f9278c782d1bada6e 100644 (file)
@@ -17,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.binding.mqtt.generic.values.OnOffValue;
 import org.openhab.binding.mqtt.generic.values.PercentageValue;
@@ -32,6 +33,7 @@ import org.openhab.core.types.UnDefType;
  * @author Anton Kharuzhy - Initial contribution
  */
 @SuppressWarnings("ConstantConditions")
+@NonNullByDefault
 public class VacuumTests extends AbstractComponentTests {
     public static final String CONFIG_TOPIC = "vacuum/rockrobo_vacuum";
 
@@ -248,6 +250,7 @@ public class VacuumTests extends AbstractComponentTests {
         assertPublished("vacuum/send_command", "custom_command");
     }
 
+    @Override
     protected Set<String> getConfigTopics() {
         return Set.of(CONFIG_TOPIC);
     }
index cfb2a005041663f2dd6869ac212434590392a330..d8614eaa3520969b7e8a8e125c6e2c486c41742d 100644 (file)
@@ -12,8 +12,7 @@
  */
 package org.openhab.binding.mqtt.homeassistant.internal.discovery;
 
-import static org.hamcrest.CoreMatchers.hasItems;
-import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.*;
 import static org.hamcrest.MatcherAssert.assertThat;
 
 import java.util.Collection;
@@ -46,8 +45,9 @@ import org.openhab.core.thing.ThingUID;
  */
 @SuppressWarnings({ "ConstantConditions", "unchecked" })
 @ExtendWith(MockitoExtension.class)
+@NonNullByDefault
 public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
-    private HomeAssistantDiscovery discovery;
+    private @NonNullByDefault({}) HomeAssistantDiscovery discovery;
 
     @BeforeEach
     public void beforeEach() {
@@ -89,11 +89,11 @@ public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
         }
     }
 
-    @NonNullByDefault
     private static class LatchDiscoveryListener implements DiscoveryListener {
         private final CopyOnWriteArrayList<DiscoveryResult> discoveryResults = new CopyOnWriteArrayList<>();
         private @Nullable CountDownLatch latch;
 
+        @Override
         public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
             discoveryResults.add(result);
             if (latch != null) {
@@ -101,9 +101,11 @@ public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests {
             }
         }
 
+        @Override
         public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
         }
 
+        @Override
         public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
                 @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
             return Collections.emptyList();
index 29f4387d1d4120f3a7986b779fd70052c3ec22e2..4b851692607d869c2897a29ff68548f32599be5a 100644 (file)
 package org.openhab.binding.mqtt.homeassistant.internal.handler;
 
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.eq;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.timeout;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
 
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.hamcrest.CoreMatchers;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -47,6 +42,7 @@ import org.openhab.core.thing.binding.ThingHandlerCallback;
  */
 @SuppressWarnings({ "ConstantConditions" })
 @ExtendWith(MockitoExtension.class)
+@NonNullByDefault
 public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
     private static final int SUBSCRIBE_TIMEOUT = 10000;
     private static final int ATTRIBUTE_RECEIVE_TIMEOUT = 2000;
@@ -62,8 +58,8 @@ public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
     private static final List<String> MQTT_TOPICS = CONFIG_TOPICS.stream()
             .map(AbstractHomeAssistantTests::configTopicToMqtt).collect(Collectors.toList());
 
-    private @Mock ThingHandlerCallback callback;
-    private HomeAssistantThingHandler thingHandler;
+    private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
+    private @NonNullByDefault({}) HomeAssistantThingHandler thingHandler;
 
     @BeforeEach
     public void setup() {
@@ -72,12 +68,12 @@ public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
         config.put(HandlerConfiguration.PROPERTY_BASETOPIC, HandlerConfiguration.DEFAULT_BASETOPIC);
         config.put(HandlerConfiguration.PROPERTY_TOPICS, CONFIG_TOPICS);
 
-        when(callback.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
+        when(callbackMock.getBridge(eq(BRIDGE_UID))).thenReturn(bridgeThing);
 
         thingHandler = new HomeAssistantThingHandler(haThing, channelTypeProvider, transformationServiceProvider,
                 SUBSCRIBE_TIMEOUT, ATTRIBUTE_RECEIVE_TIMEOUT);
         thingHandler.setConnection(bridgeConnection);
-        thingHandler.setCallback(callback);
+        thingHandler.setCallback(callbackMock);
         thingHandler = spy(thingHandler);
     }
 
@@ -86,7 +82,7 @@ public class HomeAssistantThingHandlerTests extends AbstractHomeAssistantTests {
         // When initialize
         thingHandler.initialize();
 
-        verify(callback).statusUpdated(eq(haThing), any());
+        verify(callbackMock).statusUpdated(eq(haThing), any());
         // Expect a call to the bridge status changed, the start, the propertiesChanged method
         verify(thingHandler).bridgeStatusChanged(any());
         verify(thingHandler, timeout(SUBSCRIBE_TIMEOUT)).start(any());
index 824087f28e9833fe87e4908d547731f6c28596c1..0405bbb1e872bd55896d67cc55a1ec2624810f43 100644 (file)
 package org.openhab.binding.mqtt.homie.generic.internal;
 
 import java.util.Set;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.binding.mqtt.generic.MqttChannelStateDescriptionProvider;
 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
 import org.openhab.binding.mqtt.homie.internal.handler.HomieThingHandler;
@@ -45,9 +42,8 @@ import org.osgi.service.component.annotations.Reference;
 @NonNullByDefault
 public class MqttThingHandlerFactory extends BaseThingHandlerFactory implements TransformationServiceProvider {
     private @NonNullByDefault({}) MqttChannelTypeProvider typeProvider;
-    private @NonNullByDefault({}) MqttChannelStateDescriptionProvider stateDescriptionProvider;
-    private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
-            .of(MqttBindingConstants.HOMIE300_MQTT_THING).collect(Collectors.toSet());
+    private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
+            .of(MqttBindingConstants.HOMIE300_MQTT_THING);
 
     @Override
     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
@@ -66,15 +62,6 @@ public class MqttThingHandlerFactory extends BaseThingHandlerFactory implements
         super.deactivate(componentContext);
     }
 
-    @Reference
-    protected void setStateDescriptionProvider(MqttChannelStateDescriptionProvider stateDescription) {
-        this.stateDescriptionProvider = stateDescription;
-    }
-
-    protected void unsetStateDescriptionProvider(MqttChannelStateDescriptionProvider stateDescription) {
-        this.stateDescriptionProvider = null;
-    }
-
     @Reference
     protected void setChannelProvider(MqttChannelTypeProvider provider) {
         this.typeProvider = provider;
index a2e207907b83fc54f5861ebbec18d6dd081e88fa..be4a35cd89e2a005c1b46f0109e6472f6b4a33f7 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.mqtt.homie.internal.homie300;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.stream.Collectors;
@@ -275,8 +276,9 @@ public class Device implements AbstractMqttAttributeClass.AttributeChanged {
 
     CompletableFuture<@Nullable Void> applyNodes(MqttBrokerConnection connection, ScheduledExecutorService scheduler,
             int timeout) {
-        return nodes.apply(attributes.nodes, node -> node.subscribe(connection, scheduler, timeout), this::createNode,
-                this::notifyNodeRemoved).exceptionally(e -> {
+        return nodes.apply(Objects.requireNonNull(attributes.nodes),
+                node -> node.subscribe(connection, scheduler, timeout), this::createNode, this::notifyNodeRemoved)
+                .exceptionally(e -> {
                     logger.warn("Could not subscribe", e);
                     return null;
                 });
@@ -311,15 +313,11 @@ public class Device implements AbstractMqttAttributeClass.AttributeChanged {
      * @return Returns a list of relative topics
      */
     public List<String> getRetainedTopics() {
-        List<String> topics = new ArrayList<>();
+        List<String> topics = new ArrayList<>(Stream.of(this.attributes.getClass().getDeclaredFields())
+                .map(f -> String.format("%s/$%s", this.deviceID, f.getName())).collect(Collectors.toList()));
 
-        topics.addAll(Stream.of(this.attributes.getClass().getDeclaredFields()).map(f -> {
-            return String.format("%s/$%s", this.deviceID, f.getName());
-        }).collect(Collectors.toList()));
-
-        this.nodes.stream().map(n -> n.getRetainedTopics().stream().map(a -> {
-            return String.format("%s/%s", this.deviceID, a);
-        }).collect(Collectors.toList())).collect(Collectors.toList()).forEach(topics::addAll);
+        this.nodes.stream().map(n -> n.getRetainedTopics().stream().map(a -> String.format("%s/%s", this.deviceID, a))
+                .collect(Collectors.toList())).collect(Collectors.toList()).forEach(topics::addAll);
 
         return topics;
     }
index 437964e2a62a5923679b91e86b21538a24ae8e9f..2cc1144f642427bf2e201b8320dc2f77e6228113 100644 (file)
@@ -12,7 +12,8 @@
  */
 package org.openhab.binding.mqtt.homie.internal.homie300;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.mqtt.generic.mapping.AbstractMqttAttributeClass;
 import org.openhab.binding.mqtt.generic.mapping.MQTTvalueTransform;
 import org.openhab.binding.mqtt.generic.mapping.MandatoryField;
@@ -24,6 +25,7 @@ import org.openhab.binding.mqtt.generic.mapping.TopicPrefix;
  * @author David Graeff - Initial contribution
  */
 @TopicPrefix
+@NonNullByDefault
 public class DeviceAttributes extends AbstractMqttAttributeClass {
     // Lower-case enum value names required. Those are identifiers for the MQTT/homie protocol.
     public enum ReadyState {
@@ -36,13 +38,13 @@ public class DeviceAttributes extends AbstractMqttAttributeClass {
         alert
     }
 
-    public @MandatoryField String homie;
-    public @MandatoryField String name;
+    public @MandatoryField @Nullable String homie;
+    public @MandatoryField @Nullable String name;
     public @MandatoryField ReadyState state = ReadyState.unknown;
-    public @MandatoryField @MQTTvalueTransform(splitCharacter = ",") String[] nodes;
+    public @MandatoryField @MQTTvalueTransform(splitCharacter = ",") String @Nullable [] nodes;
 
     @Override
-    public @NonNull Object getFieldsOf() {
+    public Object getFieldsOf() {
         return this;
     }
 }
index 4d4405a927442b33c29afdc10bb0713f15300234..7a9e5b05578f90163fb3a74ec7598559da572dc6 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.mqtt.homie.internal.homie300;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.stream.Collectors;
@@ -173,8 +174,9 @@ public class Node implements AbstractMqttAttributeClass.AttributeChanged {
 
     protected CompletableFuture<@Nullable Void> applyProperties(MqttBrokerConnection connection,
             ScheduledExecutorService scheduler, int timeout) {
-        return properties.apply(attributes.properties, prop -> prop.subscribe(connection, scheduler, timeout),
-                this::createProperty, this::notifyPropertyRemoved).exceptionally(e -> {
+        return properties.apply(Objects.requireNonNull(attributes.properties),
+                prop -> prop.subscribe(connection, scheduler, timeout), this::createProperty,
+                this::notifyPropertyRemoved).exceptionally(e -> {
                     logger.warn("Could not subscribe", e);
                     return null;
                 });
@@ -189,10 +191,8 @@ public class Node implements AbstractMqttAttributeClass.AttributeChanged {
         // Special case: Not all fields were known before
         if (!attributes.isComplete()) {
             attributesReceived(connection, scheduler, 500);
-        } else {
-            if ("properties".equals(name)) {
-                applyProperties(connection, scheduler, 500);
-            }
+        } else if ("properties".equals(name)) {
+            applyProperties(connection, scheduler, 500);
         }
         callback.nodeAddedOrChanged(this);
     }
@@ -208,15 +208,12 @@ public class Node implements AbstractMqttAttributeClass.AttributeChanged {
      * @return Returns a list of relative topics
      */
     public List<String> getRetainedTopics() {
-        List<String> topics = new ArrayList<>();
+        List<String> topics = new ArrayList<>(Stream.of(this.attributes.getClass().getDeclaredFields())
+                .map(f -> String.format("%s/$%s", this.nodeID, f.getName())).collect(Collectors.toList()));
 
-        topics.addAll(Stream.of(this.attributes.getClass().getDeclaredFields()).map(f -> {
-            return String.format("%s/$%s", this.nodeID, f.getName());
-        }).collect(Collectors.toList()));
-
-        this.properties.stream().map(p -> p.getRetainedTopics().stream().map(a -> {
-            return String.format("%s/%s", this.nodeID, a);
-        }).collect(Collectors.toList())).collect(Collectors.toList()).forEach(topics::addAll);
+        this.properties.stream().map(p -> p.getRetainedTopics().stream()
+                .map(a -> String.format("%s/%s", this.nodeID, a)).collect(Collectors.toList()))
+                .collect(Collectors.toList()).forEach(topics::addAll);
 
         return topics;
     }
index aa22f0dc9f7143fb286ffef8c61d9aca1907fa1a..3d3937d9a99289a35a844186b85ed0c01ba91ce9 100644 (file)
@@ -12,7 +12,8 @@
  */
 package org.openhab.binding.mqtt.homie.internal.homie300;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.mqtt.generic.mapping.AbstractMqttAttributeClass;
 import org.openhab.binding.mqtt.generic.mapping.MQTTvalueTransform;
 import org.openhab.binding.mqtt.generic.mapping.MandatoryField;
@@ -24,14 +25,15 @@ import org.openhab.binding.mqtt.generic.mapping.TopicPrefix;
  * @author David Graeff - Initial contribution
  */
 @TopicPrefix
+@NonNullByDefault
 public class NodeAttributes extends AbstractMqttAttributeClass {
-    public @MandatoryField String name;
-    public @MandatoryField @MQTTvalueTransform(splitCharacter = ",") String[] properties;
+    public @MandatoryField String name = "";
+    public @MandatoryField @MQTTvalueTransform(splitCharacter = ",") String @Nullable [] properties;
     // Type has no meaning yet and is currently purely of textual, descriptive nature
-    public String type;
+    public @Nullable String type;
 
     @Override
-    public @NonNull Object getFieldsOf() {
+    public Object getFieldsOf() {
         return this;
     }
 }
index ac092abe65ff3ab3b80cdb9ed8ef0d3d29a297f4..38b14dc3b9e61b15c25c7ca11ffda6e8646b06cd 100644 (file)
@@ -20,6 +20,7 @@ import static org.mockito.Mockito.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.function.Function;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -40,8 +41,9 @@ import org.openhab.binding.mqtt.homie.internal.homie300.NodeAttributes;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class HomieChildMapTests {
-    private @Mock DeviceCallback callback;
+    private @Mock @NonNullByDefault({}) DeviceCallback callbackMock;
 
     private final String deviceID = ThingChannelConstants.TEST_HOMIE_THING.getId();
     private final String deviceTopic = "homie/" + deviceID;
@@ -52,7 +54,7 @@ public class HomieChildMapTests {
     ChildMap<Node> subject = new ChildMap<>();
 
     private Node createNode(String id) {
-        Node node = new Node(deviceTopic, id, ThingChannelConstants.TEST_HOMIE_THING, callback,
+        Node node = new Node(deviceTopic, id, ThingChannelConstants.TEST_HOMIE_THING, callbackMock,
                 spy(new NodeAttributes()));
         doReturn(future).when(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
         doReturn(future).when(node.attributes).unsubscribe();
@@ -60,7 +62,7 @@ public class HomieChildMapTests {
     }
 
     private void removedNode(Node node) {
-        callback.nodeRemoved(node);
+        callbackMock.nodeRemoved(node);
     }
 
     public static class AddedAction implements Function<Node, CompletableFuture<Void>> {
@@ -85,6 +87,6 @@ public class HomieChildMapTests {
 
         Node soonToBeRemoved = subject.get("def");
         subject.apply(new String[] { "abc" }, addedAction, this::createNode, this::removedNode);
-        verify(callback).nodeRemoved(eq(soonToBeRemoved));
+        verify(callbackMock).nodeRemoved(eq(soonToBeRemoved));
     }
 }
index 82a16140bd8235161378bae5a1c7b86cb6f178d9..9dd7e60dc86bf87a7c4ed36bc46759db7ccced09 100644 (file)
@@ -30,7 +30,7 @@ import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -82,20 +82,20 @@ import org.openhab.core.types.TypeParser;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class HomieThingHandlerTests {
 
-    private Thing thing;
+    private @Mock @NonNullByDefault({}) AbstractBrokerHandler bridgeHandlerMock;
+    private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
+    private @Mock @NonNullByDefault({}) MqttBrokerConnection connectionMock;
+    private @Mock @NonNullByDefault({}) ScheduledExecutorService schedulerMock;
+    private @Mock @NonNullByDefault({}) ScheduledFuture<?> scheduledFutureMock;
+    private @Mock @NonNullByDefault({}) ThingTypeRegistry thingTypeRegistryMock;
 
-    private @Mock AbstractBrokerHandler bridgeHandler;
-    private @Mock ThingHandlerCallback callback;
-    private @Mock MqttBrokerConnection connection;
-    private @Mock ScheduledExecutorService scheduler;
-    private @Mock ScheduledFuture<?> scheduledFuture;
-    private @Mock ThingTypeRegistry thingTypeRegistry;
+    private @NonNullByDefault({}) Thing thing;
+    private @NonNullByDefault({}) HomieThingHandler thingHandler;
 
-    private HomieThingHandler thingHandler;
-
-    private final MqttChannelTypeProvider channelTypeProvider = new MqttChannelTypeProvider(thingTypeRegistry);
+    private final MqttChannelTypeProvider channelTypeProvider = new MqttChannelTypeProvider(thingTypeRegistryMock);
 
     private final String deviceID = ThingChannelConstants.TEST_HOMIE_THING.getId();
     private final String deviceTopic = "homie/" + deviceID;
@@ -116,26 +116,27 @@ public class HomieThingHandlerTests {
         thing.setStatusInfo(thingStatus);
 
         // Return the mocked connection object if the bridge handler is asked for it
-        when(bridgeHandler.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connection));
+        when(bridgeHandlerMock.getConnectionAsync()).thenReturn(CompletableFuture.completedFuture(connectionMock));
 
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribeAll();
-        doReturn(CompletableFuture.completedFuture(true)).when(connection).publish(any(), any(), anyInt(),
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).subscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).unsubscribe(any(), any());
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).unsubscribeAll();
+        doReturn(CompletableFuture.completedFuture(true)).when(connectionMock).publish(any(), any(), anyInt(),
                 anyBoolean());
 
-        doReturn(false).when(scheduledFuture).isDone();
-        doReturn(scheduledFuture).when(scheduler).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
+        doReturn(false).when(scheduledFutureMock).isDone();
+        doReturn(scheduledFutureMock).when(schedulerMock).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
 
         final HomieThingHandler handler = new HomieThingHandler(thing, channelTypeProvider, 1000, 30, 5);
         thingHandler = spy(handler);
-        thingHandler.setCallback(callback);
+        thingHandler.setCallback(callbackMock);
         final Device device = new Device(thing.getUID(), thingHandler, spy(new DeviceAttributes()),
                 spy(new ChildMap<>()));
-        thingHandler.setInternalObjects(spy(device), spy(new DelayedBatchProcessing<>(500, thingHandler, scheduler)));
+        thingHandler.setInternalObjects(spy(device),
+                spy(new DelayedBatchProcessing<>(500, thingHandler, schedulerMock)));
 
         // Return the bridge handler if the thing handler asks for it
-        doReturn(bridgeHandler).when(thingHandler).getBridgeHandler();
+        doReturn(bridgeHandlerMock).when(thingHandler).getBridgeHandler();
 
         // We are by default online
         doReturn(thingStatus).when(thingHandler).getBridgeStatus();
@@ -153,7 +154,7 @@ public class HomieThingHandlerTests {
         // Pretend that a device state change arrived.
         thingHandler.device.attributes.state = ReadyState.ready;
 
-        verify(callback, times(0)).statusUpdated(eq(thing), any());
+        verify(callbackMock, times(0)).statusUpdated(eq(thing), any());
 
         thingHandler.initialize();
 
@@ -166,8 +167,8 @@ public class HomieThingHandlerTests {
 
         assertThat(thingHandler.device.isInitialized(), is(true));
 
-        verify(callback).statusUpdated(eq(thing), argThat((arg) -> arg.getStatus().equals(ThingStatus.ONLINE)
-                && arg.getStatusDetail().equals(ThingStatusDetail.NONE)));
+        verify(callbackMock).statusUpdated(eq(thing), argThat(arg -> ThingStatus.ONLINE.equals(arg.getStatus())
+                && ThingStatusDetail.NONE.equals(arg.getStatusDetail())));
     }
 
     @Test
@@ -182,8 +183,8 @@ public class HomieThingHandlerTests {
 
         thingHandler.initialize();
 
-        verify(callback).statusUpdated(eq(thing), argThat((arg) -> arg.getStatus().equals(ThingStatus.OFFLINE)
-                && arg.getStatusDetail().equals(ThingStatusDetail.COMMUNICATION_ERROR)));
+        verify(callbackMock).statusUpdated(eq(thing), argThat(arg -> ThingStatus.OFFLINE.equals(arg.getStatus())
+                && ThingStatusDetail.COMMUNICATION_ERROR.equals(arg.getStatusDetail())));
     }
 
     @Test
@@ -199,8 +200,8 @@ public class HomieThingHandlerTests {
         thingHandler.initialize();
         assertThat(thingHandler.device.isInitialized(), is(true));
 
-        verify(callback).statusUpdated(eq(thing), argThat((arg) -> arg.getStatus().equals(ThingStatus.OFFLINE)
-                && arg.getStatusDetail().equals(ThingStatusDetail.GONE)));
+        verify(callbackMock).statusUpdated(eq(thing), argThat(arg -> ThingStatus.OFFLINE.equals(arg.getStatus())
+                && ThingStatusDetail.GONE.equals(arg.getStatusDetail())));
     }
 
     @SuppressWarnings("null")
@@ -222,12 +223,12 @@ public class HomieThingHandlerTests {
         node.properties.put(property.propertyID, property);
         thingHandler.device.nodes.put(node.nodeID, node);
 
-        ThingHandlerHelper.setConnection(thingHandler, connection);
+        ThingHandlerHelper.setConnection(thingHandler, connectionMock);
         // we need to set a channel value first, undefined values ignored on REFRESH
         property.getChannelState().getCache().update(new StringType("testString"));
 
         thingHandler.handleCommand(property.channelUID, RefreshType.REFRESH);
-        verify(callback).stateUpdated(argThat(arg -> property.channelUID.equals(arg)),
+        verify(callbackMock).stateUpdated(argThat(arg -> property.channelUID.equals(arg)),
                 argThat(arg -> property.getChannelState().getCache().getChannelState().equals(arg)));
     }
 
@@ -252,14 +253,14 @@ public class HomieThingHandlerTests {
 
         ChannelState channelState = requireNonNull(property.getChannelState());
         assertNotNull(channelState);
-        ChannelStateHelper.setConnection(channelState, connection);// Pretend we called start()
-        ThingHandlerHelper.setConnection(thingHandler, connection);
+        ChannelStateHelper.setConnection(channelState, connectionMock);// Pretend we called start()
+        ThingHandlerHelper.setConnection(thingHandler, connectionMock);
 
         StringType updateValue = new StringType("UPDATE");
         thingHandler.handleCommand(property.channelUID, updateValue);
 
         assertThat(property.getChannelState().getCache().getChannelState().toString(), is("UPDATE"));
-        verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean());
+        verify(connectionMock, times(1)).publish(any(), any(), anyInt(), anyBoolean());
 
         // Check non writable property
         property.attributes.settable = false;
@@ -274,7 +275,7 @@ public class HomieThingHandlerTests {
             thingHandler.handleCommand(property.channelUID, updateValue);
             // Expect old value and no MQTT publish
             assertThat(property.getChannelState().getCache().getChannelState().toString(), is("OLDVALUE"));
-            verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean());
+            verify(connectionMock, times(1)).publish(any(), any(), anyInt(), anyBoolean());
         }
     }
 
@@ -320,7 +321,7 @@ public class HomieThingHandlerTests {
     @Test
     public void propertiesChanged() throws InterruptedException, ExecutionException {
         thingHandler.device.initialize("homie", "device", new ArrayList<>());
-        ThingHandlerHelper.setConnection(thingHandler, connection);
+        ThingHandlerHelper.setConnection(thingHandler, connectionMock);
 
         // Create mocked homie device tree with one node and one property
         doAnswer(this::createSubscriberAnswer).when(thingHandler.device.attributes).createSubscriber(any(), any(),
@@ -355,14 +356,14 @@ public class HomieThingHandlerTests {
         thingHandler.delayedProcessing.forceProcessNow();
 
         // Called for the updated property + for the new channels
-        verify(callback, atLeast(2)).thingUpdated(any());
+        verify(callbackMock, atLeast(2)).thingUpdated(any());
 
-        final List<@NonNull Channel> channels = thingHandler.getThing().getChannels();
+        final List<Channel> channels = thingHandler.getThing().getChannels();
         assertThat(channels.size(), is(1));
         assertThat(channels.get(0).getLabel(), is("testprop"));
         assertThat(channels.get(0).getKind(), is(ChannelKind.STATE));
 
-        final Map<@NonNull String, @NonNull String> properties = thingHandler.getThing().getProperties();
+        final Map<String, String> properties = thingHandler.getThing().getProperties();
         assertThat(properties.get(MqttBindingConstants.HOMIE_PROPERTY_VERSION), is("3.0"));
         assertThat(properties.size(), is(1));
     }
index 9dd6750230d91167787fe9fa054f5c3fd84cf0d5..13132a5215c617361ccdbb5d4821114a86556d1f 100644 (file)
@@ -12,8 +12,9 @@
  */
 package org.openhab.binding.mqtt.homie.internal.handler;
 
-import static org.openhab.binding.mqtt.homie.generic.internal.MqttBindingConstants.*;
+import static org.openhab.binding.mqtt.homie.generic.internal.MqttBindingConstants.HOMIE300_MQTT_THING;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.thing.ThingUID;
 
 /**
@@ -21,6 +22,7 @@ import org.openhab.core.thing.ThingUID;
  *
  * @author David Graeff - Initial contribution
  */
+@NonNullByDefault
 public class ThingChannelConstants {
     public static final ThingUID TEST_HOMIE_THING = new ThingUID(HOMIE300_MQTT_THING, "device123");
 }
index 129f6cc78fe24d604f78e68f68300bcab02bf6b0..617050e6f8e21ba9616d4d3da1fbd749bb4d7ecd 100644 (file)
@@ -154,13 +154,13 @@ public class BrokerHandler extends AbstractBrokerHandler implements PinnedCallba
             try {
                 Pin pin;
                 if (config.certificate.isBlank()) {
-                    pin = Pin.LearningPin(PinType.CERTIFICATE_TYPE);
+                    pin = Pin.learningPin(PinType.CERTIFICATE_TYPE);
                 } else {
                     String[] split = config.certificate.split(":");
                     if (split.length != 2) {
                         throw new NoSuchAlgorithmException("Algorithm is missing");
                     }
-                    pin = Pin.CheckingPin(PinType.CERTIFICATE_TYPE, new PinMessageDigest(split[0]),
+                    pin = Pin.checkingPin(PinType.CERTIFICATE_TYPE, new PinMessageDigest(split[0]),
                             HexUtils.hexToBytes(split[1]));
                 }
                 trustManager.addPinning(pin);
@@ -172,13 +172,13 @@ public class BrokerHandler extends AbstractBrokerHandler implements PinnedCallba
             try {
                 Pin pin;
                 if (config.publickey.isBlank()) {
-                    pin = Pin.LearningPin(PinType.PUBLIC_KEY_TYPE);
+                    pin = Pin.learningPin(PinType.PUBLIC_KEY_TYPE);
                 } else {
                     String[] split = config.publickey.split(":");
                     if (split.length != 2) {
                         throw new NoSuchAlgorithmException("Algorithm is missing");
                     }
-                    pin = Pin.CheckingPin(PinType.PUBLIC_KEY_TYPE, new PinMessageDigest(split[0]),
+                    pin = Pin.checkingPin(PinType.PUBLIC_KEY_TYPE, new PinMessageDigest(split[0]),
                             HexUtils.hexToBytes(split[1]));
                 }
                 trustManager.addPinning(pin);
index 8c1f1adff16ce5aa68bdfbb64354903e9ee06340..a3fd005c9d76f6af3378098da9aa67afed6527ba 100644 (file)
@@ -34,8 +34,6 @@ import org.openhab.core.thing.binding.BaseThingHandlerFactory;
 import org.openhab.core.thing.binding.ThingHandler;
 import org.openhab.core.thing.binding.ThingHandlerFactory;
 import org.osgi.service.component.annotations.Component;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * The {@link MqttBrokerHandlerFactory} is responsible for creating things and thing
@@ -52,8 +50,6 @@ public class MqttBrokerHandlerFactory extends BaseThingHandlerFactory implements
     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
             .of(MqttBindingConstants.BRIDGE_TYPE_BROKER).collect(Collectors.toSet());
 
-    private final Logger logger = LoggerFactory.getLogger(MqttBrokerHandlerFactory.class);
-
     /**
      * This Map provides a lookup between a Topic string (key) and a Set of MQTTTopicDiscoveryParticipants (value),
      * where the Set itself is a list of participants which are subscribed to the respective Topic.
index 4069e4c403fa136483939ff6b4db38e2b3ead0ea..c67fa651356a9f7717f09b6f01d14ca35bb00f87 100644 (file)
@@ -12,6 +12,7 @@
  */
 package org.openhab.binding.mqtt.internal;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.mqtt.MqttBindingConstants;
 import org.openhab.core.thing.ThingUID;
 
@@ -20,6 +21,7 @@ import org.openhab.core.thing.ThingUID;
  *
  * @author David Graeff - Initial contribution
  */
+@NonNullByDefault
 public class MqttThingID {
     /**
      * Convert the url (tcp://122.123.111.123:1883) to a version without colons, dots or slashes
index 54201776a19ce58e1832e7893eeab66a11938311..6e1c470917aef135878ebd08226af9937a504a07 100644 (file)
@@ -72,11 +72,11 @@ public class Pin {
         this.pinData = data;
     }
 
-    public static Pin LearningPin(PinType pinType) {
+    public static Pin learningPin(PinType pinType) {
         return new Pin(pinType, null, true, null);
     }
 
-    public static Pin CheckingPin(PinType pinType, PinMessageDigest method, byte[] pinData) {
+    public static Pin checkingPin(PinType pinType, PinMessageDigest method, byte[] pinData) {
         return new Pin(pinType, method, false, pinData);
     }
 
index a14e67067da9cbfbddd0adf4e7e8b63d0dff9d8a..20801767f4818daab4b83070355a1397eb670c25 100644 (file)
  */
 package org.openhab.binding.mqtt.internal.ssl;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * A {@link Pin} is either a Public Key or Certificate Pin.
  *
  * @author David Graeff - Initial contribution
  */
+@NonNullByDefault
 public enum PinType {
     PUBLIC_KEY_TYPE,
     CERTIFICATE_TYPE
index 13d1a3ff1d32d281c99d51a6af7e654375eedcef..2e0afc62a2cd992622811938ada7fbf94491ea96 100644 (file)
@@ -14,7 +14,7 @@ package org.openhab.binding.mqtt.handler;
 
 import static org.mockito.Mockito.verify;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.mockito.Mockito;
 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
 import org.openhab.core.thing.Bridge;
@@ -25,6 +25,7 @@ import org.openhab.core.thing.Bridge;
  *
  * @author David Graeff - Initial contribution
  */
+@NonNullByDefault
 public class BrokerHandlerEx extends BrokerHandler {
     final MqttBrokerConnectionEx e;
 
@@ -34,7 +35,7 @@ public class BrokerHandlerEx extends BrokerHandler {
     }
 
     @Override
-    protected @NonNull MqttBrokerConnection createBrokerConnection() throws IllegalArgumentException {
+    protected MqttBrokerConnection createBrokerConnection() throws IllegalArgumentException {
         return e;
     }
 
index 7414b3a076a26d55029c35484f9774f7629b6c09..16dac9abf92be0de1d9c35be058d242644d86765 100644 (file)
@@ -21,6 +21,7 @@ import static org.mockito.Mockito.*;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -48,15 +49,15 @@ import org.osgi.service.cm.ConfigurationException;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class BrokerHandlerTest extends JavaTest {
-    private ScheduledExecutorService scheduler;
 
-    private @Mock ThingHandlerCallback callback;
-    private @Mock Bridge thing;
+    private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
+    private @Mock @NonNullByDefault({}) Bridge thingMock;
 
-    private MqttBrokerConnectionEx connection;
-
-    private BrokerHandler handler;
+    private @NonNullByDefault({}) MqttBrokerConnectionEx connection;
+    private @NonNullByDefault({}) BrokerHandler handler;
+    private @NonNullByDefault({}) ScheduledExecutorService scheduler;
 
     @BeforeEach
     public void setUp() {
@@ -66,10 +67,10 @@ public class BrokerHandlerTest extends JavaTest {
         connection.setConnectionCallback(connection);
 
         Configuration config = new Configuration();
-        when(thing.getConfiguration()).thenReturn(config);
+        when(thingMock.getConfiguration()).thenReturn(config);
 
-        handler = spy(new BrokerHandlerEx(thing, connection));
-        handler.setCallback(callback);
+        handler = spy(new BrokerHandlerEx(thingMock, connection));
+        handler.setCallback(callbackMock);
     }
 
     @AfterEach
@@ -80,7 +81,7 @@ public class BrokerHandlerTest extends JavaTest {
     @Test
     public void handlerInitWithoutUrl() throws IllegalArgumentException {
         // Assume it is a real handler and not a mock as defined above
-        handler = new BrokerHandler(thing);
+        handler = new BrokerHandler(thingMock);
         assertThrows(IllegalArgumentException.class, this::initializeHandlerWaitForTimeout);
     }
 
@@ -89,7 +90,7 @@ public class BrokerHandlerTest extends JavaTest {
         Configuration config = new Configuration();
         config.put("host", "10.10.0.10");
         config.put("port", 80);
-        when(thing.getConfiguration()).thenReturn(config);
+        when(thingMock.getConfiguration()).thenReturn(config);
         handler.initialize();
         verify(handler).createBrokerConnection();
     }
@@ -99,7 +100,7 @@ public class BrokerHandlerTest extends JavaTest {
         assertThat(initializeHandlerWaitForTimeout(), is(true));
 
         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
-        verify(callback, atLeast(3)).statusUpdated(eq(thing), statusInfoCaptor.capture());
+        verify(callbackMock, atLeast(3)).statusUpdated(eq(thingMock), statusInfoCaptor.capture());
         assertThat(statusInfoCaptor.getValue().getStatus(), is(ThingStatus.ONLINE));
     }
 
index fe55d602c9f655b62694f5424b465d7eb7ce871e..28189ff0a11967f11c00ec37a0c17f8c41bd1a4b 100644 (file)
@@ -16,7 +16,6 @@ import static org.mockito.Mockito.spy;
 
 import java.util.Map;
 
-import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
@@ -65,7 +64,7 @@ public class MqttBrokerConnectionEx extends MqttBrokerConnection {
     }
 
     @Override
-    public @NonNull MqttConnectionState connectionState() {
+    public MqttConnectionState connectionState() {
         return connectionStateOverwrite;
     }
 }
index 53838b5d94b86e64b0f5711ddbcf5c4a53d3efb5..788e6cb5c8818669c63e98beaabd3d5b2baa43ec 100644 (file)
@@ -14,7 +14,7 @@ package org.openhab.binding.mqtt.handler;
 
 import java.util.concurrent.Semaphore;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
 import org.openhab.core.io.transport.mqtt.MqttConnectionState;
@@ -24,6 +24,7 @@ import org.openhab.core.io.transport.mqtt.MqttConnectionState;
  *
  * @author David Graeff - Initial contribution
  */
+@NonNullByDefault
 public class MqttConnectionObserverEx implements MqttConnectionObserver {
     public int counter = 0;
     public Semaphore semaphore = new Semaphore(1);
@@ -33,7 +34,7 @@ public class MqttConnectionObserverEx implements MqttConnectionObserver {
     }
 
     @Override
-    public void connectionStateChanged(@NonNull MqttConnectionState state, @Nullable Throwable error) {
+    public void connectionStateChanged(MqttConnectionState state, @Nullable Throwable error) {
         // First we expect a CONNECTING state and then a DISCONNECTED state change
         if (counter == 0 && state == MqttConnectionState.CONNECTING) {
             counter = 1;
index e41f4dffb06a909b33d50a350a872c90619ada80..60ec704ec881b49d7c1a408bcf48ea4b892d9094 100644 (file)
@@ -20,6 +20,7 @@ import static org.mockito.Mockito.*;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -44,29 +45,23 @@ import org.openhab.core.thing.binding.ThingHandlerCallback;
  */
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.WARN)
+@NonNullByDefault
 public class MQTTTopicDiscoveryServiceTest {
-    private ScheduledExecutorService scheduler;
 
-    private MqttBrokerHandlerFactory subject;
+    private @Mock @NonNullByDefault({}) Bridge thingMock;
+    private @Mock @NonNullByDefault({}) ThingHandlerCallback callbackMock;
+    private @Mock @NonNullByDefault({}) MQTTTopicDiscoveryParticipant listenerMock;
 
-    @Mock
-    private Bridge thing;
-
-    @Mock
-    private ThingHandlerCallback callback;
-
-    @Mock
-    MQTTTopicDiscoveryParticipant listener;
-
-    private MqttBrokerConnectionEx connection;
-
-    private BrokerHandler handler;
+    private @NonNullByDefault({}) MqttBrokerConnectionEx connection;
+    private @NonNullByDefault({}) BrokerHandler handler;
+    private @NonNullByDefault({}) ScheduledExecutorService scheduler;
+    private @NonNullByDefault({}) MqttBrokerHandlerFactory subject;
 
     @BeforeEach
     public void setUp() {
         scheduler = new ScheduledThreadPoolExecutor(1);
 
-        when(thing.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
+        when(thingMock.getUID()).thenReturn(MqttThingID.getThingUID("10.10.0.10", 80));
         connection = spy(new MqttBrokerConnectionEx("10.10.0.10", 80, false, "BrokerHandlerTest"));
         connection.setTimeoutExecutor(scheduler, 10);
         connection.setConnectionCallback(connection);
@@ -74,10 +69,10 @@ public class MQTTTopicDiscoveryServiceTest {
         Configuration config = new Configuration();
         config.put("host", "10.10.0.10");
         config.put("port", 80);
-        when(thing.getConfiguration()).thenReturn(config);
+        when(thingMock.getConfiguration()).thenReturn(config);
 
-        handler = spy(new BrokerHandlerEx(thing, connection));
-        handler.setCallback(callback);
+        handler = spy(new BrokerHandlerEx(thingMock, connection));
+        handler.setCallback(callbackMock);
 
         subject = new MqttBrokerHandlerFactory();
     }
@@ -92,13 +87,13 @@ public class MQTTTopicDiscoveryServiceTest {
         handler.initialize();
         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
 
-        subject.subscribe(listener, "topic");
+        subject.subscribe(listenerMock, "topic");
         subject.createdHandler(handler);
-        assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
+        assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
         // Simulate receiving
         final byte[] bytes = "TEST".getBytes();
         connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
-        verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
+        verify(listenerMock).receivedMessage(eq(thingMock.getUID()), eq(connection), eq("topic"), eq(bytes));
     }
 
     @Test
@@ -107,20 +102,20 @@ public class MQTTTopicDiscoveryServiceTest {
         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
 
         subject.createdHandler(handler);
-        subject.subscribe(listener, "topic");
-        assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
+        subject.subscribe(listenerMock, "topic");
+        assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
 
         // Simulate receiving
         final byte[] bytes = "TEST".getBytes();
         connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
-        verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
+        verify(listenerMock).receivedMessage(eq(thingMock.getUID()), eq(connection), eq("topic"), eq(bytes));
     }
 
     @Test
     public void handlerInitializeAfterSubscribe() {
         subject.createdHandler(handler);
-        subject.subscribe(listener, "topic");
-        assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
+        subject.subscribe(listenerMock, "topic");
+        assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
 
         // Init handler -> create connection
         handler.initialize();
@@ -129,7 +124,7 @@ public class MQTTTopicDiscoveryServiceTest {
         // Simulate receiving
         final byte[] bytes = "TEST".getBytes();
         connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
-        verify(listener).receivedMessage(eq(thing.getUID()), eq(connection), eq("topic"), eq(bytes));
+        verify(listenerMock).receivedMessage(eq(thingMock.getUID()), eq(connection), eq("topic"), eq(bytes));
     }
 
     @Test
@@ -138,12 +133,12 @@ public class MQTTTopicDiscoveryServiceTest {
         BrokerHandlerEx.verifyCreateBrokerConnection(handler, 1);
 
         subject.createdHandler(handler);
-        subject.subscribe(listener, "topic");
-        assertThat(subject.discoveryTopics.get("topic"), hasItem(listener));
+        subject.subscribe(listenerMock, "topic");
+        assertThat(subject.discoveryTopics.get("topic"), hasItem(listenerMock));
 
         // Simulate receiving
         final byte[] bytes = "".getBytes();
         connection.getSubscribers().get("topic").messageArrived("topic", bytes, false);
-        verify(listener).topicVanished(eq(thing.getUID()), eq(connection), eq("topic"));
+        verify(listenerMock).topicVanished(eq(thingMock.getUID()), eq(connection), eq("topic"));
     }
 }
index 31f6971196865060e7d773e823f4c3aa277ac304..789417be3618868623d61908a9499bae1a85a8d0 100644 (file)
@@ -25,7 +25,7 @@ import java.security.cert.CertificateException;
 import java.security.cert.CertificateFactory;
 import java.security.cert.X509Certificate;
 
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 import org.openhab.core.util.HexUtils;
 
@@ -34,6 +34,7 @@ import org.openhab.core.util.HexUtils;
  *
  * @author David Graeff - Initial contribution
  */
+@NonNullByDefault
 public class PinningSSLContextProviderTest {
 
     @Test
@@ -61,7 +62,7 @@ public class PinningSSLContextProviderTest {
     @Test
     public void certPinCallsX509CertificateGetEncoded() throws NoSuchAlgorithmException, CertificateException {
         PinTrustManager pinTrustManager = new PinTrustManager();
-        pinTrustManager.addPinning(Pin.LearningPin(PinType.CERTIFICATE_TYPE));
+        pinTrustManager.addPinning(Pin.learningPin(PinType.CERTIFICATE_TYPE));
 
         // Mock a certificate
         X509Certificate certificate = mock(X509Certificate.class);
@@ -77,7 +78,7 @@ public class PinningSSLContextProviderTest {
     @Test
     public void pubKeyPinCallsX509CertificateGetPublicKey() throws NoSuchAlgorithmException, CertificateException {
         PinTrustManager pinTrustManager = new PinTrustManager();
-        pinTrustManager.addPinning(Pin.LearningPin(PinType.PUBLIC_KEY_TYPE));
+        pinTrustManager.addPinning(Pin.learningPin(PinType.PUBLIC_KEY_TYPE));
 
         // Mock a certificate
         PublicKey publicKey = mock(PublicKey.class);
@@ -102,8 +103,7 @@ public class PinningSSLContextProviderTest {
         }
 
         @Override
-        @NonNull
-        PinMessageDigest getMessageDigestForSigAlg(@NonNull String sigAlg) throws CertificateException {
+        PinMessageDigest getMessageDigestForSigAlg(String sigAlg) throws CertificateException {
             return pinMessageDigest;
         }
     }
@@ -116,7 +116,7 @@ public class PinningSSLContextProviderTest {
         byte[] digestOfTestCert = pinMessageDigest.digest(testCert);
 
         // Add a certificate pin in learning mode to a trust manager
-        Pin pin = Pin.LearningPin(PinType.CERTIFICATE_TYPE);
+        Pin pin = Pin.learningPin(PinType.CERTIFICATE_TYPE);
         pinTrustManager.addPinning(pin);
         assertThat(pinTrustManager.pins.size(), is(1));
 
@@ -150,7 +150,7 @@ public class PinningSSLContextProviderTest {
         byte[] digestOfTestCert = pinMessageDigest.digest(testCert);
 
         // Add a certificate pin in checking mode to a trust manager
-        Pin pin = Pin.CheckingPin(PinType.CERTIFICATE_TYPE, pinMessageDigest, digestOfTestCert);
+        Pin pin = Pin.checkingPin(PinType.CERTIFICATE_TYPE, pinMessageDigest, digestOfTestCert);
         pinTrustManager.addPinning(pin);
         assertThat(pinTrustManager.pins.size(), is(1));