Merge "GRPC Client impl to send process message to CDS blueprint-processor endpoint"
authorJorge Hernandez <jorge.hernandez-herrero@att.com>
Wed, 12 Jun 2019 18:28:59 +0000 (18:28 +0000)
committerGerrit Code Review <gerrit@onap.org>
Wed, 12 Jun 2019 18:28:59 +0000 (18:28 +0000)
models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java
models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java

index 1c414ee..a664010 100644 (file)
@@ -115,7 +115,7 @@ public class LegacyGuardPolicyMapper
 
             final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
             legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
-            legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
+            legacyGuardPolicyOutput.setVersion(toscaPolicy.getKey().getVersion());
 
             if (toscaPolicy.getMetadata() == null) {
                 String errorMessage = "no metadata defined on TOSCA policy";
@@ -123,14 +123,11 @@ public class LegacyGuardPolicyMapper
                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
             }
 
-            final Map<String, Object> metadata = new LinkedHashMap<>();
-            for (Entry<String, String> metadataEntry : toscaPolicy.getMetadata().entrySet()) {
-                if (POLICY_VERSION.equals(metadataEntry.getKey())) {
-                    metadata.put(POLICY_VERSION, Integer.parseInt(metadataEntry.getValue()));
-                } else {
-                    metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
-                }
-            }
+            final Map<String, Object> metadata = new LinkedHashMap<>(toscaPolicy.getMetadata());
+
+            // if version exists, convert it to int
+            metadata.computeIfPresent(POLICY_VERSION, (key,val) -> Integer.parseInt(val.toString()));
+
             legacyGuardPolicyOutput.setMetadata(metadata);
 
             if (toscaPolicy.getProperties() == null) {
index f2fb53d..6d32c6d 100644 (file)
 package org.onap.policy.models.tosca.legacy.mapping;
 
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
 
 import java.util.LinkedHashMap;
-
+import java.util.Map;
 import org.junit.Test;
 import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
@@ -44,7 +46,7 @@ public class LegacyGuardPolicyMapperTest {
         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
 
-        JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "0.0.1"));
+        JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "2.0.0"));
         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy.getKey(), policy);
 
         assertThatThrownBy(() -> {
@@ -55,5 +57,11 @@ public class LegacyGuardPolicyMapperTest {
         assertThatThrownBy(() -> {
             new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
         }).hasMessageContaining("no properties defined on TOSCA policy");
+
+        policy.setProperties(new LinkedHashMap<>());
+        Map<String, LegacyGuardPolicyOutput> guardPolicyMap =
+                new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
+        LegacyGuardPolicyOutput guardPolicy = guardPolicyMap.values().iterator().next();
+        assertEquals("2.0.0", guardPolicy.getVersion());
     }
 }