Add tests for null-returning transformations in ChannelTransformationTest (#4356)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
jimtng 2024-08-24 18:59:21 +10:00 committed by GitHub
parent 17ad247519
commit e8e55443c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<String> 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<String> pattern = List.of(NULL_NAME + ":" + NULL_PATTERN, T2_NAME + ":" + T2_PATTERN);
ChannelTransformation transformation = new ChannelTransformation(pattern);
Optional<String> result = transformation.apply(NULL_INPUT);
assertTrue(result.isEmpty());
}
@Test
public void testSecondTransformationReturningNull() {
List<String> pattern = List.of(T1_NAME + ":" + T1_PATTERN, NULL_NAME + ":" + NULL_PATTERN);
ChannelTransformation transformation = new ChannelTransformation(pattern);
Optional<String> result = transformation.apply(T1_INPUT);
assertTrue(result.isEmpty());
}
@Test
public void testColonDoubleTransformationWithoutSpaces() {
String pattern = T1_NAME + ":" + T1_PATTERN + "" + T2_NAME + ":" + T2_PATTERN;