diff options
author | liamfallon <liam.fallon@est.tech> | 2019-03-19 01:16:38 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-03-19 01:16:38 +0000 |
commit | 923a9943a1d59a9d1e87d24490eea78fa9869de7 (patch) | |
tree | 23558e0aa07330a29b2d102740ec87785625d273 /model/utilities | |
parent | b804d0b81b2f6be2561b7606cf67fbad37928307 (diff) |
Use Assertions class from policy-common
Change import line in all classes that use
the Assertions class, there are a lot of them.
Issue-ID: POLICY-1264
Change-Id: I8480264be4e36348b3fc63acf1bc36e9c9dd250b
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'model/utilities')
-rw-r--r-- | model/utilities/src/main/java/org/onap/policy/apex/model/utilities/Assertions.java | 191 | ||||
-rw-r--r-- | model/utilities/src/test/java/org/onap/policy/apex/model/utilities/AssertionsTest.java | 93 |
2 files changed, 0 insertions, 284 deletions
diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/Assertions.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/Assertions.java deleted file mode 100644 index 02a91a72c..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/Assertions.java +++ /dev/null @@ -1,191 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.model.utilities; - -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The Class Assertions is a template class that is used as a shorthand for assertions in the source code. - * - * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com) - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public final class Assertions { - // Logger for this class - private static final XLogger LOGGER = XLoggerFactory.getXLogger(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/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/AssertionsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/AssertionsTest.java deleted file mode 100644 index d1040b64b..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/AssertionsTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.model.utilities; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.onap.policy.apex.model.utilities.Assertions; - -/** - * 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()); - } - } -} |