Repository: apache/streampipes
Pull request #3029 (2024-07-15 08:56:23)

feat: Implement visit for mapping property nary in pipeline element templates


<!--
  ~ 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.
  ~
  -->

  <!--
Thanks for contributing! Here are some tips you can follow to help us incorporate your contribution quickly and easily:
1. If this is your first time, please read our contributor guidelines:
    - https://streampipes.apache.org/community/get-involved/
    - https://cwiki.apache.org/confluence/display/STREAMPIPES/Getting+Started
2. Make sure the PR title is formatted like: `[#<GitHub issue id>] PR title ...`
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., `[WIP][#<GitHub issue id>] PR title ...`.
4. Please write your PR title to summarize what this PR proposes/fixes.
5. Link the PR to the corresponding GitHub issue (if present) in the `Development` section in the right menu bar.
6. Be sure to keep the PR description updated to reflect all changes.
7. If possible, provide a concise example to reproduce the issue for a faster review.
8. Make sure tests pass via `mvn clean install`.
9. (Optional) If the contribution is large, please file an Apache ICLA
    - http://apache.org/licenses/icla.pdf
-->

### Purpose
<!--
Please clarify what changes you are proposing and describe how those changes will address the issue.
Furthermore, describe potential consequences the changes might have.
-->
This functionality is needed to refactor the unit tests for the `StringToStateProcessor` class. The processor requires a `requiredPropertyWithNaryMapping` input from the user. The unit test framework for processing elements uses the pipeline element template functionality.

Is it possible that this new functionality of this PR will break the templates in the UI? Because we also do not have the original event schema.

### Remarks
<!--
Is there anything left we need to pay attention on?
Are there some references that might be important? E.g. links to Confluence, or discussions
on the mailing list or GitHub.
-->
PR introduces (a) breaking change(s): no

PR introduces (a) deprecation(s): no


tenthe: It seems that the UI prevents a user from saving this information.
Therefore, we should be able to safely merge this PR.

see screenshot:

<img width="750" alt="grafik" src="https://github.com/user-attachments/assets/ef852725-0c70-4869-9384-ef91aaf4344b">


Patch:
diff --git a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitor.java b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitor.java
index 62e037c146..b301a5243a 100644
--- a/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitor.java
+++ b/streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitor.java
@@ -105,7 +105,10 @@ public class PipelineElementTemplateVisitor implements StaticPropertyVisitor {
 
   @Override
   public void visit(MappingPropertyNary mappingPropertyNary) {
-    // Do nothing, not supported by pipeline element templates
+    if (hasKey(mappingPropertyNary)) {
+      var selectedProperties = getValueAsList(mappingPropertyNary);
+      mappingPropertyNary.setSelectedProperties(selectedProperties);
+    }
   }
 
   @Override


Test patch:
diff --git a/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitorTest.java b/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitorTest.java
new file mode 100644
index 0000000000..672817786c
--- /dev/null
+++ b/streampipes-pipeline-management/src/test/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitorTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.manager.template;
+
+import org.apache.streampipes.model.staticproperty.MappingPropertyNary;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class PipelineElementTemplateVisitorTest {
+  private static final String PROPERTY_NAME = "propertyName";
+
+  private PipelineElementTemplateVisitor visitor;
+  private Map<String, Object> configs;
+  private MappingPropertyNary mappingPropertyNary;
+
+  @BeforeEach
+  void setUp() {
+    configs = new HashMap<>();
+    visitor = new PipelineElementTemplateVisitor(configs);
+    mappingPropertyNary = mock(MappingPropertyNary.class);
+    when(mappingPropertyNary.getInternalName()).thenReturn(PROPERTY_NAME);
+  }
+
+  @Test
+  void visitMappingPropertyNary_SetsSelectedPropertiesWhenKeyExists() {
+    List<String> expectedValues = List.of("value1", "value2");
+    configs.put(PROPERTY_NAME, expectedValues);
+    visitor.visit(mappingPropertyNary);
+
+    verify(mappingPropertyNary).setSelectedProperties(expectedValues);
+  }
+
+  @Test
+  void visitMappingPropertyNary_DoesNothingWhenKeyDoesNotExist() {
+    visitor.visit(mappingPropertyNary);
+
+    verify(mappingPropertyNary, never()).setSelectedProperties(anyList());
+  }
+
+  @Test
+  void visitMappingPropertyNary_HandlesEmptySelectedPropertiesCorrectly() {
+    List<String> expectedValues = List.of();
+    configs.put(PROPERTY_NAME, expectedValues);
+    visitor.visit(mappingPropertyNary);
+
+    verify(mappingPropertyNary).setSelectedProperties(expectedValues);
+  }
+}