Make type and type_version mandatory on policies 50/107250/1
authorliamfallon <liam.fallon@est.tech>
Wed, 6 May 2020 15:19:38 +0000 (16:19 +0100)
committerliamfallon <liam.fallon@est.tech>
Wed, 6 May 2020 15:27:30 +0000 (16:27 +0100)
Up until now, the "type" and "type_version" fields defaulted to "NULL"
and "0.0.0" respectively when they were not specified because the
"type_version" field was not always specified on policies in previous
releases. In cases where the "type_version" field was not specified, it
was assumend that a "0.0.0" version of the policy type existed. In
parallel, if the version field was not specified on the policy type, the
version was stored as 0.0.0.

This behaviour is now changed and the "type" and "type_version" fields
are now always mandatory.

Issue-ID: POLICY-2538
Change-Id: I4279adfa86f531205879dbb87986453604624032
Signed-off-by: liamfallon <liam.fallon@est.tech>
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java

index 1f5c76e..a7650c5 100644 (file)
@@ -173,7 +173,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
                     propertyMap.put(entry.getKey(), coder.decode(entry.getValue(), Object.class));
                 } catch (CoderException ce) {
                     String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey()
-                            + ", value=" + entry.getValue();
+                        + ", value=" + entry.getValue();
                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
                 }
             }
@@ -191,13 +191,17 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
         if (toscaPolicy.getType() != null) {
             type.setName(toscaPolicy.getType());
         } else {
-            type.setName(PfKey.NULL_KEY_NAME);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
+                "PolicyType type not specified, the type of the PolicyType for this policy must be specified in "
+                    + "the type field");
         }
 
         if (toscaPolicy.getTypeVersion() != null) {
             type.setVersion(toscaPolicy.getTypeVersion());
         } else {
-            type.setVersion(PfKey.NULL_KEY_VERSION);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
+                "PolicyType version not specified, the version of the PolicyType for this policy must be specified in "
+                    + "the type_version field");
         }
 
         if (toscaPolicy.getProperties() != null) {
@@ -215,7 +219,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
                     properties.put(propertyEntry.getKey(), coder.encode(propertyEntry.getValue()));
                 } catch (CoderException ce) {
                     String errorMessage = "error encoding property JSON value for database: key="
-                            + propertyEntry.getKey() + ", value=" + propertyEntry.getValue();
+                        + propertyEntry.getKey() + ", value=" + propertyEntry.getValue();
                     throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
                 }
             }
@@ -263,12 +267,12 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
 
         if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) {
             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                    "key version is a null version"));
+                "key version is a null version"));
         }
 
         if (type == null || type.isNullKey()) {
             result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                    "type is null or a null key"));
+                "type is null or a null key"));
         } else {
             result = type.validate(result);
         }
@@ -294,10 +298,10 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
         for (Entry<String, String> propertyEntry : properties.entrySet()) {
             if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "policy property key may not be null "));
+                    "policy property key may not be null "));
             } else if (propertyEntry.getValue() == null) {
                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "policy property value may not be null "));
+                    "policy property value may not be null "));
             }
         }
     }
@@ -314,7 +318,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
         for (PfConceptKey target : targets) {
             if (target == null) {
                 result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
-                        "policy target may not be null "));
+                    "policy target may not be null "));
             } else {
                 result = target.validate(result);
             }
index c595254..aeba9bb 100644 (file)
@@ -55,9 +55,13 @@ public class JpaToscaPolicyTest {
         assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey()));
         assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy()));
 
-        ToscaPolicy pol = new ToscaPolicy();
+        final ToscaPolicy pol = new ToscaPolicy();
         pol.setType("type");
-        assertNotNull(new JpaToscaPolicy(pol));
+        assertThatThrownBy(() -> {
+            new JpaToscaPolicy(pol);
+        }).hasMessage(
+            "PolicyType version not specified, the version of the PolicyType for this policy must be specified in the "
+                + "type_version field");
 
         assertThatThrownBy(() -> {
             new JpaToscaPolicy((PfConceptKey) null);
@@ -170,12 +174,12 @@ public class JpaToscaPolicyTest {
             tp.fromAuthorative(null);
         }).hasMessageMatching("toscaPolicy is marked .*on.*ull but is null");
 
-        pol = new ToscaPolicy();
-        pol.setName("policy");
-        pol.setVersion("1.2.3");
-        pol.setType("poltype");
-        pol.setTypeVersion("2.2.3");
-        tp.fromAuthorative(pol);
+        ToscaPolicy pol1 = new ToscaPolicy();
+        pol1.setName("policy");
+        pol1.setVersion("1.2.3");
+        pol1.setType("poltype");
+        pol1.setTypeVersion("2.2.3");
+        tp.fromAuthorative(pol1);
         assertEquals("2.2.3", tp.getType().getVersion());
     }
 }
index f2da23c..a258eed 100644 (file)
@@ -41,6 +41,7 @@ import org.onap.policy.models.dao.DaoParameters;
 import org.onap.policy.models.dao.PfDao;
 import org.onap.policy.models.dao.PfDaoFactory;
 import org.onap.policy.models.dao.impl.DefaultPfDao;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
@@ -372,6 +373,66 @@ public class SimpleToscaProviderTest {
             createdServiceTemplate.getTopologyTemplate().getPolicies());
     }
 
+    @Test
+    public void testPolicyCreateTypeAndVersion() throws Exception {
+        ToscaServiceTemplate toscaServiceTemplate =
+            standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class);
+
+        createPolicyTypes();
+
+        ToscaPolicy toscaPolicy =
+            toscaServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap().values().iterator().next();
+
+        JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate();
+
+        final String originalPolicyType = toscaPolicy.getType();
+        final String originalPolicyTypeVersion = toscaPolicy.getTypeVersion();
+        toscaPolicy.setType(null);
+        toscaPolicy.setTypeVersion(null);
+
+        assertThatThrownBy(() -> {
+            originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
+        }).hasMessage("PolicyType type not specified, the type of the PolicyType for this policy must be "
+            + "specified in the type field");
+
+        toscaPolicy.setType("IDontExist");
+        assertThatThrownBy(() -> {
+            originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
+        }).hasMessage("PolicyType version not specified, the version of the PolicyType for this policy must be "
+            + "specified in the type_version field");
+
+        toscaPolicy.setTypeVersion("hello");
+        assertThatThrownBy(() -> {
+            originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
+        }).hasMessageContaining("value \"hello\", does not match regular expression");
+
+        toscaPolicy.setTypeVersion("99.100.101");
+        originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
+
+        assertThatThrownBy(() -> {
+            new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
+        }).hasMessageContaining("policy type IDontExist:99.100.101 referenced in policy not found");
+
+        toscaPolicy.setType("IDontExist");
+        originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
+
+        toscaPolicy.setType(null);
+
+        assertThatThrownBy(() -> {
+            originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
+        }).hasMessage("PolicyType type not specified, the type of the PolicyType for this policy must be "
+            + "specified in the type field");
+
+        toscaPolicy.setType(originalPolicyType);
+        toscaPolicy.setTypeVersion(originalPolicyTypeVersion);
+
+        originalServiceTemplate.fromAuthorative(toscaServiceTemplate);
+        JpaToscaServiceTemplate createdServiceTemplate =
+            new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
+        assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies(),
+            createdServiceTemplate.getTopologyTemplate().getPolicies());
+    }
+
     @Test
     public void testPolicyUpdate() throws Exception {
         ToscaServiceTemplate toscaServiceTemplate =