Convert models to JUnit 5
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / utils / ToscaUtilsTest.java
index a5d145e..9e65590 100644 (file)
@@ -1,6 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 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.
 
 package org.onap.policy.models.tosca.utils;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
 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.assertTrue;
+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.assertTrue;
 
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
-import org.onap.policy.models.base.PfValidationResult;
+import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
@@ -43,10 +47,10 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
  *
  * @author Liam Fallon (liam.fallon@est.tech)
  */
-public class ToscaUtilsTest {
+class ToscaUtilsTest {
 
     @Test
-    public void testAssertDataTypes() {
+    void testAssertDataTypes() {
         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
 
         assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
@@ -76,7 +80,7 @@ public class ToscaUtilsTest {
     }
 
     @Test
-    public void testAssertPolicyTypes() {
+    void testAssertPolicyTypes() {
         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
 
         assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
@@ -105,7 +109,7 @@ public class ToscaUtilsTest {
     }
 
     @Test
-    public void testAssertPolicies() {
+    void testAssertPolicies() {
         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
 
         assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
@@ -143,13 +147,13 @@ public class ToscaUtilsTest {
     }
 
     @Test
-    public void testGetentityTypeAncestors() {
+    void testGetentityTypeAncestors() {
         assertThatThrownBy(() -> {
             ToscaUtils.getEntityTypeAncestors(null, null, null);
         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
 
         assertThatThrownBy(() -> {
-            ToscaUtils.getEntityTypeAncestors(null, null, new PfValidationResult());
+            ToscaUtils.getEntityTypeAncestors(null, null, new BeanValidationResult("", null));
         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
 
         assertThatThrownBy(() -> {
@@ -157,7 +161,7 @@ public class ToscaUtilsTest {
         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
 
         assertThatThrownBy(() -> {
-            ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new PfValidationResult());
+            ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new BeanValidationResult("", null));
         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
 
         assertThatThrownBy(() -> {
@@ -165,93 +169,111 @@ public class ToscaUtilsTest {
         }).hasMessageMatching("entityType is marked .*on.*ull but is null");
 
         assertThatThrownBy(() -> {
-            ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new PfValidationResult());
+            ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new BeanValidationResult("", null));
         }).hasMessageMatching("entityType is marked .*on.*ull but is null");
 
         assertThatThrownBy(() -> {
             ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), new JpaToscaDataType(), null);
         }).hasMessageMatching("result is marked .*on.*ull but is null");
+    }
+
+    @Test
+    void testGetentityTypeAncestorsDataType() {
 
         JpaToscaDataTypes dataTypes = new JpaToscaDataTypes();
         JpaToscaDataType dt0 = new JpaToscaDataType();
         dt0.setKey(new PfConceptKey("dt0", "0.0.1"));
         dt0.setDescription("dt0 description");
-        PfValidationResult result = new PfValidationResult();
+        BeanValidationResult result = new BeanValidationResult("", null);
 
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
+        assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result)).isEmpty();
 
         dataTypes.getConceptMap().put(dt0.getKey(), dt0);
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertTrue(result.isValid());
+        checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
 
         dt0.setDerivedFrom(null);
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertTrue(result.isValid());
+        checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
 
         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertTrue(result.isValid());
+        checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
 
         dt0.setDerivedFrom(new PfConceptKey("some.thing.Else", PfKey.NULL_KEY_VERSION));
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
+        assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result)).isEmpty();
         assertFalse(result.isValid());
-        assertTrue(result.toString().contains("parent some.thing.Else:0.0.0 of entity not found"));
+        assertThat(result.getResult()).contains("parent").contains("some.thing.Else:0.0.0")
+                        .contains(Validated.NOT_FOUND);
 
-        result = new PfValidationResult();
+        result = new BeanValidationResult("", null);
         dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
 
         JpaToscaDataType dt1 = new JpaToscaDataType();
         dt1.setKey(new PfConceptKey("dt1", "0.0.1"));
         dt1.setDescription("dt1 description");
         dataTypes.getConceptMap().put(dt1.getKey(), dt1);
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
-        assertTrue(result.isValid());
+        checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
+        checkSingleEmptyEntityTypeAncestor(dataTypes, dt1, result);
 
         dt1.setDerivedFrom(dt0.getKey());
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
-        assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
-        assertTrue(result.isValid());
+        checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
 
         JpaToscaDataType dt2 = new JpaToscaDataType();
         dt2.setKey(new PfConceptKey("dt2", "0.0.1"));
         dt2.setDescription("dt2 description");
         dataTypes.getConceptMap().put(dt2.getKey(), dt2);
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
-        assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
-        assertTrue(result.isValid());
+        checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
 
         dt2.setDerivedFrom(dt1.getKey());
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
-        assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
-        assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
+        checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt1, result, 1);
+        checkMultipleEmptyEntityTypeAncestors(dataTypes, dt0, dt2, result, 2);
+
+        dt0.setDerivedFrom(dt0.getKey());
+        assertThatThrownBy(() -> {
+            ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, new BeanValidationResult("", null));
+        }).hasMessageContaining("entity type").hasMessageContaining("ancestor of itself");
+
+        dt0.setDerivedFrom(null);
         assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
-        assertTrue(result.isValid());
 
         dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION));
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty());
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty());
-        assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
-        assertEquals(0, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size());
-        assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size());
-        assertTrue(result.isValid());
+        checkSingleEmptyEntityTypeAncestor(dataTypes, dt0, result);
+        checkMultipleEmptyEntityTypeAncestors(dataTypes, dt1, dt2, result, 1, 0);
 
         dataTypes.getConceptMap().remove(dt1.getKey());
-        assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty());
+        assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result)).isEmpty();
         assertFalse(result.isValid());
-        assertTrue(result.toString().contains("parent dt1:0.0.1 of entity not found"));
+        assertThat(result.getResult()).contains("parent").contains("dt1:0.0.1").contains(Validated.NOT_FOUND);
+    }
+
+    private void checkSingleEmptyEntityTypeAncestor(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
+            BeanValidationResult result) {
+        assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
+        assertTrue(result.isValid());
+    }
+
+    private void checkMultipleEmptyEntityTypeAncestors(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
+            JpaToscaDataType notemptydt, BeanValidationResult result, int size1) {
+        assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
+        assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).isEmpty());
+        assertEquals(size1, ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).size());
+        assertTrue(result.isValid());
+    }
+
+    private void checkMultipleEmptyEntityTypeAncestors(JpaToscaDataTypes dataTypes, JpaToscaDataType emptydt,
+            JpaToscaDataType notemptydt, BeanValidationResult result, int size1, int size2) {
+        assertThat(ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result)).isEmpty();
+        assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).isEmpty());
+        assertEquals(size1, ToscaUtils.getEntityTypeAncestors(dataTypes, notemptydt, result).size());
+        assertEquals(size2, ToscaUtils.getEntityTypeAncestors(dataTypes, emptydt, result).size());
+        assertTrue(result.isValid());
     }
 
     @Test
-    public void testGetPredefinedDataTypes() {
+    void testGetPredefinedDataTypes() {
         assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
     }
 
     @Test
-    public void testgetEntityTree() {
+    void testgetEntityTree() {
         assertThatThrownBy(() -> {
             ToscaUtils.getEntityTree(null, null, null);
         }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
@@ -266,7 +288,7 @@ public class ToscaUtilsTest {
         filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
         ToscaUtils.getEntityTree(filteredDataTypes, "IDontExist", "0.0.0");
         assertNotEquals(dataTypes, filteredDataTypes);
-        assertTrue(filteredDataTypes.getConceptMap().isEmpty());
+        assertThat(filteredDataTypes.getConceptMap()).isEmpty();
 
         filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
         ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey().getName(), dt0.getKey().getVersion());
@@ -361,6 +383,7 @@ public class ToscaUtilsTest {
         assertThatThrownBy(() -> {
             final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
             ToscaUtils.getEntityTree(badDataTypes, dt9.getKey().getName(), dt9.getKey().getVersion());
-        }).hasMessageContaining("parent i.dont.Exist:0.0.0 of entity not found");
+        }).hasMessageContaining("parent").hasMessageContaining("i.dont.Exist:0.0.0")
+                        .hasMessageContaining(Validated.NOT_FOUND);
     }
 }