DataBrokerTestModule: use AbstractDataBrokerTest without inheritance 45/43645/3
authorMichael Vorburger <vorburger@redhat.com>
Wed, 10 Aug 2016 15:22:30 +0000 (17:22 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Tue, 16 Aug 2016 20:14:24 +0000 (20:14 +0000)
While starting to write JUnit tests (for functional components) with the
AbstractDataBrokerTest, I found myself not liking its design which
forces one to use a class SomeTest extends AbstractDataBrokerTest...

Modern JUnit tests should be written by composition, not enforced
inheritance.  Thus this new DataBrokerTestModule allows one to obtain a
DataBroker suitable for Tests from anywhere.

The *(Test)Module naming convention is obviously inspired by a DI point
of view (think Spring Framework's @Configuration classes, or Google
Guice or Dagger (yay!) *Module classes - which is exactly what this
really is - a particular way to obtain an instance of an
implementationof some service (here a DataBroker) in a certain
environment (here for tests).

The internal implementation of DataBrokerTestModule could be changed
later; I guess ideally what's in AbstractDataBrokerTest could go into
DataBrokerTestModule and AbstractDataBrokerTest could use it, but for
now I'm too lazy to change that, as this does the trick nicely.  Later
implementation changes would be transparent to users of
DataBrokerTestModule.

Change-Id: I9641f527bbc0cb92732f2e513cdd64cc6a837200
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
opendaylight/md-sal/sal-binding-broker/pom.xml
opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/DataBrokerTestModule.java [new file with mode: 0644]
opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/AbstractDataBrokerTestTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/DataBrokerTestModuleTest.java [new file with mode: 0644]

index bc5b4bfddc30eb4b3309f7bcaf18f97c47e78059..1e53fe20738ec16715c67570f2a48e94c9e60832 100644 (file)
       <artifactId>mockito-all</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.google.truth</groupId>
+      <artifactId>truth</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.opendaylight.yangtools</groupId>
       <artifactId>yang-parser-impl</artifactId>
diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/DataBrokerTestModule.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/DataBrokerTestModule.java
new file mode 100644 (file)
index 0000000..96facbe
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.binding.test;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+
+// @Module
+public class DataBrokerTestModule {
+
+    // Suppress IllegalCatch because of AbstractDataBrokerTest (change later)
+    @SuppressWarnings({ "checkstyle:IllegalCatch", "checkstyle:IllegalThrows" })
+    public static /* @Provides @Singleton */ DataBroker dataBroker() throws RuntimeException {
+        try {
+            // This is a little bit "upside down" - in the future,
+            // we should probably put what is in AbstractDataBrokerTest
+            // into this DataBrokerTestModule, and make AbstractDataBrokerTest
+            // use it, instead of the way around it currently is (the opposite);
+            // this is just for historical reasons... and works for now.
+            AbstractDataBrokerTest dataBrokerTest = new AbstractDataBrokerTest();
+            dataBrokerTest.setup();
+            return dataBrokerTest.getDataBroker();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/AbstractDataBrokerTestTest.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/AbstractDataBrokerTestTest.java
new file mode 100644 (file)
index 0000000..e46d442
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.binding.test.tests;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
+
+public class AbstractDataBrokerTestTest extends AbstractDataBrokerTest {
+
+    @Test
+    public void ensureDataBrokerTestModuleWorksWithoutException() {
+        assertThat(getDataBroker()).isNotNull();
+    }
+
+}
diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/DataBrokerTestModuleTest.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/DataBrokerTestModuleTest.java
new file mode 100644 (file)
index 0000000..2016013
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.binding.test.tests;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.test.DataBrokerTestModule;
+
+public class DataBrokerTestModuleTest {
+
+    @Test
+    public void ensureDataBrokerTestModuleWorksWithoutException() {
+        assertThat(DataBrokerTestModule.dataBroker()).isNotNull();
+    }
+}