Replace expectException with AssertJ 03/75503/1
authorJim Hahn <jrh3@att.com>
Tue, 8 Jan 2019 22:15:07 +0000 (17:15 -0500)
committerJim Hahn <jrh3@att.com>
Tue, 8 Jan 2019 22:16:01 +0000 (17:16 -0500)
Change-Id: I73c186f181960ac7ac8cd985e55db1831963910f
Issue-ID: POLICY-1392
Signed-off-by: Jim Hahn <jrh3@att.com>
policy-endpoints/pom.xml
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/BusTopicFactoryTestBase.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/DmaapTopicFactoryTestBase.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicSinkFactoryTest.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/TopicFactoryTestBase.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/UebTopicFactoryTestBase.java
utils-test/pom.xml
utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java

index 059d110..87139bc 100644 (file)
@@ -1,7 +1,7 @@
 <!--
   ============LICENSE_START=======================================================
    Copyright (C) 2018 Ericsson. All rights reserved.
-   Modifications Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+   Modifications 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.
             <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>com.openpojo</groupId>
index d68f55e..71d4fe2 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-endpoints
  * ================================================================================
- * 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.common.endpoints.event.comm.bus;
 
+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;
@@ -104,12 +105,10 @@ public abstract class BusTopicFactoryTestBase<T extends Topic> extends TopicFact
      */
     public void testBuildBusTopicParams_Ex() {
         // null topic
-        RuntimeException actual = expectException(() -> buildTopic(makeBuilder().topic(null).build()));
-        assertEquals(IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic(null).build()));
 
         // empty topic
-        actual = expectException(() -> buildTopic(makeBuilder().topic("").build()));
-        assertEquals(IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().isThrownBy(() -> buildTopic(makeBuilder().topic("").build()));
     }
 
     /**
index 440120e..877246e 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-endpoints
  * ================================================================================
- * 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.common.endpoints.event.comm.bus;
 
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
 import static org.junit.Assert.assertEquals;
 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_CONN_TIMEOUT_SUFFIX;
 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_DMAAP_DME2_EP_READ_TIMEOUT_MS_SUFFIX;
@@ -116,18 +117,15 @@ public abstract class DmaapTopicFactoryTestBase<T extends Topic> extends BusTopi
     @Override
     public void testGet_Ex() {
         // null topic
-        RuntimeException actual = expectException(() -> getTopic(null));
-        assertEquals("null topic", IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
 
         // empty topic
-        actual = expectException(() -> getTopic(""));
-        assertEquals("empty topic", IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
 
         // unknown topic
         initFactory();
         buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
 
-        actual = expectException(() -> getTopic(TOPIC2));
-        assertEquals("unknown topic", IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));
     }
 }
index 2ba6fb3..82a9df4 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-endpoints
  * ================================================================================
- * 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.common.endpoints.event.comm.bus;
 
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -112,7 +113,7 @@ public class NoopTopicSinkFactoryTest extends TopicFactoryTestBase<NoopTopicSink
         initFactory();
         assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
                         .setTopicProperty(PROPERTY_MANAGED_SUFFIX, "false").build()).size());
-        assertNotNull(expectException(() -> factory.get(MY_TOPIC)));
+        assertThatThrownBy(() -> factory.get(MY_TOPIC));
 
         // managed undefined - default to true
         initFactory();
index 29816c6..419f950 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-endpoints
  * ================================================================================
- * 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.common.endpoints.event.comm.bus;
 
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
 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.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
 
 import java.util.List;
@@ -175,12 +176,10 @@ public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBas
      */
     public void testDestroyString_Ex() {
         // null topic
-        RuntimeException actual = expectException(() -> destroyTopic(null));
-        assertEquals(IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> destroyTopic(null));
 
         // empty topic
-        actual = expectException(() -> destroyTopic(""));
-        assertEquals(IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> destroyTopic(""));
     }
 
     /**
@@ -210,36 +209,15 @@ public abstract class TopicFactoryTestBase<T extends Topic> extends TopicTestBas
      */
     public void testGet_Ex() {
         // null topic
-        RuntimeException actual = expectException(() -> getTopic(null));
-        assertEquals("null topic", IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("null topic").isThrownBy(() -> getTopic(null));
 
         // empty topic
-        actual = expectException(() -> getTopic(""));
-        assertEquals("empty topic", IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("empty topic").isThrownBy(() -> getTopic(""));
 
         // unknown topic
         initFactory();
         buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
 
-        actual = expectException(() -> getTopic(TOPIC2));
-        assertEquals("unknown topic", IllegalStateException.class, actual.getClass());
-    }
-
-    /**
-     * Runs a function that is expected to throw an exception. Invokes fail() if the
-     * function does not throw an exception.
-     *
-     * @param function the function to run
-     * @return the exception thrown by the function
-     */
-    public RuntimeException expectException(Runnable function) {
-        try {
-            function.run();
-            fail("missing exception");
-            return null;
-
-        } catch (RuntimeException e) {
-            return e;
-        }
+        assertThatIllegalStateException().as("unknown topic").isThrownBy(() -> getTopic(TOPIC2));
     }
 }
index fbc2e46..41dbac8 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-endpoints
  * ================================================================================
- * 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,7 +20,7 @@
 
 package org.onap.policy.common.endpoints.event.comm.bus;
 
-import static org.junit.Assert.assertEquals;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
 
 import java.util.Collections;
 import org.onap.policy.common.endpoints.event.comm.Topic;
@@ -38,11 +38,11 @@ public abstract class UebTopicFactoryTestBase<T extends Topic> extends BusTopicF
         super.testBuildBusTopicParams_Ex();
 
         // null servers
-        RuntimeException actual = expectException(() -> buildTopic(makeBuilder().servers(null).build()));
-        assertEquals(IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("null servers")
+                        .isThrownBy(() -> buildTopic(makeBuilder().servers(null).build()));
 
         // empty servers
-        actual = expectException(() -> buildTopic(makeBuilder().servers(Collections.emptyList()).build()));
-        assertEquals(IllegalArgumentException.class, actual.getClass());
+        assertThatIllegalArgumentException().as("empty servers")
+                        .isThrownBy(() -> buildTopic(makeBuilder().servers(Collections.emptyList()).build()));
     }
 }
index cf5da46..bf36472 100644 (file)
@@ -2,7 +2,7 @@
   ============LICENSE_START=======================================================
   ONAP Policy Engine - Common Modules
   ================================================================================
-  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.
             <artifactId>junit</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <version>3.11.1</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
index 613897d..36812b3 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Engine - Common Modules
  * ================================================================================
- * 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.common.utils.io;
 
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -116,7 +117,7 @@ public class SerializerTest {
             }
         });
 
-        assertEquals(ex, expectException(() -> Serializer.serialize(new MyObject(100))));
+        assertThatThrownBy(() -> Serializer.serialize(new MyObject(100))).isEqualTo(ex);
     }
 
     @Test
@@ -133,7 +134,7 @@ public class SerializerTest {
             }
         });
 
-        assertEquals(ex, expectException(() -> Serializer.serialize(new MyObject(110))));
+        assertThatThrownBy(() -> Serializer.serialize(new MyObject(110))).isEqualTo(ex);
     }
 
     @Test
@@ -159,7 +160,7 @@ public class SerializerTest {
             }
         });
 
-        assertEquals(ex, expectException(() -> Serializer.serialize(new MyObject(120))));
+        assertThatThrownBy(() -> Serializer.serialize(new MyObject(120))).isEqualTo(ex);
     }
 
     @Test
@@ -206,7 +207,7 @@ public class SerializerTest {
             }
         });
 
-        assertEquals(ex2, expectException(() -> Serializer.serialize(new MyObject(130))));
+        assertThatThrownBy(() -> Serializer.serialize(new MyObject(130))).isEqualTo(ex2);
         
     }
 
@@ -246,7 +247,7 @@ public class SerializerTest {
         });
 
         byte[] data = Serializer.serialize(new MyObject(300));
-        assertEquals(ex, expectException(() -> Serializer.deserialize(MyObject.class, data)));
+        assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex);
     }
 
     @Test
@@ -264,7 +265,7 @@ public class SerializerTest {
         });
 
         byte[] data = Serializer.serialize(new MyObject(310));
-        assertEquals(ex, expectException(() -> Serializer.deserialize(MyObject.class, data)));
+        assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex);
     }
 
     @Test
@@ -288,7 +289,7 @@ public class SerializerTest {
         });
 
         byte[] data = Serializer.serialize(new MyObject(320));
-        assertEquals(ex, expectException(() -> Serializer.deserialize(MyObject.class, data)));
+        assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex);
     }
 
     @Test
@@ -323,7 +324,7 @@ public class SerializerTest {
         });
 
         byte[] data = Serializer.serialize(new MyObject(330));
-        assertEquals(ex2, expectException(() -> Serializer.deserialize(MyObject.class, data)));
+        assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex2);
     }
 
     @Test
@@ -348,28 +349,6 @@ public class SerializerTest {
         Whitebox.setInternalState(Serializer.class, "factory", factory);
     }
 
-    /**
-     * Applies a function, which is expected to throw an exception.
-     *
-     * @param func the function to apply
-     * @return the exception thrown by the function, or {@code null} if it did not throw
-     *         an exception
-     */
-    private Exception expectException(RunnerWithEx func) {
-        try {
-            func.apply();
-            return null;
-
-        } catch (Exception ex) {
-            return ex;
-        }
-    }
-
-    @FunctionalInterface
-    private static interface RunnerWithEx {
-        public void apply() throws Exception;
-    }
-
     /**
      * Simple, serializable object.
      */