diff options
Diffstat (limited to 'models-base/src')
27 files changed, 1613 insertions, 246 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java index b74b0374d..c41b0de56 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java @@ -23,7 +23,7 @@ package org.onap.policy.models.base; import java.io.Serializable; import java.util.List; -import org.onap.policy.common.utils.validation.Assertions; +import lombok.NonNull; /** * This class is the base class for all Policy Framework concept classes. It enforces implementation @@ -43,8 +43,7 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> { * * @param copyConcept the concept to copy from */ - public PfConcept(final PfConcept copyConcept) { - Assertions.argumentNotNull(copyConcept, "copy concept may not be null"); + public PfConcept(@NonNull final PfConcept copyConcept) { copyConcept.copyTo(this); } @@ -70,7 +69,7 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> { * @return the validation result that was passed in in the @{link result} field with the result * of this validation added */ - public abstract PfValidationResult validate(PfValidationResult result); + public abstract PfValidationResult validate(@NonNull final PfValidationResult result); /** * Clean this concept, tidy up any superfluous information such as leading and trailing white @@ -78,27 +77,12 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> { */ public abstract void clean(); - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public abstract boolean equals(Object otherObject); - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ @Override public abstract String toString(); - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ @Override public abstract int hashCode(); @@ -109,7 +93,7 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> { * @param target the target object to which this object is copied * @return the copied object */ - public abstract PfConcept copyTo(PfConcept target); + public abstract PfConcept copyTo(@NonNull PfConcept target); /** * Gets the ID string of this concept. @@ -126,9 +110,7 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> { * @param id the key ID to match against * @return true, if this key matches the ID */ - public final boolean matchesId(final String id) { - Assertions.argumentNotNull(id, "id may not be null"); - + public final boolean matchesId(@NonNull final String id) { // Check the ID return getId().equals(id); } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java new file mode 100644 index 000000000..46094610a --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java @@ -0,0 +1,268 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.NavigableMap; +import java.util.Set; +import java.util.TreeMap; + +import javax.persistence.CascadeType; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.ManyToMany; +import javax.persistence.Table; +import javax.ws.rs.core.Response; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * This class is a concept container and holds a map of concepts. The {@link PfConceptContainer} + * class implements the helper methods of the {@link PfConceptGetter} interface to allow + * {@link PfConceptContainer} instances to be retrieved by calling methods directly on this class + * without referencing the contained map. + * + * <p>Validation checks that the container key is not null. An error is issued if no concepts are + * defined in the container. Each concept entry is checked to ensure that its key and value are not + * null and that the key matches the key in the map value. Each concept entry is then validated + * individually. + * + * @param C the concept being contained + */ +@Entity +@Table(name = "PfConceptContainer") +@Data +@EqualsAndHashCode(callSuper = false) + +public class PfConceptContainer<C extends PfConcept> extends PfConcept implements PfConceptGetter<C> { + private static final long serialVersionUID = -324211738823208318L; + + @EmbeddedId + private PfConceptKey key; + + @ManyToMany(cascade = CascadeType.ALL) + private Map<PfConceptKey, C> conceptMap; + + /** + * The Default Constructor creates a {@link PfConceptContainer} object with a null artifact key + * and creates an empty concept map. + */ + public PfConceptContainer() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link PfConceptContainer} object with the given artifact key + * and creates an empty concept map. + * + * @param key the concept key + */ + public PfConceptContainer(@NonNull final PfConceptKey key) { + this(key, new TreeMap<PfConceptKey, C>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public PfConceptContainer(@NonNull final PfConceptKey key, @NonNull final Map<PfConceptKey, C> conceptMap) { + super(); + + this.key = key; + this.conceptMap = new TreeMap<>(conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public PfConceptContainer(@NonNull final PfConceptContainer<C> copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = key.getKeys(); + + for (final C concept : conceptMap.values()) { + keyList.addAll(concept.getKeys()); + } + + return keyList; + } + + @Override + public void clean() { + key.clean(); + for (final Entry<PfConceptKey, C> conceptEntry : conceptMap.entrySet()) { + conceptEntry.getKey().clean(); + conceptEntry.getValue().clean(); + } + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.equals(PfConceptKey.getNullKey())) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (conceptMap.isEmpty()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "conceptMap may not be empty")); + } else { + result = validateConceptMap(result); + } + + return result; + } + + /** + * Validate the concept map of the container. + * + * @param resultIn the incoming validation results so far + * @return the validation results with the results of this validation added + */ + private PfValidationResult validateConceptMap(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (final Entry<PfConceptKey, C> conceptEntry : conceptMap.entrySet()) { + if (conceptEntry.getKey().equals(PfConceptKey.getNullKey())) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "key on concept entry " + conceptEntry.getKey() + " may not be the null key")); + } else if (conceptEntry.getValue() == null) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "value on concept entry " + conceptEntry.getKey() + " may not be null")); + } else if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), + ValidationResult.INVALID, "key on concept entry key " + conceptEntry.getKey() + + " does not equal concept value key " + conceptEntry.getValue().getKey())); + result = conceptEntry.getValue().validate(result); + } else { + result = conceptEntry.getValue().validate(result); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + @SuppressWarnings("unchecked") + final PfConceptContainer<C> other = (PfConceptContainer<C>) otherConcept; + int retVal = key.compareTo(other.key); + if (retVal != 0) { + return retVal; + } + + if (!conceptMap.equals(other.conceptMap)) { + return (conceptMap.hashCode() - other.conceptMap.hashCode()); + } + + return 0; + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + Assertions.instanceOf(target, PfConceptContainer.class); + + @SuppressWarnings("unchecked") + final PfConceptContainer<C> copy = (PfConceptContainer<C>) target; + copy.setKey(new PfConceptKey(key)); + final Map<PfConceptKey, C> newConceptMap = new TreeMap<>(); + for (final Entry<PfConceptKey, C> conceptMapEntry : conceptMap.entrySet()) { + newConceptMap.put(new PfConceptKey(conceptMapEntry.getKey()), + new ConceptCloner().cloneConcept(conceptMapEntry.getValue())); + } + copy.setConceptMap(newConceptMap); + + return copy; + } + + @Override + public C get(final PfConceptKey conceptKey) { + return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey); + } + + @Override + public C get(final String conceptKeyName) { + return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName); + } + + @Override + public C get(final String conceptKeyName, final String conceptKeyVersion) { + return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName, + conceptKeyVersion); + } + + @Override + public Set<C> getAll(final String conceptKeyName) { + return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName); + } + + @Override + public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) { + return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName, + conceptKeyVersion); + } + + /** + * Private inner class that returns a clone of a concept by calling the copy constructor on the + * original class. + */ + private class ConceptCloner { + @SuppressWarnings("unchecked") + public C cloneConcept(final C originalConcept) { + try { + C clonedConcept = (C) originalConcept.getClass().newInstance(); + originalConcept.copyTo(clonedConcept); + return clonedConcept; + } catch (Exception ex) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, + "Failed to create a clone of class \"" + originalConcept.getClass().getCanonicalName() + "\"", + ex); + } + } + } +} 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 efcbe392c..695ca4712 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 @@ -28,6 +28,7 @@ import javax.persistence.Embeddable; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NonNull; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.models.base.PfValidationResult.ValidationResult; @@ -67,7 +68,7 @@ public class PfConceptKey extends PfKey { * * @param copyConcept the concept to copy from */ - public PfConceptKey(final PfConceptKey copyConcept) { + public PfConceptKey(@NonNull final PfConceptKey copyConcept) { super(copyConcept); } @@ -77,7 +78,7 @@ public class PfConceptKey extends PfKey { * @param name the key name * @param version the key version */ - public PfConceptKey(final String name, final String 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); @@ -88,9 +89,7 @@ public class PfConceptKey extends PfKey { * * @param id the key ID in a format that respects the KEY_ID_REGEXP */ - public PfConceptKey(final String id) { - Assertions.argumentNotNull(id, "id may not be null"); - + public PfConceptKey(@NonNull final String id) { // Check the incoming ID is valid Assertions.validateStringParameter("id", id, KEY_ID_REGEXP); @@ -131,7 +130,12 @@ public class PfConceptKey extends PfKey { } @Override - public PfKey.Compatibility getCompatibility(final PfKey otherKey) { + public boolean isNullKey() { + return this.equals(PfConceptKey.getNullKey()); + } + + @Override + public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) { if (!(otherKey instanceof PfConceptKey)) { return Compatibility.DIFFERENT; } @@ -161,7 +165,7 @@ public class PfConceptKey extends PfKey { } @Override - public boolean isCompatible(final PfKey otherKey) { + public boolean isCompatible(@NonNull final PfKey otherKey) { if (!(otherKey instanceof PfConceptKey)) { return false; } @@ -212,7 +216,7 @@ public class PfConceptKey extends PfKey { } @Override - public int compareTo(final PfConcept otherObj) { + public int compareTo(@NonNull final PfConcept otherObj) { Assertions.argumentNotNull(otherObj, "comparison object may not be null"); if (this == otherObj) { diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfKey.java index dda4cdc03..6e9035e95 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfKey.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfKey.java @@ -20,6 +20,8 @@ package org.onap.policy.models.base; +import lombok.NonNull; + /** * The key uniquely identifies every entity in the system. This class is an abstract class to give a common parent for * all key types in the system. @@ -89,7 +91,7 @@ public abstract class PfKey extends PfConcept { * @param otherKey the key to check compatibility against * @return the compatibility result of the check */ - public abstract Compatibility getCompatibility(PfKey otherKey); + public abstract Compatibility getCompatibility(@NonNull PfKey otherKey); /** * Check if two keys are compatible, that is the keys are IDENTICAL or have only MINOR, PATCH differences. @@ -97,5 +99,12 @@ public abstract class PfKey extends PfConcept { * @param otherKey the key to check compatibility against * @return true, if the keys are compatible */ - public abstract boolean isCompatible(PfKey otherKey); + public abstract boolean isCompatible(@NonNull PfKey otherKey); + + /** + * Check if a key equals its null key. + * + * @return true, if the key is a null key + */ + public abstract boolean isNullKey(); } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java b/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java index 0eb55a711..57141c2fa 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java @@ -22,16 +22,20 @@ package org.onap.policy.models.base; import java.util.List; +import javax.ws.rs.core.Response; + import lombok.EqualsAndHashCode; +import lombok.NonNull; import lombok.ToString; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.models.base.PfValidationResult.ValidationResult; /** - * This class records a usage of a key in the system. When the list of keys being used by a concept is built using the - * getKeys() method of the {@link PfConcept} class, an instance of this class is created for every key occurrence. The - * list of keys returned by the getKeys() method is a list of {@link PfKeyUse} objects. + * This class records a usage of a key in the system. When the list of keys being used by a concept + * is built using the getKeys() method of the {@link PfConcept} class, an instance of this class is + * created for every key occurrence. The list of keys returned by the getKeys() method is a list of + * {@link PfKeyUse} objects. * * <p>Validation checks that each key is valid. */ @@ -50,22 +54,21 @@ public class PfKeyUse extends PfKey { } /** - * Copy constructor. + * This constructor creates an instance of this class, and holds a reference to a used key. * - * @param copyConcept the concept to copy from + * @param usedKey a used key */ - public PfKeyUse(final PfKeyUse copyConcept) { - super(copyConcept); + public PfKeyUse(@NonNull final PfKey usedKey) { + this.usedKey = usedKey; } /** - * This constructor creates an instance of this class, and holds a reference to a used key. + * Copy constructor. * - * @param usedKey a used key + * @param copyConcept the concept to copy from */ - public PfKeyUse(final PfKey usedKey) { - Assertions.argumentNotNull(usedKey, "usedKey may not be null"); - this.usedKey = usedKey; + public PfKeyUse(@NonNull final PfKeyUse copyConcept) { + super(copyConcept); } @Override @@ -83,56 +86,42 @@ public class PfKeyUse extends PfKey { return usedKey.getId(); } + @Override + public boolean isNullKey() { + return usedKey.isNullKey(); + } + /** * Sets the key. * * @param key the key */ - public void setKey(final PfKey key) { - Assertions.argumentNotNull(key, "usedKey may not be null"); + public void setKey(@NonNull final PfKey key) { this.usedKey = key; } @Override - public PfKey.Compatibility getCompatibility(final PfKey otherKey) { + public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) { return usedKey.getCompatibility(otherKey); } @Override - public boolean isCompatible(final PfKey otherKey) { + public boolean isCompatible(@NonNull final PfKey otherKey) { return usedKey.isCompatible(otherKey); } @Override - public PfValidationResult validate(final PfValidationResult result) { - if (usedKey.equals(PfConceptKey.getNullKey())) { - result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID, - "usedKey is a null key")); - } - return usedKey.validate(result); - } - - @Override public void clean() { usedKey.clean(); } @Override - public PfConcept copyTo(final PfConcept target) { - Assertions.argumentNotNull(target, "target may not be null"); - - final Object copyObject = target; - Assertions.instanceOf(copyObject, PfKeyUse.class); - - final PfKeyUse copy = ((PfKeyUse) copyObject); - try { - copy.usedKey = usedKey.getClass().newInstance(); - } catch (final Exception e) { - throw new PfModelRuntimeException("error copying concept key: " + e.getMessage(), e); + public PfValidationResult validate(@NonNull final PfValidationResult result) { + if (usedKey.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID, + "usedKey is a null key")); } - usedKey.copyTo(copy.usedKey); - - return copy; + return usedKey.validate(result); } @Override @@ -150,4 +139,21 @@ public class PfKeyUse extends PfKey { return usedKey.compareTo(other.usedKey); } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfKeyUse.class); + + final PfKeyUse copy = ((PfKeyUse) copyObject); + try { + copy.usedKey = usedKey.getClass().newInstance(); + } catch (final Exception e) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, + "error copying concept key: " + e.getMessage(), e); + } + usedKey.copyTo(copy.usedKey); + + return copy; + } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java b/models-base/src/main/java/org/onap/policy/models/base/PfModel.java index c9174bde8..3dc233b02 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModel.java @@ -63,8 +63,7 @@ public abstract class PfModel extends PfConcept { private static final long serialVersionUID = -771659065637205430L; @EmbeddedId - @NonNull - private PfConceptKey key = PfConceptKey.getNullKey(); + private PfConceptKey key; /** * The Default Constructor creates this concept with a NULL artifact key. @@ -74,20 +73,11 @@ public abstract class PfModel extends PfConcept { } /** - * Copy constructor. - * - * @param copyConcept the concept to copy from - */ - public PfModel(final PfModel copyConcept) { - super(copyConcept); - } - - /** * Constructor to create this concept with the specified key. * * @param key the key of this concept */ - public PfModel(final PfConceptKey key) { + public PfModel(@NonNull final PfConceptKey key) { super(); Assertions.argumentNotNull(key, "key may not be null"); @@ -95,6 +85,15 @@ public abstract class PfModel extends PfConcept { } /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public PfModel(@NonNull final PfModel copyConcept) { + super(copyConcept); + } + + /** * Registers this model with the {@link PfModelService}. All models are registered with the * model service so that models can be references from anywhere in the Policy Framework system * without being passed as references through deep call chains. @@ -107,10 +106,15 @@ public abstract class PfModel extends PfConcept { } @Override - public PfValidationResult validate(final PfValidationResult resultIn) { + public void clean() { + key.clean(); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { PfValidationResult result = resultIn; - if (key.equals(PfConceptKey.getNullKey())) { + if (key.isNullKey()) { result.addValidationMessage( new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); } @@ -129,7 +133,7 @@ public abstract class PfModel extends PfConcept { } else if (pfKey instanceof PfReferenceKey) { result = validateReferenceKeyInModel((PfReferenceKey) pfKey, referenceKeySet, result); } - // It must be an PfKeyUse, nothing else is legal + // It must be a PfKeyUse, nothing else is legal else { usedKeySet.add((PfKeyUse) pfKey); } @@ -160,7 +164,7 @@ public abstract class PfModel extends PfConcept { private PfValidationResult validateArtifactKeyInModel(final PfConceptKey artifactKey, final Set<PfConceptKey> artifactKeySet, final PfValidationResult result) { // Null key check - if (artifactKey.equals(PfConceptKey.getNullKey())) { + if (artifactKey.isNullKey()) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key " + artifactKey + IS_A_NULL_KEY)); } @@ -194,13 +198,13 @@ public abstract class PfModel extends PfConcept { private PfValidationResult validateReferenceKeyInModel(final PfReferenceKey referenceKey, final Set<PfReferenceKey> referenceKeySet, final PfValidationResult result) { // Null key check - if (referenceKey.equals(PfReferenceKey.getNullKey())) { + if (referenceKey.isNullKey()) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key " + referenceKey + IS_A_NULL_KEY)); } // Null parent key check - if (referenceKey.getParentConceptKey().equals(PfConceptKey.getNullKey())) { + if (referenceKey.getParentConceptKey().isNullKey()) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "parent artifact key of key " + referenceKey + IS_A_NULL_KEY)); } @@ -262,29 +266,6 @@ public abstract class PfModel extends PfConcept { } @Override - public void clean() { - key.clean(); - } - - @Override - public PfConcept copyTo(final PfConcept target) { - Assertions.argumentNotNull(target, "target may not be null"); - - final Object copyObject = target; - Assertions.instanceOf(copyObject, PfModel.class); - - final PfModel copy = ((PfModel) copyObject); - copy.setKey(new PfConceptKey(key)); - - return copy; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Comparable#compareTo(java.lang.Object) - */ - @Override public int compareTo(final PfConcept otherObj) { if (otherObj == null) { return -1; @@ -300,4 +281,14 @@ public abstract class PfModel extends PfConcept { return key.compareTo(other.key); } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + Assertions.instanceOf(target, PfModel.class); + + final PfModel copy = ((PfModel) target); + copy.setKey(new PfConceptKey(key)); + + return copy; + } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java b/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java index 3d1bb1794..97ea7de00 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModelException.java @@ -20,54 +20,71 @@ package org.onap.policy.models.base; +import javax.ws.rs.core.Response; + +import lombok.Getter; +import lombok.ToString; + /** * This class is a base exception from which all model exceptions are sub classes. */ +@Getter +@ToString public class PfModelException extends Exception { private static final long serialVersionUID = -8507246953751956974L; + // The status code on the exception + private final Response.Status statusCode; + // The object on which the exception was thrown private final transient Object object; /** * Instantiates a new model exception. * + * @param statusCode the return code for the exception * @param message the message on the exception */ - public PfModelException(final String message) { - this(message, null); + public PfModelException(final Response.Status statusCode, final String message) { + this(statusCode, message, null); } /** * Instantiates a new model exception. * + * @param statusCode the return code for the exception * @param message the message on the exception * @param object the object that the exception was thrown on */ - public PfModelException(final String message, final Object object) { + public PfModelException(final Response.Status statusCode, final String message, final Object object) { super(message); + this.statusCode = statusCode; this.object = object; } /** * Instantiates a new model exception. * + * @param statusCode the return code for the exception * @param message the message on the exception * @param exception the exception that caused this exception */ - public PfModelException(final String message, final Exception exception) { - this(message, exception, null); + public PfModelException(final Response.Status statusCode, final String message, final Exception exception) { + this(statusCode, message, exception, null); } /** * Instantiates a new exception. * + * @param statusCode the return code for the exception * @param message the message on the exception * @param exception the exception that caused this exception * @param object the object that the exception was thrown on */ - public PfModelException(final String message, final Exception exception, final Object object) { + public PfModelException(final Response.Status statusCode, final String message, final Exception exception, + final Object object) { super(message, exception); + this.statusCode = statusCode; this.object = object; } @@ -97,13 +114,4 @@ public class PfModelException extends Exception { return builder.toString(); } - - /** - * Get the object on which the exception was thrown. - * - * @return The object on which the exception was thrown - */ - public Object getObject() { - return object; - } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java b/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java index 0780a9e20..c4684bc09 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModelRuntimeException.java @@ -20,56 +20,73 @@ package org.onap.policy.models.base; +import javax.ws.rs.core.Response; + +import lombok.Getter; +import lombok.ToString; + /** * This class is a base model run time exception from which all model run time exceptions are sub * classes. */ +@Getter +@ToString public class PfModelRuntimeException extends RuntimeException { private static final long serialVersionUID = -8507246953751956974L; + // The return code on the exception + private final Response.Status statusCode; + // The object on which the exception was thrown private final transient Object object; /** * Instantiates a new model runtime exception. * + * @param statusCode the return code for the exception * @param message the message on the exception */ - public PfModelRuntimeException(final String message) { - this(message, null); + public PfModelRuntimeException(final Response.Status statusCode, final String message) { + this(statusCode, message, null); } /** * Instantiates a new model runtime exception. * + * @param statusCode the return code for the exception * @param message the message on the exception * @param object the object that the exception was thrown on */ - public PfModelRuntimeException(final String message, final Object object) { + public PfModelRuntimeException(final Response.Status statusCode, final String message, final Object object) { super(message); this.object = object; + this.statusCode = statusCode; } /** * Instantiates a new model runtime exception. * + * @param statusCode the return code for the exception * @param message the message on the exception * @param exception the exception that caused this model exception */ - public PfModelRuntimeException(final String message, final Exception exception) { - this(message, exception, null); + public PfModelRuntimeException(final Response.Status statusCode, final String message, final Exception exception) { + this(statusCode, message, exception, null); } /** * Instantiates a new model runtime exception. * + * @param statusCode the return code for the exception * @param message the message on the exception * @param exception the exception that caused this model exception * @param object the object that the exception was thrown on */ - public PfModelRuntimeException(final String message, final Exception exception, final Object object) { + public PfModelRuntimeException(final Response.Status statusCode, final String message, final Exception exception, + final Object object) { super(message, exception); this.object = object; + this.statusCode = statusCode; } /** @@ -80,13 +97,4 @@ public class PfModelRuntimeException extends RuntimeException { public String getCascadedMessage() { return PfModelException.buildCascadedMessage(this); } - - /** - * Get the object on which the exception was thrown. - * - * @return The object - */ - public Object getObject() { - return object; - } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModelService.java b/models-base/src/main/java/org/onap/policy/models/base/PfModelService.java index c02de6aa7..67ba59c8b 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfModelService.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfModelService.java @@ -23,6 +23,10 @@ package org.onap.policy.models.base; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.ws.rs.core.Response; + +import lombok.NonNull; + /** * The model service makes Policy Framework models available to all classes in a JVM. * @@ -37,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap; */ public abstract class PfModelService { // The map holding the models - private static Map<Class<?>, PfConcept> modelMap = new ConcurrentHashMap<>(); + private static Map<String, PfConcept> modelMap = new ConcurrentHashMap<>(); /** * This class is an abstract static class that cannot be extended. @@ -48,37 +52,36 @@ public abstract class PfModelService { * Register a model with the model service. * * @param <M> the generic type - * @param modelClass the class of the model, used to index the model + * @param modelKey the key of the model, used to index the model * @param model The model */ - public static <M extends PfConcept> void registerModel(final Class<M> modelClass, final M model) { - modelMap.put(modelClass, model); + public static <M extends PfConcept> void registerModel(@NonNull final String modelKey, @NonNull final M model) { + modelMap.put(modelKey, model); } /** * Remove a model from the model service. * - * @param <M> the generic type - * @param modelClass the class of the model, used to index the model + * @param modelKey the key of the model, used to index the model */ - public static <M extends PfConcept> void deregisterModel(final Class<M> modelClass) { - modelMap.remove(modelClass); + public static void deregisterModel(@NonNull final String modelKey) { + modelMap.remove(modelKey); } /** * Get a model from the model service. * * @param <M> the generic type - * @param modelClass the class of the model, used to index the model + * @param modelKey the key of the model, used to index the model * @return The model */ @SuppressWarnings("unchecked") - public static <M extends PfConcept> M getModel(final Class<M> modelClass) { - final M model = (M) modelMap.get(modelClass); + public static <M extends PfConcept> M getModel(@NonNull final String modelKey) { + final M model = (M) modelMap.get(modelKey); if (model == null) { - throw new PfModelRuntimeException( - "Model for " + modelClass.getCanonicalName() + " not found in model service"); + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, + "Model for name " + modelKey + " not found in model service"); } return model; @@ -87,12 +90,11 @@ public abstract class PfModelService { /** * Check if a model is defined on the model service. * - * @param <M> the generic type - * @param modelClass the class of the model, used to index the model + * @param modelKey the key of the model, used to index the model * @return true if the model is defined */ - public static <M extends PfConcept> boolean existsModel(final Class<M> modelClass) { - return modelMap.get(modelClass) != null; + public static boolean existsModel(final String modelKey) { + return modelMap.get(modelKey) != null; } /** diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java index e3b92c0e9..2e9f48ba5 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java @@ -243,6 +243,11 @@ public class PfReferenceKey extends PfKey { return parentKeyName + ':' + parentKeyVersion + ':' + parentLocalName + ':' + localName; } + @Override + public boolean isNullKey() { + return this.equals(PfReferenceKey.getNullKey()); + } + /** * Gets the parent concept key of this reference key. * diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java new file mode 100644 index 000000000..a18315ceb --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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; + +/** + * Utility class for Policy Framework concept utilities. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public final class PfUtils { + private PfUtils() { + // Cannot be subclassed + } + + /** + * Compare two objects using their equals methods, nulls are allowed. + * + * @param leftObject the first object + * @param rightObject the second object + * @return a measure of the comparison + */ + public static int compareObjects(final Object leftObject, final Object rightObject) { + if (leftObject == null && rightObject == null) { + return 0; + } + + if (leftObject == null) { + return 1; + } + + if (rightObject == null) { + return -1; + } + + if (!leftObject.equals(rightObject)) { + return leftObject.hashCode() - rightObject.hashCode(); + } + + return 0; + } +} diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java b/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java index 284c11ac2..4f8d06bdc 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java @@ -23,9 +23,12 @@ package org.onap.policy.models.base; import java.util.LinkedList; import java.util.List; +import lombok.Getter; + /** - * This class records the result of a validation and holds all validatino observation messages. + * This class records the result of a validation and holds all validation observation messages. */ +@Getter public class PfValidationResult { /** * The ValidationResult enumeration describes the severity of a validation result. @@ -77,24 +80,6 @@ public class PfValidationResult { } /** - * Gets the validation result. - * - * @return the validation result on a concept - */ - public ValidationResult getValidationResult() { - return validationResult; - } - - /** - * Gets the list of validation results on the concept. - * - * @return the list of validaiton results - */ - public List<PfValidationMessage> getMessageList() { - return messageList; - } - - /** * Adds a validation message to the validation result, used by validate() implementations on * {@link PfConcept} subclasses to report validaiton observations. * diff --git a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java index c0898c187..0a5b6a0a6 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/ExceptionsTest.java @@ -25,32 +25,35 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; +import javax.ws.rs.core.Response; + import org.junit.Test; public class ExceptionsTest { @Test public void test() { - assertNotNull(new PfModelException("Message")); - assertNotNull(new PfModelException("Message", "String")); - assertNotNull(new PfModelException("Message", new IOException())); - assertNotNull(new PfModelException("Message", new IOException(), "String")); + assertNotNull(new PfModelException(Response.Status.OK, "Message")); + assertNotNull(new PfModelException(Response.Status.OK, "Message", "String")); + assertNotNull(new PfModelException(Response.Status.OK, "Message", new IOException())); + assertNotNull(new PfModelException(Response.Status.OK, "Message", new IOException(), "String")); String key = "A String"; - PfModelException ae = new PfModelException("Message", new IOException("IO exception message"), key); + PfModelException ae = + new PfModelException(Response.Status.OK, "Message", new IOException("IO exception message"), key); assertEquals("Message\ncaused by: Message\ncaused by: IO exception message", ae.getCascadedMessage()); assertEquals(key, ae.getObject()); - assertNotNull(new PfModelRuntimeException("Message")); - assertNotNull(new PfModelRuntimeException("Message", "String")); - assertNotNull(new PfModelRuntimeException("Message", new IOException())); - assertNotNull(new PfModelRuntimeException("Message", new IOException(), "String")); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message")); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message", "String")); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message", new IOException())); + assertNotNull(new PfModelRuntimeException(Response.Status.OK, "Message", new IOException(), "String")); String rkey = "A String"; - PfModelRuntimeException re = new PfModelRuntimeException("Runtime Message", - new IOException("IO runtime exception message"), rkey); + PfModelRuntimeException re = new PfModelRuntimeException(Response.Status.OK, "Runtime Message", + new IOException("IO runtime exception message"), rkey); assertEquals("Runtime Message\ncaused by: Runtime Message\ncaused by: IO runtime exception message", - re.getCascadedMessage()); + re.getCascadedMessage()); assertEquals(key, re.getObject()); } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/ModelServiceTest.java b/models-base/src/test/java/org/onap/policy/models/base/ModelServiceTest.java new file mode 100644 index 000000000..0e790d3dc --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/ModelServiceTest.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +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.testconcepts.DummyPfModel; + +public class ModelServiceTest { + + @Test + public void testModelService() { + PfModelService.clear(); + + assertFalse(PfModelService.existsModel("NonExistantName")); + try { + PfModelService.getModel("NonExistantName"); + } catch (final Exception e) { + assertEquals("Model for name NonExistantName not found in model service", e.getMessage()); + } + + PfModelService.registerModel("ModelName", new DummyPfModel()); + assertTrue(PfModelService.existsModel("ModelName")); + assertNotNull(PfModelService.getModel("ModelName")); + + PfModelService.deregisterModel("ModelName"); + + assertFalse(PfModelService.existsModel("ModelName")); + try { + PfModelService.getModel("ModelName"); + } catch (final Exception e) { + assertEquals("Model for name ModelName not found in model service", e.getMessage()); + } + + PfModelService.registerModel("ModelName", new DummyPfModel()); + assertTrue(PfModelService.existsModel("ModelName")); + assertNotNull(PfModelService.getModel("ModelName")); + + PfModelService.clear(); + assertFalse(PfModelService.existsModel("ModelName")); + try { + PfModelService.getModel("ModelName"); + } catch (final Exception e) { + assertEquals("Model for name ModelName not found in model service", e.getMessage()); + } + + try { + PfModelService.registerModel(null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.registerModel("nullModelName", null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("model is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.registerModel(null, new DummyPfModel()); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.deregisterModel(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfModelService.getModel(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("modelKey is marked @NonNull but is null", exc.getMessage()); + } + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java new file mode 100644 index 000000000..0ed04c4e6 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java @@ -0,0 +1,199 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +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.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 { + + @Test + public void test() { + DummyPfConceptContainer container = new DummyPfConceptContainer(); + assertNotNull(container); + + container = new DummyPfConceptContainer(); + assertNotNull(container); + + container = new DummyPfConceptContainer(new PfConceptKey()); + assertNotNull(container); + + container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>()); + assertNotNull(container); + + try { + container = new DummyPfConceptContainer((PfConceptKey) null, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + container = new DummyPfConceptContainer(new PfConceptKey(), null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); + } + + try { + container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + container.getKey().setName("Dummy"); + DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container); + assertNotNull(clonedContainer); + assertEquals("Dummy", clonedContainer.getKey().getName()); + + try { + DummyPfConceptContainer conceptContainter = null; + container = new DummyPfConceptContainer(conceptContainter); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + List<PfKey> keyList = container.getKeys(); + assertEquals(1, keyList.size()); + + PfConceptKey conceptKey = new PfConceptKey("Key", "0.0.1"); + Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>(); + conceptMap.put(conceptKey, new DummyPfConcept(conceptKey)); + + container.setConceptMap(conceptMap); + keyList = container.getKeys(); + assertEquals(2, keyList.size()); + + clonedContainer = new DummyPfConceptContainer(container); + assertNotNull(clonedContainer); + assertEquals("Dummy", clonedContainer.getKey().getName()); + assertEquals(2, clonedContainer.getKeys().size()); + + assertEquals(clonedContainer, container); + container.clean(); + assertEquals(clonedContainer, container); + + PfValidationResult result = new PfValidationResult(); + result = container.validate(result); + assertTrue(result.isOk()); + + assertEquals(0, container.compareTo(clonedContainer)); + + try { + container.copyTo(null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertFalse(container.compareTo(null) == 0); + assertEquals(0, container.compareTo(container)); + assertFalse(container.compareTo(conceptKey) == 0); + + DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container); + testContainer.getKey().setVersion("0.0.2"); + assertFalse(container.compareTo(testContainer) == 0); + testContainer.getKey().setVersion(container.getKey().getVersion()); + assertEquals(0, container.compareTo(testContainer)); + + PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1"); + testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertFalse(container.compareTo(testContainer) == 0); + + try { + container.validate(null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + DummyPfConceptContainer validateContainer = new DummyPfConceptContainer(); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1")); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey())); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.getConceptMap().remove(PfConceptKey.getNullKey()); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(testConceptKey, null); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey)); + assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); + + assertEquals(conceptKey, container.get(conceptKey).getKey()); + assertEquals(conceptKey, container.get(conceptKey.getName()).getKey()); + assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey()); + + Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName()); + assertEquals(conceptKey, returnSet.iterator().next().getKey()); + + returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion()); + assertEquals(conceptKey, returnSet.iterator().next().getKey()); + + container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey)); + + DummyPfConceptContainer exceptionOnCopyContainer = new DummyPfConceptContainer(); + try { + container.copyTo(exceptionOnCopyContainer); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals( + "Failed to create a clone of class \"org.onap.policy.models.base.testconcepts.DummyPfConceptSub\"", + exc.getMessage()); + } + } + + @Test(expected = NullPointerException.class) + public void testnullKey() { + PfConceptKey nullKey = null; + new DummyPfConceptContainer(nullKey); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java index 0a5ccdc5a..ae5b2ff2b 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfConceptGetterImplTest.java @@ -32,12 +32,12 @@ import java.util.TreeSet; import org.junit.Test; /** - * Test the AxConceptGetterImpl class. + * Test the given concept class. */ public class PfConceptGetterImplTest { @Test - public void testAxConceptGetterImpl() { + public void testPfConceptGetterImpl() { NavigableMap<PfConceptKey, PfConceptKey> keyMap = new TreeMap<>(); PfConceptGetterImpl<PfConceptKey> getter = new PfConceptGetterImpl<>(keyMap); 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/PfKeyTest.java index 1ffc5d262..848889cc3 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/PfKeyTest.java @@ -31,8 +31,8 @@ import java.lang.reflect.Field; import org.junit.Test; import org.onap.policy.models.base.PfKey.Compatibility; -import org.onap.policy.models.base.testpojos.DummyPfConcept; -import org.onap.policy.models.base.testpojos.DummyPfKey; +import org.onap.policy.models.base.testconcepts.DummyPfConcept; +import org.onap.policy.models.base.testconcepts.DummyPfKey; public class PfKeyTest { @@ -67,6 +67,9 @@ public class PfKeyTest { PfConceptKey someKey4 = new PfConceptKey(someKey1); someKey4.setVersion("0.1.2"); + PfConceptKey someKey4a = new PfConceptKey(someKey1); + someKey4a.setVersion("0"); + PfConceptKey someKey5 = new PfConceptKey(someKey1); someKey5.setVersion("1.2.2"); @@ -77,13 +80,25 @@ public class PfKeyTest { PfConcept pfc = new DummyPfConcept(); assertEquals(PfConceptKey.getNullKey().getId(), pfc.getId()); + assertTrue(PfConceptKey.getNullKey().matchesId(pfc.getId())); + assertTrue(PfConceptKey.getNullKey().isNullKey()); + + try { + PfConceptKey.getNullKey().matchesId(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("id is marked @NonNull but is null", exc.getMessage()); + } + assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new DummyPfKey())); assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1)); assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1)); assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1)); assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1)); + assertEquals(Compatibility.PATCH, someKey4a.getCompatibility(someKey1)); + assertEquals(Compatibility.PATCH, someKey1.getCompatibility(someKey4a)); assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1)); assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1)); @@ -119,8 +134,9 @@ public class PfKeyTest { try { someKey0.compareTo(null); - } catch (IllegalArgumentException e) { - assertEquals("comparison object may not be null", e.getMessage()); + fail("test should throw an exception here"); + } catch (NullPointerException e) { + assertEquals("otherObj is marked @NonNull but is null", e.getMessage()); } assertEquals(0, someKey0.compareTo(someKey0)); @@ -131,6 +147,51 @@ public class PfKeyTest { assertFalse(((PfKey) someKey0).equals(new DummyPfKey())); } + @Test + public void testNullArguments() { + try { + new PfConceptKey((String)null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("id is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey((PfConceptKey)null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey(null, null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("name is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey("name", null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("version is marked @NonNull but is null", exc.getMessage()); + } + + try { + new PfConceptKey(null, "0.0.1"); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("name is marked @NonNull but is null", exc.getMessage()); + } + + try { + PfConceptKey key = new PfConceptKey("AKey", "0.0.1"); + key.isCompatible(null); + fail("id is marked @NonNull but is null"); + } catch (Exception exc) { + assertEquals("otherKey is marked @NonNull but is null", exc.getMessage()); + } + } @Test public void testValidation() { diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java index 7dbde7414..ccdc72dcd 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java @@ -25,24 +25,35 @@ 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; +import org.onap.policy.models.base.testconcepts.DummyPfConceptKeySub; public class PfKeyUseTest { + @SuppressWarnings("unlikely-arg-type") @Test - public void 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()); + } + PfConceptKey key = new PfConceptKey("Key", "0.0.1"); PfKeyUse keyUse = new PfKeyUse(); keyUse.setKey(key); assertEquals(key, keyUse.getKey()); assertEquals("Key:0.0.1", keyUse.getId()); assertEquals(key, keyUse.getKeys().get(0)); + assertFalse(keyUse.isNullKey()); assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key)); assertTrue(keyUse.isCompatible(key)); @@ -74,5 +85,51 @@ public class PfKeyUseTest { PfKeyUse keyUseNull = new PfKeyUse(PfConceptKey.getNullKey()); 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()); + } + + 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()); + } } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java new file mode 100644 index 000000000..cf7c41f6b --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java @@ -0,0 +1,145 @@ +package org.onap.policy.models.base; +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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========================================================= + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +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.testconcepts.DummyPfModel; + +/** + * Test of the PfModel clas. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PfModelTest { + + @Test + public void testPfModel() { + assertNotNull(new DummyPfModel()); + assertNotNull(new DummyPfModel(new PfConceptKey())); + assertNotNull(new DummyPfModel(new DummyPfModel())); + + try { + new DummyPfModel((PfConceptKey)null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + + try { + DummyPfModel nullModel = null; + new DummyPfModel(nullModel); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); + } + + DummyPfModel dpm = new DummyPfModel(new PfConceptKey("modelKey", "0.0.1")); + DummyPfModel dpmClone = new DummyPfModel(dpm); + assertEquals(dpm, dpmClone); + + assertEquals(1, dpm.getKeys().size()); + + dpmClone.clean(); + assertEquals(dpm, dpmClone); + + try { + dpm.copyTo(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("target is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(0, dpm.compareTo(dpmClone)); + assertEquals(-1, dpm.compareTo(null)); + assertEquals(0, dpm.compareTo(dpm)); + assertFalse(dpm.compareTo(dpm.getKey()) == 0); + } + + @Test + public void testPfModelValidation() { + PfConceptKey dpmKey = new PfConceptKey("modelKey", "0.0.1"); + DummyPfModel dpm = new DummyPfModel(dpmKey); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + try { + dpm.validate(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); + } + + dpm.setKey(PfConceptKey.getNullKey()); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.setKey(dpmKey); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + dpm.getKeyList().add(PfReferenceKey.getNullKey()); + dpm.getKeyList().add(new PfKeyUse(PfReferenceKey.getNullKey())); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().clear(); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfConceptKey goodCKey = new PfConceptKey("goodCKey", "0.0.1"); + PfReferenceKey goodRKey = new PfReferenceKey(goodCKey, "goodLocalName"); + + dpm.getKeyList().add(goodCKey); + dpm.getKeyList().add(goodRKey); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfConceptKey goodCKeyDup = new PfConceptKey(goodCKey); + dpm.getKeyList().add(goodCKeyDup); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(goodCKeyDup); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfReferenceKey goodRKeyDup = new PfReferenceKey(goodRKey); + dpm.getKeyList().add(goodRKeyDup); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(goodRKeyDup); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfKeyUse goodCKeyUse = new PfKeyUse(goodCKey); + dpm.getKeyList().add(goodCKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfKeyUse goodRKeyUse = new PfKeyUse(goodRKey); + dpm.getKeyList().add(goodRKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfConceptKey badCKey = new PfConceptKey("badCKey", "0.0.1"); + PfKeyUse badCKeyUse = new PfKeyUse(badCKey); + dpm.getKeyList().add(badCKeyUse); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(badCKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + + PfKeyUse badRKeyUse = new PfKeyUse(new PfReferenceKey(badCKey, "badLocalName")); + dpm.getKeyList().add(badRKeyUse); + assertFalse(dpm.validate(new PfValidationResult()).isValid()); + dpm.getKeyList().remove(badRKeyUse); + assertTrue(dpm.validate(new PfValidationResult()).isValid()); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java index feedc2cc3..64d4bc6b8 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java @@ -47,10 +47,20 @@ public class PfReferenceKeyTest { assertEquals(PfReferenceKey.getNullKey().getKey(), PfReferenceKey.getNullKey()); assertEquals("NULL:0.0.0:NULL:NULL", PfReferenceKey.getNullKey().getId()); + try { + new PfReferenceKey(new PfConceptKey(), null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("parameter \"localName\" is null", exc.getMessage()); + } + PfReferenceKey testReferenceKey = new PfReferenceKey(); testReferenceKey.setParentConceptKey(new PfConceptKey("PN", "0.0.1")); assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId()); + assertEquals(1, testReferenceKey.getKeys().size()); + assertFalse(testReferenceKey.isNullKey()); + testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", "0.0.1", "LN")); assertEquals("PN:0.0.1:NULL:LN", testReferenceKey.getParentReferenceKey().getId()); diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java new file mode 100644 index 000000000..2b495a1e7 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +/** + * Test the PfUtils class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PfUtilsTest { + + @Test + public void testPfUtils() { + assertEquals(0, PfUtils.compareObjects(null, null)); + assertEquals(-1, PfUtils.compareObjects("hello", null)); + assertEquals(1, PfUtils.compareObjects(null, "hello")); + assertFalse(PfUtils.compareObjects("hello", "goodbye") == 0); + assertEquals(0, PfUtils.compareObjects("hello", "hello")); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java new file mode 100644 index 000000000..9fb6b5793 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.testconcepts; + +import java.util.List; + +import javax.persistence.EmbeddedId; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +@Data +@EqualsAndHashCode(callSuper = false) +public class DummyPfConcept extends PfConcept { + private static final long serialVersionUID = 1L; + @EmbeddedId + private PfConceptKey key; + + private String description; + + /** + * The Default Constructor creates a {@link DummyPfConcept} object with a null key. + */ + public DummyPfConcept() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link DummyPfConcept} object with the given concept key. + * + * @param key the key + */ + public DummyPfConcept(@NonNull final PfConceptKey key) { + this.key = key; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public DummyPfConcept(final DummyPfConcept copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + return keyList; + } + + @Override + public void clean() { + key.clean(); + + description = (description != null ? description.trim() : null); + } + + @Override + public PfValidationResult validate(PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property description may not be blank")); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final DummyPfConcept other = (DummyPfConcept) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + return ObjectUtils.compare(description, other.description); + } + + @Override + public PfConcept copyTo(@NonNull PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfConcept.class); + + final DummyPfConcept copy = ((DummyPfConcept) copyObject); + copy.setKey(new PfConceptKey(key)); + copy.setDescription(description); + + return copy; + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptContainer.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptContainer.java new file mode 100644 index 000000000..ac72ef8f6 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptContainer.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.testconcepts; + +import java.util.Map; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; + +/** + * Dummy container for PF concepts. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyPfConceptContainer extends PfConceptContainer<DummyPfConcept> { + private static final long serialVersionUID = -3018432331484294280L; + + + /** + * The Default Constructor creates a {@link DummyPfConceptContainer} object with a null artifact key + * and creates an empty concept map. + */ + public DummyPfConceptContainer() { + super(); + } + + /** + * The Key Constructor creates a {@link DummyPfConceptContainer} object with the given artifact key and + * creates an empty concept map. + * + * @param key the concept key + */ + public DummyPfConceptContainer(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public DummyPfConceptContainer(@NonNull final PfConceptKey key, + @NonNull final Map<PfConceptKey, DummyPfConcept> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public DummyPfConceptContainer(@NonNull final DummyPfConceptContainer copyConcept) { + super(copyConcept); + } + +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfConcept.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java index f28477f70..da18cba20 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfConcept.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java @@ -18,60 +18,36 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.base.testpojos; +package org.onap.policy.models.base.testconcepts; -import java.util.Arrays; -import java.util.List; +import javax.ws.rs.core.Response; + +import lombok.NonNull; -import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfKey; -import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfModelRuntimeException; -public class DummyPfConcept extends PfConcept { +/** + * KeyUse subclass that throws exception on default constructor for testing. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyPfConceptKeySub extends PfConceptKey { private static final long serialVersionUID = 1L; - @Override - public int compareTo(PfConcept concept) { - return 0; - } - - @Override - public PfKey getKey() { - return new PfConceptKey(); - } - - @Override - public List<PfKey> getKeys() { - return Arrays.asList(getKey()); - } - - @Override - public PfValidationResult validate(PfValidationResult result) { - return null; - } - - @Override - public void clean() { - } - - @Override - public boolean equals(Object otherObject) { - return false; - } - - @Override - public String toString() { - return null; - } - - @Override - public int hashCode() { - return 0; + /** + * The Default Constructor creates this concept with a null key. + */ + public DummyPfConceptKeySub() { + throw new PfModelRuntimeException(Response.Status.BAD_GATEWAY, "Some error message"); } - @Override - public PfConcept copyTo(PfConcept target) { - return null; + /** + * This constructor creates an instance of this class, and holds a reference to a used key. + * + * @param usedKey a used key + */ + public DummyPfConceptKeySub(@NonNull final PfConceptKey usedKey) { + super(usedKey); } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptSub.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptSub.java new file mode 100644 index 000000000..12875eaca --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptSub.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.testconcepts; + +import javax.ws.rs.core.Response; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelRuntimeException; + +public class DummyPfConceptSub extends DummyPfConcept { + private static final long serialVersionUID = 1L; + + /** + * The Default Constructor creates a {@link DummyPfConceptSub} object with a null key. + */ + public DummyPfConceptSub() { + throw new PfModelRuntimeException(Response.Status.BAD_GATEWAY, "Some error message"); + } + + /** + * The Key Constructor creates a {@link DummyPfConceptSub} object with the given concept key. + * + * @param key the key + */ + public DummyPfConceptSub(@NonNull final PfConceptKey key) { + super(key); + } +} diff --git a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfKey.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java index 247ea88d1..6cf41e60c 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testpojos/DummyPfKey.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.base.testpojos; +package org.onap.policy.models.base.testconcepts; import java.util.Arrays; import java.util.List; @@ -41,6 +41,11 @@ public class DummyPfKey extends PfKey { } @Override + public boolean isNullKey() { + return false; + } + + @Override public Compatibility getCompatibility(PfKey otherKey) { return null; } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java new file mode 100644 index 000000000..199a37f59 --- /dev/null +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java @@ -0,0 +1,174 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.testconcepts; + +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.core.Response; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModel; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfValidationResult; + +@Data +@EqualsAndHashCode(callSuper = true) +public class DummyPfModel extends PfModel { + private static final long serialVersionUID = 8800599637708309945L; + + private List<PfKey> keyList; + + /** + * The Default Constructor creates a {@link DummyPfModel} object with a null concept key and + * creates an empty TOSCA model. + */ + public DummyPfModel() { + super(); + super.setKey(new PfConceptKey()); + this.keyList = new ArrayList<PfKey>(); + } + + /** + * The Key Constructor creates a {@link DummyPfModel} object with the given concept key and + * creates an empty TOSCA model. + * + * @param key the TOSCA model key + */ + public DummyPfModel(final PfConceptKey key) { + super(key); + this.keyList = new ArrayList<PfKey>(); + } + + /** + * Constructor that initiates a {@link ToscaModel} with all its fields. + * + * @param key the TOSCA model key + * @param keyList the service templates in the event model + */ + public DummyPfModel(final PfConceptKey key, final List<PfKey> keyList) { + super(key); + this.keyList = keyList; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public DummyPfModel(final DummyPfModel copyConcept) { + super(copyConcept); + } + + @Override + public void register() { + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> listOfKeys = super.getKeys(); + + listOfKeys.addAll(keyList); + + return listOfKeys; + } + + @Override + public void clean() { + super.clean(); + for (PfKey pfKey : keyList) { + pfKey.clean(); + } + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + for (PfKey pfKey : keyList) { + result = pfKey.validate(result); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (super.compareTo(otherConcept) != 0) { + return super.compareTo(otherConcept); + } + + if (otherConcept == null) { + return -1; + } + + if (this == otherConcept) { + return 0; + } + + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final DummyPfModel other = (DummyPfModel) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + if (!keyList.equals(other.keyList)) { + return (keyList.hashCode() - other.keyList.hashCode()); + } + + return 0; + } + + @Override + public PfConcept copyTo(final PfConcept targetObject) { + super.copyTo(targetObject); + + Assertions.instanceOf(targetObject, DummyPfModel.class); + + final DummyPfModel copy = ((DummyPfModel) targetObject); + + final List<PfKey> newKeyList = new ArrayList<>(); + for (final PfKey pfKey : keyList) { + PfKey newPfKey; + try { + newPfKey = pfKey.getClass().newInstance(); + } catch (final Exception e) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, + "error copying concept key: " + e.getMessage(), e); + } + newPfKey.copyTo(pfKey); + newKeyList.add(newPfKey); + } + copy.setKeyList(newKeyList); + + + return copy; + } +} |