From 4e7280f435add3bfe69cd70a25cb1ecffbb71653 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Fri, 16 Jan 2026 19:19:18 +0000 Subject: [PATCH] Remove exception test helpers from policy-common Issue-ID: POLICY-5532 Change-Id: I4ecad091a0de354ae2d321a4c2a9727e0192994d Signed-off-by: danielhanrahan --- .../bus/event/client/TopicSinkClientException.java | 19 +- .../common/utils/test/ConstructionError.java | 64 --- .../policy/common/utils/test/ErrorsTester.java | 48 -- .../policy/common/utils/test/ExceptionsTester.java | 208 -------- .../policy/common/utils/test/ThrowablesTester.java | 564 --------------------- .../bus/event/client/TopicClientExceptionTest.java | 6 +- .../common/utils/test/ConstructionErrorTest.java | 35 -- .../policy/common/utils/test/ErrorsTesterTest.java | 78 --- .../common/utils/test/ExceptionsTesterTest.java | 113 ----- .../common/utils/test/ThrowablesTesterTest.java | 231 --------- 10 files changed, 4 insertions(+), 1362 deletions(-) delete mode 100644 policy-common/src/main/java/org/onap/policy/common/utils/test/ConstructionError.java delete mode 100644 policy-common/src/main/java/org/onap/policy/common/utils/test/ErrorsTester.java delete mode 100644 policy-common/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java delete mode 100644 policy-common/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java delete mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.java delete mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/ErrorsTesterTest.java delete mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/ExceptionsTesterTest.java delete mode 100644 policy-common/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java diff --git a/policy-common/src/main/java/org/onap/policy/common/message/bus/event/client/TopicSinkClientException.java b/policy-common/src/main/java/org/onap/policy/common/message/bus/event/client/TopicSinkClientException.java index fad5e1199..1f618a782 100644 --- a/policy-common/src/main/java/org/onap/policy/common/message/bus/event/client/TopicSinkClientException.java +++ b/policy-common/src/main/java/org/onap/policy/common/message/bus/event/client/TopicSinkClientException.java @@ -3,7 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019, 2023-2024 Nordix Foundation. + * Modifications Copyright (C) 2019-2026 OpenInfra Foundation Europe. 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. @@ -30,25 +30,8 @@ public class TopicSinkClientException extends Exception { @Serial private static final long serialVersionUID = 1L; - public TopicSinkClientException() { - super(); - } - public TopicSinkClientException(final String message) { super(message); } - public TopicSinkClientException(final Throwable cause) { - super(cause); - } - - public TopicSinkClientException(final String message, final Throwable cause) { - super(message, cause); - } - - public TopicSinkClientException(final String message, final Throwable cause, final boolean enableSuppression, - final boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - } diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/ConstructionError.java b/policy-common/src/main/java/org/onap/policy/common/utils/test/ConstructionError.java deleted file mode 100644 index d34385776..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/utils/test/ConstructionError.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * ============LICENSE_START==================================================== - * Common Utils-Test - * ============================================================================= - * Copyright (C) 2018 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.utils.test; - -/** - * An error that occurred while trying to construct an object for a junit test. - */ -public class ConstructionError extends AssertionError { - private static final long serialVersionUID = 1L; - - /** - * Constructor. - */ - public ConstructionError() { - super(); - } - - /** - * Constructor. - * - * @param message denotes the error message - */ - public ConstructionError(final String message) { - super(message); - } - - /** - * Constructor. - * - * @param cause denotes the cause of the error - */ - public ConstructionError(final Throwable cause) { - super(cause); - } - - /** - * Constructor. - * - * @param message denotes the error message - * @param cause denotes the cause of the error - */ - public ConstructionError(final String message, final Throwable cause) { - super(message, cause); - } - -} diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/ErrorsTester.java b/policy-common/src/main/java/org/onap/policy/common/utils/test/ErrorsTester.java deleted file mode 100644 index ab876b6ea..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/utils/test/ErrorsTester.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * ============LICENSE_START==================================================== - * Common Utils-Test - * ============================================================================= - * Copyright (C) 2018 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.utils.test; - -/** - * Used to test various Error subclasses. Uses reflection to identify the - * constructors that the subclass supports. - */ -public class ErrorsTester extends ThrowablesTester { - - /** - * Runs tests, on an Error subclass, for all of the standard - * constructors.If the Error subclass does not support a given - * type of constructor, then it skips that test. - * Does not throw an exception if no standard constructors - * are found. - * - * @param claz subclass to be tested - * @param this needs to be declared - * - * @return the number of constructors that were found/tested - * @throws ConstructionError - * if the Error subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public int testAllError(final Class claz) { - return testAllThrowable(claz); - } -} diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java b/policy-common/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java deleted file mode 100644 index 34dc5681c..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * ============LICENSE_START==================================================== - * Common Utils-Test - * ============================================================================= - * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2024 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. - * ============LICENSE_END====================================================== - */ - -package org.onap.policy.common.utils.test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.lang.reflect.Constructor; - -/** - * Used to test various Exception subclasses. Uses reflection to identify the - * constructors that the subclass supports. - */ -public class ExceptionsTester extends ThrowablesTester { - - /** - * Runs tests, on an Exception subclass, for all of the standard - * constructors. If the Exception subclass does not support a given - * type of constructor, then it skips that test. - * - * @param claz subclass to be tested - * @param Type of the class - * - * @return the number of constructors that were found/tested - * @throws ConstructionError - * if the Exception subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public int test(final Class claz) { - int ncons = testAllException(claz); - - assertTrue(ncons > 0); - - return ncons; - } - - /** - * Runs tests, on an Exception subclass, for all of the standard - * constructors. If the Exception subclass does not support a given - * type of constructor, then it skips that test. Does not throw - * an exception if no standard constructors are found. - * - * @param claz subclass to be tested - * @param type of the class - * - * @return the number of constructors that were found/tested - * @throws ConstructionError - * if the Exception subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public int testAllException(final Class claz) { - var ncons = 0; - - ncons += testAllThrowable(claz); - ncons += testException(claz); - ncons += testStringException(claz); - ncons += testStringExceptionBooleanBoolean(claz); - - return ncons; - } - - /** - * Tests exceptions created via the constructor that takes just an - * Exception. Verifies that: - *
    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the cause's message
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
- * - *

If the Exception subclass does not support this type of constructor, - * then this method simply returns. - * - * @param claz subclass to be tested - * @param Type of the class - * - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Exception subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public int testException(final Class claz) { - Constructor cons = getConstructor(claz, "exception", - Exception.class); - - if (cons == null) { - return 0; - } - - var cause = new Exception(EXPECTED_EXCEPTION_MSG); - var ex = newInstance(cons, cause); - - assertNotNull(ex.toString()); - assertEquals(ex.getMessage(), ex.getMessage()); - assertEquals(cause, ex.getCause()); - - return 1; - } - - /** - * Tests exceptions created via the constructor that takes a String - * and an Exception. Verifies that: - *

    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to - * the constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
- * - *

If the Exception subclass does not support this type of - * constructor, then this method simply returns. - * - * @param claz subclass to be tested - * @param Type of the class - * - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Exception subclass cannot be constructed - */ - public int testStringException( - final Class claz) { - Constructor cons = getConstructor(claz, "string-exception", - String.class, Exception.class); - if (cons == null) { - return 0; - } - - var cause = new Exception(EXPECTED_EXCEPTION_MSG); - var ex = newInstance(cons, "world", cause); - - assertNotNull(ex.toString()); - assertEquals("world", ex.getMessage()); - assertEquals(cause, ex.getCause()); - - return 1; - } - - /** - * Tests exceptions created via the constructor that takes a String, an - * Exception, and two booleans. Verifies that: - *

    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
  • suppressed exceptions can be added, if enabled
  • - *
  • the stack trace can be added, if enabled
  • - *
- * - *

If the Exception subclass does not support this type of constructor, - * then this method simply returns. - * - * @param claz subclass to be tested - * @param Type of the class - * - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Exception subclass cannot be constructed - */ - public int testStringExceptionBooleanBoolean( - final Class claz) { - - Constructor cons = getConstructor(claz, - "string-exception-flags", String.class, Exception.class, - Boolean.TYPE, Boolean.TYPE); - - if (cons == null) { - return 0; - } - - // test each combination of "message" and "cause" - testMessageCauseCombos(cons); - - // test each combination of the boolean flags - testFlagCombos(cons); - - return 1; - } -} diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java b/policy-common/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java deleted file mode 100644 index 62584c768..000000000 --- a/policy-common/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java +++ /dev/null @@ -1,564 +0,0 @@ -/* - * ============LICENSE_START==================================================== - * Common Utils-Test - * ============================================================================= - * Copyright (C) 2018, 2020-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2024 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. - * ============LICENSE_END====================================================== - */ - -package org.onap.policy.common.utils.test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Used to test various Throwable subclasses. Uses reflection to identify the - * constructors that the subclass supports. - */ -public class ThrowablesTester { - - private static Logger logger = - LoggerFactory.getLogger(ThrowablesTester.class); - - public static final String EXPECTED_EXCEPTION_MSG = - "expected exception"; - private static final String EXPECTED_SUPPRESSED_EXCEPTION_MSG = - "expected suppressed exception"; - - /** - * Passed as a "cause" to constructors. - */ - public static final Exception CAUSE = - new Exception(EXPECTED_EXCEPTION_MSG); - - /** - * Passed to new objects via the addSuppressed() method.. - */ - public static final Throwable SUPPRESSED = - new Throwable(EXPECTED_SUPPRESSED_EXCEPTION_MSG); - - /** - * Runs tests, on an Throwable subclass, for all of the - * standard constructors. If the Throwable subclass does - * not support a given type of constructor, then it skips - * that test. Does not throw an exception if no - * standard constructors are found. - * - * @param claz subclass to be tested - * @param To be defined - * @return the number of constructors that were found/tested - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final int testAllThrowable( - final Class claz) { - var ncons = 0; - - ncons += testDefault(claz); - ncons += testString(claz); - ncons += testThrowable(claz); - ncons += testStringThrowable(claz); - ncons += testStringThrowableBooleanBoolean(claz); - - return ncons; - } - - /** - * Tests Throwable objects created via the default constructor. Verifies - * that: - *

    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns null
  • - *
  • getCause() returns null
  • - *
- * - *

If the Throwable subclass does not support this type of - * constructor, then this method simply returns. - * - * @param claz subclass to be tested - * @param to be defined - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final int testDefault( - final Class claz) { - Constructor cons = getConstructor(claz, "default"); - if (cons == null) { - return 0; - } - - var ex = newInstance(cons); - - assertNotNull(ex.toString()); - assertNull(ex.getMessage()); - assertNull(ex.getCause()); - - return 1; - } - - /** - * Tests Throwable objects created via the constructor that takes just a - * String. Verifies that: - *

    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns null
  • - *
- * - *

If the Throwable subclass does not support this type of constructor, - * then this method simply returns. - * - * @param claz - * subclass to be tested - * @param to be defined - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final int testString(final Class claz) { - Constructor cons = getConstructor(claz, "string", - String.class); - if (cons == null) { - return 0; - } - - var ex = newInstance(cons, "hello"); - - assertNotNull(ex.toString()); - assertEquals("hello", ex.getMessage()); - assertNull(ex.getCause()); - - return 1; - } - - /** - * Tests Throwable objects created via the constructor that takes just a - * Throwable. Verifies that: - *

    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the cause's message
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
- * - *

If the Throwable subclass does not support this type of constructor, - * then this method simply returns. - * - * @param claz - * subclass to be tested - * @param to be defined - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final int testThrowable( - final Class claz) { - Constructor cons = getConstructor(claz, "throwable", - Throwable.class); - if (cons == null) { - return 0; - } - - var ex = newInstance(cons, CAUSE); - - assertEquals(ex.getMessage(), ex.getMessage()); - assertNotNull(ex.toString()); - assertEquals(CAUSE, ex.getCause()); - - return 1; - } - - /** - * Tests Throwable objects created via the constructor that takes - * a String and a Throwable. Verifies that: - *

    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
- * - *

If the Throwable subclass does not support this type of constructor, - * then this method simply returns. - * - * @param claz subclass to be tested - * @param to be defined - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final int testStringThrowable( - final Class claz) { - Constructor cons = getConstructor(claz, "string-throwable", - String.class, Throwable.class); - if (cons == null) { - return 0; - } - - var ex = newInstance(cons, "world", CAUSE); - - assertNotNull(ex.toString()); - assertEquals("world", ex.getMessage()); - assertEquals(CAUSE, ex.getCause()); - - return 1; - } - - /** - * Tests Throwable objects created via the constructor that takes - * a String, a Throwable, and two booleans. Verifies that: - *

    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
  • suppressed exceptions can be added, if enabled
  • - *
  • the stack trace can be added, if enabled
  • - *
- * - *

If the Throwable subclass does not support this type of constructor, - * then this method simply returns. - * - * @param claz - * subclass to be tested - * @param to be defined - * @return {@code 1}, if the subclass supports this type of constructor, - * {@code 0} otherwise - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final int - testStringThrowableBooleanBoolean( - final Class claz) { - Constructor cons = getConstructor(claz, - "string-throwable-flags", - String.class, Throwable.class, - Boolean.TYPE, Boolean.TYPE); - if (cons == null) { - return 0; - } - - // test each combination of "message" and "cause" - testMessageCauseCombos(cons); - - // test each combination of the boolean flags - testSuppressStack(cons); - testSuppressNoStack(cons); - testNoSuppressStack(cons); - testNoSuppressNoStack(cons); - - return 1; - } - - /** - * Tests each combination of values for the "message" and the "cause" - * when using the constructor that takes a String, - * a Throwable/Exception, and two booleans. Verifies that expected - * values are returned - *

    - * toString(), - * getMessage(), and getCause(). - *
- * - * @param cons - * constructor to be invoked - * @param to be defined - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final void testMessageCauseCombos( - final Constructor cons) { - T ex; - - ex = newInstance(cons, null, null, true, true); - assertNotNull(ex.toString()); - assertNull(ex.getMessage()); - assertNull(ex.getCause()); - - ex = newInstance(cons, "abc", null, true, true); - assertNotNull(ex.toString()); - assertEquals("abc", ex.getMessage()); - assertNull(ex.getCause()); - - ex = newInstance(cons, null, CAUSE, true, true); - assertNotNull(ex.toString()); - assertNull(ex.getMessage()); - assertEquals(CAUSE, ex.getCause()); - - ex = newInstance(cons, "xyz", CAUSE, true, true); - assertNotNull(ex.toString()); - assertEquals("xyz", ex.getMessage()); - assertEquals(CAUSE, ex.getCause()); - } - - /** - * Tests each combination of values for the "message" and the - * "cause" when using the constructor that takes a String, - * a Throwable/Exception, and two booleans. Verifies that - * expected values are returned by - *
    - * toString(), - * getMessage(), and getCause(). - *
- * - * @param cons - * constructor to be invoked - * @param to be defined - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final void testFlagCombos( - final Constructor cons) { - testSuppressStack(cons); - testSuppressNoStack(cons); - testNoSuppressStack(cons); - testNoSuppressNoStack(cons); - } - - /** - * Tests Throwable objects constructed with - * {@code enableSuppression=true} and - * {@code writableStackTrace=true}. Verifies that: - *
    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
  • suppressed exceptions are added
  • - *
  • the stack trace is added
  • - *
- * - * @param cons - * the throwable's class constructor - * @param to be defined - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final void testSuppressStack( - final Constructor cons) { - var ex = newInstance(cons, "yes,yes", CAUSE, true, true); - - ex.addSuppressed(SUPPRESSED); - - assertNotNull(ex.toString()); - assertEquals("yes,yes", ex.getMessage()); - assertEquals(CAUSE, ex.getCause()); - - assertEquals(1, ex.getSuppressed().length); - assertEquals(SUPPRESSED, ex.getSuppressed()[0]); - - assertTrue(ex.getStackTrace().length > 0); - } - - /** - * Tests Throwable objects constructed with - * {@code enableSuppression=true} and - * {@code writableStackTrace=false}. Verifies that: - *
    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
  • suppressed exceptions are added
  • - *
  • the stack trace is not added
  • - *
- * - * @param cons - * the throwable's class constructor - * @param to be defined - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final void testSuppressNoStack( - final Constructor cons) { - var ex = newInstance(cons, "yes,no", CAUSE, true, false); - - ex.addSuppressed(SUPPRESSED); - - assertNotNull(ex.toString()); - assertEquals("yes,no", ex.getMessage()); - assertEquals(CAUSE, ex.getCause()); - - assertEquals(1, ex.getSuppressed().length); - assertEquals(SUPPRESSED, ex.getSuppressed()[0]); - - assertEquals(0, ex.getStackTrace().length); - } - - /** - * Tests Throwable objects constructed with - * {@code enableSuppression=false} and - * {@code writableStackTrace=true}. Verifies that: - *
    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
  • suppressed exceptions are not added
  • - *
  • the stack trace is added
  • - *
- * - * @param cons - * the throwable's class constructor - * @param to be defined - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final void testNoSuppressStack( - final Constructor cons) { - var ex = newInstance(cons, "no,yes", CAUSE, false, true); - - ex.addSuppressed(SUPPRESSED); - - assertNotNull(ex.toString()); - assertEquals("no,yes", ex.getMessage()); - assertEquals(CAUSE, ex.getCause()); - - assertEquals(0, ex.getSuppressed().length); - - assertTrue(ex.getStackTrace().length > 0); - } - - /** - * Tests Throwable objects constructed with - * {@code enableSuppression=false} and - * {@code writableStackTrace=false}. Verifies that: - *
    - *
  • toString() returns a non-null value
  • - *
  • getMessage() returns the original message passed to the - * constructor
  • - *
  • getCause() returns the original cause passed to the - * constructor
  • - *
  • suppressed exceptions are not added
  • - *
  • the stack trace is not added
  • - *
- * @param cons - * the throwable's class constructor - * @param to be defined - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - * @throws AssertionError - * if the constructed objects fail to pass various tests - */ - public final void testNoSuppressNoStack( - final Constructor cons) { - var ex = newInstance(cons, "no,no", CAUSE, false, false); - - ex.addSuppressed(SUPPRESSED); - - assertNotNull(ex.toString()); - assertEquals("no,no", ex.getMessage()); - assertEquals(CAUSE, ex.getCause()); - - assertEquals(0, ex.getSuppressed().length); - assertEquals(0, ex.getStackTrace().length); - } - - /** - * Attempts to get a constructor for objects of a given type. - * - * @param claz - * class of objects whose constructor is to be gotten - * @param to be defined - * @param testType - * type of test being run - * @param argTypes - * argument types to be passed to the constructor - * @return the desired constructor, or {@code null} if the desired - * constructor is not available - */ - protected Constructor getConstructor( - final Class claz, - final String testType, - final Class... argTypes) { - - try { - return claz.getConstructor(argTypes); - - } catch (NoSuchMethodException | SecurityException e) { - // this constructor is not defined so nothing to test - logger.debug("skipped test, no constructor for: {}", claz, e); - return null; - } - } - - /** - * Creates a new instance of an Throwable subclass. - * - * @param cons - * subclass constructor - * @param to be defined - * @param args - * arguments to be passed to the constructor - * @return a new instance of the Throwable subclass - * @throws ConstructionError - * if the Throwable subclass cannot be constructed - */ - protected T newInstance( - final Constructor cons, - final Object... args) { - try { - return cons.newInstance(args); - - } catch (InstantiationException | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException e) { - - throw new ConstructionError(e); - } - - } -} diff --git a/policy-common/src/test/java/org/onap/policy/common/message/bus/event/client/TopicClientExceptionTest.java b/policy-common/src/test/java/org/onap/policy/common/message/bus/event/client/TopicClientExceptionTest.java index 93341a9ee..9b8c91f11 100644 --- a/policy-common/src/test/java/org/onap/policy/common/message/bus/event/client/TopicClientExceptionTest.java +++ b/policy-common/src/test/java/org/onap/policy/common/message/bus/event/client/TopicClientExceptionTest.java @@ -3,7 +3,7 @@ * ONAP PAP * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019, 2024 Nordix Foundation. + * Modifications Copyright (C) 2019-2026 OpenInfra Foundation Europe. 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. @@ -24,12 +24,12 @@ package org.onap.policy.common.message.bus.event.client; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; -import org.onap.policy.common.utils.test.ExceptionsTester; class TopicClientExceptionTest { @Test void test() { - assertEquals(5, new ExceptionsTester().test(TopicSinkClientException.class)); + var exception = new TopicSinkClientException("test"); + assertEquals("test", exception.getMessage()); } } diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.java deleted file mode 100644 index 95bba10de..000000000 --- a/policy-common/src/test/java/org/onap/policy/common/utils/test/ConstructionErrorTest.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Common Utils-Test - * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2024 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.utils.test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class ConstructionErrorTest extends ErrorsTester { - - @Test - void test() { - assertEquals(4, testAllError(ConstructionError.class)); - } - -} diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/ErrorsTesterTest.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/ErrorsTesterTest.java deleted file mode 100644 index b6d91f07e..000000000 --- a/policy-common/src/test/java/org/onap/policy/common/utils/test/ErrorsTesterTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Common Utils-Test - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2024 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.utils.test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class ErrorsTesterTest { - - @Test - void test() { - assertEquals(2, new ErrorsTester().testAllError(SimpleError.class)); - assertEquals(5, new ErrorsTester().testAllError(StaticError.class)); - } - - /** - * Used to test a simple success case. - */ - public static class SimpleError extends Error { - private static final long serialVersionUID = 1L; - - public SimpleError() { - super(); - } - - public SimpleError(String message) { - super(message); - } - } - - /** - * Used to test the exhaustive success case. - */ - public static class StaticError extends Error { - private static final long serialVersionUID = 1L; - - public StaticError() { - super(); - } - - public StaticError(String message) { - super(message); - } - - public StaticError(Throwable cause) { - super(cause); - } - - public StaticError(String message, Throwable cause) { - super(message, cause); - } - - public StaticError(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - } - -} diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/ExceptionsTesterTest.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/ExceptionsTesterTest.java deleted file mode 100644 index 2004009f5..000000000 --- a/policy-common/src/test/java/org/onap/policy/common/utils/test/ExceptionsTesterTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Common Utils-Test - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2024 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.utils.test; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class ExceptionsTesterTest { - - @Test - void test() { - assertEquals(2, new ExceptionsTester().test(SimpleException.class)); - assertEquals(8, new ExceptionsTester().test(StaticException.class)); - } - - @Test - void testNoConstructorsException() { - ExceptionsTester tester = new ExceptionsTester(); - assertThatThrownBy(() -> tester.test(NoConstructorsException.class)) - .isInstanceOf(AssertionError.class); - } - - /** - * Used to test a failure case - this has no standard constructors. The only constructor it has - * takes an "int", thus it is not one of the standard constructors. - */ - public static class NoConstructorsException extends Exception { - private static final long serialVersionUID = 1L; - - @SuppressWarnings("unused") - public NoConstructorsException(int value) { - super(); - } - } - - /** - * Used to test a simple success case. - */ - public static class SimpleException extends Exception { - private static final long serialVersionUID = 1L; - - public SimpleException() { - super(); - } - - public SimpleException(String message) { - super(message); - } - } - - /** - * Used to test the exhaustive success case. - */ - public static class StaticException extends Exception { - private static final long serialVersionUID = 1L; - - public StaticException() { - super(); - } - - public StaticException(String message) { - super(message); - } - - public StaticException(Throwable cause) { - super(cause); - } - - public StaticException(String message, Throwable cause) { - super(message, cause); - } - - public StaticException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - - // same as above, but with Exceptions substituted for Throwables - - public StaticException(Exception cause) { - super(cause); - } - - public StaticException(String message, Exception cause) { - super(message, cause); - } - - public StaticException(String message, Exception cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - } - -} diff --git a/policy-common/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java b/policy-common/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java deleted file mode 100644 index 0a437b57c..000000000 --- a/policy-common/src/test/java/org/onap/policy/common/utils/test/ThrowablesTesterTest.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Common Utils-Test - * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2024 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.common.utils.test; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class ThrowablesTesterTest { - - @Test - void test() { - assertEquals(2, new ThrowablesTester().testAllThrowable(SimpleThrowable.class)); - assertEquals(5, new ThrowablesTester().testAllThrowable(StaticThrowable.class)); - } - - @Test - void testNoConstructorsThrowable() { - // this will not throw an error, but it should return 0, as there are - // no matching constructors - assertEquals(0, new ThrowablesTester().testAllThrowable(NoConstructorsThrowable.class)); - } - - @Test - void testIgnoreMessageThrowable() { - ThrowablesTester tester = new ThrowablesTester(); - assertThatThrownBy(() -> tester.testAllThrowable(IgnoreMessageThrowable.class)) - .isInstanceOf(AssertionError.class); - } - - @Test - void testIgnoreCauseThrowable() { - ThrowablesTester tester = new ThrowablesTester(); - assertThatThrownBy(() -> tester.testAllThrowable(IgnoreCauseThrowable.class)) - .isInstanceOf(AssertionError.class); - } - - @Test - void testAlwaysSuppressThrowable() { - ThrowablesTester tester = new ThrowablesTester(); - assertThatThrownBy(() -> tester.testAllThrowable(AlwaysSuppressThrowable.class)) - .isInstanceOf(AssertionError.class); - } - - @Test - void testNeverSuppressThrowable() { - ThrowablesTester tester = new ThrowablesTester(); - assertThatThrownBy(() -> tester.testAllThrowable(NeverSuppressThrowable.class)) - .isInstanceOf(AssertionError.class); - } - - @Test - void testAlwaysWritableThrowable() { - ThrowablesTester tester = new ThrowablesTester(); - assertThatThrownBy(() -> tester.testAllThrowable(AlwaysWritableThrowable.class)) - .isInstanceOf(AssertionError.class); - } - - @Test - void testNeverWritableThrowable() { - ThrowablesTester tester = new ThrowablesTester(); - assertThatThrownBy(() -> tester.testAllThrowable(NeverWritableThrowable.class)) - .isInstanceOf(AssertionError.class); - } - - @Test - void testThrowInstantiationException() { - ThrowablesTester tester = new ThrowablesTester(); - assertThatThrownBy(() -> tester.testAllThrowable(ThrowInstantiationThrowable.class)) - .isInstanceOf(AssertionError.class); - } - - /** - * Used to test a failure case - message text is ignored. - */ - public static class IgnoreMessageThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public IgnoreMessageThrowable(String message) { - super("bogus"); - } - } - - /** - * Used to test a failure case - cause is ignored. - */ - public static class IgnoreCauseThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public IgnoreCauseThrowable(Throwable cause) { - super(new Throwable("another cause")); - } - } - - /** - * Used to test a failure case - this has no standard constructors. The only constructor it has - * takes an "int", thus it is not one of the standard constructors. - */ - public static class NoConstructorsThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - @SuppressWarnings("unused") - public NoConstructorsThrowable(int value) { - super(); - } - } - - /** - * Used to test a failure case - always suppresses. - */ - public static class AlwaysSuppressThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public AlwaysSuppressThrowable(String message, Throwable cause, boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, true, writableStackTrace); - } - } - - /** - * Used to test a failure case - never suppresses. - */ - public static class NeverSuppressThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public NeverSuppressThrowable(String message, Throwable cause, boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, false, writableStackTrace); - } - } - - /** - * Used to test a failure case - always allows stack writes. - */ - public static class AlwaysWritableThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public AlwaysWritableThrowable(String message, Throwable cause, boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, enableSuppression, true); - } - } - - /** - * Used to test a failure case - never allows stack writes. - */ - public static class NeverWritableThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public NeverWritableThrowable(String message, Throwable cause, boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, enableSuppression, false); - } - } - - /** - * Used to test a failure case - throws InstantiationException when constructed. - */ - public static class ThrowInstantiationThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public ThrowInstantiationThrowable(String message, Throwable cause, boolean enableSuppression, - boolean writableStackTrace) throws InstantiationException { - - throw new InstantiationException(ThrowablesTester.EXPECTED_EXCEPTION_MSG); - } - } - - /** - * Used to test a simple success case. - */ - public static class SimpleThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public SimpleThrowable() { - super(); - } - - public SimpleThrowable(String message) { - super(message); - } - } - - /** - * Used to test the exhaustive success case. - */ - public static class StaticThrowable extends Throwable { - private static final long serialVersionUID = 1L; - - public StaticThrowable() { - super(); - } - - public StaticThrowable(String message) { - super(message); - } - - public StaticThrowable(Throwable cause) { - super(cause); - } - - public StaticThrowable(String message, Throwable cause) { - super(message, cause); - } - - public StaticThrowable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - } - -} -- 2.16.6