Fix sonar issues for tests in policy-models
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfReferenceKeyTest.java
index ba36b9e..ce1c3e8 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 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.
@@ -21,6 +21,7 @@
 
 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;
@@ -29,8 +30,8 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.lang.reflect.Field;
-
 import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationResult;
 
 public class PfReferenceKeyTest {
 
@@ -41,7 +42,7 @@ public class PfReferenceKeyTest {
     private static final String VERSION001 = "0.0.1";
 
     @Test
-    public void testPfReferenceKey() {
+    public void testPfReferenceKeyNotNull() {
         assertNotNull(new PfReferenceKey());
         assertNotNull(new PfReferenceKey(new PfConceptKey()));
         assertNotNull(new PfReferenceKey(new PfConceptKey(), LOCAL_NAME));
@@ -56,7 +57,10 @@ public class PfReferenceKeyTest {
 
         assertThatThrownBy(() -> new PfReferenceKey(new PfConceptKey(), null))
             .hasMessage("parameter \"localName\" is null");
+    }
 
+    @Test
+    public void testPfReferenceKey() {
         PfReferenceKey testReferenceKey = new PfReferenceKey();
         testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
         assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
@@ -94,26 +98,28 @@ public class PfReferenceKeyTest {
         assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
         assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
 
-        PfValidationResult result = new PfValidationResult();
-        result = testReferenceKey.validate(result);
-        assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult());
+        assertTrue(testReferenceKey.validate("").isValid());
+    }
 
+    @Test
+    public void testMultiplePfReferenceKey() {
+        PfReferenceKey testReferenceKey = setTestReferenceKey();
         testReferenceKey.clean();
 
         PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
         assertEquals("PfReferenceKey(parentKeyName=NPKN, parentKeyVersion=0.0.1, parentLocalName=NPKLN, localName=NLN)",
             clonedReferenceKey.toString());
 
-        assertFalse(testReferenceKey.hashCode() == 0);
+        assertNotEquals(0, testReferenceKey.hashCode());
 
-        assertTrue(testReferenceKey.equals(testReferenceKey));
-        assertTrue(testReferenceKey.equals(clonedReferenceKey));
-        assertFalse(testReferenceKey.equals("Hello"));
-        assertFalse(testReferenceKey.equals(new PfReferenceKey("PKN", VERSION002, "PLN", "LN")));
-        assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION002, "PLN", "LN")));
-        assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, "PLN", "LN")));
-        assertFalse(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN")));
-        assertTrue(testReferenceKey.equals(new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN")));
+        assertEquals(testReferenceKey, (Object) testReferenceKey);
+        assertEquals(testReferenceKey, clonedReferenceKey);
+        assertNotEquals(testReferenceKey, (Object) "Hello");
+        assertNotEquals(testReferenceKey, new PfReferenceKey("PKN", VERSION002, "PLN", "LN"));
+        assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION002, "PLN", "LN"));
+        assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, "PLN", "LN"));
+        assertNotEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN"));
+        assertEquals(testReferenceKey, new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN"));
 
         assertEquals(0, testReferenceKey.compareTo(testReferenceKey));
         assertEquals(0, testReferenceKey.compareTo(clonedReferenceKey));
@@ -124,7 +130,7 @@ public class PfReferenceKeyTest {
         assertNotEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, "NPLN", "LN")));
         assertEquals(0, testReferenceKey.compareTo(new PfReferenceKey("NPKN", VERSION001, NPKLN, "NLN")));
 
-        assertFalse(testReferenceKey.equals(null));
+        assertNotEquals(testReferenceKey, null);
 
         assertThatThrownBy(() -> new PfReferenceKey((PfReferenceKey) null)).isInstanceOf(NullPointerException.class);
 
@@ -140,49 +146,49 @@ public class PfReferenceKeyTest {
         Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
         parentNameField.setAccessible(true);
         parentNameField.set(testReferenceKey, "Parent Name");
-        PfValidationResult validationResult = new PfValidationResult();
-        testReferenceKey.validate(validationResult);
+        ValidationResult validationResult = testReferenceKey.validate("");
         parentNameField.set(testReferenceKey, "ParentName");
         parentNameField.setAccessible(false);
-        assertEquals(
-            "parentKeyName invalid-parameter parentKeyName with value Parent Name "
-                + "does not match regular expression " + PfKey.NAME_REGEXP,
-            validationResult.getMessageList().get(0).getMessage());
+        assertThat(validationResult.getResult()).contains("\"parentKeyName\"")
+                        .contains("does not match regular expression " + PfKey.NAME_REGEXP);
 
         Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
         parentVersionField.setAccessible(true);
         parentVersionField.set(testReferenceKey, "Parent Version");
-        PfValidationResult validationResult2 = new PfValidationResult();
-        testReferenceKey.validate(validationResult2);
+        ValidationResult validationResult2 = testReferenceKey.validate("");
         parentVersionField.set(testReferenceKey, VERSION001);
         parentVersionField.setAccessible(false);
-        assertEquals(
-            "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
-                + "does not match regular expression " + PfKey.VERSION_REGEXP,
-            validationResult2.getMessageList().get(0).getMessage());
+        assertThat(validationResult2.getResult()).contains("\"parentKeyVersion\"")
+            .contains("does not match regular expression " + PfKey.VERSION_REGEXP);
 
         Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
         parentLocalNameField.setAccessible(true);
         parentLocalNameField.set(testReferenceKey, "Parent Local Name");
-        PfValidationResult validationResult3 = new PfValidationResult();
-        testReferenceKey.validate(validationResult3);
+        ValidationResult validationResult3 = testReferenceKey.validate("");
         parentLocalNameField.set(testReferenceKey, PARENT_LOCAL_NAME);
         parentLocalNameField.setAccessible(false);
-        assertEquals(
-            "parentLocalName invalid-parameter parentLocalName with value "
-                + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
-            validationResult3.getMessageList().get(0).getMessage());
+        assertThat(validationResult3.getResult()).contains("\"parentLocalName\"")
+            .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");
 
         Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
         localNameField.setAccessible(true);
         localNameField.set(testReferenceKey, "Local Name");
-        PfValidationResult validationResult4 = new PfValidationResult();
-        testReferenceKey.validate(validationResult4);
+        ValidationResult validationResult4 = testReferenceKey.validate("");
         localNameField.set(testReferenceKey, LOCAL_NAME);
         localNameField.setAccessible(false);
-        assertEquals(
-            "localName invalid-parameter localName with value Local Name "
-                + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
-            validationResult4.getMessageList().get(0).getMessage());
+        assertThat(validationResult4.getResult()).contains("\"localName\"")
+        .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");
+    }
+
+    private PfReferenceKey setTestReferenceKey() {
+        PfReferenceKey testReferenceKey = new PfReferenceKey();
+        testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
+        testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
+        testReferenceKey.setParentKeyName("NPKN");
+        testReferenceKey.setParentKeyVersion(VERSION001);
+        testReferenceKey.setParentLocalName(NPKLN);
+        testReferenceKey.setLocalName("NLN");
+
+        return testReferenceKey;
     }
 }