From e0819c56a40458d9eac0ebbbe1d2049f795cfe95 Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Wed, 10 Aug 2016 17:22:30 +0200 Subject: [PATCH] DataBrokerTestModule: use AbstractDataBrokerTest without inheritance 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 --- .../md-sal/sal-binding-broker/pom.xml | 5 +++ .../binding/test/DataBrokerTestModule.java | 31 +++++++++++++++++++ .../tests/AbstractDataBrokerTestTest.java | 22 +++++++++++++ .../test/tests/DataBrokerTestModuleTest.java | 21 +++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/DataBrokerTestModule.java create mode 100644 opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/AbstractDataBrokerTestTest.java create mode 100644 opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/DataBrokerTestModuleTest.java diff --git a/opendaylight/md-sal/sal-binding-broker/pom.xml b/opendaylight/md-sal/sal-binding-broker/pom.xml index bc5b4bfddc..1e53fe2073 100644 --- a/opendaylight/md-sal/sal-binding-broker/pom.xml +++ b/opendaylight/md-sal/sal-binding-broker/pom.xml @@ -78,6 +78,11 @@ mockito-all test + + com.google.truth + truth + test + org.opendaylight.yangtools yang-parser-impl 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 index 0000000000..96facbed9c --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/DataBrokerTestModule.java @@ -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 index 0000000000..e46d442b21 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/AbstractDataBrokerTestTest.java @@ -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 index 0000000000..20160131bc --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/tests/DataBrokerTestModuleTest.java @@ -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(); + } +} -- 2.36.6