diff options
Diffstat (limited to 'common-parameters')
3 files changed, 70 insertions, 0 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java index 57232e1c..0b66a533 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java @@ -23,6 +23,7 @@ package org.onap.policy.common.parameters; import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import org.onap.policy.common.parameters.annotations.Max; import org.onap.policy.common.parameters.annotations.Min; import org.onap.policy.common.parameters.annotations.NotBlank; import org.onap.policy.common.parameters.annotations.NotNull; @@ -64,6 +65,11 @@ public class ParameterValidationResult implements ValidationResult { if (minAnnot != null && ((Number) parameterValue).longValue() < minAnnot.value()) { setResult(ValidationStatus.INVALID, "must be >= " + minAnnot.value()); } + + Max maxAnnot = field.getAnnotation(Max.class); + if (maxAnnot != null && ((Number) parameterValue).longValue() > maxAnnot.value()) { + setResult(ValidationStatus.INVALID, "must be <= " + maxAnnot.value()); + } } } diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java new file mode 100644 index 00000000..1d8fd90f --- /dev/null +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java @@ -0,0 +1,39 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.parameters.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(RUNTIME) +@Target(FIELD) +public @interface Max { + + /** + * The maximum value allowed. + * + * @return the maximum value allowed + */ + long value(); +} diff --git a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java index 8adf1165..eb673222 100644 --- a/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java +++ b/common-parameters/src/test/java/org/onap/policy/common/parameters/TestValidation.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.junit.Test; +import org.onap.policy.common.parameters.annotations.Max; import org.onap.policy.common.parameters.annotations.Min; import org.onap.policy.common.parameters.annotations.NotBlank; import org.onap.policy.common.parameters.annotations.NotNull; @@ -50,6 +51,7 @@ public class TestValidation { private static final String NOT_NULL_OBJECT_NAME = "notNullObject"; private static final String NOT_NULL_STRING_NAME = "notNullString"; private static final String MIN_LONG_NAME = "minLong"; + private static final String MAX_LONG_NAME = "maxLong"; @NotNull private String notNullString; @@ -66,6 +68,9 @@ public class TestValidation { @Min(value = 10) private long minLong; + @Max(value = 10) + private long maxLong; + @Test public void testValidationOk() throws IOException { TestParametersL00 l0Parameters = new TestParametersL00("l0Parameters"); @@ -362,6 +367,26 @@ public class TestValidation { assertEquals(ValidationStatus.CLEAN, result.getStatus()); } + @Test + public void testParameterValidationResult_Max() throws Exception { + ParameterValidationResult result = + new ParameterValidationResult(TestValidation.class.getDeclaredField(MAX_LONG_NAME), 11L); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + assertEquals("field 'maxLong' type 'long' value '11' INVALID, must be <= 10\n".replace('\'', '"'), + result.getResult()); + + result = new ParameterValidationResult(TestValidation.class.getDeclaredField(MAX_LONG_NAME), 20); + assertEquals(ValidationStatus.INVALID, result.getStatus()); + assertEquals("field 'maxLong' type 'long' value '20' INVALID, must be <= 10\n".replace('\'', '"'), + result.getResult()); + + result = new ParameterValidationResult(TestValidation.class.getDeclaredField(MAX_LONG_NAME), 10L); + assertEquals(ValidationStatus.CLEAN, result.getStatus()); + + result = new ParameterValidationResult(TestValidation.class.getDeclaredField(MAX_LONG_NAME), 9); + assertEquals(ValidationStatus.CLEAN, result.getStatus()); + } + // these classes are used to test class-level annotations |