Skip to content

Commit

Permalink
Add tests for null-returning transformations in ChannelTransformation…
Browse files Browse the repository at this point in the history
…Test (#4356)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng authored Aug 24, 2024
1 parent 17ad247 commit e8e5544
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

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;
Expand Down Expand Up @@ -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;

Expand All @@ -78,6 +86,8 @@ public void init() throws TransformationException {
.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");
Expand Down Expand Up @@ -105,6 +115,16 @@ public void testMissingTransformation() {
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;
Expand Down Expand Up @@ -155,6 +175,26 @@ public void testInvalidSecondTransformation() {
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;
Expand Down

0 comments on commit e8e5544

Please sign in to comment.