From 5daf4ff076809c32b3e84c4ba328f0bc66627b7b Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sun, 27 Aug 2023 11:15:15 +0200 Subject: [PATCH] Fix SAT and null analysis issues (#3781) Signed-off-by: Wouter Born --- .../script/profile/ScriptProfileFactory.java | 4 +++- .../rest/internal/RuleResource.java | 1 - .../openhab/core/io/rest/auth/AuthFilter.java | 5 +++- .../core/karaf/internal/FeatureInstaller.java | 20 ---------------- .../CommunicationManagerConversionTest.java | 8 +++---- .../AbstractStorageBasedTypeProviderTest.java | 23 ++++++++----------- .../items/ItemStateConverterImpl.java | 4 ++-- .../core/library/items/NumberItem.java | 21 +++++++---------- .../internal/i18n/I18nProviderImplTest.java | 3 +-- .../core/library/types/QuantityTypeTest.java | 4 ++-- .../internal/module/RunRuleModuleTest.java | 9 ++++---- .../internal/module/RuntimeRuleTest.java | 9 ++++---- .../internal/BasicConditionHandlerTest.java | 7 +++--- .../DayOfWeekConditionHandlerTest.java | 5 ++-- .../timer/internal/RuntimeRuleTest.java | 9 ++++---- .../core/automation/event/RuleEventTest.java | 7 +++--- ...tractStorageBasedTypeProviderOSGiTest.java | 12 ++++------ .../CommunicationManagerOSGiTest.java | 16 ++++++------- 18 files changed, 66 insertions(+), 101 deletions(-) diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfileFactory.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfileFactory.java index 6e46afa5f..48961c93c 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfileFactory.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/profile/ScriptProfileFactory.java @@ -74,7 +74,9 @@ public class ScriptProfileFactory implements ProfileFactory, ProfileTypeProvider public void bindScriptTransformationService(ScriptTransformationService service, Map properties) { String serviceId = (String) properties.get(TransformationService.SERVICE_PROPERTY_NAME); String serviceLabel = (String) properties.get(TransformationService.SERVICE_PROPERTY_LABEL); - services.put(serviceId, new ServiceRecord(service, serviceLabel)); + if (serviceId != null && serviceLabel != null) { + services.put(serviceId, new ServiceRecord(service, serviceLabel)); + } } public void unbindScriptTransformationService(ScriptTransformationService service, Map properties) { diff --git a/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java b/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java index 5d8dcebb4..cb78591b1 100644 --- a/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java +++ b/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java @@ -169,7 +169,6 @@ public class RuleResource implements RESTResource { @QueryParam("prefix") final @Nullable String prefix, @QueryParam("tags") final @Nullable List tags, @QueryParam("summary") @Parameter(description = "summary fields only") @Nullable Boolean summary, @DefaultValue("false") @QueryParam("staticDataOnly") @Parameter(description = "provides a cacheable list of values not expected to change regularly and honors the If-Modified-Since header, all other parameters are ignored") boolean staticDataOnly) { - if ((summary == null || !summary) && !securityContext.isUserInRole(Role.ADMIN)) { // users may only access the summary return JSONResponse.createErrorResponse(Status.UNAUTHORIZED, "Authentication required"); diff --git a/bundles/org.openhab.core.io.rest.auth/src/main/java/org/openhab/core/io/rest/auth/AuthFilter.java b/bundles/org.openhab.core.io.rest.auth/src/main/java/org/openhab/core/io/rest/auth/AuthFilter.java index 738e15fe5..31fe5574f 100644 --- a/bundles/org.openhab.core.io.rest.auth/src/main/java/org/openhab/core/io/rest/auth/AuthFilter.java +++ b/bundles/org.openhab.core.io.rest.auth/src/main/java/org/openhab/core/io/rest/auth/AuthFilter.java @@ -53,7 +53,10 @@ import org.openhab.core.config.core.ConfigParser; import org.openhab.core.config.core.ConfigurableService; import org.openhab.core.io.rest.JSONResponse; import org.openhab.core.io.rest.RESTConstants; -import org.openhab.core.io.rest.auth.internal.*; +import org.openhab.core.io.rest.auth.internal.ExpiringUserSecurityContextCache; +import org.openhab.core.io.rest.auth.internal.JwtHelper; +import org.openhab.core.io.rest.auth.internal.JwtSecurityContext; +import org.openhab.core.io.rest.auth.internal.UserSecurityContext; import org.osgi.framework.Constants; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; diff --git a/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java b/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java index 4613a77c9..5f49fa5f7 100644 --- a/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java +++ b/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java @@ -470,26 +470,6 @@ public class FeatureInstaller implements ConfigurationListener { } } - private boolean installFeature(String name) { - try { - Feature[] features = featuresService.listInstalledFeatures(); - if (!anyMatchingFeature(features, withName(name))) { - featuresService.installFeature(name, - EnumSet.of(FeaturesService.Option.Upgrade, FeaturesService.Option.NoAutoRefreshBundles)); - features = featuresService.listInstalledFeatures(); - if (anyMatchingFeature(features, withName(name))) { - logger.debug("Installed '{}'", name); - postInstalledEvent(name); - return true; - } - } - } catch (Exception e) { - logger.error("Failed installing '{}': {}", name, e.getMessage(), debugException(e)); - configMapCache = null; // make sure we retry the installation - } - return false; - } - private void uninstallFeature(String name) { try { Feature[] features = featuresService.listInstalledFeatures(); diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java index f191b1b19..432a43983 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java @@ -62,11 +62,11 @@ import org.openhab.core.types.UnDefType; public class CommunicationManagerConversionTest { // TODO: remove test - only to show CommunicationManager is too complex - private static final List> itemTypes = List.of(CallItem.class, ColorItem.class, + private static final List> ITEM_TYPES = List.of(CallItem.class, ColorItem.class, ContactItem.class, DateTimeItem.class, DimmerItem.class, ImageItem.class, LocationItem.class, PlayerItem.class, RollershutterItem.class, StringItem.class); - private static final List> types = List.of(DateTimeType.class, DecimalType.class, + private static final List> TYPES = List.of(DateTimeType.class, DecimalType.class, HSBType.class, IncreaseDecreaseType.class, NextPreviousType.class, OnOffType.class, OpenClosedType.class, PercentType.class, PlayPauseType.class, PointType.class, QuantityType.class, RawType.class, RewindFastforwardType.class, StringType.class, UpDownType.class, UnDefType.class); @@ -74,9 +74,9 @@ public class CommunicationManagerConversionTest { private static Stream arguments() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { List arguments = new ArrayList<>(); - for (Class itemType : itemTypes) { + for (Class itemType : ITEM_TYPES) { Item item = itemType.getDeclaredConstructor(String.class).newInstance("testItem"); - for (Class type : types) { + for (Class type : TYPES) { if (type.isEnum()) { arguments.add(Arguments.of(item, type.getEnumConstants()[0])); } else if (type == RawType.class) { diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderTest.java index 22ac0a2e5..3b06e2753 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderTest.java @@ -13,15 +13,12 @@ package org.openhab.core.thing.binding; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.*; import java.net.URI; import java.util.List; import java.util.Map; +import java.util.Objects; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.Test; @@ -101,9 +98,8 @@ public class AbstractStorageBasedTypeProviderTest { List actualChannelDefinitions = actual.getChannelDefinitions(); assertThat(actualChannelDefinitions.size(), is(expectedChannelDefinitions.size())); for (ChannelDefinition expectedChannelDefinition : expectedChannelDefinitions) { - ChannelDefinition actualChannelDefinition = actualChannelDefinitions.stream() - .filter(d -> d.getId().equals(expectedChannelDefinition.getId())).findFirst().orElse(null); - assertThat(actualChannelDefinition, is(notNullValue())); + ChannelDefinition actualChannelDefinition = Objects.requireNonNull(actualChannelDefinitions.stream() + .filter(d -> d.getId().equals(expectedChannelDefinition.getId())).findFirst().orElse(null)); assertChannelDefinition(actualChannelDefinition, expectedChannelDefinition); } } @@ -145,18 +141,17 @@ public class AbstractStorageBasedTypeProviderTest { List actualChannelDefinitions = actual.getChannelDefinitions(); assertThat(actualChannelDefinitions.size(), is(expectedChannelDefinitions.size())); for (ChannelDefinition expectedChannelDefinition : expectedChannelDefinitions) { - ChannelDefinition actualChannelDefinition = actualChannelDefinitions.stream() - .filter(d -> d.getId().equals(expectedChannelDefinition.getId())).findFirst().orElse(null); - assertThat(actualChannelDefinition, is(notNullValue())); + ChannelDefinition actualChannelDefinition = Objects.requireNonNull(actualChannelDefinitions.stream() + .filter(d -> d.getId().equals(expectedChannelDefinition.getId())).findFirst().orElse(null)); assertChannelDefinition(actualChannelDefinition, expectedChannelDefinition); } List expectedChannelGroupDefinitions = expected.getChannelGroupDefinitions(); List actualChannelGroupDefinitions = actual.getChannelGroupDefinitions(); assertThat(actualChannelGroupDefinitions.size(), is(expectedChannelGroupDefinitions.size())); for (ChannelGroupDefinition expectedChannelGroupDefinition : expectedChannelGroupDefinitions) { - ChannelGroupDefinition actualChannelGroupDefinition = actualChannelGroupDefinitions.stream() - .filter(d -> d.getId().equals(expectedChannelGroupDefinition.getId())).findFirst().orElse(null); - assertThat(actualChannelGroupDefinition, is(notNullValue())); + ChannelGroupDefinition actualChannelGroupDefinition = Objects.requireNonNull(actualChannelGroupDefinitions + .stream().filter(d -> d.getId().equals(expectedChannelGroupDefinition.getId())).findFirst() + .orElse(null)); assertChannelGroupDefinition(actualChannelGroupDefinition, expectedChannelGroupDefinition); } } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemStateConverterImpl.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemStateConverterImpl.java index 4a47fdf3e..275530a97 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemStateConverterImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemStateConverterImpl.java @@ -85,8 +85,8 @@ public class ItemStateConverterImpl implements ItemStateConverter { Class> dimension = numberItem.getDimension(); @SuppressWarnings({ "unchecked", "rawtypes" }) // explicit cast to Class as JDK compiler complains - Unit> conversionUnit = unitProvider - .getUnit((Class) dimension); + Unit> conversionUnit = dimension == null ? null + : unitProvider.getUnit((Class) dimension); if (conversionUnit != null && UnitUtils.isDifferentMeasurementSystem(conversionUnit, quantityState.getUnit())) { return convertOrUndef(quantityState, conversionUnit); diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/NumberItem.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/NumberItem.java index 437274089..e72185080 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/NumberItem.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/items/NumberItem.java @@ -73,7 +73,8 @@ public class NumberItem extends GenericItem implements MetadataAwareItem { String itemTypeExtension = ItemUtil.getItemTypeExtension(getType()); if (itemTypeExtension != null) { - dimension = UnitUtils.parseDimension(itemTypeExtension); + Class> dimension = UnitUtils.parseDimension(itemTypeExtension); + this.dimension = dimension; if (dimension == null) { throw new IllegalArgumentException("The given dimension " + itemTypeExtension + " is unknown."); } else if (unitProvider == null) { @@ -104,13 +105,11 @@ public class NumberItem extends GenericItem implements MetadataAwareItem { if (dimension == null) { DecimalType strippedCommand = new DecimalType(command.toBigDecimal()); internalSend(strippedCommand); + } else if (command.getUnit().isCompatible(unit) || command.getUnit().inverse().isCompatible(unit)) { + internalSend(command); } else { - if (command.getUnit().isCompatible(unit) || command.getUnit().inverse().isCompatible(unit)) { - internalSend(command); - } else { - logger.warn("Command '{}' to item '{}' was rejected because it is incompatible with the item unit '{}'", - command, name, unit); - } + logger.warn("Command '{}' to item '{}' was rejected because it is incompatible with the item unit '{}'", + command, name, unit); } } @@ -199,12 +198,7 @@ public class NumberItem extends GenericItem implements MetadataAwareItem { public void addedMetadata(Metadata metadata) { if (dimension != null && UNIT_METADATA_NAMESPACE.equals(metadata.getUID().getNamespace())) { Unit unit = UnitUtils.parseUnit(metadata.getValue()); - if (unit == null) { - logger.warn("Unit '{}' could not be parsed to a known unit. Keeping old unit '{}' for item '{}'.", - metadata.getValue(), this.unit, name); - return; - } - if (!unit.isCompatible(this.unit) && !unit.inverse().isCompatible(this.unit)) { + if ((unit == null) || (!unit.isCompatible(this.unit) && !unit.inverse().isCompatible(this.unit))) { logger.warn("Unit '{}' could not be parsed to a known unit. Keeping old unit '{}' for item '{}'.", metadata.getValue(), this.unit, name); return; @@ -222,6 +216,7 @@ public class NumberItem extends GenericItem implements MetadataAwareItem { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public void removedMetadata(Metadata metadata) { + Class> dimension = this.dimension; if (dimension != null && UNIT_METADATA_NAMESPACE.equals(metadata.getUID().getNamespace())) { assert unitProvider != null; unit = unitProvider.getUnit((Class) dimension); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java index bbbca4f4e..8efc084e2 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/internal/i18n/I18nProviderImplTest.java @@ -176,8 +176,7 @@ public class I18nProviderImplTest { @MethodSource("getAllDimensions") @SuppressWarnings("unchecked") public > void assertThatUnitProviderIsComplete(String dimensionName) { - Class> dimension = UnitUtils.parseDimension(dimensionName); - assertThat(dimension, is(notNullValue())); + Class> dimension = Objects.requireNonNull(UnitUtils.parseDimension(dimensionName)); Unit defaultUnit = i18nProviderImpl.getUnit((Class) dimension); assertThat(dimensionName + " has no default unit", defaultUnit, notNullValue()); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java index 512723358..d8c7f84d4 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java @@ -463,11 +463,11 @@ public class QuantityTypeTest { assertEquals(8, bits.byteValue()); bytes = new QuantityType<>("1 MB"); assertEquals("1 MB", bytes.toString()); - bytes = new QuantityType(1, MetricPrefix.MEGA(Units.BYTE)); + bytes = new QuantityType<>(1, MetricPrefix.MEGA(Units.BYTE)); assertEquals("1 MB", bytes.toString()); bytes = new QuantityType<>("1 GiB"); assertEquals("1 GiB", bytes.toString()); - bytes = new QuantityType(1, BinaryPrefix.GIBI(Units.BYTE)); + bytes = new QuantityType<>(1, BinaryPrefix.GIBI(Units.BYTE)); assertEquals("1 GiB", bytes.toString()); QuantityType bigAmount = new QuantityType<>("1 kio"); QuantityType octets = bigAmount.toUnit(Units.OCTET); diff --git a/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RunRuleModuleTest.java b/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RunRuleModuleTest.java index c8242fa37..75f943c06 100644 --- a/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RunRuleModuleTest.java +++ b/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RunRuleModuleTest.java @@ -14,8 +14,7 @@ package org.openhab.core.automation.internal.module; import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.util.ArrayList; import java.util.Collection; @@ -77,8 +76,8 @@ public class RunRuleModuleTest extends JavaOSGiTest { startLevelService = mock(StartLevelService.class); when(startLevelService.getStartLevel()).thenReturn(100); registerService(startLevelService, StartLevelService.class.getName()); - EventPublisher eventPublisher = getService(EventPublisher.class); - ItemRegistry itemRegistry = getService(ItemRegistry.class); + EventPublisher eventPublisher = Objects.requireNonNull(getService(EventPublisher.class)); + ItemRegistry itemRegistry = Objects.requireNonNull(getService(ItemRegistry.class)); CoreModuleHandlerFactory coreModuleHandlerFactory = new CoreModuleHandlerFactory(getBundleContext(), eventPublisher, itemRegistry, mock(TimeZoneProvider.class), mock(StartLevelService.class)); mock(CoreModuleHandlerFactory.class); @@ -206,6 +205,6 @@ public class RunRuleModuleTest extends JavaOSGiTest { }; ServiceReference subscriberReference = registerService(eventSubscriber).getReference(); - assertNotNull(getServices(EventSubscriber.class, (reference) -> reference.equals(subscriberReference))); + assertNotNull(getServices(EventSubscriber.class, reference -> reference.equals(subscriberReference))); } } diff --git a/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RuntimeRuleTest.java b/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RuntimeRuleTest.java index cca9d6cc1..866bb0667 100644 --- a/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RuntimeRuleTest.java +++ b/itests/org.openhab.core.automation.module.core.tests/src/main/java/org/openhab/core/automation/internal/module/RuntimeRuleTest.java @@ -14,8 +14,7 @@ package org.openhab.core.automation.internal.module; import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.util.Collection; import java.util.Collections; @@ -87,8 +86,8 @@ public class RuntimeRuleTest extends JavaOSGiTest { startLevelService = mock(StartLevelService.class); when(startLevelService.getStartLevel()).thenReturn(100); registerService(startLevelService, StartLevelService.class.getName()); - EventPublisher eventPublisher = getService(EventPublisher.class); - ItemRegistry itemRegistry = getService(ItemRegistry.class); + EventPublisher eventPublisher = Objects.requireNonNull(getService(EventPublisher.class)); + ItemRegistry itemRegistry = Objects.requireNonNull(getService(ItemRegistry.class)); CoreModuleHandlerFactory coreModuleHandlerFactory = new CoreModuleHandlerFactory(getBundleContext(), eventPublisher, itemRegistry, mock(TimeZoneProvider.class), mock(StartLevelService.class)); mock(CoreModuleHandlerFactory.class); @@ -136,7 +135,7 @@ public class RuntimeRuleTest extends JavaOSGiTest { }; ServiceReference subscriberReference = registerService(eventSubscriber).getReference(); - assertNotNull(getServices(EventSubscriber.class, (reference) -> reference.equals(subscriberReference))); + assertNotNull(getServices(EventSubscriber.class, reference -> reference.equals(subscriberReference))); } @Test diff --git a/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/BasicConditionHandlerTest.java b/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/BasicConditionHandlerTest.java index 1e1cf9338..a12dfb2b5 100644 --- a/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/BasicConditionHandlerTest.java +++ b/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/BasicConditionHandlerTest.java @@ -15,8 +15,7 @@ package org.openhab.core.automation.module.timer.internal; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.util.ArrayList; import java.util.Collection; @@ -91,8 +90,8 @@ public abstract class BasicConditionHandlerTest extends JavaOSGiTest { startLevelService = mock(StartLevelService.class); when(startLevelService.getStartLevel()).thenReturn(100); registerService(startLevelService, StartLevelService.class.getName()); - EventPublisher eventPublisher = getService(EventPublisher.class); - ItemRegistry itemRegistry = getService(ItemRegistry.class); + EventPublisher eventPublisher = Objects.requireNonNull(getService(EventPublisher.class)); + ItemRegistry itemRegistry = Objects.requireNonNull(getService(ItemRegistry.class)); CoreModuleHandlerFactory coreModuleHandlerFactory = new CoreModuleHandlerFactory(getBundleContext(), eventPublisher, itemRegistry, mock(TimeZoneProvider.class), mock(StartLevelService.class)); mock(CoreModuleHandlerFactory.class); diff --git a/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/DayOfWeekConditionHandlerTest.java b/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/DayOfWeekConditionHandlerTest.java index 61d3c6c54..97807370e 100644 --- a/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/DayOfWeekConditionHandlerTest.java +++ b/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/DayOfWeekConditionHandlerTest.java @@ -23,6 +23,7 @@ import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; @@ -59,8 +60,8 @@ public class DayOfWeekConditionHandlerTest extends BasicConditionHandlerTest { @BeforeEach public void before() { - EventPublisher eventPublisher = getService(EventPublisher.class); - ItemRegistry itemRegistry = getService(ItemRegistry.class); + EventPublisher eventPublisher = Objects.requireNonNull(getService(EventPublisher.class)); + ItemRegistry itemRegistry = Objects.requireNonNull(getService(ItemRegistry.class)); CoreModuleHandlerFactory coreModuleHandlerFactory = new CoreModuleHandlerFactory(getBundleContext(), eventPublisher, itemRegistry, mock(TimeZoneProvider.class), mock(StartLevelService.class)); mock(CoreModuleHandlerFactory.class); diff --git a/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/RuntimeRuleTest.java b/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/RuntimeRuleTest.java index 2775e3c76..197a01176 100644 --- a/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/RuntimeRuleTest.java +++ b/itests/org.openhab.core.automation.module.timer.tests/src/main/java/org/openhab/core/automation/module/timer/internal/RuntimeRuleTest.java @@ -15,8 +15,7 @@ package org.openhab.core.automation.module.timer.internal; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.util.Collection; import java.util.HashMap; @@ -83,8 +82,8 @@ public class RuntimeRuleTest extends JavaOSGiTest { startLevelService = mock(StartLevelService.class); when(startLevelService.getStartLevel()).thenReturn(100); registerService(startLevelService, StartLevelService.class.getName()); - EventPublisher eventPublisher = getService(EventPublisher.class); - ItemRegistry itemRegistry = getService(ItemRegistry.class); + EventPublisher eventPublisher = Objects.requireNonNull(getService(EventPublisher.class)); + ItemRegistry itemRegistry = Objects.requireNonNull(getService(ItemRegistry.class)); CoreModuleHandlerFactory coreModuleHandlerFactory = new CoreModuleHandlerFactory(getBundleContext(), eventPublisher, itemRegistry, mock(TimeZoneProvider.class), mock(StartLevelService.class)); mock(CoreModuleHandlerFactory.class); @@ -174,7 +173,7 @@ public class RuntimeRuleTest extends JavaOSGiTest { if (event.getTopic().contains(testItemName)) { itemEvents.add(event); } - }; + } @Override public java.util.Set getSubscribedEventTypes() { diff --git a/itests/org.openhab.core.automation.tests/src/main/java/org/openhab/core/automation/event/RuleEventTest.java b/itests/org.openhab.core.automation.tests/src/main/java/org/openhab/core/automation/event/RuleEventTest.java index 22e4ee1e5..78ef39f22 100644 --- a/itests/org.openhab.core.automation.tests/src/main/java/org/openhab/core/automation/event/RuleEventTest.java +++ b/itests/org.openhab.core.automation.tests/src/main/java/org/openhab/core/automation/event/RuleEventTest.java @@ -16,8 +16,7 @@ import static java.util.Map.entry; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.util.ArrayList; import java.util.Collection; @@ -89,8 +88,8 @@ public class RuleEventTest extends JavaOSGiTest { startLevelService = mock(StartLevelService.class); when(startLevelService.getStartLevel()).thenReturn(100); registerService(startLevelService, StartLevelService.class.getName()); - EventPublisher eventPublisher = getService(EventPublisher.class); - ItemRegistry itemRegistry = getService(ItemRegistry.class); + EventPublisher eventPublisher = Objects.requireNonNull(getService(EventPublisher.class)); + ItemRegistry itemRegistry = Objects.requireNonNull(getService(ItemRegistry.class)); CoreModuleHandlerFactory coreModuleHandlerFactory = new CoreModuleHandlerFactory(getBundleContext(), eventPublisher, itemRegistry, mock(TimeZoneProvider.class), mock(StartLevelService.class)); mock(CoreModuleHandlerFactory.class); diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderOSGiTest.java b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderOSGiTest.java index 509315948..9c8232e6c 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderOSGiTest.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/binding/AbstractStorageBasedTypeProviderOSGiTest.java @@ -13,12 +13,9 @@ package org.openhab.core.thing.binding; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.*; + +import java.util.Objects; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; @@ -58,8 +55,7 @@ public class AbstractStorageBasedTypeProviderOSGiTest extends JavaOSGiTest { public void setup() { registerVolatileStorageService(); - StorageService storageService = getService(StorageService.class); - assertThat(storageService, is(notNullValue())); + StorageService storageService = Objects.requireNonNull(getService(StorageService.class)); typeProvider = new AbstractStorageBasedTypeProvider(storageService) { }; diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java index 3bac9fc85..628dafbe7 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java @@ -104,7 +104,7 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest { } } - private static final UnitProvider unitProviderMock = mock(UnitProvider.class); + private static final UnitProvider UNIT_PROVIDER_MOCK = mock(UnitProvider.class); private static final String EVENT = "event"; private static final String ITEM_NAME_1 = "testItem1"; @@ -116,7 +116,7 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest { private static final SwitchItem ITEM_2 = new SwitchItem(ITEM_NAME_2); private static final NumberItem ITEM_3 = new NumberItem(ITEM_NAME_3); private static final NumberItem ITEM_4 = new NumberItem(ITEM_NAME_4); - private static NumberItem ITEM_5 = new NumberItem(ITEM_NAME_5); // will be replaced later by dimension item + private NumberItem item5 = new NumberItem(ITEM_NAME_5); // will be replaced later by dimension item private static final ThingTypeUID THING_TYPE_UID = new ThingTypeUID("test", "type"); private static final ThingUID THING_UID = new ThingUID("test", "thing"); @@ -167,8 +167,8 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest { @BeforeEach public void beforeEach() { - when(unitProviderMock.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS); - ITEM_5 = new NumberItem("Number:Temperature", ITEM_NAME_5, unitProviderMock); + when(UNIT_PROVIDER_MOCK.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS); + item5 = new NumberItem("Number:Temperature", ITEM_NAME_5, UNIT_PROVIDER_MOCK); safeCaller = getService(SafeCaller.class); assertNotNull(safeCaller); @@ -179,7 +179,7 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest { manager = new CommunicationManager(autoUpdateManagerMock, channelTypeRegistryMock, profileFactory, iclRegistry, itemRegistryMock, itemStateConverterMock, eventPublisherMock, safeCaller, thingRegistryMock, - unitProviderMock); + UNIT_PROVIDER_MOCK); doAnswer(invocation -> { switch (((Channel) invocation.getArguments()[0]).getKind()) { @@ -227,7 +227,7 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest { when(itemRegistryMock.get(eq(ITEM_NAME_2))).thenReturn(ITEM_2); when(itemRegistryMock.get(eq(ITEM_NAME_3))).thenReturn(ITEM_3); when(itemRegistryMock.get(eq(ITEM_NAME_4))).thenReturn(ITEM_4); - when(itemRegistryMock.get(eq(ITEM_NAME_5))).thenReturn(ITEM_5); + when(itemRegistryMock.get(eq(ITEM_NAME_5))).thenReturn(item5); ChannelType channelType4 = mock(ChannelType.class); when(channelType4.getItemType()).thenReturn("Number:Temperature"); @@ -238,7 +238,7 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest { when(thingRegistryMock.get(eq(THING_UID))).thenReturn(THING); - manager.addItemFactory(new CoreItemFactory(unitProviderMock)); + manager.addItemFactory(new CoreItemFactory(UNIT_PROVIDER_MOCK)); } @Test @@ -307,7 +307,7 @@ public class CommunicationManagerOSGiTest extends JavaOSGiTest { public void testItemCommandEventDecimal2Quantity2() { MetadataKey key = new MetadataKey(NumberItem.UNIT_METADATA_NAMESPACE, ITEM_NAME_5); Metadata metadata = new Metadata(key, ImperialUnits.FAHRENHEIT.toString(), null); - ITEM_5.addedMetadata(metadata); + item5.addedMetadata(metadata); manager.receive(ItemEventFactory.createCommandEvent(ITEM_NAME_5, DecimalType.valueOf("20"))); waitForAssert(() -> {