This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new 72555fb0ac refactor: Refactor unit tests for transformation rules 
(#3287)
72555fb0ac is described below

commit 72555fb0aced283b3065836ed7a46cbdd46c8592
Author: Philipp Zehnder <[email protected]>
AuthorDate: Fri Oct 4 17:08:56 2024 +0200

    refactor: Refactor unit tests for transformation rules (#3287)
---
 .../transform/value/CorrectionValueTest.java       |  69 ++++-----
 .../transform/value/UnitTransformRuleTest.java     | 160 ++++++++++-----------
 .../transform/value/ValueEventTransformerTest.java |  41 +++---
 3 files changed, 120 insertions(+), 150 deletions(-)

diff --git 
a/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/CorrectionValueTest.java
 
b/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/CorrectionValueTest.java
index eaec0d6a85..5f3ba9717d 100644
--- 
a/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/CorrectionValueTest.java
+++ 
b/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/CorrectionValueTest.java
@@ -18,11 +18,6 @@
 
 package org.apache.streampipes.connect.shared.preprocessing.transform.value;
 
-import org.apache.streampipes.model.schema.EventProperty;
-import org.apache.streampipes.model.schema.EventPropertyPrimitive;
-import org.apache.streampipes.model.schema.EventSchema;
-
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -30,126 +25,114 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class CorrectionValueTest {
 
   private Map<String, Object> event;
 
-  private final String propertyNameBasicValue = "basicValue";
-  private final String propertyNameOtherValue = "otherValue";
+  private final String doubleProperty = "basicValue";
+  private final String stringProperty = "otherValue";
 
   @BeforeEach
   public void setUp() {
-
-    EventSchema eventSchema = new EventSchema();
-    EventProperty eventProperty = new EventPropertyPrimitive();
-    eventProperty.setLabel(propertyNameBasicValue);
-    eventProperty.setRuntimeName(propertyNameBasicValue);
-    eventSchema.addEventProperty(eventProperty);
-
-    EventProperty eventPropertyOther = new EventPropertyPrimitive();
-    eventPropertyOther.setLabel(propertyNameBasicValue);
-    eventPropertyOther.setRuntimeName(propertyNameBasicValue);
-    eventSchema.addEventProperty(eventPropertyOther);
-
     event = new HashMap<>();
-    event.put(propertyNameBasicValue, 100.0);
-    event.put(propertyNameOtherValue, "Hello");
+    event.put(doubleProperty, 100.0);
+    event.put(stringProperty, "Hello");
   }
 
   @Test
   public void testAdd() {
 
     var correctionRule = new CorrectionValueTransformationRule(
-         List.of(propertyNameBasicValue),
+         List.of(doubleProperty),
          10.0,
          "ADD"
      );
 
     var resultEvent = correctionRule.apply(event);
-    Assertions.assertNotNull(resultEvent);
-    Assertions.assertEquals(110.0, resultEvent.get(propertyNameBasicValue));
+    assertNotNull(resultEvent);
+    assertEquals(110.0, resultEvent.get(doubleProperty));
   }
 
   @Test
   public void testSubtract() {
 
     var correctionRule = new CorrectionValueTransformationRule(
-        List.of(propertyNameBasicValue),
+        List.of(doubleProperty),
         10.0,
         "SUBTRACT"
     );
     var resultEvent = correctionRule.apply(event);
-    Assertions.assertNotNull(resultEvent);
-    Assertions.assertEquals(90.0, resultEvent.get(propertyNameBasicValue));
+    assertNotNull(resultEvent);
+    assertEquals(90.0, resultEvent.get(doubleProperty));
   }
 
   @Test
   public void testMultiply() {
 
     var correctionRule = new CorrectionValueTransformationRule(
-        List.of(propertyNameBasicValue),
+        List.of(doubleProperty),
         1.5,
         "MULTIPLY"
     );
     var resultEvent = correctionRule.apply(event);
-    Assertions.assertNotNull(resultEvent);
-    Assertions.assertEquals(150.0, resultEvent.get(propertyNameBasicValue));
+    assertNotNull(resultEvent);
+    assertEquals(150.0, resultEvent.get(doubleProperty));
   }
 
   @Test
   public void testDivide() {
 
     var correctionRule = new CorrectionValueTransformationRule(
-        List.of(propertyNameBasicValue),
+        List.of(doubleProperty),
         5,
         "DIVIDE"
     );
     var resultEvent = correctionRule.apply(event);
-    Assertions.assertNotNull(resultEvent);
-    Assertions.assertEquals(20.0, resultEvent.get(propertyNameBasicValue));
+    assertNotNull(resultEvent);
+    assertEquals(20.0, resultEvent.get(doubleProperty));
   }
 
   @Test
   public void testDivideByZero() {
 
     var correctionRule = new CorrectionValueTransformationRule(
-        List.of(propertyNameBasicValue),
+        List.of(doubleProperty),
         0.0,
         "DIVIDE"
     );
     var resultEvent = correctionRule.apply(event);
-    Assertions.assertNotNull(resultEvent);
-    Assertions.assertEquals(Double.POSITIVE_INFINITY, 
resultEvent.get(propertyNameBasicValue));
+    assertNotNull(resultEvent);
+    assertEquals(Double.POSITIVE_INFINITY, resultEvent.get(doubleProperty));
   }
 
   @Test
   public void testNonNumericValue() {
 
     var correctionRule = new CorrectionValueTransformationRule(
-        List.of(propertyNameOtherValue),
+        List.of(stringProperty),
         10.0,
         "ADD"
     );
     assertThrows(
         RuntimeException.class,
-        () -> correctionRule.apply(event).get(propertyNameOtherValue)
+        () -> correctionRule.apply(event).get(stringProperty)
     );
-
-
   }
 
   @Test
   public void testUnsupportedOperation() {
 
     var correctionRule = new CorrectionValueTransformationRule(
-        List.of(propertyNameBasicValue),
+        List.of(doubleProperty),
         10.0,
         "TEST"
     );
     var resultEvent = correctionRule.apply(event);
-    Assertions.assertNotNull(resultEvent);
-    Assertions.assertEquals(100.0, resultEvent.get(propertyNameBasicValue));
+    assertNotNull(resultEvent);
+    assertEquals(100.0, resultEvent.get(doubleProperty));
   }
 }
diff --git 
a/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/UnitTransformRuleTest.java
 
b/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/UnitTransformRuleTest.java
index f6d9beaed0..37416809ac 100644
--- 
a/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/UnitTransformRuleTest.java
+++ 
b/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/UnitTransformRuleTest.java
@@ -18,134 +18,122 @@
 
 package org.apache.streampipes.connect.shared.preprocessing.transform.value;
 
-import org.apache.streampipes.model.schema.EventProperty;
-import org.apache.streampipes.model.schema.EventPropertyList;
-import org.apache.streampipes.model.schema.EventPropertyNested;
-import org.apache.streampipes.model.schema.EventPropertyPrimitive;
-import org.apache.streampipes.model.schema.EventSchema;
-
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-@SuppressWarnings("unchecked")
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 public class UnitTransformRuleTest {
 
+  private static final String PARENT_PROPERTY = "parentProperty";
+  private static final String CHILD_PROPERTY = "childProperty";
+  private static final String PROPERTY_ONE = "property1";
+  private static final String PROPERTY_TWO = "property2";
+
+  private static final String UNIT_DEGREE_CELSIUS = 
"http://qudt.org/vocab/unit#DegreeCelsius";;
+  private static final String UNIT_KELVIN = 
"http://qudt.org/vocab/unit#Kelvin";;
+
   @Test
   public void transformList() {
-    EventSchema eventSchema = new EventSchema();
-    EventPropertyList eventPropertyList = new EventPropertyList();
-    eventPropertyList.setRuntimeName("list");
-    EventProperty eventPropertyValue = new EventPropertyPrimitive();
-    eventPropertyValue.setLabel("value");
-    eventPropertyValue.setRuntimeName("value");
-    eventPropertyList.setEventProperty(eventPropertyValue);
-    
eventSchema.setEventProperties(Collections.singletonList(eventPropertyList));
-
     Map<String, Object> event = new HashMap<>();
     Map<String, Object> subEvent = new HashMap<>();
-    subEvent.put("value", 0.0);
-    event.put("list", subEvent);
-
-    List<String> keys = new ArrayList<>();
-    keys.add("list");
-    keys.add("value");
+    subEvent.put(CHILD_PROPERTY, 0.0);
+    event.put(PARENT_PROPERTY, subEvent);
 
-    UnitTransformationRule unitTransformationRule = new 
UnitTransformationRule(keys,
-        "http://qudt.org/vocab/unit#DegreeCelsius";, 
"http://qudt.org/vocab/unit#Kelvin";);
+    var unitTransformationRule = new UnitTransformationRule(
+        List.of(PARENT_PROPERTY, CHILD_PROPERTY),
+        UNIT_DEGREE_CELSIUS,
+        UNIT_KELVIN
+    );
 
     var result = unitTransformationRule.apply(event);
 
-    Assertions.assertEquals(1,
-                            result.keySet().size());
-    Assertions.assertEquals(273.15, ((Map<String, Object>) 
result.get(eventPropertyList.getRuntimeName()))
-        .get(eventPropertyValue.getRuntimeName()));
+    assertEquals(
+        1,
+        result.keySet()
+              .size()
+    );
+
+    assertEquals(273.15, ((Map<String, Object>) result.get(PARENT_PROPERTY))
+        .get(CHILD_PROPERTY));
   }
 
 
   @Test
   public void transformNested() {
-    EventSchema eventSchema = new EventSchema();
-    EventPropertyNested eventPropertyMainKey = new EventPropertyNested();
-    eventPropertyMainKey.setLabel("mainKey");
-    eventPropertyMainKey.setRuntimeName("mainKey");
-    EventProperty eventPropertyValue = new EventPropertyPrimitive();
-    eventPropertyValue.setLabel("value");
-    eventPropertyValue.setRuntimeName("value");
-    
eventPropertyMainKey.setEventProperties(Collections.singletonList(eventPropertyValue));
-    
eventSchema.setEventProperties(Collections.singletonList(eventPropertyMainKey));
-
     Map<String, Object> event = new HashMap<>();
     Map<String, Object> subEvent = new HashMap<>();
-    subEvent.put("value", 10.0);
-    event.put("mainKey", subEvent);
+    subEvent.put(CHILD_PROPERTY, 10.0);
+    event.put(PARENT_PROPERTY, subEvent);
 
-    List<String> keys = new ArrayList<>();
-    keys.add("mainKey");
-    keys.add("value");
-
-    UnitTransformationRule unitTransformationRule = new 
UnitTransformationRule(keys,
-        "http://qudt.org/vocab/unit#DegreeCelsius";, 
"http://qudt.org/vocab/unit#Kelvin";);
+    var unitTransformationRule = new UnitTransformationRule(
+        List.of(PARENT_PROPERTY, CHILD_PROPERTY),
+        UNIT_DEGREE_CELSIUS,
+        UNIT_KELVIN
+    );
 
     var result = unitTransformationRule.apply(event);
 
-    Assertions.assertEquals(1,
-                            result.keySet().size());
-    Assertions.assertEquals(283.15, ((Map<String, Object>) 
result.get(eventPropertyMainKey.getRuntimeName()))
-        .get(eventPropertyValue.getRuntimeName()));
+    assertEquals(
+        1,
+        result.keySet()
+              .size()
+    );
+    assertEquals(283.15, ((Map<String, Object>) result.get(PARENT_PROPERTY))
+        .get(CHILD_PROPERTY));
   }
 
 
   @Test
   public void transformMultiEvent() {
-    EventSchema eventSchema = new EventSchema();
-    EventProperty eventPropertyValue1 = new EventPropertyPrimitive();
-    eventPropertyValue1.setLabel("value1");
-    eventPropertyValue1.setRuntimeName("value1");
-    EventProperty eventPropertyValue2 = new EventPropertyPrimitive();
-    eventPropertyValue2.setLabel("value2");
-    eventPropertyValue2.setRuntimeName("value2");
-    eventSchema.addEventProperty(eventPropertyValue1);
-    eventSchema.addEventProperty(eventPropertyValue2);
-
-    List<String> keys = new ArrayList<>();
-    keys.add("value2");
-
-    UnitTransformationRule unitTransformationRule = new 
UnitTransformationRule(keys,
-        "http://qudt.org/vocab/unit#DegreeCelsius";, 
"http://qudt.org/vocab/unit#Kelvin";);
-    Map<String, Object> event = new HashMap<>();
-    event.put("value1", 0.0);
-    event.put("value2", 10.0);
+
+    var unitTransformationRule = new UnitTransformationRule(
+        List.of(PROPERTY_TWO),
+        UNIT_DEGREE_CELSIUS,
+        UNIT_KELVIN
+    );
+
+    Map<String, Object> event = getEventWithTwoProperties(0.0, 10.0);
 
     var result = unitTransformationRule.apply(event);
-    Assertions.assertEquals(2,
-                            result.keySet().size());
-    Assertions.assertEquals(283.15, 
result.get(eventPropertyValue2.getLabel()));
+    assertEquals(
+        2,
+        result.keySet()
+              .size()
+    );
+    assertEquals(283.15, result.get(PROPERTY_TWO));
 
 
-    event = new HashMap<>();
-    event.put("value1", 20.0);
-    event.put("value2", 20.0);
+    event = getEventWithTwoProperties(20.0, 20.0);
 
     result = unitTransformationRule.apply(event);
-    Assertions.assertEquals(2,
-                            result.keySet().size());
-    Assertions.assertEquals(293.15, 
result.get(eventPropertyValue2.getRuntimeName()));
+    assertEquals(
+        2,
+        result.keySet()
+              .size()
+    );
+    assertEquals(293.15, result.get(PROPERTY_TWO));
 
 
-    event = new HashMap<>();
-    event.put("value1", 0.0);
-    event.put("value2", 0.0);
+    event = getEventWithTwoProperties(0.0, 0.0);
 
     result = unitTransformationRule.apply(event);
-    Assertions.assertEquals(2,
-                            result.keySet().size());
-    Assertions.assertEquals(273.15, 
result.get(eventPropertyValue2.getRuntimeName()));
+    assertEquals(
+        2,
+        result.keySet()
+              .size()
+    );
+    assertEquals(273.15, result.get(PROPERTY_TWO));
+  }
+
+  private Map<String, Object> getEventWithTwoProperties(double value1, double 
value2) {
+    Map<String, Object> event = new HashMap<>();
+    event.put(PROPERTY_ONE, value1);
+    event.put(PROPERTY_TWO, value2);
+    return event;
   }
 
 }
diff --git 
a/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/ValueEventTransformerTest.java
 
b/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/ValueEventTransformerTest.java
index 286be99a00..bec73c2155 100644
--- 
a/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/ValueEventTransformerTest.java
+++ 
b/streampipes-connect-shared/src/test/java/org/apache/streampipes/connect/shared/preprocessing/transform/value/ValueEventTransformerTest.java
@@ -18,46 +18,45 @@
 
 package org.apache.streampipes.connect.shared.preprocessing.transform.value;
 
-import org.apache.streampipes.extensions.api.connect.TransformationRule;
-import org.apache.streampipes.model.schema.EventProperty;
-import org.apache.streampipes.model.schema.EventPropertyPrimitive;
-import org.apache.streampipes.model.schema.EventSchema;
-
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 
 public class ValueEventTransformerTest {
 
+  private static final String UNIT_DEGREE_CELSIUS = 
"http://qudt.org/vocab/unit#DegreeCelsius";;
+  private static final String UNIT_KELVIN = 
"http://qudt.org/vocab/unit#Kelvin";;
+  private static final String EVENT_PROPEPRTY = "property";
+
   @Test
   public void transform() {
-    EventSchema eventSchema = new EventSchema();
-    EventProperty eventPropertyf = new EventPropertyPrimitive();
-    eventPropertyf.setLabel("a");
-    eventPropertyf.setRuntimeName("a");
-    eventSchema.addEventProperty(eventPropertyf);
 
     Map<String, Object> event = new HashMap<>();
-    event.put("a", 273.15);
+    event.put(EVENT_PROPEPRTY, 273.15);
 
-    List<String> keys = new ArrayList<>();
-    keys.add("a");
+    var unitTransformationRule = new UnitTransformationRule(
+        List.of(EVENT_PROPEPRTY),
+        UNIT_KELVIN,
+        UNIT_DEGREE_CELSIUS
+    );
 
-    List<TransformationRule> rules = new ArrayList<>();
-    rules.add(new UnitTransformationRule(keys,
-        "http://qudt.org/vocab/unit#Kelvin";, 
"http://qudt.org/vocab/unit#DegreeCelsius";));
+    var correctionRule = new CorrectionValueTransformationRule(
+        List.of(EVENT_PROPEPRTY),
+        10.0,
+        "ADD"
+    );
 
-    for (var rule: rules) {
+    var rules = List.of(unitTransformationRule, correctionRule);
+
+    for (var rule : rules) {
       event = rule.apply(event);
     }
 
-    Assertions.assertEquals(0.0, event.get(eventPropertyf.getRuntimeName()));
-
+    assertEquals(10.0, event.get(EVENT_PROPEPRTY));
   }
 
 }

Reply via email to