Fix Sonar Issues models-tosca-simple
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTypeTest.java
index 7dac268..5cca879 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.models.tosca.simple.concepts;
 
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
 import org.junit.Test;
 import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
-import org.onap.policy.models.base.PfValidationResult;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 
 /**
  * DAO test for ToscaPolicyType.
@@ -46,31 +46,28 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 public class JpaToscaPolicyTypeTest {
+    private static final String A_DESCRIPTION = "A Description";
+    private static final String VERSION_001 = "0.0.1";
 
     @Test
-    public void testPolicyTypePojo() {
+    public void testPolicyTypeNull() {
         assertNotNull(new JpaToscaPolicyType());
         assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
         assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
 
-        try {
-            new JpaToscaPolicyType((PfConceptKey) null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("key is marked @NonNull but is null", exc.getMessage());
-        }
-
-        try {
-            new JpaToscaPolicyType((JpaToscaPolicyType) null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
-        }
-
-        PfConceptKey ptKey = new PfConceptKey("tdt", "0.0.1");
+        assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null))
+                .hasMessageMatching("key is marked .*on.*ull but is null");
+
+        assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null))
+                .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void testPolicyTypePojo() {
+        PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
         JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
 
-        PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", "0.0.1");
+        PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
         tpt.setDerivedFrom(derivedFromKey);
 
         Map<String, String> metadata = new HashMap<>();
@@ -78,17 +75,17 @@ public class JpaToscaPolicyTypeTest {
         tpt.setMetadata(metadata);
         assertEquals(metadata, tpt.getMetadata());
 
-        tpt.setDescription("A Description");
+        tpt.setDescription(A_DESCRIPTION);
 
-        PfConceptKey propTypeKey = new PfConceptKey("propType", "0.0.1");
-        List<JpaToscaProperty> properties = new ArrayList<>();
+        PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
+        Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
         JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
-        properties.add(tp);
+        properties.put(tp.getKey().getLocalName(), tp);
         tpt.setProperties(properties);
         assertEquals(properties, tpt.getProperties());
 
         List<PfConceptKey> targets = new ArrayList<>();
-        PfConceptKey target = new PfConceptKey("target", "0.0.1");
+        PfConceptKey target = new PfConceptKey("target", VERSION_001);
         targets.add(target);
         tpt.setTargets(targets);
         assertEquals(targets, tpt.getTargets());
@@ -103,110 +100,188 @@ public class JpaToscaPolicyTypeTest {
         assertEquals(tpt, tdtClone0);
         assertEquals(0, tpt.compareTo(tdtClone0));
 
-        JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType();
-        tpt.copyTo(tdtClone1);
+        JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType(tpt);
         assertEquals(tpt, tdtClone1);
         assertEquals(0, tpt.compareTo(tdtClone1));
 
         assertEquals(-1, tpt.compareTo(null));
         assertEquals(0, tpt.compareTo(tpt));
-        assertFalse(tpt.compareTo(tpt.getKey()) == 0);
+        assertNotEquals(0, tpt.compareTo(tpt.getKey()));
 
-        PfConceptKey otherDtKey = new PfConceptKey("otherDt", "0.0.1");
+        PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
         JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
 
-        assertFalse(tpt.compareTo(otherDt) == 0);
+        assertNotEquals(0, tpt.compareTo(otherDt));
         otherDt.setKey(ptKey);
-        assertFalse(tpt.compareTo(otherDt) == 0);
+        assertNotEquals(0, tpt.compareTo(otherDt));
         otherDt.setDerivedFrom(derivedFromKey);
-        assertFalse(tpt.compareTo(otherDt) == 0);
+        assertNotEquals(0, tpt.compareTo(otherDt));
         otherDt.setMetadata(metadata);
-        assertFalse(tpt.compareTo(otherDt) == 0);
-        otherDt.setDescription("A Description");
-        assertFalse(tpt.compareTo(otherDt) == 0);
+        assertNotEquals(0, tpt.compareTo(otherDt));
+        otherDt.setDescription(A_DESCRIPTION);
+        assertNotEquals(0, tpt.compareTo(otherDt));
         otherDt.setProperties(properties);
-        assertFalse(tpt.compareTo(otherDt) == 0);
+        assertNotEquals(0, tpt.compareTo(otherDt));
         otherDt.setTargets(targets);
-        assertFalse(tpt.compareTo(otherDt) == 0);
+        assertNotEquals(0, tpt.compareTo(otherDt));
         otherDt.setTriggers(triggers);
         assertEquals(0, tpt.compareTo(otherDt));
 
-        try {
-            tpt.copyTo(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("target is marked @NonNull but is null", exc.getMessage());
-        }
-
-        assertEquals(6, tpt.getKeys().size());
-        assertEquals(1, new JpaToscaPolicyType().getKeys().size());
-
         new JpaToscaPolicyType().clean();
         tpt.clean();
         assertEquals(tdtClone0, tpt);
+    }
 
-        assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
+    @Test
+    public void testPolicyTypeValidation() {
+        JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
+
+        assertEquals(6, tpt.getKeys().size());
+        assertEquals(1, new JpaToscaPolicyType().getKeys().size());
 
-        tpt.getProperties().add(null);
-        assertFalse(tpt.validate(new PfValidationResult()).isValid());
+        assertFalse(new JpaToscaPolicyType().validate("").isValid());
+        assertTrue(tpt.validate("").isValid());
+
+        tpt.getProperties().put(null, null);
+        assertFalse(tpt.validate("").isValid());
         tpt.getProperties().remove(null);
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
+        assertTrue(tpt.validate("").isValid());
 
         tpt.getTargets().add(null);
-        assertFalse(tpt.validate(new PfValidationResult()).isValid());
+        assertFalse(tpt.validate("").isValid());
         tpt.getTargets().remove(null);
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
+        assertTrue(tpt.validate("").isValid());
 
         tpt.getTriggers().add(null);
-        assertFalse(tpt.validate(new PfValidationResult()).isValid());
+        assertFalse(tpt.validate("").isValid());
         tpt.getTriggers().remove(null);
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
+        assertTrue(tpt.validate("").isValid());
+    }
+
+    @Test
+    public void testPolicyTypeValidation2() {
+        JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
 
         tpt.getMetadata().put(null, null);
-        assertFalse(tpt.validate(new PfValidationResult()).isValid());
+        assertFalse(tpt.validate("").isValid());
         tpt.getMetadata().remove(null);
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
+        assertTrue(tpt.validate("").isValid());
 
         tpt.getMetadata().put("nullKey", null);
-        assertFalse(tpt.validate(new PfValidationResult()).isValid());
+        assertFalse(tpt.validate("").isValid());
         tpt.getMetadata().remove("nullKey");
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
+        assertTrue(tpt.validate("").isValid());
 
-        tpt.setDescription("");;
-        assertFalse(tpt.validate(new PfValidationResult()).isValid());
-        tpt.setDescription("A Description");
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
+        tpt.setDescription("");
+
+        assertFalse(tpt.validate("").isValid());
+        tpt.setDescription(A_DESCRIPTION);
+        assertTrue(tpt.validate("").isValid());
 
         tpt.setDerivedFrom(PfConceptKey.getNullKey());
-        assertFalse(tpt.validate(new PfValidationResult()).isValid());
+        assertFalse(tpt.validate("").isValid());
+
+        PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
         tpt.setDerivedFrom(derivedFromKey);
-        assertTrue(tpt.validate(new PfValidationResult()).isValid());
-
-        try {
-            tpt.validate(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
-        }
-
-        try {
-            new JpaToscaEntityType((PfConceptKey) null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("key is marked @NonNull but is null", exc.getMessage());
-        }
-
-        try {
-            new JpaToscaEntityType((JpaToscaEntityType) null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
-        }
-
-        JpaToscaEntityType tet = new JpaToscaEntityType(tpt.getKey());
+        assertTrue(tpt.validate("").isValid());
+    }
+
+    @Test
+    public void testPolicyTypeEntity() {
+        JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
+
+        assertThatThrownBy(() -> tpt.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
+
+        assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
+                .hasMessageMatching("key is marked .*on.*ull but is null");
+
+        assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
+                .isInstanceOf(NullPointerException.class);
+
+        JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
         assertEquals(-1, tet.compareTo(null));
         assertEquals(0, tet.compareTo(tet));
-        assertFalse(tet.compareTo(tet.getKey()) == 0);
+        assertNotEquals(0, tet.compareTo(tet.getKey()));
+
+        assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
+
+        assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));
+    }
+
+    @Test
+    public void testGetReferencedDataTypes() {
+        JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0", "0.0.1"));
+
+        assertTrue(pt0.getReferencedDataTypes().isEmpty());
+
+        pt0.setProperties(new LinkedHashMap<>());
+        assertTrue(pt0.getReferencedDataTypes().isEmpty());
+
+        JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
+        prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
+        assertTrue(prop0.validate("").isValid());
+
+        pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
+        assertTrue(pt0.getReferencedDataTypes().isEmpty());
+
+        JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop1"));
+        prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
+        assertTrue(prop1.validate("").isValid());
+
+        pt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
+        assertEquals(1, pt0.getReferencedDataTypes().size());
+
+        JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop2"));
+        prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
+        assertTrue(prop2.validate("").isValid());
+
+        pt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
+        assertEquals(1, pt0.getReferencedDataTypes().size());
+
+        JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
+        prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
+        prop3.setEntrySchema(new JpaToscaSchemaDefinition());
+        prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
+        assertTrue(prop3.validate("").isValid());
+
+        pt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
+        assertEquals(3, pt0.getReferencedDataTypes().size());
+
+        JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
+        prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
+        prop4.setEntrySchema(new JpaToscaSchemaDefinition());
+        prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
+        assertTrue(prop4.validate("").isValid());
+
+        pt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
+        assertEquals(3, pt0.getReferencedDataTypes().size());
+    }
+
+    private JpaToscaPolicyType setUpJpaToscaPolicyType() {
+        PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
+        JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
+
+        PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
+        tpt.setDerivedFrom(derivedFromKey);
+        // Maps and Lists need to be modifiable
+        Map<String, String> metadata = new HashMap<>(Map.of("key", "value"));
+        tpt.setMetadata(metadata);
+
+        tpt.setDescription(A_DESCRIPTION);
+
+        PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
+        JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
+        Map<String, JpaToscaProperty> properties = new LinkedHashMap<>(Map.of(tp.getKey().getLocalName(), tp));
+        tpt.setProperties(properties);
+
+        PfConceptKey target = new PfConceptKey("target", VERSION_001);
+        List<PfConceptKey> targets = new ArrayList<>(List.of(target));
+        tpt.setTargets(targets);
+
+        JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
+        List<JpaToscaTrigger> triggers = new ArrayList<>(List.of(trigger));
+        tpt.setTriggers(triggers);
+
+        return tpt;
     }
 }