summaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java190
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java98
2 files changed, 288 insertions, 0 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java b/utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java
new file mode 100644
index 00000000..19a25295
--- /dev/null
+++ b/utils/src/main/java/org/onap/policy/common/utils/validation/Assertions.java
@@ -0,0 +1,190 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * Modifications 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.common.utils.validation;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The Class Assertions is a static class that is used as a shorthand for assertions in the source code.
+ * It throws runtime exceptions on assertion fails.
+ */
+public final class Assertions {
+ // Logger for this class
+ private static final Logger LOGGER = LoggerFactory.getLogger(Assertions.class);
+
+ /**
+ * Private constructor used to prevent sub class instantiation.
+ */
+ private Assertions() {
+ }
+
+ /**
+ * Gets the validation message for a string parameter.
+ *
+ * @param parameterName the string parameter name
+ * @param parameterValue the string parameter value
+ * @param pattern The regular expression
+ * @return null if the parameter is valid, the validation message otherwise
+ */
+ public static String getStringParameterValidationMessage(final String parameterName, final String parameterValue,
+ final String pattern) {
+ try {
+ validateStringParameter(parameterName, parameterValue, pattern);
+ } catch (IllegalArgumentException e) {
+ String message = "parameter " + parameterName + " with value " + parameterValue
+ + " does not match regular expression " + pattern;
+ if (LOGGER.isTraceEnabled()) {
+ LOGGER.trace(message, e);
+ }
+
+ return message;
+ }
+
+ return null;
+ }
+
+ /**
+ * Checks if a string parameter matches a regular expression.
+ *
+ * @param parameterName the string parameter name
+ * @param parameterValue the string parameter value
+ * @param pattern The regular expression
+ * @return the trimmed string
+ */
+ public static String validateStringParameter(final String parameterName, final String parameterValue,
+ final String pattern) {
+ argumentNotNull(parameterName, "parameter name is null");
+ argumentNotNull(parameterValue, "parameter \"" + parameterName + "\" is null");
+ argumentNotNull(pattern, "parameter pattern is null");
+
+ final String trimmedValue = parameterValue.trim();
+ if (trimmedValue.matches(pattern)) {
+ return trimmedValue;
+ } else {
+ throw new IllegalArgumentException("parameter \"" + parameterName + "\": value \"" + parameterValue
+ + "\", does not match regular expression \"" + pattern + "\"");
+ }
+ }
+
+ /**
+ * Used as a shorthand to check that method arguments are not null, throws IllegalArgumentException on error.
+ *
+ * @param <T> the generic type of the argument to check
+ * @param value the value of the type
+ * @param message the error message to issue
+ */
+ public static <T> void argumentNotNull(final T value, final String message) {
+ if (value == null) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Used as a shorthand to check that method arguments are not false, throws IllegalArgumentException on error.
+ *
+ * @param value the value to check if false
+ * @param message the error message to issue
+ */
+ public static void argumentNotFalse(final boolean value, final String message) {
+ if (!value) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Used as a shorthand to check that method arguments are not null, throws an exception of the specified type on
+ * error.
+ *
+ * @param <T> the generic type of the argument to check
+ * @param <E> the exception to throw if incoming value is null
+ * @param value the value of the type
+ * @param exceptionClass the class of exception to return an instance of
+ * @param message the error message to issue
+ * @throws E an instance of the passed Exception Class
+ */
+ public static <T, E extends Exception> void argumentOfClassNotNull(final T value, final Class<E> exceptionClass,
+ final String message) throws E {
+ if (value == null) {
+ // Instantiate the exception and throw it
+ try {
+ throw exceptionClass.getConstructor(String.class).newInstance(message);
+ } catch (final Exception errorException) {
+ throw new IllegalArgumentException(message, errorException);
+ }
+ }
+ }
+
+ /**
+ * Used as a shorthand to check that method argument is not false, throws an exception of the specified type on
+ * error.
+ *
+ * @param <E> the exception to throw if incoming value is false
+ * @param value the value to check if false
+ * @param exceptionClass the class of exception to return an instance of
+ * @param message the error message to issue
+ * @throws E an instance of the passed Exception Class
+ */
+ public static <E extends Exception> void argumentOfClassNotFalse(final boolean value, final Class<E> exceptionClass,
+ final String message) throws E {
+ if (!value) {
+ // Instantiate the exception and throw it
+ try {
+ throw exceptionClass.getConstructor(String.class).newInstance(message);
+ } catch (final Exception errorException) {
+ throw new IllegalArgumentException(message, errorException);
+ }
+ }
+ }
+
+ /**
+ * Used as a shorthand to check that an object is an instance of a given class, throws IllegalArgumentException on
+ * error.
+ *
+ * @param <T> the generic type of the argument to check
+ * @param objectInstance the object instance for which to check the class
+ * @param requiredClass the class that the object should be an instance of
+ * @throws IllegalArgumentException if the incoming object is not an instance of requiredClass
+ */
+ public static <T> void instanceOf(final Object objectInstance, final Class<T> requiredClass) {
+ if (!requiredClass.isAssignableFrom(objectInstance.getClass())) {
+ throw new IllegalArgumentException(objectInstance.getClass().getCanonicalName() + " is not an instance of "
+ + requiredClass.getCanonicalName());
+ }
+ }
+
+ /**
+ * Used as a shorthand to check that an instance of a class can be an instance of a given class, throws
+ * IllegalArgumentException on error.
+ *
+ * @param <T> the generic type of the argument to check
+ * @param checkClass the class to check
+ * @param requiredClass the class that the object should be an instance of
+ * @throws IllegalArgumentException if the incoming object is not an instance of requiredClass
+ */
+ public static <T> void assignableFrom(final Class<?> checkClass, final Class<T> requiredClass) {
+ if (!requiredClass.isAssignableFrom(checkClass)) {
+ throw new IllegalArgumentException(checkClass.getCanonicalName() + " is not an instance of "
+ + requiredClass.getCanonicalName());
+ }
+ }
+}
diff --git a/utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java b/utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java
new file mode 100644
index 00000000..e39058bd
--- /dev/null
+++ b/utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java
@@ -0,0 +1,98 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * Modifications 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.common.utils.validation;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+/**
+ * The Class ResourceUtilsTest.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class AssertionsTest {
+ @Test
+ public void testAssertions() {
+ Assertions.argumentNotFalse(true, "it is true");
+
+ try {
+ Assertions.argumentNotFalse(false, "it is false");
+ } catch (IllegalArgumentException e) {
+ assertEquals("it is false", e.getMessage());
+ }
+
+ Assertions.argumentOfClassNotFalse(true, ArithmeticException.class, "it is true");
+
+ try {
+ Assertions.argumentOfClassNotFalse(false, ArithmeticException.class, "it is false");
+ } catch (Exception e) {
+ assertEquals("it is false", e.getMessage());
+ }
+
+ Assertions.argumentNotNull("Hello", "it is OK");
+
+ try {
+ Assertions.argumentNotNull(null, "it is null");
+ } catch (IllegalArgumentException e) {
+ assertEquals("it is null", e.getMessage());
+ }
+
+ Assertions.argumentOfClassNotNull(true, ArithmeticException.class, "it is OK");
+
+ try {
+ Assertions.argumentOfClassNotNull(null, ArithmeticException.class, "it is null");
+ } catch (Exception e) {
+ assertEquals("it is null", e.getMessage());
+ }
+
+ Assertions.assignableFrom(java.util.TreeMap.class, java.util.Map.class);
+
+ try {
+ Assertions.assignableFrom(java.util.Map.class, java.util.TreeMap.class);
+ } catch (IllegalArgumentException e) {
+ assertEquals("java.util.Map is not an instance of java.util.TreeMap", e.getMessage());
+ }
+
+ Assertions.instanceOf("Hello", String.class);
+
+ try {
+ Assertions.instanceOf(100, String.class);
+ } catch (IllegalArgumentException e) {
+ assertEquals("java.lang.Integer is not an instance of java.lang.String", e.getMessage());
+ }
+
+ Assertions.validateStringParameter("name", "MyName", "^M.*e$");
+
+ try {
+ Assertions.validateStringParameter("name", "MyName", "^M.*f$");
+ } catch (IllegalArgumentException e) {
+ assertEquals("parameter \"name\": value \"MyName\", does not match regular expression \"^M.*f$\"",
+ e.getMessage());
+ }
+
+ assertNull(Assertions.getStringParameterValidationMessage("Greeting", "Hello", "^H.*o$"));
+ assertEquals("parameter Greeting with value Hello does not match regular expression Goodbye",
+ Assertions.getStringParameterValidationMessage("Greeting", "Hello", "Goodbye"));
+ }
+}