Default should be an object on TOSCA properties 12/113112/3
authorliamfallon <liam.fallon@est.tech>
Thu, 24 Sep 2020 15:12:50 +0000 (16:12 +0100)
committerliamfallon <liam.fallon@est.tech>
Thu, 15 Oct 2020 10:00:40 +0000 (11:00 +0100)
Property default value is serialized to the database as Yaml and
deserialized back into an object on a DB read.

Issue-ID: POLICY-2844
Change-Id: I69f2843a93cd5f831359a0965223298b769498d9
Signed-off-by: liamfallon <liam.fallon@est.tech>
models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java
models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaProperty.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java
models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPropertyTest.java [new file with mode: 0644]

index e588a59..b242bff 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Model
  * ================================================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -88,7 +88,7 @@ public class ToscaEntity implements PfNameVersion {
      *
      * @param listOfMapsOfEntities the incoming list of maps of entities
      * @return The entities on a regular map
-     * @throws PfModelException on duplicate entity entries
+     * @throws PfModelRuntimeException on duplicate entity entries
      */
     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityListMapAsMap(
             List<Map<String, T>> listOfMapsOfEntities) {
@@ -114,7 +114,7 @@ public class ToscaEntity implements PfNameVersion {
      *
      * @param mapOfEntities the incoming list of maps of entities
      * @return The entities on a regular map
-     * @throws PfModelException on duplicate entity entries
+     * @throws PfModelRuntimeException on duplicate entity entries
      */
     public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityMapAsMap(Map<String, T> mapOfEntities) {
         // Declare the return map
index fd8a86a..59fbc8b 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Policy Model
  * ================================================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ public class ToscaProperty {
 
     @ApiModelProperty(name = "default")
     @SerializedName("default")
-    private String defaultValue;
+    private Object defaultValue;
 
     private boolean required = false;
 
index 25d2266..60e2fa0 100644 (file)
@@ -35,14 +35,19 @@ import javax.persistence.Entity;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
 import javax.persistence.Table;
+import javax.ws.rs.core.Response;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.YamlJsonTranslator;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
+import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.base.PfReferenceKey;
 import org.onap.policy.models.base.PfUtils;
 import org.onap.policy.models.base.PfValidationMessage;
@@ -112,7 +117,7 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
     /**
      * The Key Constructor creates a {@link JpaToscaProperty} object with the given concept key.
      *
-     * @param key the key
+     * @param key  the key
      * @param type the key of the property type
      */
     public JpaToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
@@ -159,9 +164,12 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
 
         toscaProperty.setDescription(description);
         toscaProperty.setRequired(required);
-        toscaProperty.setDefaultValue(defaultValue);
         toscaProperty.setStatus(status);
 
+        if (defaultValue != null) {
+            toscaProperty.setDefaultValue(new YamlJsonTranslator().fromYaml(defaultValue, Object.class));
+        }
+
         if (constraints != null) {
             List<ToscaConstraint> toscaConstraints = new ArrayList<>();
 
@@ -196,9 +204,14 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
 
         description = toscaProperty.getDescription();
         required = toscaProperty.isRequired();
-        defaultValue = toscaProperty.getDefaultValue();
         status = toscaProperty.getStatus();
 
+        if (toscaProperty.getDefaultValue() != null) {
+            defaultValue = new YamlJsonTranslator().toYaml(toscaProperty.getDefaultValue()).trim();
+        } else {
+            defaultValue = null;
+        }
+
         if (toscaProperty.getConstraints() != null) {
             constraints = new ArrayList<>();
 
@@ -262,14 +275,14 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
 
         if (key.isNullKey()) {
             result.addValidationMessage(
-                    new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
+                new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
         }
 
         result = key.validate(result);
 
         if (type == null || type.isNullKey()) {
             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "property type may not be null"));
+                "property type may not be null"));
         }
 
         return validateFields(result);
@@ -286,19 +299,19 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr
 
         if (description != null && description.trim().length() == 0) {
             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "property description may not be blank"));
+                "property description may not be blank"));
         }
 
         if (defaultValue != null && defaultValue.trim().length() == 0) {
             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                    "property default value may not be null"));
+                "property default value may not be null"));
         }
 
         if (constraints != null) {
             for (JpaToscaConstraint constraint : constraints) {
                 if (constraint == null) {
                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
-                            "property constraint may not be null "));
+                        "property constraint may not be null "));
                 }
             }
         }
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPropertyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPropertyTest.java
new file mode 100644 (file)
index 0000000..6aa2e16
--- /dev/null
@@ -0,0 +1,57 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2019-2020 Nordix Foundation.
+ * ================================================================================
+ * Licensed 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.tosca.authorative.concepts;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
+
+public class ToscaPropertyTest {
+    @Test
+    public void testToscaPropertyDefaultValue() {
+        ToscaProperty property = new ToscaProperty();
+        property.setName("test");
+        property.setType("testType");
+
+        JpaToscaProperty jpaProperty = new JpaToscaProperty(property);
+        assertNull(jpaProperty.getDefaultValue());
+        ToscaProperty outProperty = jpaProperty.toAuthorative();
+        assertNull(outProperty.getDefaultValue());
+
+        List<String> testList = new ArrayList<>();
+        property.setDefaultValue(testList);
+        jpaProperty = new JpaToscaProperty(property);
+        assertEquals("[]", jpaProperty.getDefaultValue());
+        outProperty = jpaProperty.toAuthorative();
+        assertEquals("[]", outProperty.getDefaultValue().toString());
+
+        testList.add("Foo");
+        testList.add("Bar");
+        jpaProperty = new JpaToscaProperty(property);
+        assertEquals("- Foo\n- Bar", jpaProperty.getDefaultValue());
+        outProperty = jpaProperty.toAuthorative();
+        assertEquals("[Foo, Bar]", outProperty.getDefaultValue().toString());
+    }
+}