You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2024/03/12 08:20:44 UTC
(streampipes) branch refactor-boolean-filter-processor-test created (now 60c598aba2)
This is an automated email from the ASF dual-hosted git repository.
zehnder pushed a change to branch refactor-boolean-filter-processor-test
in repository https://gitbox.apache.org/repos/asf/streampipes.git
at 60c598aba2 refactor(#2525): Refactor TestBooleanFilterProcessor
This branch includes the following new commits:
new 60c598aba2 refactor(#2525): Refactor TestBooleanFilterProcessor
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(streampipes) 01/01: refactor(#2525): Refactor TestBooleanFilterProcessor
Posted by ze...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
zehnder pushed a commit to branch refactor-boolean-filter-processor-test
in repository https://gitbox.apache.org/repos/asf/streampipes.git
commit 60c598aba2f7f9fd9eea4e176bd94069a8058733
Author: Philipp Zehnder <te...@users.noreply.github.com>
AuthorDate: Tue Mar 12 09:20:36 2024 +0100
refactor(#2525): Refactor TestBooleanFilterProcessor
---
.../streampipes-processors-filters-jvm/pom.xml | 10 +
.../booleanfilter/BooleanFilterProcessor.java | 8 +-
.../booleanfilter/TestBooleanFilterProcessor.java | 203 ++++++++++-----------
3 files changed, 116 insertions(+), 105 deletions(-)
diff --git a/streampipes-extensions/streampipes-processors-filters-jvm/pom.xml b/streampipes-extensions/streampipes-processors-filters-jvm/pom.xml
index 8d4bd2ebbe..ed05e4cbc5 100644
--- a/streampipes-extensions/streampipes-processors-filters-jvm/pom.xml
+++ b/streampipes-extensions/streampipes-processors-filters-jvm/pom.xml
@@ -50,6 +50,16 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.streampipes</groupId>
<artifactId>streampipes-test-utils</artifactId>
diff --git a/streampipes-extensions/streampipes-processors-filters-jvm/src/main/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/BooleanFilterProcessor.java b/streampipes-extensions/streampipes-processors-filters-jvm/src/main/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/BooleanFilterProcessor.java
index c44c62a522..f142436abb 100644
--- a/streampipes-extensions/streampipes-processors-filters-jvm/src/main/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/BooleanFilterProcessor.java
+++ b/streampipes-extensions/streampipes-processors-filters-jvm/src/main/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/BooleanFilterProcessor.java
@@ -38,7 +38,7 @@ import org.apache.streampipes.wrapper.standalone.StreamPipesDataProcessor;
public class BooleanFilterProcessor extends StreamPipesDataProcessor {
- private static final String BOOLEAN_MAPPING = "boolean-mapping";
+ public static final String BOOLEAN_MAPPING = "boolean-mapping";
public static final String VALUE = "value";
private static final String OPTION_FALSE = "False";
@@ -66,10 +66,12 @@ public class BooleanFilterProcessor extends StreamPipesDataProcessor {
}
@Override
- public void onInvocation(ProcessorParams processorParams, SpOutputCollector spOutputCollector,
+ public void onInvocation(ProcessorParams processorParams,
+ SpOutputCollector spOutputCollector,
EventProcessorRuntimeContext eventProcessorRuntimeContext) throws SpRuntimeException {
- String selectedSingleValue = processorParams.extractor().selectedSingleValue(VALUE, String.class);
this.mappingField = processorParams.extractor().mappingPropertyValue(BOOLEAN_MAPPING);
+
+ String selectedSingleValue = processorParams.extractor().selectedSingleValue(VALUE, String.class);
this.valueToFilter = selectedSingleValue.equals(OPTION_TRUE);
}
diff --git a/streampipes-extensions/streampipes-processors-filters-jvm/src/test/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/TestBooleanFilterProcessor.java b/streampipes-extensions/streampipes-processors-filters-jvm/src/test/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/TestBooleanFilterProcessor.java
index 43ee222166..3f473f6f61 100644
--- a/streampipes-extensions/streampipes-processors-filters-jvm/src/test/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/TestBooleanFilterProcessor.java
+++ b/streampipes-extensions/streampipes-processors-filters-jvm/src/test/java/org/apache/streampipes/processors/filters/jvm/processor/booleanfilter/TestBooleanFilterProcessor.java
@@ -18,107 +18,106 @@
package org.apache.streampipes.processors.filters.jvm.processor.booleanfilter;
-//@RunWith(Parameterized.class)
+import org.apache.streampipes.extensions.api.pe.context.EventProcessorRuntimeContext;
+import org.apache.streampipes.model.runtime.Event;
+import org.apache.streampipes.model.runtime.EventFactory;
+import org.apache.streampipes.model.runtime.SchemaInfo;
+import org.apache.streampipes.model.runtime.SourceInfo;
+import org.apache.streampipes.sdk.extractor.ProcessingElementParameterExtractor;
+import org.apache.streampipes.test.extensions.api.StoreEventCollector;
+import org.apache.streampipes.wrapper.params.compat.ProcessorParams;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
public class TestBooleanFilterProcessor {
-//
-// private static final Logger LOG = LoggerFactory.getLogger(TestBooleanFilterProcessor.class);
-//
-// @org.junit.runners.Parameterized.Parameters
-// public static Iterable<Object[]> data() {
-// List<Boolean> allTrue = Arrays.asList(true, true, true);
-// List<Boolean> allFalse = Arrays.asList(false, false, false);
-// List<Boolean> someTrueSomeFalse = Arrays.asList(true, false, true, false, true, false, true, false, true);
-// List<Boolean> empty = Arrays.asList();
-// return Arrays.asList(new Object[][]{
-// {"True", "Test", someTrueSomeFalse, 5},
-// {"True", "Test", allTrue, 3},
-// {"True", "Test", allFalse, 0},
-// {"True", "Test", empty, 0},
-// {"False", "Test", someTrueSomeFalse, 4},
-// {"False", "Test", allTrue, 0},
-// {"False", "Test", allFalse, 3},
-// {"False", "Test", empty, 0},
-// });
-// }
-//
-// @org.junit.runners.Parameterized.Parameter
-// public String boolToKeep;
-//
-// @org.junit.runners.Parameterized.Parameter(1)
-// public String fieldName;
-//
-// @org.junit.runners.Parameterized.Parameter(2)
-// public List<Boolean> eventBooleans;
-//
-// @org.junit.runners.Parameterized.Parameter(3)
-// public int expectedFilteredBooleanCount;
-//
-// @Test
-// public void testBoolenFilter() {
-// BooleanFilterProcessor bfp = new BooleanFilterProcessor();
-// DataProcessorDescription originalGraph = bfp.declareModel();
-// originalGraph.setSupportedGrounding(EventGroundingGenerator.makeDummyGrounding());
-//
-// DataProcessorInvocation graph =
-// InvocationGraphGenerator.makeEmptyInvocation(originalGraph);
-//
-// graph.setInputStreams(Collections
-// .singletonList(EventStreamGenerator
-// .makeStreamWithProperties(Collections.singletonList(fieldName))));
-//
-// graph.setOutputStream(EventStreamGenerator.makeStreamWithProperties(Collections.singletonList(fieldName)));
-//
-// graph.getOutputStream().getEventGrounding().getTransportProtocol().getTopicDefinition()
-// .setActualTopicName("output-topic");
-//
-// graph.getStaticProperties().stream()
-// .filter(p -> p instanceof MappingPropertyUnary)
-// .map((p -> (MappingPropertyUnary) p))
-// // Must hardcode since BOOLEAN_MAPPING is private
-// .filter(p -> p.getInternalName().equals("boolean-mapping"))
-// .findFirst().get().setSelectedProperty("s0::" + fieldName);
-// ProcessorParams params = new ProcessorParams(graph);
-// params.extractor().getStaticPropertyByName(BooleanFilterProcessor.VALUE, OneOfStaticProperty.class).getOptions()
-// .stream().filter(ot -> ot.getName().equals(boolToKeep)).findFirst()
-// .get().setSelected(true);
-// StoreEventCollector collector = new StoreEventCollector();
-//
-// bfp.onInvocation(params, collector, null);
-//
-// int result = sendEvents(bfp, collector);
-//
-// LOG.info("Expected filtered boolean count is {}", expectedFilteredBooleanCount);
-// LOG.info("Actual filtered boolean count is {}", result);
-// assertEquals(expectedFilteredBooleanCount, result);
-// }
-//
-// private int sendEvents(BooleanFilterProcessor processor, StoreEventCollector collector) {
-// List<Event> events = makeEvents();
-// for (Event event : events) {
-// LOG.info("Sending event with value "
-// + event.getFieldBySelector("s0::" + fieldName).getAsPrimitive().getAsBoolean());
-// processor.onEvent(event, collector);
-// try {
-// Thread.sleep(100);
-// } catch (InterruptedException e) {
-// e.printStackTrace();
-// }
-// }
-// return collector.getEvents().size();
-// }
-//
-// private List<Event> makeEvents() {
-// List<Event> events = new ArrayList<>();
-// for (Boolean eventSetting : eventBooleans) {
-// events.add(makeEvent(eventSetting));
-// }
-// return events;
-// }
-//
-// private Event makeEvent(Boolean value) {
-// Map<String, Object> map = new HashMap<>();
-// map.put(fieldName, value);
-// return EventFactory.fromMap(map, new SourceInfo("test" + "-topic", "s0"),
-// new SchemaInfo(null, new ArrayList<>()));
-// }
+ private static final String STREAM_PREFIX = "s0::";
+
+ private static final Logger LOG = LoggerFactory.getLogger(TestBooleanFilterProcessor.class);
+
+ static Stream<Arguments> data() {
+ return Stream.of(
+ Arguments.of("True", Arrays.asList(true, true, false, true, false, true, false, true), 5),
+ Arguments.of("True", Arrays.asList(true, true, true), 3),
+ Arguments.of("True", Arrays.asList(false, false, false), 0),
+ Arguments.of("True", Collections.emptyList(), 0),
+ Arguments.of("False", Arrays.asList(true, false, true, false, true, false, true, false, true), 4),
+ Arguments.of("False", Arrays.asList(true, true, true), 0),
+ Arguments.of("False", Arrays.asList(false, false, false), 3),
+ Arguments.of("False", Collections.emptyList(), 0)
+ );
+ }
+
+ @ParameterizedTest
+ @MethodSource("data")
+ public void testBoolenFilter(
+ String boolToKeep,
+ List<Boolean> eventBooleans,
+ int expectedFilteredBooleanCount
+ ) {
+
+ var fieldName = "Test";
+ var processorParams = mock(ProcessorParams.class);
+ var eventProcessorRuntimeContext = mock(EventProcessorRuntimeContext.class);
+
+ var processor = new BooleanFilterProcessor();
+ var extractor = mock(ProcessingElementParameterExtractor.class);
+ when(processorParams.extractor()).thenReturn(extractor);
+ when(extractor.mappingPropertyValue(BooleanFilterProcessor.BOOLEAN_MAPPING)).thenReturn(STREAM_PREFIX + fieldName);
+ when(extractor.selectedSingleValue(BooleanFilterProcessor.VALUE, String.class)).thenReturn(boolToKeep);
+
+ var collector = new StoreEventCollector();
+ processor.onInvocation(processorParams, collector, eventProcessorRuntimeContext);
+
+ sendEvents(processor, collector, eventBooleans, fieldName);
+
+ assertEquals(expectedFilteredBooleanCount, collector.getEvents().size());
+ }
+
+ private void sendEvents(
+ BooleanFilterProcessor processor,
+ StoreEventCollector collector,
+ List<Boolean> eventBooleans,
+ String fieldName) {
+ List<Event> events = makeEvents(eventBooleans, fieldName);
+ for (Event event : events) {
+ LOG.info("Sending event with value "
+ + event.getFieldBySelector(STREAM_PREFIX + fieldName)
+ .getAsPrimitive()
+ .getAsBoolean());
+ processor.onEvent(event, collector);
+
+ }
+ }
+
+ private List<Event> makeEvents(List<Boolean> eventBooleans, String fieldName) {
+ List<Event> events = new ArrayList<>();
+ for (Boolean eventSetting : eventBooleans) {
+ events.add(makeEvent(eventSetting, fieldName));
+ }
+ return events;
+ }
+
+ private Event makeEvent(Boolean value, String fieldName) {
+ Map<String, Object> map = new HashMap<>();
+ map.put(fieldName, value);
+ return EventFactory.fromMap(map, new SourceInfo("test" + "-topic", "s0"),
+ new SchemaInfo(null, new ArrayList<>())
+ );
+ }
}