diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java index 9dd0c6914..a18fe50e8 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/binding/generic/ChannelTransformationTest.java @@ -14,12 +14,15 @@ package org.openhab.core.thing.binding.generic; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import java.util.List; +import java.util.Optional; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -59,6 +62,11 @@ public class ChannelTransformationTest { private static final String T3_INPUT = T2_RESULT; private static final String T3_RESULT = "T3Result"; + private static final String NULL_NAME = T1_NAME; + private static final String NULL_PATTERN = "NullPattern"; + private static final String NULL_INPUT = T1_RESULT; + private static final @Nullable String NULL_RESULT = null; + private @Mock @NonNullByDefault({}) TransformationService transformationService1Mock; private @Mock @NonNullByDefault({}) TransformationService transformationService2Mock; @@ -78,6 +86,8 @@ public class ChannelTransformationTest { .thenAnswer(answer -> T2_RESULT); Mockito.when(transformationService2Mock.transform(eq(T3_PATTERN), eq(T3_INPUT))) .thenAnswer(answer -> T3_RESULT); + Mockito.when(transformationService1Mock.transform(eq(NULL_PATTERN), eq(NULL_INPUT))) + .thenAnswer(answer -> NULL_RESULT); Mockito.when(serviceRef1Mock.getProperty(any())).thenReturn("TRANSFORM1"); Mockito.when(serviceRef2Mock.getProperty(any())).thenReturn("TRANSFORM2"); @@ -105,6 +115,16 @@ public class ChannelTransformationTest { assertNull(result); } + @Test + public void testNullReturningTransformation() { + String pattern = NULL_NAME + ":" + NULL_PATTERN; + + ChannelTransformation transformation = new ChannelTransformation(pattern); + Optional result = transformation.apply(NULL_INPUT); + + assertTrue(result.isEmpty()); + } + @Test public void testSingleTransformationWithColon() { String pattern = T1_NAME + ":" + T1_PATTERN; @@ -155,6 +175,26 @@ public class ChannelTransformationTest { assertNull(result); } + @Test + public void testFirstTransformationReturningNull() { + List pattern = List.of(NULL_NAME + ":" + NULL_PATTERN, T2_NAME + ":" + T2_PATTERN); + + ChannelTransformation transformation = new ChannelTransformation(pattern); + Optional result = transformation.apply(NULL_INPUT); + + assertTrue(result.isEmpty()); + } + + @Test + public void testSecondTransformationReturningNull() { + List pattern = List.of(T1_NAME + ":" + T1_PATTERN, NULL_NAME + ":" + NULL_PATTERN); + + ChannelTransformation transformation = new ChannelTransformation(pattern); + Optional result = transformation.apply(T1_INPUT); + + assertTrue(result.isEmpty()); + } + @Test public void testColonDoubleTransformationWithoutSpaces() { String pattern = T1_NAME + ":" + T1_PATTERN + "∩" + T2_NAME + ":" + T2_PATTERN;