diff options
Diffstat (limited to 'models-base/src')
-rw-r--r-- | models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java | 232 | ||||
-rw-r--r-- | models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java | 306 | ||||
-rw-r--r-- | models-base/src/main/java/org/onap/policy/models/base/PfSearchableKey.java | 111 | ||||
-rw-r--r-- | models-base/src/test/java/org/onap/policy/models/base/PfConceptKeyTest.java | 78 | ||||
-rw-r--r-- | models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java (renamed from models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java) | 103 | ||||
-rw-r--r-- | models-base/src/test/java/org/onap/policy/models/base/PfSearchableKeyTest.java | 77 |
6 files changed, 650 insertions, 257 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java index f5baae7fe..5e3295ec7 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java @@ -21,19 +21,12 @@ package org.onap.policy.models.base; -import java.util.ArrayList; -import java.util.List; - import javax.persistence.Column; import javax.persistence.Embeddable; - import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NonNull; -import lombok.ToString; - import org.onap.policy.common.utils.validation.Assertions; -import org.onap.policy.models.base.PfValidationResult.ValidationResult; /** * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system @@ -45,14 +38,10 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; */ @Embeddable @Getter -@ToString @EqualsAndHashCode(callSuper = false) -public class PfConceptKey extends PfKey { +public class PfConceptKey extends PfKeyImpl { private static final long serialVersionUID = 8932717618579392561L; - private static final String NAME_TOKEN = "name"; - private static final String VERSION_TOKEN = "version"; - @Column(name = NAME_TOKEN, length = 120) private String name; @@ -71,10 +60,8 @@ public class PfConceptKey extends PfKey { * * @param copyConcept the concept to copy from */ - public PfConceptKey(@NonNull final PfConceptKey copyConcept) { + public PfConceptKey(final PfConceptKey copyConcept) { super(copyConcept); - this.name = copyConcept.name; - this.version = copyConcept.version; } /** @@ -83,10 +70,8 @@ public class PfConceptKey extends PfKey { * @param name the key name * @param version the key version */ - public PfConceptKey(@NonNull final String name, @NonNull final String version) { - super(); - this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP); - this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP); + public PfConceptKey(final String name, final String version) { + super(name, version); } /** @@ -94,218 +79,29 @@ public class PfConceptKey extends PfKey { * * @param id the key ID in a format that respects the KEY_ID_REGEXP */ - public PfConceptKey(@NonNull final String id) { - // Check the incoming ID is valid - Assertions.validateStringParameter("id", id, KEY_ID_REGEXP); - - // Split on colon, if the id passes the regular expression test above - // it'll have just one colon separating the name and version - // No need for range checks or size checks on the array - final String[] nameVersionArray = id.split(":"); - - // Return the new key - name = Assertions.validateStringParameter(NAME_TOKEN, nameVersionArray[0], NAME_REGEXP); - version = Assertions.validateStringParameter(VERSION_TOKEN, nameVersionArray[1], VERSION_REGEXP); - } - - /** - * Get a null concept key. - * - * @return a null concept key - */ - public static final PfConceptKey getNullKey() { - return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION); - } - - @Override - public PfConceptKey getKey() { - return this; + public PfConceptKey(final String id) { + super(id); } public void setName(@NonNull final String name) { - this.name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP); + this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx()); } public void setVersion(@NonNull final String version) { - this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP); - } - - @Override - public List<PfKey> getKeys() { - final List<PfKey> keyList = new ArrayList<>(); - keyList.add(getKey()); - return keyList; - } - - @Override - public String getId() { - return name + ':' + version; - } - - @Override - public boolean isNullKey() { - return this.equals(PfConceptKey.getNullKey()); + this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx()); } /** - * Determines if the version is "null". + * Get a null concept key. * - * @return {@code true} if the version is null, {@code false} otherwise + * @return a null concept key */ - public boolean isNullVersion() { - return PfKey.NULL_KEY_VERSION.equals(getVersion()); - } - - @Override - public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) { - if (!(otherKey instanceof PfConceptKey)) { - return Compatibility.DIFFERENT; - } - final PfConceptKey otherConceptKey = (PfConceptKey) otherKey; - - if (this.equals(otherConceptKey)) { - return Compatibility.IDENTICAL; - } - if (!this.getName().equals(otherConceptKey.getName())) { - return Compatibility.DIFFERENT; - } - - final String[] thisVersionArray = getVersion().split("\\."); - final String[] otherVersionArray = otherConceptKey.getVersion().split("\\."); - - // There must always be at least one element in each version - if (!thisVersionArray[0].equals(otherVersionArray[0])) { - return Compatibility.MAJOR; - } - - if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2 - && !thisVersionArray[1].equals(otherVersionArray[1])) { - return Compatibility.MINOR; - } - - return Compatibility.PATCH; - } - - @Override - public boolean isCompatible(@NonNull final PfKey otherKey) { - if (!(otherKey instanceof PfConceptKey)) { - return false; - } - final PfConceptKey otherConceptKey = (PfConceptKey) otherKey; - - final Compatibility compatibility = this.getCompatibility(otherConceptKey); - - return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR); - } - - @Override - public boolean isNewerThan(@NonNull final PfKey otherKey) { - Assertions.instanceOf(otherKey, PfConceptKey.class); - - final PfConceptKey otherConceptKey = (PfConceptKey) otherKey; - - if (this.equals(otherConceptKey)) { - return false; - } - - if (!this.getName().equals(otherConceptKey.getName())) { - return this.getName().compareTo(otherConceptKey.getName()) > 0; - } - - final String[] thisVersionArray = getVersion().split("\\."); - final String[] otherVersionArray = otherConceptKey.getVersion().split("\\."); - - // There must always be at least one element in each version - if (!thisVersionArray[0].equals(otherVersionArray[0])) { - return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]); - } - - if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2 - && !thisVersionArray[1].equals(otherVersionArray[1])) { - return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]); - } - - if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3 - && !thisVersionArray[2].equals(otherVersionArray[2])) { - return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]); - } - - return false; - } - - @Override - public int getMajorVersion() { - final String[] versionArray = getVersion().split("\\."); - - // There must always be at least one element in each version - return Integer.parseInt(versionArray[0]); - } - - @Override - public int getMinorVersion() { - final String[] versionArray = getVersion().split("\\."); - - if (versionArray.length >= 2) { - return Integer.parseInt(versionArray[1]); - } - else { - return 0; - } - } - - @Override - public int getPatchVersion() { - final String[] versionArray = getVersion().split("\\."); - - if (versionArray.length >= 3) { - return Integer.parseInt(versionArray[2]); - } - else { - return 0; - } - } - - @Override - public PfValidationResult validate(final PfValidationResult result) { - final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, name, - NAME_REGEXP); - if (nameValidationErrorMessage != null) { - result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "name invalid-" + nameValidationErrorMessage)); - } - - final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN, - version, VERSION_REGEXP); - if (versionValidationErrorMessage != null) { - result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "version invalid-" + versionValidationErrorMessage)); - } - - return result; - } - - @Override - public void clean() { - name = Assertions.validateStringParameter(NAME_TOKEN, name, NAME_REGEXP); - version = Assertions.validateStringParameter(VERSION_TOKEN, version, VERSION_REGEXP); + public static final PfConceptKey getNullKey() { + return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION); } @Override - public int compareTo(@NonNull final PfConcept otherObj) { - Assertions.argumentNotNull(otherObj, "comparison object may not be null"); - - if (this == otherObj) { - return 0; - } - if (getClass() != otherObj.getClass()) { - return getClass().getName().compareTo(otherObj.getClass().getName()); - } - - final PfConceptKey other = (PfConceptKey) otherObj; - - if (!name.equals(other.name)) { - return name.compareTo(other.name); - } - return version.compareTo(other.version); + public String toString() { + return "PfConceptKey(name=" + getName() + ", version=" + getVersion() + ")"; } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java b/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java new file mode 100644 index 000000000..461fd2495 --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/PfKeyImpl.java @@ -0,0 +1,306 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.base; + +import java.util.ArrayList; +import java.util.List; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * A key, upon which other key subclasses can be built, providing implementations of the methods. + */ +@Getter +@ToString +public abstract class PfKeyImpl extends PfKey { + private static final long serialVersionUID = 8932717618579392561L; + + public static final String NAME_TOKEN = "name"; + public static final String VERSION_TOKEN = "version"; + + /** + * The default constructor creates a null concept key. + */ + public PfKeyImpl() { + this(NULL_KEY_NAME, NULL_KEY_VERSION); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public PfKeyImpl(final PfKeyImpl copyConcept) { + super(copyConcept); + setName(copyConcept.getName()); + setVersion(copyConcept.getVersion()); + } + + /** + * Constructor to create a key with the specified name and version. + * + * @param name the key name + * @param version the key version + */ + public PfKeyImpl(@NonNull final String name, @NonNull final String version) { + super(); + setName(name); + setVersion(version); + } + + /** + * Constructor to create a key using the key and version from the specified key ID. + * + * @param id the key ID in a format that respects the KEY_ID_REGEXP + */ + public PfKeyImpl(@NonNull final String id) { + // Check the incoming ID is valid + Assertions.validateStringParameter("id", id, getKeyIdRegEx()); + + // Split on colon, if the id passes the regular expression test above + // it'll have just one colon separating the name and version + // No need for range checks or size checks on the array + final String[] nameVersionArray = id.split(":"); + + // Return the new key + setName(nameVersionArray[0]); + setVersion(nameVersionArray[1]); + } + + public abstract void setName(@NonNull final String name); + + public abstract void setVersion(@NonNull final String version); + + @Override + public PfKeyImpl getKey() { + return this; + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = new ArrayList<>(); + keyList.add(getKey()); + return keyList; + } + + @Override + public String getId() { + return getName() + ':' + getVersion(); + } + + @Override + public boolean isNullKey() { + return (PfKey.NULL_KEY_NAME.equals(getName()) && PfKey.NULL_KEY_VERSION.equals(getVersion())); + } + + /** + * Determines if the version is "null". + * + * @return {@code true} if the version is null, {@code false} otherwise + */ + public boolean isNullVersion() { + return PfKey.NULL_KEY_VERSION.equals(getVersion()); + } + + @Override + public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) { + if (!(otherKey instanceof PfKeyImpl)) { + return Compatibility.DIFFERENT; + } + final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey; + + if (this.equals(otherConceptKey)) { + return Compatibility.IDENTICAL; + } + if (!this.getName().equals(otherConceptKey.getName())) { + return Compatibility.DIFFERENT; + } + + final String[] thisVersionArray = getVersion().split("\\."); + final String[] otherVersionArray = otherConceptKey.getVersion().split("\\."); + + // There must always be at least one element in each version + if (!thisVersionArray[0].equals(otherVersionArray[0])) { + return Compatibility.MAJOR; + } + + if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2 + && !thisVersionArray[1].equals(otherVersionArray[1])) { + return Compatibility.MINOR; + } + + return Compatibility.PATCH; + } + + @Override + public boolean isCompatible(@NonNull final PfKey otherKey) { + if (!(otherKey instanceof PfKeyImpl)) { + return false; + } + final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey; + + final Compatibility compatibility = this.getCompatibility(otherConceptKey); + + return !(compatibility == Compatibility.DIFFERENT || compatibility == Compatibility.MAJOR); + } + + @Override + public boolean isNewerThan(@NonNull final PfKey otherKey) { + Assertions.instanceOf(otherKey, PfKeyImpl.class); + + final PfKeyImpl otherConceptKey = (PfKeyImpl) otherKey; + + if (this.equals(otherConceptKey)) { + return false; + } + + if (!this.getName().equals(otherConceptKey.getName())) { + return this.getName().compareTo(otherConceptKey.getName()) > 0; + } + + final String[] thisVersionArray = getVersion().split("\\."); + final String[] otherVersionArray = otherConceptKey.getVersion().split("\\."); + + // There must always be at least one element in each version + if (!thisVersionArray[0].equals(otherVersionArray[0])) { + return Integer.valueOf(thisVersionArray[0]) > Integer.valueOf(otherVersionArray[0]); + } + + if (thisVersionArray.length >= 2 && otherVersionArray.length >= 2 + && !thisVersionArray[1].equals(otherVersionArray[1])) { + return Integer.valueOf(thisVersionArray[1]) > Integer.valueOf(otherVersionArray[1]); + } + + if (thisVersionArray.length >= 3 && otherVersionArray.length >= 3 + && !thisVersionArray[2].equals(otherVersionArray[2])) { + return Integer.valueOf(thisVersionArray[2]) > Integer.valueOf(otherVersionArray[2]); + } + + return false; + } + + @Override + public int getMajorVersion() { + final String[] versionArray = getVersion().split("\\."); + + // There must always be at least one element in each version + return Integer.parseInt(versionArray[0]); + } + + @Override + public int getMinorVersion() { + final String[] versionArray = getVersion().split("\\."); + + if (versionArray.length >= 2) { + return Integer.parseInt(versionArray[1]); + } + else { + return 0; + } + } + + @Override + public int getPatchVersion() { + final String[] versionArray = getVersion().split("\\."); + + if (versionArray.length >= 3) { + return Integer.parseInt(versionArray[2]); + } + else { + return 0; + } + } + + @Override + public PfValidationResult validate(final PfValidationResult result) { + final String nameValidationErrorMessage = Assertions.getStringParameterValidationMessage(NAME_TOKEN, getName(), + getNameRegEx()); + if (nameValidationErrorMessage != null) { + result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, + "name invalid-" + nameValidationErrorMessage)); + } + + final String versionValidationErrorMessage = Assertions.getStringParameterValidationMessage(VERSION_TOKEN, + getVersion(), getVersionRegEx()); + if (versionValidationErrorMessage != null) { + result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, + "version invalid-" + versionValidationErrorMessage)); + } + + return result; + } + + @Override + public void clean() { + setName(getName()); + setVersion(getVersion()); + } + + @Override + public int compareTo(@NonNull final PfConcept otherObj) { + Assertions.argumentNotNull(otherObj, "comparison object may not be null"); + + if (this == otherObj) { + return 0; + } + if (getClass() != otherObj.getClass()) { + return getClass().getName().compareTo(otherObj.getClass().getName()); + } + + final PfKeyImpl other = (PfKeyImpl) otherObj; + + if (!getName().equals(other.getName())) { + return getName().compareTo(other.getName()); + } + return getVersion().compareTo(other.getVersion()); + } + + /** + * Gets the regular expression used to validate a name. + * + * @return the regular expression used to validate a name + */ + protected String getNameRegEx() { + return NAME_REGEXP; + } + + /** + * Gets the regular expression used to validate a version. + * + * @return the regular expression used to validate a version + */ + protected String getVersionRegEx() { + return VERSION_REGEXP; + } + + /** + * Gets the regular expression used to validate a key id. + * + * @return the regular expression used to validate a key id + */ + protected String getKeyIdRegEx() { + return KEY_ID_REGEXP; + } +} diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfSearchableKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfSearchableKey.java new file mode 100644 index 000000000..bed1a3598 --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/PfSearchableKey.java @@ -0,0 +1,111 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.base; + +import javax.persistence.Column; +import javax.persistence.Embeddable; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import org.onap.policy.common.utils.validation.Assertions; + +/** + * A key that is used to search for other concept keys. The name field may contain a + * trailing ".*", to indicate wild-card matching. + */ +@Embeddable +@Getter +@EqualsAndHashCode(callSuper = false) +public class PfSearchableKey extends PfKeyImpl { + private static final long serialVersionUID = 8932717618579392561L; + + /** Regular expression to specify the structure of key names. */ + public static final String WILDCARD_NAME_REGEXP = "^[A-Za-z0-9\\-_\\.]+(?:\\.\\*)?$"; + + @Column(name = NAME_TOKEN, length = 120) + private String name; + + @Column(name = VERSION_TOKEN, length = 20) + private String version; + + /** + * The default constructor creates a null key. + */ + public PfSearchableKey() { + this(NULL_KEY_NAME, NULL_KEY_VERSION); + } + + /** + * Copy constructor. + * + * @param copyKey the key to copy from + */ + public PfSearchableKey(final PfSearchableKey copyKey) { + super(copyKey); + } + + /** + * Constructor to create a key with the specified name and version. + * + * @param name the key name + * @param version the key version + */ + public PfSearchableKey(final String name, final String version) { + super(name, version); + } + + /** + * Constructor to create a key using the key and version from the specified key ID. + * + * @param id the key ID in a format that respects the KEY_ID_REGEXP + */ + public PfSearchableKey(final String id) { + super(id); + } + + public void setName(@NonNull final String name) { + this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx()); + } + + public void setVersion(@NonNull final String version) { + this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx()); + } + + /** + * Get a null key. + * + * @return a null key + */ + public static final PfSearchableKey getNullKey() { + return new PfSearchableKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION); + } + + @Override + protected String getNameRegEx() { + return WILDCARD_NAME_REGEXP; + } + + @Override + public String toString() { + return "PfSearchableKey(name=" + getName() + ", version=" + getVersion() + ")"; + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfConceptKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfConceptKeyTest.java new file mode 100644 index 000000000..a29858393 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfConceptKeyTest.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * Modifications Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.base; + +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.assertTrue; + +import org.junit.Test; +import org.onap.policy.models.base.testconcepts.DummyPfConcept; + +public class PfConceptKeyTest { + + private static final String VERSION001 = "0.0.1"; + private static final String ID_IS_NULL = "id is marked @NonNull but is null"; + + @Test + public void testConceptKey() { + PfConceptKey someKey0 = new PfConceptKey(); + assertEquals(PfConceptKey.getNullKey(), someKey0); + assertTrue(someKey0.isNullKey()); + assertEquals("PfConceptKey(name=NULL, version=0.0.0)", someKey0.toString()); + + PfConceptKey someKey1 = new PfConceptKey("my-name", VERSION001); + PfConceptKey someKey2 = new PfConceptKey(someKey1); + PfConceptKey someKey3 = new PfConceptKey(someKey1.getId()); + assertEquals(someKey1, someKey2); + assertEquals(someKey1, someKey3); + assertFalse(someKey1.isNullVersion()); + assertEquals("PfConceptKey(name=my-name, version=0.0.1)", someKey1.toString()); + + assertEquals("my-name", someKey1.getName()); + assertEquals(VERSION001, someKey1.getVersion()); + + assertEquals(someKey2, someKey1.getKey()); + assertEquals(1, someKey1.getKeys().size()); + + + PfConcept pfc = new DummyPfConcept(); + assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId()); + + assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId())); + + assertTrue(PfConceptKey.getNullKey().isNullKey()); + + assertThatThrownBy(() -> PfConceptKey.getNullKey().matchesId(null)).hasMessage(ID_IS_NULL); + + assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class) + .hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class) + .hasMessage("version is marked @NonNull but is null"); + + assertThatIllegalArgumentException().isThrownBy(() -> new PfConceptKey("my-name.*", VERSION001)).withMessage( + "parameter 'name': value 'my-name.*', does not match regular expression '^[A-Za-z0-9\\-_\\.]+$'" + .replace('\'', '"')); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java index fca73b0d6..f467ec35a 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyImplTest.java @@ -29,12 +29,15 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.lang.reflect.Field; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import org.junit.Test; import org.onap.policy.models.base.PfKey.Compatibility; -import org.onap.policy.models.base.testconcepts.DummyPfConcept; import org.onap.policy.models.base.testconcepts.DummyPfKey; -public class PfKeyTest { +public class PfKeyImplTest { private static final String OTHER_IS_NULL = "otherKey is marked @NonNull but is null"; private static final String ID_IS_NULL = "id is marked @NonNull but is null"; @@ -44,21 +47,23 @@ public class PfKeyTest { @Test public void testConceptKey() { - assertThatIllegalArgumentException().isThrownBy(() -> new PfConceptKey("some bad key id")) + 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 PfConceptKey((PfConceptKey) null)) + assertThatThrownBy(() -> new MyKey((MyKey) null)) .hasMessage("copyConcept is marked @NonNull but is null"); - PfConceptKey someKey0 = new PfConceptKey(); - assertEquals(PfConceptKey.getNullKey(), someKey0); + MyKey someKey0 = new MyKey(); + assertTrue(someKey0.isNullKey()); + assertEquals(new MyKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION), someKey0); - PfConceptKey someKey1 = new PfConceptKey("name", VERSION001); - PfConceptKey someKey2 = new PfConceptKey(someKey1); - PfConceptKey someKey3 = new PfConceptKey(someKey1.getId()); + 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()); @@ -69,28 +74,22 @@ public class PfKeyTest { someKey3.setVersion("0.0.2"); - PfConceptKey someKey4 = new PfConceptKey(someKey1); + MyKey someKey4 = new MyKey(someKey1); someKey4.setVersion("0.1.2"); - PfConceptKey someKey4a = new PfConceptKey(someKey1); + MyKey someKey4a = new MyKey(someKey1); someKey4a.setVersion("0.0.0"); - PfConceptKey someKey5 = new PfConceptKey(someKey1); + MyKey someKey5 = new MyKey(someKey1); someKey5.setVersion("1.2.2"); - PfConceptKey someKey6 = new PfConceptKey(someKey1); + MyKey someKey6 = new MyKey(someKey1); someKey6.setVersion("3.0.0"); assertEquals("name:0.1.2", someKey4.getId()); - PfConcept pfc = new DummyPfConcept(); - assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId()); - - assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId())); - - assertTrue(PfConceptKey.getNullKey().isNullKey()); - - assertThatThrownBy(() -> PfConceptKey.getNullKey().matchesId(null)).hasMessage(ID_IS_NULL); + assertThatThrownBy(() -> someKey0.getCompatibility(null)).isInstanceOf(NullPointerException.class) + .hasMessage("otherKey is marked @NonNull but is null"); assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey())); assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1)); @@ -127,7 +126,7 @@ public class PfKeyTest { someKey0.clean(); assertNotNull(someKey0.toString()); - PfConceptKey someKey7 = new PfConceptKey(someKey1); + MyKey someKey7 = new MyKey(someKey1); assertEquals(244799191, someKey7.hashCode()); assertEquals(0, someKey7.compareTo(someKey1)); assertEquals(-12, someKey7.compareTo(someKey0)); @@ -141,27 +140,31 @@ public class PfKeyTest { assertFalse(someKey0.equals(null)); assertTrue(someKey0.equals(someKey0)); assertFalse(someKey0.equals(new DummyPfKey())); + + MyKey someKey8 = new MyKey(); + someKey8.setVersion(VERSION001); + assertFalse(someKey8.isNullKey()); } @Test public void testNullArguments() { - assertThatThrownBy(() -> new PfConceptKey((String) null)).hasMessage(ID_IS_NULL); + assertThatThrownBy(() -> new MyKey((String) null)).hasMessage(ID_IS_NULL); - assertThatThrownBy(() -> new PfConceptKey((PfConceptKey) null)) + assertThatThrownBy(() -> new MyKey((MyKey) null)) .hasMessage("copyConcept is marked @NonNull but is null"); - assertThatThrownBy(() -> new PfConceptKey(null, null)).hasMessage("name is marked @NonNull but is null"); + assertThatThrownBy(() -> new MyKey(null, null)).hasMessage("name is marked @NonNull but is null"); - assertThatThrownBy(() -> new PfConceptKey("name", null)).hasMessage("version is marked @NonNull but is null"); + assertThatThrownBy(() -> new MyKey("name", null)).hasMessage("version is marked @NonNull but is null"); - assertThatThrownBy(() -> new PfConceptKey(null, VERSION001)).hasMessage("name is marked @NonNull but is null"); + assertThatThrownBy(() -> new MyKey(null, VERSION001)).hasMessage("name is marked @NonNull but is null"); - assertThatThrownBy(() -> new PfConceptKey("AKey", VERSION001).isCompatible(null)).hasMessage(OTHER_IS_NULL); + assertThatThrownBy(() -> new MyKey("AKey", VERSION001).isCompatible(null)).hasMessage(OTHER_IS_NULL); } @Test public void testValidation() throws Exception { - PfConceptKey testKey = new PfConceptKey("TheKey", VERSION001); + MyKey testKey = new MyKey("TheKey", VERSION001); assertEquals("TheKey:0.0.1", testKey.getId()); Field nameField = testKey.getClass().getDeclaredField("name"); @@ -191,20 +194,20 @@ public class PfKeyTest { @Test public void testkeynewerThan() { - PfConceptKey key1 = new PfConceptKey("Key1", VERSION123); + MyKey key1 = new MyKey("Key1", VERSION123); assertThatThrownBy(() -> key1.isNewerThan(null)).hasMessage(OTHER_IS_NULL); assertThatThrownBy(() -> key1.isNewerThan(new PfReferenceKey())) .hasMessage("org.onap.policy.models.base.PfReferenceKey is not " - + "an instance of org.onap.policy.models.base.PfConceptKey"); + + "an instance of " + PfKeyImpl.class.getName()); assertFalse(key1.isNewerThan(key1)); - PfConceptKey key1a = new PfConceptKey("Key1a", VERSION123); + MyKey key1a = new MyKey("Key1a", VERSION123); assertFalse(key1.isNewerThan(key1a)); - PfConceptKey key1b = new PfConceptKey("Key0", VERSION123); + MyKey key1b = new MyKey("Key0", VERSION123); assertTrue(key1.isNewerThan(key1b)); key1a.setName("Key1"); @@ -240,28 +243,50 @@ public class PfKeyTest { assertThatThrownBy(() -> refKey.isNewerThan(null)).hasMessage(OTHER_IS_NULL); - assertThatThrownBy(() -> refKey.isNewerThan(new PfConceptKey())) - .hasMessage("org.onap.policy.models.base.PfConceptKey is not " - + "an instance of org.onap.policy.models.base.PfReferenceKey"); + assertThatThrownBy(() -> refKey.isNewerThan(new MyKey())) + .hasMessage(MyKey.class.getName() + " is not an instance of " + PfReferenceKey.class.getName()); assertFalse(refKey.isNewerThan(refKey)); } @Test public void testmajorMinorPatch() { - PfConceptKey key = new PfConceptKey("Key", VERSION100); + MyKey key = new MyKey("Key", VERSION100); assertEquals(1, key.getMajorVersion()); assertEquals(0, key.getMinorVersion()); assertEquals(0, key.getPatchVersion()); - key = new PfConceptKey("Key", "1.2.0"); + key = new MyKey("Key", "1.2.0"); assertEquals(1, key.getMajorVersion()); assertEquals(2, key.getMinorVersion()); assertEquals(0, key.getPatchVersion()); - key = new PfConceptKey("Key", VERSION123); + key = new MyKey("Key", VERSION123); assertEquals(1, key.getMajorVersion()); assertEquals(2, key.getMinorVersion()); assertEquals(3, key.getPatchVersion()); } + + @Getter + @Setter + @EqualsAndHashCode(callSuper = false) + @NoArgsConstructor + private static class MyKey extends PfKeyImpl { + private static final long serialVersionUID = 1L; + + private String name; + private String version; + + public MyKey(String name, String version) { + super(name, version); + } + + public MyKey(String id) { + super(id); + } + + public MyKey(MyKey myKey) { + super(myKey); + } + } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfSearchableKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfSearchableKeyTest.java new file mode 100644 index 000000000..942f47c1d --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfSearchableKeyTest.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * Modifications Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +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.assertTrue; + +import org.junit.Test; +import org.onap.policy.models.base.testconcepts.DummyPfConcept; + +public class PfSearchableKeyTest { + + private static final String VERSION001 = "0.0.1"; + private static final String ID_IS_NULL = "id is marked @NonNull but is null"; + + @Test + public void testSearchableKey() { + PfSearchableKey someKey0 = new PfSearchableKey(); + assertEquals(PfSearchableKey.getNullKey(), someKey0); + assertTrue(someKey0.isNullKey()); + assertEquals("PfSearchableKey(name=NULL, version=0.0.0)", someKey0.toString()); + + PfSearchableKey someKey1 = new PfSearchableKey("my-name", VERSION001); + PfSearchableKey someKey2 = new PfSearchableKey(someKey1); + PfSearchableKey someKey3 = new PfSearchableKey(someKey1.getId()); + assertEquals(someKey1, someKey2); + assertEquals(someKey1, someKey3); + assertFalse(someKey1.isNullVersion()); + assertEquals("PfSearchableKey(name=my-name, version=0.0.1)", someKey1.toString()); + + assertEquals("my-name", someKey1.getName()); + assertEquals(VERSION001, someKey1.getVersion()); + + assertEquals(someKey2, someKey1.getKey()); + assertEquals(1, someKey1.getKeys().size()); + + + PfConcept pfc = new DummyPfConcept(); + assertEquals(PfSearchableKey.getNullKey().getId(), pfc.getId()); + + assertTrue(PfSearchableKey.getNullKey().matchesId(pfc.getId())); + + assertTrue(PfSearchableKey.getNullKey().isNullKey()); + + assertThatThrownBy(() -> PfSearchableKey.getNullKey().matchesId(null)).hasMessage(ID_IS_NULL); + + assertThatThrownBy(() -> someKey0.setName(null)).isInstanceOf(NullPointerException.class) + .hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> someKey0.setVersion(null)).isInstanceOf(NullPointerException.class) + .hasMessage("version is marked @NonNull but is null"); + + PfSearchableKey someKey4 = new PfSearchableKey("my-name.*", VERSION001); + assertEquals("my-name.*", someKey4.getName()); + assertEquals(VERSION001, someKey4.getVersion()); + } +} |