### Question:
InsufficientPeersPermissioningProvider implements ContextualNodePermissioningProvider { @Override public Optional<Boolean> isPermitted( final EnodeURL sourceEnode, final EnodeURL destinationEnode) { Optional<EnodeURL> maybeSelfEnode = p2pNetwork.getLocalEnode(); if (nonBootnodePeerConnections > 0) { return Optional.empty(); } else if (!maybeSelfEnode.isPresent()) { return Optional.empty(); } else if (checkEnode(maybeSelfEnode.get(), sourceEnode) && checkEnode(maybeSelfEnode.get(), destinationEnode)) { return Optional.of(true); } else { return Optional.empty(); } } InsufficientPeersPermissioningProvider(
      final P2PNetwork p2pNetwork, final Collection<EnodeURL> bootnodeEnodes); @Override Optional<Boolean> isPermitted(
      final EnodeURL sourceEnode, final EnodeURL destinationEnode); @Override long subscribeToUpdates(final Runnable callback); @Override boolean unsubscribeFromUpdates(final long id);  }### Answer:
@Test public void noResultWhenNoBootnodes() { final Collection<EnodeURL> bootnodes = Collections.emptyList(); when(p2pNetwork.getPeers()).thenReturn(Collections.emptyList()); final InsufficientPeersPermissioningProvider provider = new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes); assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).isEmpty(); }
@Test public void noResultWhenOtherConnections() { final PeerConnection neverMatchPeerConnection = mock(PeerConnection.class); when(neverMatchPeerConnection.getRemoteEnode()).thenReturn(ENODE_5); when(p2pNetwork.getPeers()).thenReturn(Collections.singletonList(neverMatchPeerConnection)); final Collection<EnodeURL> bootnodes = Collections.singletonList(ENODE_2); final InsufficientPeersPermissioningProvider provider = new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes); assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty(); assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).isEmpty(); }
@Test public void allowsConnectionIfBootnodeAndNoConnections() { final Collection<EnodeURL> bootnodes = Collections.singletonList(ENODE_2); when(p2pNetwork.getPeers()).thenReturn(Collections.emptyList()); final InsufficientPeersPermissioningProvider provider = new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes); assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).contains(true); assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty(); }
@Test public void noResultWhenLocalNodeNotReady() { final Collection<EnodeURL> bootnodes = Collections.singletonList(ENODE_2); when(p2pNetwork.getPeers()).thenReturn(Collections.emptyList()); when(p2pNetwork.getLocalEnode()).thenReturn(Optional.empty()); final InsufficientPeersPermissioningProvider provider = new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes); assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).isEmpty(); assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty(); }
@Test public void allowsConnectionIfBootnodeAndOnlyBootnodesConnected() { final Collection<EnodeURL> bootnodes = Collections.singletonList(ENODE_2); final PeerConnection bootnodeMatchPeerConnection = mock(PeerConnection.class); when(bootnodeMatchPeerConnection.getRemoteEnode()).thenReturn(ENODE_2); when(p2pNetwork.getPeers()).thenReturn(Collections.singletonList(bootnodeMatchPeerConnection)); final InsufficientPeersPermissioningProvider provider = new InsufficientPeersPermissioningProvider(p2pNetwork, bootnodes); assertThat(provider.isPermitted(SELF_ENODE, ENODE_2)).contains(true); assertThat(provider.isPermitted(SELF_ENODE, ENODE_3)).isEmpty(); }