============LICENSE_START=======================================================
ONAP Policy Engine - Drools PDP
================================================================================
- 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.
<version>2.13.0</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <version>3.11.1</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
* ============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.
package org.onap.policy.drools.pooling;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
@Test
public void testPublish() throws PoolingFeatureException {
// cannot publish before starting
- expectException("publish,pre", () -> mgr.publish(MSG));
+ assertThatThrownBy(() -> mgr.publish(MSG)).as("publish,pre");
mgr.startPublisher();
// stop and verify we can no longer publish
mgr.stopPublisher(0);
- expectException("publish,stopped", () -> mgr.publish(MSG));
+ assertThatThrownBy(() -> mgr.publish(MSG)).as("publish,stopped");
}
@Test(expected = PoolingFeatureException.class)
mgr.publish(MSG);
}
-
- private void expectException(String testnm, VFunction func) {
- try {
- func.apply();
- fail(testnm + " missing exception");
-
- } catch (PoolingFeatureException expected) {
- // OK
- }
- }
/**
* Manager with overrides.
return Arrays.asList(mock(TopicSink.class), sink, mock(TopicSink.class));
}
}
-
- @FunctionalInterface
- public static interface VFunction {
- public void apply() throws PoolingFeatureException;
- }
}
* ============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.
package org.onap.policy.drools.pooling;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
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.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
*/
PolicyController ctlr = mock(PolicyController.class);
- PoolingFeatureRtException ex = expectException(PoolingFeatureRtException.class,
- () -> new PoolingManagerTest(MY_HOST, ctlr, poolProps, active));
- assertNotNull(ex.getCause());
- assertTrue(ex.getCause() instanceof ClassCastException);
+ assertThatThrownBy(() -> new PoolingManagerTest(MY_HOST, ctlr, poolProps, active))
+ .isInstanceOf(PoolingFeatureRtException.class).hasCauseInstanceOf(ClassCastException.class);
}
@Test
public void testPoolingManagerImpl_PoolEx() throws PoolingFeatureException {
// throw an exception when we try to create the dmaap manager
PoolingFeatureException ex = new PoolingFeatureException();
-
- PoolingFeatureRtException ex2 = expectException(PoolingFeatureRtException.class,
- () -> new PoolingManagerTest(MY_HOST, controller, poolProps, active) {
- @Override
- protected DmaapManager makeDmaapManager(String topic) throws PoolingFeatureException {
- throw ex;
- }
- });
-
- assertEquals(ex, ex2.getCause());
+
+ assertThatThrownBy(() -> new PoolingManagerTest(MY_HOST, controller, poolProps, active) {
+ @Override
+ protected DmaapManager makeDmaapManager(String topic) throws PoolingFeatureException {
+ throw ex;
+ }
+ }).isInstanceOf(PoolingFeatureRtException.class).hasCause(ex);
}
@Test
}
- /**
- * Invokes a method that is expected to throw an exception.
- *
- * @param exClass class of exception that is expected
- * @param func function to invoke
- * @return the exception that was thrown
- * @throws AssertionError if no exception was thrown
- */
- private <T extends Exception> T expectException(Class<T> exClass, ExFunction<T> func) {
- try {
- func.apply();
- throw new AssertionError("missing exception");
-
- } catch (Exception e) {
- return exClass.cast(e);
- }
- }
-
- /**
- * Function that is expected to throw an exception.
- *
- * @param <T> type of exception the function is expected to throw
- */
- @FunctionalInterface
- private static interface ExFunction<T extends Exception> {
-
- /**
- * Invokes the function.
- *
- * @throws T if an error occurs
- */
- public void apply() throws T;
-
- }
-
/**
* Manager with overrides.
*/
============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.
<artifactId>powermock-api-mockito</artifactId>
<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>
* ============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.
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.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;
@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);
@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);
@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
@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
@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
* ============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.
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;
@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);
@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);
@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
@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
@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
+++ /dev/null
-/*
- * ============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;
- }
-}