### Question:
PeerTable { public AddResult tryAdd(final DiscoveryPeer peer) { final Bytes id = peer.getId(); final int distance = distanceFrom(peer); if (distance == 0) { return AddResult.self(); } final Bucket bucket = table[distance]; final Optional<DiscoveryPeer> res; try { res = bucket.add(peer); } catch (final IllegalArgumentException ex) { return AddResult.existed(); } if (!res.isPresent()) { idBloom.put(id); distanceCache.put(id, distance); return AddResult.added(); } return res.map(AddResult::bucketFull).get(); } PeerTable(final Bytes nodeId, final int bucketSize);  PeerTable(final Bytes nodeId); Optional<DiscoveryPeer> get(final PeerId peer); AddResult tryAdd(final DiscoveryPeer peer); EvictResult tryEvict(final PeerId peer); List<DiscoveryPeer> nearestPeers(final Bytes target, final int limit); Stream<DiscoveryPeer> streamAllPeers();  }### Answer:
@Test public void peerExists() { final PeerTable table = new PeerTable(Peer.randomId(), 16); final DiscoveryPeer peer = helper.createDiscoveryPeer(); assertThat(table.tryAdd(peer).getOutcome()).isEqualTo(AddOutcome.ADDED); assertThat(table.tryAdd(peer)) .satisfies( result -> { assertThat(result.getOutcome()).isEqualTo(AddOutcome.ALREADY_EXISTED); assertThat(result.getEvictionCandidate()).isNull(); }); }
@Test public void peerExists_withDifferentIp() { final PeerTable table = new PeerTable(Peer.randomId(), 16); final Bytes peerId = KeyPair.generate().getPublicKey().getEncodedBytes(); final DiscoveryPeer peer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.1", 30303, Optional.empty())); assertThat(table.tryAdd(peer).getOutcome()).isEqualTo(AddOutcome.ADDED); final DiscoveryPeer duplicatePeer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.2", 30303, Optional.empty())); assertThat(table.tryAdd(duplicatePeer)) .satisfies( result -> { assertThat(result.getOutcome()).isEqualTo(AddOutcome.ALREADY_EXISTED); assertThat(result.getEvictionCandidate()).isNull(); }); }
@Test public void peerExists_withDifferentUdpPort() { final PeerTable table = new PeerTable(Peer.randomId(), 16); final Bytes peerId = KeyPair.generate().getPublicKey().getEncodedBytes(); final DiscoveryPeer peer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.1", 30303, Optional.empty())); assertThat(table.tryAdd(peer).getOutcome()).isEqualTo(AddOutcome.ADDED); final DiscoveryPeer duplicatePeer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.1", 30301, Optional.empty())); assertThat(table.tryAdd(duplicatePeer)) .satisfies( result -> { assertThat(result.getOutcome()).isEqualTo(AddOutcome.ALREADY_EXISTED); assertThat(result.getEvictionCandidate()).isNull(); }); }
@Test public void peerExists_withDifferentIdAndUdpPort() { final PeerTable table = new PeerTable(Peer.randomId(), 16); final Bytes peerId = KeyPair.generate().getPublicKey().getEncodedBytes(); final DiscoveryPeer peer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.1", 30303, Optional.empty())); assertThat(table.tryAdd(peer).getOutcome()).isEqualTo(AddOutcome.ADDED); final DiscoveryPeer duplicatePeer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.2", 30301, Optional.empty())); assertThat(table.tryAdd(duplicatePeer)) .satisfies( result -> { assertThat(result.getOutcome()).isEqualTo(AddOutcome.ALREADY_EXISTED); assertThat(result.getEvictionCandidate()).isNull(); }); }