Java 17 Upgrade
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfConceptContainerTest.java
index a003636..c526b23 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
- *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ *  Copyright (C) 2019-2021, 2023 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.base;
 
+import static org.assertj.core.api.Assertions.assertThat;
 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;
 
@@ -33,7 +35,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
-
 import org.junit.Test;
 import org.onap.policy.models.base.testconcepts.DummyAuthorativeConcept;
 import org.onap.policy.models.base.testconcepts.DummyBadPfConceptContainer;
@@ -41,11 +42,6 @@ import org.onap.policy.models.base.testconcepts.DummyPfConcept;
 import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
 import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;
 
-/**
- * Test the PfCOnceptCOntainer class.
- *
- * @author Liam Fallon (liam.fallon@est.tech)
- */
 public class PfConceptContainerTest {
 
     private static final String NAME0 = "name0";
@@ -69,20 +65,24 @@ public class PfConceptContainerTest {
         container = new DummyPfConceptContainer(new PfConceptKey());
         assertNotNull(container);
 
-        container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
+        container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<>());
         assertNotNull(container);
 
-        assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessageMatching(KEY_IS_NULL);
+        assertThatThrownBy(() -> new PfConceptContainer(null, null)).hasMessageMatching(KEY_IS_NULL);
 
-        assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null))
+        assertThatThrownBy(() -> new DummyPfConceptContainer(null, null))
             .hasMessageMatching(KEY_IS_NULL);
 
         assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
             .hasMessageMatching("^conceptMap is marked .*on.*ull but is null$");
 
-        assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
+        assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<>()))
             .hasMessageMatching(KEY_IS_NULL);
+    }
 
+    @Test
+    public void testNamedConceptContainer() {
+        DummyPfConceptContainer container = new DummyPfConceptContainer();
         container.getKey().setName(DUMMY_VALUE);
         DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
         assertNotNull(clonedContainer);
@@ -111,55 +111,72 @@ public class PfConceptContainerTest {
         container.clean();
         assertEquals(clonedContainer, container);
 
-        PfValidationResult result = new PfValidationResult();
-        result = container.validate(result);
-        assertTrue(result.isOk());
+        assertThat(container.validate("").getResult()).isNull();
 
         assertEquals(0, container.compareTo(clonedContainer));
 
         assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
             .isInstanceOf(NullPointerException.class);
+        assertThatThrownBy(() -> container.compareTo(null)).isInstanceOf(NullPointerException.class);
 
-        assertFalse(container.compareTo(null) == 0);
         assertEquals(0, container.compareTo(container));
-        assertFalse(container.compareTo(conceptKey) == 0);
+        assertNotEquals(0, container.compareTo(conceptKey));
 
         DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
         testContainer.getKey().setVersion("0.0.2");
-        assertFalse(container.compareTo(testContainer) == 0);
+        assertNotEquals(0, container.compareTo(testContainer));
         testContainer.getKey().setVersion(container.getKey().getVersion());
         assertEquals(0, container.compareTo(testContainer));
 
         PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0_0_1);
         testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
-        assertFalse(container.compareTo(testContainer) == 0);
+        assertNotEquals(0, container.compareTo(testContainer));
+    }
+
+    @Test
+    public void testValidationContainer() {
+        DummyPfConceptContainer container = new DummyPfConceptContainer();
+        PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
+        Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
+        conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
+        container.setConceptMap(conceptMap);
 
         final DummyPfConceptContainer container3 = container;
         assertThatThrownBy(() -> container3.validate(null))
-            .hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
+            .hasMessageMatching("^fieldName is marked .*on.*ull but is null$");
 
         DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
-        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
+        assertFalse(validateContainer.validate("").isValid());
         validateContainer.setKey(new PfConceptKey("VCKey", VERSION0_0_1));
-        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
+        assertTrue(validateContainer.validate("").isValid());
 
+        PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0_0_1);
         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
-        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
+        assertTrue(validateContainer.validate("").isValid());
 
         validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
-        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
+        assertFalse(validateContainer.validate("").isValid());
         validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
-        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
+        assertTrue(validateContainer.validate("").isValid());
 
         validateContainer.getConceptMap().put(testConceptKey, null);
-        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
+        assertFalse(validateContainer.validate("").isValid());
         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
-        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
+        assertTrue(validateContainer.validate("").isValid());
 
         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
-        assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
+        assertFalse(validateContainer.validate("").isValid());
         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
-        assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
+        assertTrue(validateContainer.validate("").isValid());
+    }
+
+    @Test
+    public void testSetContainer() {
+        DummyPfConceptContainer container = new DummyPfConceptContainer();
+        PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
+        Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
+        conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
+        container.setConceptMap(conceptMap);
 
         assertEquals(conceptKey, container.get(conceptKey).getKey());
         assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
@@ -171,9 +188,28 @@ public class PfConceptContainerTest {
         returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
         assertEquals(conceptKey, returnSet.iterator().next().getKey());
 
+        returnSet = container.getAllNamesAndVersions(conceptKey.getName(), conceptKey.getVersion());
+        assertEquals(conceptKey, returnSet.iterator().next().getKey());
+        returnSet = container.getAllNamesAndVersions(null, conceptKey.getVersion());
+        assertEquals(conceptKey, returnSet.iterator().next().getKey());
+        returnSet = container.getAllNamesAndVersions(null, null);
+        assertEquals(conceptKey, returnSet.iterator().next().getKey());
+        returnSet = container.getAllNamesAndVersions(conceptKey.getName(), null);
+        assertEquals(conceptKey, returnSet.iterator().next().getKey());
+        returnSet = container.getAllNamesAndVersions(conceptKey.getName(), "0.0.0");
+        assertEquals(conceptKey, returnSet.iterator().next().getKey());
+        returnSet = container.getAllNamesAndVersions("IDontExist", "1.0.0");
+        assertTrue(returnSet.isEmpty());
+
         container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
+
+        PfConceptKey anotherKey = new PfConceptKey(conceptKey);
+        assertEquals(conceptKey, container.get(anotherKey).getKey());
+        anotherKey.setVersion(PfKey.NULL_KEY_VERSION);
+        assertEquals(conceptKey, container.get(anotherKey).getKey());
     }
 
+
     @Test
     public void testAuthorative() {
         Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
@@ -235,7 +271,7 @@ public class PfConceptContainerTest {
     }
 
     @Test(expected = NullPointerException.class)
-    public void testnullKey() {
+    public void testNullKey() {
         PfConceptKey nullKey = null;
         new DummyPfConceptContainer(nullKey);
     }