Allow semantic versioning in all templates in models
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfKeyImplTest.java
index 8b94a48..aefd389 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  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");
 
 package org.onap.policy.models.base;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
 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;
 
@@ -34,7 +34,10 @@ import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
+import org.junit.BeforeClass;
 import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationResult;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.models.base.PfKey.Compatibility;
 import org.onap.policy.models.base.testconcepts.DummyPfKey;
 
@@ -45,85 +48,124 @@ public class PfKeyImplTest {
     private static final String VERSION123 = "1.2.3";
     private static final String VERSION100 = "1.0.0";
     private static final String VERSION001 = "0.0.1";
-
-    @Test
-    public void testConceptKey() {
-        assertThatIllegalArgumentException().isThrownBy(() -> new MyKey("some bad key id"))
-            .withMessage("parameter \"id\": value \"some bad key id\", " + "does not match regular expression \""
-                + PfKey.KEY_ID_REGEXP + "\"");
-
-        assertThatThrownBy(() -> new MyKey((MyKey) null))
-            .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
-
-        MyKey someKey0 = new MyKey();
-        assertTrue(someKey0.isNullKey());
-        assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey0);
-
-        MyKey someKey1 = new MyKey("name", VERSION001);
-        MyKey someKey2 = new MyKey(someKey1);
-        MyKey someKey3 = new MyKey(someKey1.getId());
-        assertEquals(someKey1, someKey2);
-        assertEquals(someKey1, someKey3);
-        assertFalse(someKey1.isNullKey());
-        assertFalse(someKey1.isNullVersion());
-
-        assertEquals(someKey2, someKey1.getKey());
-        assertEquals(1, someKey1.getKeys().size());
+    private static final String NAME = "name";
+    private static MyKey someKey;
+    private static MyKey someKey0;
+    private static MyKey someKey1;
+    private static MyKey someKey2;
+    private static MyKey someKey3;
+    private static MyKey someKey4;
+    private static MyKey someKey4a;
+    private static MyKey someKey5;
+    private static MyKey someKey6;
+
+    private static final MyKey buildKey1 = new MyKey(NAME, "0.0.3+1");
+    private static final MyKey buildKey2 = new MyKey(NAME, "0.1.0-1");
+    private static final MyKey buildKey3 = new MyKey(NAME, "3.0.0-SNAPSHOT");
+    private static final MyKey buildKey4 = new MyKey(NAME, "1.0.0-rc.1");
+
+    /**
+     * Sets data in Keys for the tests.
+     */
+    @BeforeClass
+    public static void setUp() {
+        someKey = new MyKey();
+
+        someKey0 = new MyKey();
+        someKey1 = new MyKey(NAME, VERSION001);
+        someKey2 = new MyKey(someKey1);
+        someKey3 = new MyKey(someKey1.getId());
 
         someKey0.setName("zero");
         someKey0.setVersion("0.0.2");
-
         someKey3.setVersion("0.0.2");
 
-        MyKey someKey4 = new MyKey(someKey1);
+        someKey4 = new MyKey(someKey1);
         someKey4.setVersion("0.1.2");
 
-        MyKey someKey4a = new MyKey(someKey1);
+        someKey4a = new MyKey(someKey1);
         someKey4a.setVersion("0.0.0");
 
-        MyKey someKey5 = new MyKey(someKey1);
+        someKey5 = new MyKey(someKey1);
         someKey5.setVersion("1.2.2");
 
-        MyKey someKey6 = new MyKey(someKey1);
+        someKey6 = new MyKey(someKey1);
         someKey6.setVersion("3.0.0");
+    }
+
+    @Test
+    public void testConceptKey() {
+        assertThatIllegalArgumentException().isThrownBy(() -> new MyKey("some bad key id"))
+            .withMessage("parameter \"id\": value \"some bad key id\", " + "does not match regular expression \""
+                + PfKey.KEY_ID_REGEXP + "\"");
 
+        assertThatThrownBy(() -> new MyKey((MyKey) null))
+            .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
+
+        assertTrue(someKey.isNullKey());
+        assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey);
+
+        MyKey someKey11 = new MyKey(NAME, VERSION001);
+        MyKey someKey22 = new MyKey(someKey11);
+        MyKey someKey33 = new MyKey(someKey11.getId());
+        assertEquals(someKey11, someKey22);
+        assertEquals(someKey11, someKey33);
+        assertFalse(someKey11.isNullKey());
+        assertFalse(someKey11.isNullVersion());
+
+        assertEquals(someKey22, someKey11.getKey());
+        assertEquals(1, someKey11.getKeys().size());
+    }
+
+    @Test
+    public void testCompatibilityConceptKey() {
         assertEquals("name:0.1.2", someKey4.getId());
 
         assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class)
             .hasMessageMatching("^otherKey is marked .*on.*ull but is null$");
 
         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey()));
+        assertEquals(Compatibility.DIFFERENT, buildKey1.getCompatibility(new DummyPfKey()));
         assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
         assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
+        assertEquals(Compatibility.IDENTICAL, buildKey1.getCompatibility(new MyKey(buildKey1)));
         assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
+        assertEquals(Compatibility.PATCH, buildKey1.getCompatibility(someKey1));
         assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
+        assertEquals(Compatibility.MINOR, buildKey2.getCompatibility(buildKey1));
         assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1));
         assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a));
         assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
         assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));
+        assertEquals(Compatibility.MAJOR, buildKey3.getCompatibility(someKey1));
 
         assertTrue(someKey1.isCompatible(someKey2));
         assertTrue(someKey1.isCompatible(someKey3));
         assertTrue(someKey1.isCompatible(someKey4));
         assertFalse(someKey1.isCompatible(someKey0));
         assertFalse(someKey1.isCompatible(someKey5));
+        assertFalse(someKey1.isCompatible(buildKey3));
+        assertFalse(someKey1.isCompatible(buildKey4));
         assertFalse(someKey1.isCompatible(new DummyPfKey()));
+    }
 
-        assertEquals(PfValidationResult.ValidationResult.VALID,
-            someKey0.validate(new PfValidationResult()).getValidationResult());
-        assertEquals(PfValidationResult.ValidationResult.VALID,
-            someKey1.validate(new PfValidationResult()).getValidationResult());
-        assertEquals(PfValidationResult.ValidationResult.VALID,
-            someKey2.validate(new PfValidationResult()).getValidationResult());
-        assertEquals(PfValidationResult.ValidationResult.VALID,
-            someKey3.validate(new PfValidationResult()).getValidationResult());
-        assertEquals(PfValidationResult.ValidationResult.VALID,
-            someKey4.validate(new PfValidationResult()).getValidationResult());
-        assertEquals(PfValidationResult.ValidationResult.VALID,
-            someKey5.validate(new PfValidationResult()).getValidationResult());
-        assertEquals(PfValidationResult.ValidationResult.VALID,
-            someKey6.validate(new PfValidationResult()).getValidationResult());
+    @Test
+    public void testValidityConceptKey() {
+        assertTrue(someKey0.validate("").isValid());
+        assertTrue(someKey1.validate("").isValid());
+        assertTrue(someKey2.validate("").isValid());
+        assertTrue(someKey3.validate("").isValid());
+        assertTrue(someKey4.validate("").isValid());
+        assertTrue(someKey5.validate("").isValid());
+        assertTrue(someKey6.validate("").isValid());
+        assertTrue(buildKey1.validate("").isValid());
+        assertTrue(buildKey2.validate("").isValid());
+        assertTrue(buildKey3.validate("").isValid());
+        assertTrue(buildKey4.validate("").isValid());
+    }
 
+    @Test
+    public void testCleanConceptKey() {
         someKey0.clean();
         assertNotNull(someKey0.toString());
 
@@ -138,10 +180,6 @@ public class PfKeyImplTest {
         assertEquals(0, someKey0.compareTo(someKey0));
         assertEquals(-36, someKey0.compareTo(new DummyPfKey()));
 
-        assertNotEquals(someKey0, null);
-        assertEquals(someKey0, (Object) someKey0);
-        assertNotEquals(someKey0, (Object) new DummyPfKey());
-
         MyKey someKey8 = new MyKey();
         someKey8.setVersion(VERSION001);
         assertFalse(someKey8.isNullKey());
@@ -162,7 +200,7 @@ public class PfKeyImplTest {
 
         assertThatThrownBy(() -> new MyKey(null, null)).hasMessageMatching("name is marked .*on.*ull but is null$");
 
-        assertThatThrownBy(() -> new MyKey("name", null))
+        assertThatThrownBy(() -> new MyKey(NAME, null))
             .hasMessageMatching("^version is marked .*on.*ull but is null$");
 
         assertThatThrownBy(() -> new MyKey(null, VERSION001))
@@ -176,25 +214,23 @@ public class PfKeyImplTest {
         MyKey testKey = new MyKey("TheKey", VERSION001);
         assertEquals("TheKey:0.0.1", testKey.getId());
 
-        Field nameField = testKey.getClass().getDeclaredField("name");
+        Field nameField = testKey.getClass().getDeclaredField(NAME);
         nameField.setAccessible(true);
         nameField.set(testKey, "Key Name");
-        PfValidationResult validationResult = new PfValidationResult();
-        testKey.validate(validationResult);
+        ValidationResult validationResult = testKey.validate("");
         nameField.set(testKey, "TheKey");
         nameField.setAccessible(false);
-        assertEquals("name invalid-parameter name with value Key Name " + "does not match regular expression "
-            + PfKey.NAME_REGEXP, validationResult.getMessageList().get(0).getMessage());
+        assertThat(validationResult.getResult()).contains("\"name\"").doesNotContain("\"version\"")
+            .contains("does not match regular expression " + PfKey.NAME_REGEXP);
 
         Field versionField = testKey.getClass().getDeclaredField("version");
         versionField.setAccessible(true);
         versionField.set(testKey, "Key Version");
-        PfValidationResult validationResult2 = new PfValidationResult();
-        testKey.validate(validationResult2);
+        ValidationResult validationResult2 = testKey.validate("");
         versionField.set(testKey, VERSION001);
         versionField.setAccessible(false);
-        assertEquals("version invalid-parameter version with value Key Version " + "does not match regular expression "
-            + PfKey.VERSION_REGEXP, validationResult2.getMessageList().get(0).getMessage());
+        assertThat(validationResult2.getResult()).doesNotContain("\"name\"").contains("\"version\"")
+            .contains("does not match regular expression " + PfKey.VERSION_REGEXP);
     }
 
     @Test
@@ -275,10 +311,13 @@ public class PfKeyImplTest {
     @Setter
     @EqualsAndHashCode(callSuper = false)
     @NoArgsConstructor
-    private static class MyKey extends PfKeyImpl {
+    public static class MyKey extends PfKeyImpl {
         private static final long serialVersionUID = 1L;
 
+        @Pattern(regexp = NAME_REGEXP)
         private String name;
+
+        @Pattern(regexp = VERSION_REGEXP)
         private String version;
 
         public MyKey(String name, String version) {