diff options
Diffstat (limited to 'models-base')
4 files changed, 134 insertions, 54 deletions
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 index 77ef939bd..863a3ef02 100644 --- 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 @@ -66,7 +66,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @EqualsAndHashCode(callSuper = false) public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> extends PfConcept - implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> { + implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> { private static final long serialVersionUID = -324211738823208318L; private static final Pattern KEY_ID_PATTERN = Pattern.compile(PfKey.KEY_ID_REGEXP); @@ -193,9 +193,9 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex } incomingConceptEntry.getValue().setName(findConceptField(conceptKey, conceptKey.getName(), - incomingConceptEntry.getValue(), PfNameVersion::getName)); + incomingConceptEntry.getValue(), PfNameVersion::getName)); incomingConceptEntry.getValue().setVersion(findConceptField(conceptKey, conceptKey.getVersion(), - incomingConceptEntry.getValue(), PfNameVersion::getVersion)); + incomingConceptEntry.getValue(), PfNameVersion::getVersion)); C jpaConcept = getConceptNewInstance(); // This cast allows us to call the fromAuthorative method @@ -212,7 +212,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex if (conceptMap.isEmpty()) { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, - "An incoming list of concepts must have at least one entry"); + "An incoming list of concepts must have at least one entry"); } } @@ -246,7 +246,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex if (key.equals(PfConceptKey.getNullKey())) { result.addValidationMessage( - new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); } result = key.validate(result); @@ -270,14 +270,14 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex 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")); + "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")); + "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())); + "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); @@ -305,11 +305,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex return retVal; } - if (!conceptMap.equals(other.conceptMap)) { - return (conceptMap.hashCode() - other.conceptMap.hashCode()); - } - - return 0; + return PfUtils.compareMaps(conceptMap, other.conceptMap); } /** @@ -349,7 +345,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex @Override public C get(final String conceptKeyName, final String conceptKeyVersion) { return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKeyName, - conceptKeyVersion); + conceptKeyVersion); } @Override @@ -360,7 +356,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex @Override public Set<C> getAll(final String conceptKeyName, final String conceptKeyVersion) { return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).getAll(conceptKeyName, - conceptKeyVersion); + conceptKeyVersion); } /** @@ -372,16 +368,16 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex private C getConceptNewInstance() { try { String conceptClassName = - ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName(); + ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName(); return (C) Class.forName(conceptClassName).getDeclaredConstructor().newInstance(); } catch (Exception ex) { throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, - "failed to instantiate instance of container concept class", ex); + "failed to instantiate instance of container concept class", ex); } } private String findConceptField(final PfConceptKey conceptKey, final String keyFieldValue, - final PfNameVersion concept, final Function<PfNameVersion, String> fieldGetterFunction) { + final PfNameVersion concept, final Function<PfNameVersion, String> fieldGetterFunction) { String conceptField = fieldGetterFunction.apply(concept); @@ -389,7 +385,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex return keyFieldValue; } else { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, "Key " + conceptKey.getId() + " field " - + keyFieldValue + " does not match the value " + conceptField + " in the concept field"); + + keyFieldValue + " does not match the value " + conceptField + " in the concept field"); } } } 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 index d5b70d78a..57d23450a 100644 --- 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 @@ -22,6 +22,7 @@ package org.onap.policy.models.base; import java.lang.reflect.InvocationTargetException; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -29,6 +30,8 @@ import java.util.Map.Entry; import java.util.function.Function; import java.util.stream.Collectors; import javax.ws.rs.core.Response; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; /** * Utility class for Policy Framework concept utilities. @@ -68,6 +71,37 @@ public final class PfUtils { } /** + * Compare two collections of the same type, nulls are allowed. + * + * @param leftCollection the first collection + * @param rightCollection the second collection + * @return a measure of the comparison + */ + public static <T> int compareCollections(final Collection<T> leftCollection, final Collection<T> rightCollection) { + if (CollectionUtils.isEmpty(leftCollection) && CollectionUtils.isEmpty(rightCollection)) { + return 0; + } + + return compareObjects(leftCollection, rightCollection); + } + + + /** + * Compare two maps of the same type, nulls are allowed. + * + * @param leftMap the first map + * @param rightMap the second map + * @return a measure of the comparison + */ + public static <K, V> int compareMaps(final Map<K, V> leftMap, final Map<K, V> rightMap) { + if (MapUtils.isEmpty(leftMap) && MapUtils.isEmpty(rightMap)) { + return 0; + } + + return compareObjects(leftMap, rightMap); + } + + /** * Convenience method to apply a mapping function to all of the elements of a list, generating a new list. * * @param source list whose elements are to be mapped, or {@code null} 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 6f4633612..bbac3b7e5 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,23 +33,23 @@ public class PfValidationResult { * The ValidationResult enumeration describes the severity of a validation result. */ public enum ValidationResult { - /** No problems or observations were detected during validation. */ - VALID, - /** - * Observations were made on a concept (such as blank descriptions) of a nature that will not - * affect the use of the concept. - */ - OBSERVATION, - /** - * Warnings were made on a concept (such as defined but unused concepts) of a nature that may - * affect the use of the concept. - */ - WARNING, - /** - * Errors were detected on a concept (such as referenced but undefined concepts) of a nature - * that will affect the use of the concept. - */ - INVALID + /** No problems or observations were detected during validation. */ + VALID, + /** + * Observations were made on a concept (such as blank descriptions) of a nature that will not affect the use of + * the concept. + */ + OBSERVATION, + /** + * Warnings were made on a concept (such as defined but unused concepts) of a nature that may affect the use of + * the concept. + */ + WARNING, + /** + * Errors were detected on a concept (such as referenced but undefined concepts) of a nature that will affect + * the use of the concept. + */ + INVALID } // The actual verification result @@ -59,8 +59,8 @@ public class PfValidationResult { private final List<PfValidationMessage> messageList = new LinkedList<>(); /** - * Check if a validation reported a valid concept, returns true if the model is usable (that is, - * even if the model has warnings or observations). + * Check if a validation reported a valid concept, returns true if the model is usable (that is, even if the model + * has warnings or observations). * * @return true, if the concept is reported as valid and can be used */ @@ -69,8 +69,7 @@ public class PfValidationResult { } /** - * Check if a validation reported a concept with no errors or warnings, returns true if the - * model is OK to use. + * Check if a validation reported a concept with no errors or warnings, returns true if the model is OK to use. * * @return true, if the concept has no warnings or errors */ @@ -78,9 +77,21 @@ public class PfValidationResult { return validationResult == ValidationResult.VALID || validationResult == ValidationResult.OBSERVATION; } + + /** + * Append a validation result to another validation result. + * + * @param result2Append the result to append to the current validation result + */ + public void append(final PfValidationResult result2Append) { + for (PfValidationMessage message : result2Append.getMessageList()) { + addValidationMessage(message); + } + } + /** - * Adds a validation message to the validation result, used by validate() implementations on - * {@link PfConcept} subclasses to report validaiton observations. + * Adds a validation message to the validation result, used by validate() implementations on {@link PfConcept} + * subclasses to report validaiton observations. * * @param validationMessage the validation message */ diff --git a/models-base/src/test/java/org/onap/policy/models/base/ValidationTest.java b/models-base/src/test/java/org/onap/policy/models/base/ValidationTest.java index d536e406f..6030d2ab9 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/ValidationTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/ValidationTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,7 +35,7 @@ public class ValidationTest { private static final String SOME_MESSAGE = "Some message"; @Test - public void test() { + public void testValidation1() { PfValidationResult result = new PfValidationResult(); PfConceptKey pfKeyey = new PfConceptKey("PK", "0.0.1"); result = pfKeyey.validate(result); @@ -47,7 +47,7 @@ public class ValidationTest { assertNotNull(result.getMessageList()); PfValidationMessage vmess0 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, - ValidationResult.VALID, SOME_MESSAGE); + ValidationResult.VALID, SOME_MESSAGE); result.addValidationMessage(vmess0); assertTrue(result.isOk()); @@ -57,7 +57,7 @@ public class ValidationTest { assertNotNull(HELLO, result.toString()); PfValidationMessage vmess1 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, - ValidationResult.OBSERVATION, SOME_MESSAGE); + ValidationResult.OBSERVATION, SOME_MESSAGE); result.addValidationMessage(vmess1); assertTrue(result.isOk()); @@ -65,9 +65,15 @@ public class ValidationTest { assertEquals(PfValidationResult.ValidationResult.OBSERVATION, result.getValidationResult()); assertNotNull(result.getMessageList()); assertNotNull(HELLO, result.toString()); + } + + + @Test + public void testValidation2() { + PfValidationResult result = new PfValidationResult(); PfValidationMessage vmess2 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, - ValidationResult.WARNING, SOME_MESSAGE); + ValidationResult.WARNING, SOME_MESSAGE); result.addValidationMessage(vmess2); assertFalse(result.isOk()); @@ -77,7 +83,7 @@ public class ValidationTest { assertNotNull(HELLO, result.toString()); PfValidationMessage vmess3 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, - ValidationResult.INVALID, SOME_MESSAGE); + ValidationResult.INVALID, SOME_MESSAGE); result.addValidationMessage(vmess3); assertFalse(result.isOk()); @@ -86,9 +92,42 @@ public class ValidationTest { assertNotNull(result.getMessageList()); assertNotNull(HELLO, result.toString()); - assertEquals(PfValidationResult.ValidationResult.INVALID, result.getMessageList().get(3).getValidationResult()); - assertEquals(SOME_MESSAGE, result.getMessageList().get(3).getMessage()); - assertEquals(PfConceptKey.class.getName(), result.getMessageList().get(3).getObservedClass()); - assertEquals(PfConceptKey.getNullKey(), result.getMessageList().get(3).getObservedKey()); + assertEquals(PfValidationResult.ValidationResult.INVALID, result.getMessageList().get(1).getValidationResult()); + assertEquals(SOME_MESSAGE, result.getMessageList().get(1).getMessage()); + assertEquals(PfConceptKey.class.getName(), result.getMessageList().get(1).getObservedClass()); + assertEquals(PfConceptKey.getNullKey(), result.getMessageList().get(1).getObservedKey()); + } + + @Test + public void testValidationAppend() { + PfValidationResult result = new PfValidationResult(); + + PfValidationResult result2 = new PfValidationResult(); + PfValidationMessage vmess1 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.OBSERVATION, "Message 1"); + result2.addValidationMessage(vmess1); + + result.append(result2); + assertEquals(1, result.getMessageList().size()); + assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); + + PfValidationResult result3 = new PfValidationResult(); + PfValidationMessage vmess2 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.WARNING, "Message 2"); + result3.addValidationMessage(vmess2); + result.append(result3); + assertEquals(2, result.getMessageList().size()); + assertEquals(ValidationResult.WARNING, result.getValidationResult()); + + PfValidationResult result4 = new PfValidationResult(); + PfValidationMessage vmess3 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.INVALID, "Message 3"); + PfValidationMessage vmess4 = new PfValidationMessage(PfConceptKey.getNullKey(), PfConceptKey.class, + ValidationResult.VALID, "Message 4"); + result4.addValidationMessage(vmess3); + result4.addValidationMessage(vmess4); + result.append(result4); + assertEquals(4, result.getMessageList().size()); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); } } |