Sonar Fixes policy/models, removing model-yaml
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfKeyUseTest.java
index 6849430..e4933b2 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 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.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 org.junit.Test;
 import org.onap.policy.models.base.PfKey.Compatibility;
@@ -33,19 +34,16 @@ import org.onap.policy.models.base.testconcepts.DummyPfConceptKeySub;
 
 public class PfKeyUseTest {
 
-    @SuppressWarnings("unlikely-arg-type")
+    private static final String OTHER_KEY_IS_NULL = "^otherKey is marked .*on.*ull but is null$";
+
     @Test
     public void testKeyUse() {
         assertNotNull(new PfKeyUse());
         assertNotNull(new PfKeyUse(new PfConceptKey()));
         assertNotNull(new PfKeyUse(new PfReferenceKey()));
 
-        try {
-            new PfKeyUse((PfKeyUse) null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
-        }
+        assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null))
+            .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
 
         PfConceptKey key = new PfConceptKey("Key", "0.0.1");
         PfKeyUse keyUse = new PfKeyUse();
@@ -57,12 +55,7 @@ public class PfKeyUseTest {
 
         assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key));
 
-        try {
-            key.getCompatibility(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
-        }
+        assertThatThrownBy(() -> key.getCompatibility(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
 
         assertTrue(keyUse.isCompatible(key));
 
@@ -78,12 +71,12 @@ public class PfKeyUseTest {
         PfKeyUse clonedKeyUse = new PfKeyUse(keyUse);
         assertEquals("PfKeyUse(usedKey=PfConceptKey(name=Key, version=0.0.1))", clonedKeyUse.toString());
 
-        assertFalse(keyUse.hashCode() == 0);
+        assertNotEquals(0, keyUse.hashCode());
 
-        assertTrue(keyUse.equals(keyUse));
-        assertTrue(keyUse.equals(clonedKeyUse));
-        assertFalse(keyUse.equals("Hello"));
-        assertTrue(keyUse.equals(new PfKeyUse(key)));
+        assertEquals(keyUse, keyUse);
+        assertEquals(keyUse, clonedKeyUse);
+        assertNotEquals(keyUse, (Object) "Hello");
+        assertEquals(keyUse, new PfKeyUse(key));
 
         assertEquals(0, keyUse.compareTo(keyUse));
         assertEquals(0, keyUse.compareTo(clonedKeyUse));
@@ -94,58 +87,20 @@ public class PfKeyUseTest {
         PfValidationResult resultNull = new PfValidationResult();
         assertEquals(false, keyUseNull.validate(resultNull).isValid());
 
-        try {
-            keyUse.setKey(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("key is marked @NonNull but is null", exc.getMessage());
-        }
-
-        try {
-            keyUse.getCompatibility(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
-        }
-
-        try {
-            keyUse.isCompatible(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
-        }
-
-        try {
-            keyUse.validate(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("result is marked @NonNull but is null", exc.getMessage());
-        }
+        assertThatThrownBy(() -> keyUse.setKey(null)).hasMessageMatching("^key is marked .*on.*ull but is null$");
+
+        assertThatThrownBy(() -> keyUse.getCompatibility(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
+
+        assertThatThrownBy(() -> keyUse.isCompatible(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
+
+        assertThatThrownBy(() -> keyUse.validate(null)).hasMessageMatching("^result is marked .*on.*ull but is null$");
 
         PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey()));
-        PfKeyUse targetKeyUse = new PfKeyUse(key);
-
-        try {
-            keyUse.copyTo(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("target is marked @NonNull but is null", exc.getMessage());
-        }
-
-        try {
-            testKeyUse.copyTo(targetKeyUse);
-            keyUse.isCompatible(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("error copying concept key: Some error message", exc.getMessage());
-        }
-
-        try {
-            keyUse.isNewerThan(null);
-            fail("test should throw an exception");
-        } catch (Exception exc) {
-            assertEquals("otherKey is marked @NonNull but is null", exc.getMessage());
-        }
+        assertEquals(testKeyUse, new PfKeyUse(testKeyUse));
+
+        assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null)).isInstanceOf(NullPointerException.class);
+
+        assertThatThrownBy(() -> keyUse.isNewerThan(null)).hasMessageMatching(OTHER_KEY_IS_NULL);
 
         assertEquals(false, testKeyUse.isNewerThan(keyUse));
         assertEquals(false, testKeyUse.isNewerThan(testKeyUse));