add requireNonNull to SingleTransactionDataBroker constructor 18/69818/6
authorMichael Vorburger <vorburger@redhat.com>
Thu, 22 Mar 2018 13:54:07 +0000 (14:54 +0100)
committerMichael Vorburger <vorburger@redhat.com>
Thu, 5 Apr 2018 15:46:38 +0000 (15:46 +0000)
thought of this while seeing this which is actually broken:

  @Inject DataBroker dataBroker;
  S.T.D.B. db = new SingleTransactionDataBroker(dataBroker);

which cannot work with Dependency Injection (Guice or otherwise), and
it's better to have the null check here in the constructor already, not
fail with NPE later when one of the (non-static) methods of it is used.

see Change-Id: I63d0a93da481003b701c5a1843cece7d50cf51d1

Change-Id: I319d92c2bb50446ff59d156c4f261a51afe01863
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
interfacemanager/interfacemanager-impl/src/test/java/org/opendaylight/genius/interfacemanager/test/InterfaceManagerConfigurationTest.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/SingleTransactionDataBroker.java

index 36ad9d267a9cd05f89936be2dc9786cc4dbc526d..84708c7331bd290dd3bbb86ebf79a2f597ea17d1 100644 (file)
@@ -180,7 +180,6 @@ public class InterfaceManagerConfigurationTest {
         TestableDataTreeChangeListenerModule.class, JobCoordinatorTestModule.class);
 
     @Inject DataBroker dataBroker;
-    SingleTransactionDataBroker db = new SingleTransactionDataBroker(dataBroker);
     @Inject OdlInterfaceRpcService odlInterfaceRpcService;
     @Inject IInterfaceManager interfaceManager;
     @Inject JobCoordinatorCountedEventsWaiter coordinatorEventsWaiter;
@@ -189,8 +188,12 @@ public class InterfaceManagerConfigurationTest {
     @Inject BatchingUtils batchingUtils;
     @Inject FlowAssertTestUtils flowAssertTestUtils;
 
+    SingleTransactionDataBroker db;
+
     @Before
     public void start() throws InterruptedException, TransactionCommitFailedException {
+        db = new SingleTransactionDataBroker(dataBroker);
+
         // Create the bridge and make sure it is ready
         setupAndAssertBridgeCreation();
     }
index aceb56fd79496d8924e44c5fada4485758aacd56..48be5d669e08b9d868ffa4b6384c7db9263cc124 100644 (file)
@@ -7,9 +7,11 @@
  */
 package org.opendaylight.genius.datastoreutils;
 
+import static java.util.Objects.requireNonNull;
 import static org.opendaylight.genius.datastoreutils.TransactionCommitFailedExceptionMapper.SUBMIT_MAPPER;
 
 import com.google.common.base.Optional;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
@@ -38,8 +40,8 @@ public class SingleTransactionDataBroker {
     private final DataBroker broker;
 
     // do *NOT* use BP @Inject here, see comment above
-    public SingleTransactionDataBroker(DataBroker broker) {
-        this.broker = broker;
+    public SingleTransactionDataBroker(@Nonnull DataBroker broker) {
+        this.broker = requireNonNull(broker, "dataBroker");
     }
 
     /**