BGPCEP-742 Fix BGP NPE filter null BGP State 65/67065/3
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Thu, 11 Jan 2018 14:59:46 +0000 (15:59 +0100)
committerClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Thu, 11 Jan 2018 17:19:05 +0000 (17:19 +0000)
from BGP State collector.

Change-Id: Ic23277b22466140c2e17c429eea7467fd9d33ce8
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/state/BGPStateCollectorImpl.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/state/BGPStateCollectorImplTest.java

index 0937aac4f2e1cb114a4991318e76bdd1cbc0e52f..c865a08eb42cf6cbf7b4ae6b20babcaee17d4552 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.protocol.bgp.rib.impl.state;
 import com.google.common.collect.ImmutableList;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 import java.util.stream.Collectors;
 import javax.annotation.concurrent.GuardedBy;
 import javax.annotation.concurrent.ThreadSafe;
@@ -30,16 +31,22 @@ public class BGPStateCollectorImpl implements BGPStateProvider, BGPStateConsumer
     @Override
     public List<BGPRIBState> getRibStats() {
         synchronized (this.bgpRibStates) {
-            return ImmutableList.copyOf(this.bgpRibStates.stream().map(BGPRIBStateConsumer::getRIBState)
-                .collect(Collectors.toList()));
+            return ImmutableList.copyOf(this.bgpRibStates
+                    .stream()
+                    .map(BGPRIBStateConsumer::getRIBState)
+                    .filter(Objects::nonNull)
+                    .collect(Collectors.toList()));
         }
     }
 
     @Override
     public List<BGPPeerState> getPeerStats() {
         synchronized (this.bgpPeerStates) {
-            return ImmutableList.copyOf(this.bgpPeerStates.stream().map(BGPPeerStateConsumer::getPeerState)
-                .collect(Collectors.toList()));
+            return ImmutableList.copyOf(this.bgpPeerStates
+                    .stream()
+                    .map(BGPPeerStateConsumer::getPeerState)
+                    .filter(Objects::nonNull)
+                    .collect(Collectors.toList()));
         }
     }
 
index 9b201d52f1c0d98e5ac2e8c4ade8ae2ae996c713..dc5f3584fb6f38f9123a19d017fb7b9e2a78b350 100644 (file)
@@ -31,12 +31,12 @@ public class BGPStateCollectorImplTest {
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        doReturn(mock(BGPPeerState.class)).when(this.bgpPeerStateConsumer).getPeerState();
-        doReturn(mock(BGPRIBState.class)).when(this.bgpribStateConsumer).getRIBState();
     }
 
     @Test
-    public void getRibStats() throws Exception {
+    public void getRibStatsTest() throws Exception {
+        doReturn(mock(BGPPeerState.class)).when(this.bgpPeerStateConsumer).getPeerState();
+        doReturn(mock(BGPRIBState.class)).when(this.bgpribStateConsumer).getRIBState();
         final BGPStateCollectorImpl collector = new BGPStateCollectorImpl();
         final BGPRIBStateConsumer ribStateConsumerNull = null;
         collector.bind(ribStateConsumerNull);
@@ -56,4 +56,42 @@ public class BGPStateCollectorImplTest {
         assertTrue(collector.getRibStats().isEmpty());
         assertTrue(collector.getPeerStats().isEmpty());
     }
+
+    @Test
+    public void getRibStatsEmptyPeerTest() throws Exception {
+        doReturn(mock(BGPRIBState.class)).when(this.bgpribStateConsumer).getRIBState();
+        doReturn(null).when(this.bgpPeerStateConsumer).getPeerState();
+        final BGPStateCollectorImpl collector = new BGPStateCollectorImpl();
+        final BGPRIBStateConsumer ribStateConsumerNull = null;
+        collector.bind(ribStateConsumerNull);
+        assertTrue(collector.getRibStats().isEmpty());
+
+        final BGPPeerStateConsumer peerStateConsumerNull = null;
+        collector.bind(peerStateConsumerNull);
+        assertTrue(collector.getPeerStats().isEmpty());
+
+        collector.bind(this.bgpribStateConsumer);
+        collector.bind(this.bgpPeerStateConsumer);
+        assertFalse(collector.getRibStats().isEmpty());
+        assertTrue(collector.getPeerStats().isEmpty());
+    }
+
+    @Test
+    public void getRibStatsEmptyRibTest() throws Exception {
+        doReturn(null).when(this.bgpribStateConsumer).getRIBState();
+        doReturn(null).when(this.bgpPeerStateConsumer).getPeerState();
+        final BGPStateCollectorImpl collector = new BGPStateCollectorImpl();
+        final BGPRIBStateConsumer ribStateConsumerNull = null;
+        collector.bind(ribStateConsumerNull);
+        assertTrue(collector.getRibStats().isEmpty());
+
+        final BGPPeerStateConsumer peerStateConsumerNull = null;
+        collector.bind(peerStateConsumerNull);
+        assertTrue(collector.getPeerStats().isEmpty());
+
+        collector.bind(this.bgpribStateConsumer);
+        collector.bind(this.bgpPeerStateConsumer);
+        assertTrue(collector.getRibStats().isEmpty());
+        assertTrue(collector.getPeerStats().isEmpty());
+    }
 }
\ No newline at end of file