Skip to content

Commit

Permalink
Merge pull request #56 from rgdoliveira/sync_main
Browse files Browse the repository at this point in the history
Sync main branch with Apache main branch
  • Loading branch information
rgdoliveira authored Jan 27, 2025
2 parents 9a79ba8 + 497e498 commit 9e79a77
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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;
Expand Down Expand Up @@ -67,6 +69,29 @@ public static TypeDefinitionRegistry loadSchemaDefinitionFile(String fileName) {
}
}

@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static ProcessDefinition merge(ProcessDefinition instance, ProcessDefinit
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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")));
}
}

0 comments on commit 9e79a77

Please sign in to comment.