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;
@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()));
}
}
@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);
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