Skip to content

Commit

Permalink
apache#2374 updated test executor to non-static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaakKrut committed Apr 15, 2024
1 parent 1b33586 commit ccfe4ad
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 50 deletions.
Expand Up @@ -63,7 +63,9 @@ public void test(
List<Map<String, Object>> outputEvents = new ArrayList<>();
outputEventBooleans.forEach(bool->outputEvents.add(Map.of(FIELD_NAME, bool)));

ProcessingElementTestExecutor.run(processor, userConfiguration, events, outputEvents, null);
ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

testExecutor.run(events, outputEvents);
}

static Stream<Arguments> data() {
Expand Down
Expand Up @@ -59,7 +59,9 @@ public void test(List<Map<String, Object>> events,
invocation.setOutputStrategies(outputStrategies);
});

ProcessingElementTestExecutor.run(processor, Map.of(), events, outputEvents, invocationConfig);
ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, invocationConfig);

testExecutor.run(events, outputEvents);
}
static Stream<Arguments> data() {

Expand Down
Expand Up @@ -73,7 +73,10 @@ public void test(List<Map<String, Object>> events,
outputKeySelectors.add(S1_PREFIX + MergeByTimeProcessor.TIMESTAMP_MAPPING_STREAM_2_KEY);
});

ProcessingElementTestExecutor.run(processor, userConfiguration, events, outputEvents, invocationConfig);
ProcessingElementTestExecutor testExecutor =
new ProcessingElementTestExecutor(processor, userConfiguration, invocationConfig);

testExecutor.run(events, outputEvents);
}

static Stream<Arguments> data() {
Expand Down
Expand Up @@ -53,8 +53,9 @@ public void testLowerThenOperatorFilterNotApplied() {
Map.of(PROPERTY_NAME, 1.0f)
);

ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

ProcessingElementTestExecutor.run(processor, userConfiguration, inputEvents, outputEvents, null);
testExecutor.run(inputEvents, outputEvents);
}

@Test
Expand All @@ -73,9 +74,9 @@ public void testLowerThenOperatorFilterApplied() {

List<Map<String, Object>> outputEvents = List.of();

ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

ProcessingElementTestExecutor.run(processor, userConfiguration, inputEvents, outputEvents, null);
testExecutor.run(inputEvents, outputEvents);
}


}
Expand Up @@ -42,59 +42,46 @@
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ProcessingElementTestExecutor {

/**
* This method is used to run a data processor with a given configuration and a list of input events.
* It then verifies the output events against the expected output events.
*
* @param processor The data processor under test.
* @param userConfiguration The user input configuration for the processor.
* @param inputEvents The list of input events to be processed.
* @param expectedOutputEvents The list of expected output events.
* @param expectedException Exception expected to occur.
*/
public static void runWithException(
IStreamPipesDataProcessor processor,
Map<String, Object> userConfiguration,
List<Map<String, Object>> inputEvents,
List<Map<String, Object>> expectedOutputEvents,
Consumer<DataProcessorInvocation> invocationConfig,
Exception expectedException
) {
Exception exception = assertThrows(expectedException.getClass(), () -> {
run(processor, userConfiguration, inputEvents, expectedOutputEvents, invocationConfig);
});
private final IStreamPipesDataProcessor processor;
private final Map<String, Object> userConfiguration;
private Consumer<DataProcessorInvocation> invocationConfig;

String expectedMessage = expectedException.getMessage();
String actualMessage = exception.getMessage();
public ProcessingElementTestExecutor(IStreamPipesDataProcessor processor, Map<String, Object> userConfiguration,
Consumer<DataProcessorInvocation> invocationConfig) {
this.processor = processor;
this.userConfiguration = userConfiguration;
this.invocationConfig = invocationConfig;
}

assertTrue(actualMessage.contains(expectedMessage));
public ProcessingElementTestExecutor(IStreamPipesDataProcessor processor, Map<String, Object> userConfiguration) {
this.processor = processor;
this.userConfiguration = userConfiguration;
}

public ProcessingElementTestExecutor(IStreamPipesDataProcessor processor,
Consumer<DataProcessorInvocation> invocationConfig) {
this.processor = processor;
this.userConfiguration = new HashMap<>();
this.invocationConfig = invocationConfig;
}

/**
* This method is used to run a data processor with a given configuration and a list of input events.
* It then verifies the output events against the expected output events.
*
* @param processor The data processor under test.
* @param userConfiguration The user input configuration for the processor.
* @param inputEvents The list of input events to be processed.
* @param expectedOutputEvents The list of expected output events.
*/
public static void run(
IStreamPipesDataProcessor processor,
Map<String, Object> userConfiguration,
public void run(
List<Map<String, Object>> inputEvents,
List<Map<String, Object>> expectedOutputEvents,
Consumer<DataProcessorInvocation> invocationConfig
List<Map<String, Object>> expectedOutputEvents
) {


Expand Down
Expand Up @@ -28,6 +28,9 @@
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;


public class TestSwingingDoorTrendingFilterProcessor {

Expand Down Expand Up @@ -81,7 +84,9 @@ public void test(){
sdtValueField, 800.0)
);

ProcessingElementTestExecutor.run(processor, userConfiguration, inputEvents, outputEvents, null);
ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

testExecutor.run(inputEvents, outputEvents);
}

@Test
Expand All @@ -98,10 +103,17 @@ public void testInvalidDeviationKey(){

List<Map<String, Object>> outputEvents = new ArrayList<>();

Exception exception = new SpRuntimeException("Compression Deviation should be positive!");
Exception expectedException = new SpRuntimeException("Compression Deviation should be positive!");

ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

Exception exception = assertThrows(expectedException.getClass(), () -> {
testExecutor.run(inputEvents, outputEvents);
});
String expectedMessage = expectedException.getMessage();
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));

ProcessingElementTestExecutor
.runWithException(processor, userConfiguration, inputEvents, outputEvents, null, exception);
}

@Test
Expand All @@ -118,10 +130,16 @@ public void testNegativeMinimumTime(){

List<Map<String, Object>> outputEvents = new ArrayList<>();

Exception exception = new SpRuntimeException("Compression Minimum Time Interval should be >= 0!");
Exception expectedException = new SpRuntimeException("Compression Minimum Time Interval should be >= 0!");

ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

ProcessingElementTestExecutor
.runWithException(processor, userConfiguration, inputEvents, outputEvents, null, exception);
Exception exception = assertThrows(expectedException.getClass(), () -> {
testExecutor.run(inputEvents, outputEvents);
});
String expectedMessage = expectedException.getMessage();
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}

@Test
Expand All @@ -138,10 +156,16 @@ public void testInvalidTimeInterval(){

List<Map<String, Object>> outputEvents = new ArrayList<>();

Exception exception = new
Exception expectedException = new
SpRuntimeException("Compression Minimum Time Interval should be < Compression Maximum Time Interval!");

ProcessingElementTestExecutor
.runWithException(processor, userConfiguration, inputEvents, outputEvents, null, exception);
ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

Exception exception = assertThrows(expectedException.getClass(), () -> {
testExecutor.run(inputEvents, outputEvents);
});
String expectedMessage = expectedException.getMessage();
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
}
Expand Up @@ -63,7 +63,9 @@ public void test(String keyword,
List<Map<String, Object>> outputEvents = new ArrayList<>();
outputEventValues.forEach(value->outputEvents.add(Map.of(FIELD_NAME, value)));

ProcessingElementTestExecutor.run(processor, userConfiguration, events, outputEvents, null);
ProcessingElementTestExecutor testExecutor = new ProcessingElementTestExecutor(processor, userConfiguration);

testExecutor.run(events, outputEvents);
}


Expand Down

0 comments on commit ccfe4ad

Please sign in to comment.