Fix AutomationCompositionElement copy constructor 97/137697/2
authorFrancescoFioraEst <francesco.fiora@est.tech>
Tue, 16 Apr 2024 10:44:13 +0000 (11:44 +0100)
committerFrancescoFioraEst <francesco.fiora@est.tech>
Tue, 16 Apr 2024 14:21:46 +0000 (15:21 +0100)
Issue-ID: POLICY-4961
Change-Id: I81256e2b0ecb82e7b313ad8b203b1bcf4468dd72
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationCompositionElement.java
models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java
models/src/test/java/org/onap/policy/clamp/models/acm/utils/AcmUtilsTest.java

index 95ee55f..5afa7e0 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2021-2023 Nordix Foundation.
+ * Copyright (C) 2021-2024 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,13 +23,12 @@ package org.onap.policy.clamp.models.acm.concepts;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.UUID;
-import java.util.function.UnaryOperator;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.NonNull;
 import lombok.ToString;
+import org.onap.policy.clamp.models.acm.utils.AcmUtils;
 import org.onap.policy.models.base.PfConceptKey;
-import org.onap.policy.models.base.PfUtils;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
 /**
@@ -77,8 +76,8 @@ public class AutomationCompositionElement {
         this.definition = new ToscaConceptIdentifier(otherElement.definition);
         this.participantId = otherElement.participantId;
         this.description = otherElement.description;
-        this.properties = PfUtils.mapMap(otherElement.properties, UnaryOperator.identity());
-        this.outProperties = PfUtils.mapMap(otherElement.outProperties, UnaryOperator.identity());
+        this.properties = AcmUtils.cloneMap(otherElement.properties);
+        this.outProperties = AcmUtils.cloneMap(otherElement.outProperties);
         this.restarting = otherElement.restarting;
         this.deployState = otherElement.deployState;
         this.lockState = otherElement.lockState;
index 1155bd4..0293bd3 100644 (file)
@@ -52,6 +52,7 @@ import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
+import org.onap.policy.clamp.models.acm.persistence.concepts.StringToMapConverter;
 import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.common.parameters.ObjectValidationResult;
 import org.onap.policy.common.parameters.ValidationResult;
@@ -70,6 +71,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class AcmUtils {
     public static final String ENTRY = "entry ";
+    private static StringToMapConverter MAP_CONVERTER = new StringToMapConverter();
 
     /**
      * Get the Policy information in the service template for the deploy message to participants.
@@ -490,4 +492,9 @@ public final class AcmUtils {
             }
         }
     }
+
+    public static Map<String, Object> cloneMap(Map<String, Object> map) {
+        var str = MAP_CONVERTER.convertToDatabaseColumn(map);
+        return MAP_CONVERTER.convertToEntityAttribute(str);
+    }
 }
index 1561533..a5c93e8 100644 (file)
@@ -24,6 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -363,4 +364,16 @@ class AcmUtilsTest {
         assertNull(subMap.get("myParameterToRemove"));
         assertEquals("I am new", subMap.get("myParameter"));
     }
+
+    @Test
+    void testCopyMap() {
+        Map<String, Object> map = new HashMap<>();
+        Map<String, Object> subMap = new HashMap<>();
+        subMap.put("test", "value");
+        map.put("sub", subMap);
+        var result = AcmUtils.cloneMap(map);
+        var subMap2 = (Map<String, Object>) result.get("sub");
+        subMap2.put("test", "value2");
+        assertNotEquals(subMap.get("test"), subMap2.get("test"));
+    }
 }