This is an automated email from the ASF dual-hosted git repository.
riemer pushed a commit to branch
3237-transform-to-boolean-processor-output-bad-event
in repository https://gitbox.apache.org/repos/asf/streampipes.git
The following commit(s) were added to
refs/heads/3237-transform-to-boolean-processor-output-bad-event by this push:
new 369c0a5bf6 fix(#3237): Remove selector prefix from output event
369c0a5bf6 is described below
commit 369c0a5bf645b0b4369fb5e78ff02b0218465028
Author: Dominik Riemer <[email protected]>
AuthorDate: Mon Sep 30 21:39:32 2024 +0200
fix(#3237): Remove selector prefix from output event
---
.../TransformToBooleanProcessor.java | 21 +++++---
.../TransformToBooleanProcessorTest.java | 62 ++++++++++++++++++++++
2 files changed, 76 insertions(+), 7 deletions(-)
diff --git
a/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/transformtoboolean/TransformToBooleanProcessor.java
b/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/transformtoboolean/TransformToBooleanProcessor.java
index 316aa9c375..fff8f8283d 100644
---
a/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/transformtoboolean/TransformToBooleanProcessor.java
+++
b/streampipes-extensions/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/transformtoboolean/TransformToBooleanProcessor.java
@@ -85,11 +85,7 @@ public class TransformToBooleanProcessor
EventSchema eventSchema = new EventSchema();
EventSchema oldEventSchema =
processingElement.getInputStreams().get(0).getEventSchema();
// Gotta remove the "s0::" in the beginning
- Set<String> transformFields =
- (parameterExtractor.mappingPropertyValues(TRANSFORM_FIELDS_ID))
- .stream()
- .map(s -> s.substring(4))
- .collect(Collectors.toSet());
+ Set<String> transformFields = getTransformFieldNames(parameterExtractor);
for (EventProperty eventProperty : oldEventSchema.getEventProperties()) {
//TODO: Test, if eventProperty is a primitive type (string, number, ...)
@@ -122,13 +118,13 @@ public class TransformToBooleanProcessor
@Override
public void onEvent(Event inputEvent, SpOutputCollector collector) throws
SpRuntimeException {
for (String transformField : transformFields) {
- AbstractField field = inputEvent.getFieldBySelector(transformField);
+ AbstractField<?> field = inputEvent.getFieldBySelector(transformField);
// Is the field a primitive (and no list/nested field)?
if (field.isPrimitive()) {
// Yes. So remove the element and replace it with a boolean (if
possible)
inputEvent.removeFieldBySelector(transformField);
try {
- inputEvent.addField(transformField, toBoolean(field.getRawValue()));
+ inputEvent.addField(removeFieldPrefix(transformField),
toBoolean(field.getRawValue()));
} catch (SpRuntimeException e) {
LOG.info(e.getMessage());
return;
@@ -154,4 +150,15 @@ public class TransformToBooleanProcessor
throw new SpRuntimeException("Value " + s + " not convertible to
boolean");
}
}
+
+ private Set<String>
getTransformFieldNames(ProcessingElementParameterExtractor extractor) {
+ return extractor.mappingPropertyValues(TRANSFORM_FIELDS_ID)
+ .stream()
+ .map(this::removeFieldPrefix)
+ .collect(Collectors.toSet());
+ }
+
+ private String removeFieldPrefix(String fieldName) {
+ return fieldName.substring(4);
+ }
}
diff --git
a/streampipes-extensions/streampipes-processors-transformation-jvm/src/test/java/org/apache/streampipes/processors/transformation/jvm/processor/transformtoboolean/TransformToBooleanProcessorTest.java
b/streampipes-extensions/streampipes-processors-transformation-jvm/src/test/java/org/apache/streampipes/processors/transformation/jvm/processor/transformtoboolean/TransformToBooleanProcessorTest.java
new file mode 100644
index 0000000000..b93127a352
--- /dev/null
+++
b/streampipes-extensions/streampipes-processors-transformation-jvm/src/test/java/org/apache/streampipes/processors/transformation/jvm/processor/transformtoboolean/TransformToBooleanProcessorTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package
org.apache.streampipes.processors.transformation.jvm.processor.transformtoboolean;
+
+import org.apache.streampipes.test.executors.PrefixStrategy;
+import org.apache.streampipes.test.executors.ProcessingElementTestExecutor;
+import org.apache.streampipes.test.executors.TestConfiguration;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+public class TransformToBooleanProcessorTest {
+
+ @Test
+ void onEvent() {
+
+ var processor = new TransformToBooleanProcessor();
+
+ List<Map<String, Object>> inputEvents = List.of(Map.of(
+ "numberBoolean", 0,
+ "stringBoolean", "true",
+ "stringBooleanFalse", "false",
+ "floatBoolean", 1.0
+ ));
+
+ List<Map<String, Object>> outputEvents = List.of(Map.of(
+ "numberBoolean", false,
+ "stringBoolean", true,
+ "stringBooleanFalse", false,
+ "floatBoolean", true
+ ));
+
+ var configuration = TestConfiguration
+ .builder()
+ .config(TransformToBooleanProcessor.TRANSFORM_FIELDS_ID, List.of(
+ "s0::numberBoolean", "s0::stringBoolean",
"s0::stringBooleanFalse", "s0::floatBoolean"))
+ .prefixStrategy(PrefixStrategy.SAME_PREFIX)
+ .build();
+
+ var testExecutor = new ProcessingElementTestExecutor(processor,
configuration);
+
+ testExecutor.run(inputEvents, outputEvents);
+ }
+}