Fix NPE on reading data 24/8424/5
authorMoiz Raja <moraja@cisco.com>
Fri, 27 Jun 2014 01:31:07 +0000 (18:31 -0700)
committerEd Warnicke <eaw@cisco.com>
Fri, 4 Jul 2014 20:26:33 +0000 (20:26 +0000)
When reading data if a null value is returned return Optional.absent();

Change-Id: I2d6a3a8001492c8c4c50a779b2770c72a9953494
Signed-off-by: Moiz Raja <moraja@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionProxyTest.java

index c122761..00196eb 100644 (file)
@@ -91,6 +91,9 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction {
                         ActorContext.ASK_DURATION);
                 if(response instanceof ReadDataReply){
                     ReadDataReply reply = (ReadDataReply) response;
                         ActorContext.ASK_DURATION);
                 if(response instanceof ReadDataReply){
                     ReadDataReply reply = (ReadDataReply) response;
+                    if(reply.getNormalizedNode() == null){
+                        return Optional.absent();
+                    }
                     //FIXME : A cast should not be required here ???
                     return (Optional<NormalizedNode<?, ?>>) Optional.of(reply.getNormalizedNode());
                 }
                     //FIXME : A cast should not be required here ???
                     return (Optional<NormalizedNode<?, ?>>) Optional.of(reply.getNormalizedNode());
                 }
index 6d057a4..8b70e00 100644 (file)
@@ -57,6 +57,37 @@ public class TransactionProxyTest extends AbstractActorTest {
         Assert.assertTrue(normalizedNodeOptional.isPresent());
     }
 
         Assert.assertTrue(normalizedNodeOptional.isPresent());
     }
 
+    @Test
+    public void testReadWhenANullIsReturned() throws Exception {
+        final Props props = Props.create(DoNothingActor.class);
+        final ActorRef actorRef = getSystem().actorOf(props);
+
+        final MockActorContext actorContext = new MockActorContext(this.getSystem());
+        actorContext.setExecuteShardOperationResponse(new CreateTransactionReply(actorRef.path()));
+        actorContext.setExecuteRemoteOperationResponse("message");
+
+        TransactionProxy transactionProxy =
+            new TransactionProxy(actorContext,
+                TransactionProxy.TransactionType.READ_ONLY);
+
+
+        ListenableFuture<Optional<NormalizedNode<?, ?>>> read =
+            transactionProxy.read(TestModel.TEST_PATH);
+
+        Optional<NormalizedNode<?, ?>> normalizedNodeOptional = read.get();
+
+        Assert.assertFalse(normalizedNodeOptional.isPresent());
+
+        actorContext.setExecuteRemoteOperationResponse(new ReadDataReply(
+            null));
+
+        read = transactionProxy.read(TestModel.TEST_PATH);
+
+        normalizedNodeOptional = read.get();
+
+        Assert.assertFalse(normalizedNodeOptional.isPresent());
+    }
+
     @Test
     public void testWrite() throws Exception {
         final Props props = Props.create(MessageCollectorActor.class);
     @Test
     public void testWrite() throws Exception {
         final Props props = Props.create(MessageCollectorActor.class);

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.