### Question:
NodeManager { public synchronized List<NodeHandler> getNodes(Set<String> nodesInUse) { List<NodeHandler> handlers = new ArrayList<>(); List<Node> foundNodes = this.peerExplorer.getNodes(); if (this.discoveryEnabled && CollectionUtils.isNotEmpty(foundNodes)) { logger.debug("{} Nodes retrieved from the PE.", CollectionUtils.size(foundNodes)); foundNodes.stream().filter(n -> !nodeHandlerMap.containsKey(n.getHexId())).forEach(this::createNodeHandler); } for(NodeHandler handler : this.nodeHandlerMap.values()) { if(!nodesInUse.contains(handler.getNode().getHexId())) { handlers.add(handler); } } return handlers; } @Autowired  NodeManager(PeerExplorer peerExplorer, SystemProperties config); NodeStatistics getNodeStatistics(Node n); synchronized List<NodeHandler> getNodes(Set<String> nodesInUse);  }### Answer:
@Test public void getNodesPeerDiscoveryDisable() { List<Node> activePeers = new ArrayList<>(); activePeers.add(new Node(Hex.decode(NODE_ID_2), "127.0.0.2", 8081)); List<Node> bootNodes = new ArrayList<>(); bootNodes.add(new Node(Hex.decode(NODE_ID_3), "127.0.0.3", 8083)); Mockito.when(config.peerActive()).thenReturn(activePeers); Mockito.when(peerExplorer.getNodes()).thenReturn(bootNodes); Mockito.when(config.isPeerDiscoveryEnabled()).thenReturn(false); NodeManager nodeManager = new NodeManager(peerExplorer, config); Set<String> nodesInUse = new HashSet<>(); List<NodeHandler> availableNodes = nodeManager.getNodes(nodesInUse); Assert.assertEquals(1, availableNodes.size()); Assert.assertEquals(NODE_ID_2, availableNodes.get(0).getNode().getHexId()); nodesInUse.add(NODE_ID_2); availableNodes = nodeManager.getNodes(nodesInUse); Assert.assertEquals(0, availableNodes.size()); }
@Test public void getNodesPeerDiscoveryEnableNoPeersFound() { List<Node> activePeers = new ArrayList<>(); List<Node> bootNodes = new ArrayList<>(); Mockito.when(config.peerActive()).thenReturn(activePeers); Mockito.when(peerExplorer.getNodes()).thenReturn(bootNodes); Mockito.when(config.isPeerDiscoveryEnabled()).thenReturn(true); NodeManager nodeManager = new NodeManager(peerExplorer, config); Set<String> nodesInUse = new HashSet<>(); List<NodeHandler> availableNodes = nodeManager.getNodes(nodesInUse); Assert.assertEquals(0, availableNodes.size()); }
@Test public void getNodesPeerDiscoveryEnable() { List<Node> activePeers = new ArrayList<>(); activePeers.add(new Node(Hex.decode(NODE_ID_2), "127.0.0.2", 8081)); List<Node> bootNodes = new ArrayList<>(); bootNodes.add(new Node(Hex.decode(NODE_ID_3), "127.0.0.3", 8083)); Mockito.when(config.peerActive()).thenReturn(activePeers); Mockito.when(peerExplorer.getNodes()).thenReturn(bootNodes); Mockito.when(config.isPeerDiscoveryEnabled()).thenReturn(true); NodeManager nodeManager = new NodeManager(peerExplorer, config); Set<String> nodesInUse = new HashSet<>(); List<NodeHandler> availableNodes = nodeManager.getNodes(nodesInUse); Assert.assertEquals(2, availableNodes.size()); }