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

fjtiradosarti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-kogito-apps.git


The following commit(s) were added to refs/heads/main by this push:
     new 5c3f01be3 [Fix #2177] Merge ProcessDefinitionEvent metadata (#2178)
5c3f01be3 is described below

commit 5c3f01be30bc5e8ce5ae57b83540a0aabc74d8df
Author: Francisco Javier Tirado Sarti 
<[email protected]>
AuthorDate: Wed Jan 22 11:34:42 2025 +0100

    [Fix #2177] Merge ProcessDefinitionEvent metadata (#2178)
    
    * [Fix #2177] Merge metadata in ProcessEventDefinition
    
    Fix https://github.com/apache/incubator-kie-kogito-apps/issues/2177
    
    * [Fix #2177] Partial rollback
---
 .../java/org/kie/kogito/index/CommonUtils.java     | 25 ++++++++++
 .../index/service/ProcessDefinitionHelper.java     |  3 +-
 .../java/org/kie/kogito/index/CommonUtilsTest.java | 54 ++++++++++++++++++++++
 3 files changed, 81 insertions(+), 1 deletion(-)

diff --git 
a/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java
 
b/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java
index 47e70d8f7..9a2798bba 100644
--- 
a/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java
+++ 
b/data-index/data-index-common/src/main/java/org/kie/kogito/index/CommonUtils.java
@@ -21,6 +21,8 @@ package org.kie.kogito.index;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
 import org.slf4j.Logger;
@@ -67,6 +69,29 @@ public class CommonUtils {
         }
     }
 
+    @SuppressWarnings("unchecked")
+    public static <K, V> Map<K, V> mergeMap(Map<K, V> source, Map<K, V> 
target) {
+        if (source == null) {
+            return target;
+        } else if (target == null) {
+            return source;
+        } else {
+            Map<K, V> result = new HashMap<>(target);
+            source.forEach((key, value) -> {
+                if (value != null) {
+                    result.merge(key, value, (targetValue, srcValue) -> {
+                        if (srcValue instanceof Map && targetValue instanceof 
Map) {
+                            return (V) mergeMap((Map<K, V>) srcValue, (Map<K, 
V>) targetValue);
+                        } else {
+                            return srcValue;
+                        }
+                    });
+                }
+            });
+            return result;
+        }
+    }
+
     private static String getContext(String processId) {
         return processId != null && processId.contains(".") ? 
processId.substring(processId.lastIndexOf('.') + 1) : processId;
     }
diff --git 
a/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/ProcessDefinitionHelper.java
 
b/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/ProcessDefinitionHelper.java
index 6ce25264c..96e3b9c4c 100644
--- 
a/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/ProcessDefinitionHelper.java
+++ 
b/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/ProcessDefinitionHelper.java
@@ -29,6 +29,7 @@ import java.util.stream.Collectors;
 import org.kie.kogito.event.process.NodeDefinition;
 import org.kie.kogito.event.process.ProcessDefinitionDataEvent;
 import org.kie.kogito.event.process.ProcessDefinitionEventBody;
+import org.kie.kogito.index.CommonUtils;
 import org.kie.kogito.index.json.JsonUtils;
 import org.kie.kogito.index.model.Node;
 import org.kie.kogito.index.model.ProcessDefinition;
@@ -62,7 +63,7 @@ public class ProcessDefinitionHelper {
         instance.setEndpoint(doMerge(data.getEndpoint(), 
instance.getEndpoint()));
         instance.setDescription(doMerge(data.getDescription(), 
instance.getDescription()));
         instance.setAnnotations(doMerge(data.getAnnotations(), 
instance.getAnnotations()));
-        instance.setMetadata(doMerge(toStringMap(data.getMetadata()), 
instance.getMetadata()));
+        
instance.setMetadata(CommonUtils.mergeMap(toStringMap(data.getMetadata()), 
instance.getMetadata()));
         instance.setNodes(doMerge(nodeDefinitions(data), instance.getNodes()));
         instance.setSource(doMerge(data.getSource(), instance.getSource()));
         return instance;
diff --git 
a/data-index/data-index-common/src/test/java/org/kie/kogito/index/CommonUtilsTest.java
 
b/data-index/data-index-common/src/test/java/org/kie/kogito/index/CommonUtilsTest.java
new file mode 100644
index 000000000..7092ceafb
--- /dev/null
+++ 
b/data-index/data-index-common/src/test/java/org/kie/kogito/index/CommonUtilsTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.kie.kogito.index;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CommonUtilsTest {
+
+    @Test
+    void testSimpleMergeMap() {
+        Map<String, String> src = Map.of("name", "Javierito", "different", 
"remain");
+        Map<String, String> target = Map.of("name", "Fulanito", "other", 
"remain");
+        assertThat(CommonUtils.mergeMap(src, target)).isEqualTo(Map.of("name", 
"Javierito", "other", "remain", "different", "remain"));
+    }
+
+    @Test
+    void testNullMergeMap() {
+        Map<String, String> src = new HashMap<>();
+        src.put("name", null);
+        src.put("different", "remain");
+        Map<String, String> target = Map.of("name", "Fulanito", "other", 
"remain");
+        assertThat(CommonUtils.mergeMap(src, target)).isEqualTo(Map.of("name", 
"Fulanito", "other", "remain", "different", "remain"));
+    }
+
+    @Test
+    void testComplexMergeMap() {
+        Map<String, String> nestedSrc = Map.of("name", "Javierito", 
"different", "remain");
+        Map<String, String> nestedTarget = Map.of("name", "Fulanito", "other", 
"remain");
+        Map<String, Object> src = Map.of("nested", nestedSrc);
+        Map<String, Object> target = Map.of("nested", nestedTarget);
+        assertThat(CommonUtils.mergeMap(src, 
target)).isEqualTo(Map.of("nested", Map.of("name", "Javierito", "other", 
"remain", "different", "remain")));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to