SPQRNodeManager { public NodeRegistrationResponse registerNode(final String protocol, final String remoteHost, final int servicePort, final int adminPort) { if(StringUtils.isBlank(protocol)) return new NodeRegistrationResponse("", NodeRegistrationState.MISSING_PROTOCOL, "Missing required protocol"); if(StringUtils.isBlank(remoteHost)) return new NodeRegistrationResponse("", NodeRegistrationState.MISSING_HOST, "Missing required remote host"); if(servicePort < 1) return new NodeRegistrationResponse("", NodeRegistrationState.MISSING_SERVICE_PORT, "Missing required service port"); if(adminPort < 1) return new NodeRegistrationResponse("", NodeRegistrationState.MISSING_ADMIN_PORT, "Missing required admin port"); String nodeId = null; int retries = this.numIdentifierComputationRetries; while(retries > 0) { nodeId = uuidGenerator.generate().toString(); if(!this.processingNodes.containsKey(nodeId)) break; } if(nodeId == null || this.processingNodes.containsKey(nodeId)) return new NodeRegistrationResponse("", NodeRegistrationState.NODE_ID_COMPUTATION_FAILED, "Failed to compute node identifier"); try { this.processingNodes.put(nodeId, new SPQRNodeClient(protocol, remoteHost, servicePort, adminPort, getHttpClient(nodeId))); } catch(RequiredInputMissingException e) { return new NodeRegistrationResponse("", NodeRegistrationState.TECHNICAL_ERROR, "Failed to create spqr node client. Error: " +e.getMessage()); } return new NodeRegistrationResponse(nodeId, NodeRegistrationState.OK, ""); } SPQRNodeManager(final int numIdentifierComputationRetries, final JerseyClientBuilder httpClientBuilder); NodeRegistrationResponse registerNode(final String protocol, final String remoteHost, final int servicePort, final int adminPort); NodeDeRegistrationResponse deregisterNode(final String nodeId); boolean hasNode(final String nodeId);  }
@Test public void testRegisterNode_withEmptyProtocol() { Assert.assertEquals("Invalid input", NodeRegistrationState.MISSING_PROTOCOL, new SPQRNodeManager(5, Mockito.mock(JerseyClientBuilder.class)).registerNode("", "localhost", 8080, 9090).getState()); }
@Test public void testRegisterNode_withEmptyRemoteHost() { Assert.assertEquals("Invalid input", NodeRegistrationState.MISSING_HOST, new SPQRNodeManager(5, Mockito.mock(JerseyClientBuilder.class)).registerNode("http", "", 8080, 9090).getState()); }
@Test public void testRegisterNode_withInvalidServicePort() { Assert.assertEquals("Invalid input", NodeRegistrationState.MISSING_SERVICE_PORT, new SPQRNodeManager(5, Mockito.mock(JerseyClientBuilder.class)).registerNode("http", "localhost", -1, 9090).getState()); }
@Test public void testRegisterNode_withInvalidAdminPort() { Assert.assertEquals("Invalid input", NodeRegistrationState.MISSING_ADMIN_PORT, new SPQRNodeManager(5, Mockito.mock(JerseyClientBuilder.class)).registerNode("http", "localhost", 8080, -1).getState()); }
@Test public void testRegisterNode_withFailedNodeIdComputation() { Assert.assertEquals("Invalid input", NodeRegistrationState.NODE_ID_COMPUTATION_FAILED, new SPQRNodeManager(0, Mockito.mock(JerseyClientBuilder.class)).registerNode("http", "localhost", 8080, 9090).getState()); }