diff options
author | Jim Hahn <jrh3@att.com> | 2019-01-08 17:48:31 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-01-08 17:48:31 -0500 |
commit | 7e4d30982fde5d6bef26c52c1a8b5f60f026510b (patch) | |
tree | 7450777fa19f43f87c6c4191e00e8f9d51d2369c /policy-core | |
parent | 777131d706b8379c5fb30f8161902d0388eb96bf (diff) |
Replace expectException with AssertJ
Change-Id: I5c9c5e962a7e2a5617405d534c0719f4bdb6ff1c
Issue-ID: POLICY-1393
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-core')
4 files changed, 39 insertions, 102 deletions
diff --git a/policy-core/pom.xml b/policy-core/pom.xml index fafbdde3..42807489 100644 --- a/policy-core/pom.xml +++ b/policy-core/pom.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= ONAP Policy Engine - Drools PDP ================================================================================ - Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + Copyright (C) 2017-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. @@ -116,6 +116,12 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>3.11.1</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.onap.policy.common</groupId> <artifactId>utils-test</artifactId> <version>${policy.common.version}</version> diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java index 81b52d16..f575ce49 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/PolicyResourceLockManagerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-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. @@ -20,6 +20,7 @@ package org.onap.policy.drools.core.lock; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -32,7 +33,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.onap.policy.drools.core.lock.TestingUtils.expectException; import java.util.Arrays; import java.util.LinkedList; @@ -120,12 +120,11 @@ public class PolicyResourceLockManagerTest { @Test public void testLock_ArgEx() { - IllegalArgumentException ex = - expectException(IllegalArgumentException.class, () -> mgr.lock(null, OWNER1, MAX_AGE_SEC)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.lock(null, OWNER1, MAX_AGE_SEC)) + .withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.lock(RESOURCE_A, null, MAX_AGE_SEC)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.lock(RESOURCE_A, null, MAX_AGE_SEC)) + .withMessage(NULL_OWNER); // this should not throw an exception mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC); @@ -232,12 +231,11 @@ public class PolicyResourceLockManagerTest { @Test public void testRefresh_ArgEx() { - IllegalArgumentException ex = - expectException(IllegalArgumentException.class, () -> mgr.refresh(null, OWNER1, MAX_AGE_SEC)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.refresh(null, OWNER1, MAX_AGE_SEC)) + .withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.refresh(RESOURCE_A, null, MAX_AGE_SEC)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.refresh(RESOURCE_A, null, MAX_AGE_SEC)) + .withMessage(NULL_OWNER); // this should not throw an exception mgr.refresh(RESOURCE_A, OWNER1, MAX_AGE_SEC); @@ -343,11 +341,9 @@ public class PolicyResourceLockManagerTest { @Test public void testUnlock_ArgEx() { - IllegalArgumentException ex = expectException(IllegalArgumentException.class, () -> mgr.unlock(null, OWNER1)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.unlock(null, OWNER1)).withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.unlock(RESOURCE_A, null)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.unlock(RESOURCE_A, null)).withMessage(NULL_OWNER); } @Test @@ -458,8 +454,7 @@ public class PolicyResourceLockManagerTest { @Test public void testIsLocked_ArgEx() { - IllegalArgumentException ex = expectException(IllegalArgumentException.class, () -> mgr.isLocked(null)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.isLocked(null)).withMessage(NULL_RESOURCE_ID); } @Test @@ -512,12 +507,10 @@ public class PolicyResourceLockManagerTest { @Test public void testIsLockedBy_ArgEx() { - IllegalArgumentException ex = - expectException(IllegalArgumentException.class, () -> mgr.isLockedBy(null, OWNER1)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.isLockedBy(null, OWNER1)) + .withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.isLockedBy(RESOURCE_A, null)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.isLockedBy(RESOURCE_A, null)).withMessage(NULL_OWNER); } @Test diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java index 6afe5a83..51cf68fc 100644 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java +++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-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. @@ -20,11 +20,11 @@ package org.onap.policy.drools.core.lock; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.onap.policy.drools.core.lock.TestingUtils.expectException; import java.util.LinkedList; import java.util.concurrent.CountDownLatch; @@ -144,12 +144,11 @@ public class SimpleLockManagerTest { @Test public void testLock_ArgEx() { - IllegalArgumentException ex = - expectException(IllegalArgumentException.class, () -> mgr.lock(null, OWNER1, MAX_AGE_SEC)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.lock(null, OWNER1, MAX_AGE_SEC)) + .withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.lock(RESOURCE_A, null, MAX_AGE_SEC)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.lock(RESOURCE_A, null, MAX_AGE_SEC)) + .withMessage(NULL_OWNER); // this should not throw an exception mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC); @@ -218,12 +217,11 @@ public class SimpleLockManagerTest { @Test public void testRefresh_ArgEx() { - IllegalArgumentException ex = - expectException(IllegalArgumentException.class, () -> mgr.refresh(null, OWNER1, MAX_AGE_SEC)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.refresh(null, OWNER1, MAX_AGE_SEC)) + .withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.refresh(RESOURCE_A, null, MAX_AGE_SEC)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.refresh(RESOURCE_A, null, MAX_AGE_SEC)) + .withMessage(NULL_OWNER); // this should not throw an exception mgr.refresh(RESOURCE_A, OWNER1, MAX_AGE_SEC); @@ -239,11 +237,9 @@ public class SimpleLockManagerTest { @Test public void testUnlock_ArgEx() { - IllegalArgumentException ex = expectException(IllegalArgumentException.class, () -> mgr.unlock(null, OWNER1)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.unlock(null, OWNER1)).withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.unlock(RESOURCE_A, null)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.unlock(RESOURCE_A, null)).withMessage(NULL_OWNER); } @Test @@ -283,8 +279,7 @@ public class SimpleLockManagerTest { @Test public void testIsLocked_ArgEx() { - IllegalArgumentException ex = expectException(IllegalArgumentException.class, () -> mgr.isLocked(null)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.isLocked(null)).withMessage(NULL_RESOURCE_ID); } @Test @@ -307,12 +302,10 @@ public class SimpleLockManagerTest { @Test public void testIsLockedBy_ArgEx() { - IllegalArgumentException ex = - expectException(IllegalArgumentException.class, () -> mgr.isLockedBy(null, OWNER1)); - assertEquals(NULL_RESOURCE_ID, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.isLockedBy(null, OWNER1)) + .withMessage(NULL_RESOURCE_ID); - ex = expectException(IllegalArgumentException.class, () -> mgr.isLockedBy(RESOURCE_A, null)); - assertEquals(NULL_OWNER, ex.getMessage()); + assertThatIllegalArgumentException().isThrownBy(() -> mgr.isLockedBy(RESOURCE_A, null)).withMessage(NULL_OWNER); } @Test diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/TestingUtils.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/TestingUtils.java deleted file mode 100644 index 17a11a09..00000000 --- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/TestingUtils.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * 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.drools.core.lock; - -public class TestingUtils { - - /** - * Invokes a function that is expected to throw an exception. - * - * @param clazz class of exception that is expected - * @param func function - * @return object - */ - public static <T> T expectException(Class<T> clazz, VoidFunction func) { - try { - func.apply(); - throw new AssertionError("missing exception"); - - } catch (Exception e) { - try { - return clazz.cast(e); - - } catch (ClassCastException e2) { - throw new AssertionError("incorrect exception type", e2); - } - } - } - - /** - * Void function that may throw an exception. - */ - @FunctionalInterface - public static interface VoidFunction { - - public void apply() throws Exception; - } -} |