SPQRResourceManagerClient { public NodeRegistrationResponse registerNode(final String nodeProtocol, final String nodeHost, final int nodeServicePort, final int nodeAdminPort) throws RequiredInputMissingException, RemoteClientConnectionFailedException, IOException { if(StringUtils.isBlank(nodeProtocol)) throw new RequiredInputMissingException("Missing required protocol"); if(StringUtils.isBlank(nodeHost)) throw new RequiredInputMissingException("Missing required host"); if(nodeServicePort < 1) throw new RequiredInputMissingException("Missing valid service port"); if(nodeAdminPort < 1) throw new RequiredInputMissingException("Missing valid admin port"); NodeRegistrationRequest message = new NodeRegistrationRequest(nodeProtocol, nodeHost, nodeServicePort, nodeAdminPort); StringBuffer url = new StringBuffer(this.resourceManagerServiceBaseUrl).append("/nodes"); if(logger.isDebugEnabled()) logger.debug("Registering processing node [protocol="+nodeProtocol+", host="+nodeHost+", servicePort="+nodeServicePort+", adminPort="+nodeAdminPort+"] at resource manager " + url.toString()); try { final WebTarget webTarget = this.restClient.target(url.toString()); return webTarget.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(Entity.entity(message, MediaType.APPLICATION_JSON), NodeRegistrationResponse.class); } catch(Exception e) { throw new RemoteClientConnectionFailedException("Failed to establish a connection with the remote resource manager [url="+url.toString()+"]. Error: " + e.getMessage()); } } SPQRResourceManagerClient(final String resourceManagerProtocol, final String resourceManagerRemoteHost, 
			final int resourceManagerServicePort, final Client resourceManagerClient); NodeRegistrationResponse registerNode(final String nodeProtocol, final String nodeHost, final int nodeServicePort, final int nodeAdminPort); NodeDeRegistrationResponse deregisterNode(final String nodeId);  }
@Test public void testRegisterNode_withEmptyNodeProtocol() throws Exception { try { new SPQRResourceManagerClient("http", "resourceManager", 9090, Mockito.mock(Client.class)).registerNode("", "localhost", 8080, 8081); Assert.fail("Invalid input"); } catch(RequiredInputMissingException e) { } }
@Test public void testRegisterNode_withEmptyNodeHost() throws Exception { try { new SPQRResourceManagerClient("http", "localhost", 9090, Mockito.mock(Client.class)).registerNode("http", "", 8080, 8081); Assert.fail("Invalid input"); } catch(RequiredInputMissingException e) { } }
@Test public void testRegisterNode_withInvalidNodeServicePort() throws Exception { try { new SPQRResourceManagerClient("http", "resourceManager", 9090, Mockito.mock(Client.class)).registerNode("http", "localhost", -1, 8081); Assert.fail("Invalid input"); } catch(RequiredInputMissingException e) { } }
@Test public void testRegisterNode_withInvalidNodeAdminPort() throws Exception { try { new SPQRResourceManagerClient("http", "resourceManager", 9090, Mockito.mock(Client.class)).registerNode("http", "localhost", 8080, -1); Assert.fail("Invalid input"); } catch(RequiredInputMissingException e) { } }
@Test public void testRegisterNode_withValidInput() throws Exception { String remoteUrl = "http: NodeRegistrationResponse expectedResponse = new NodeRegistrationResponse("test-id", NodeRegistrationState.OK); Builder builder2 = Mockito.mock(Builder.class); Mockito.when(builder2.post(Mockito.isA(Entity.class), Mockito.eq(NodeRegistrationResponse.class))).thenReturn(expectedResponse); Builder builder1 = Mockito.mock(Builder.class); Mockito.when(builder1.accept(MediaType.APPLICATION_JSON)).thenReturn(builder2); WebTarget mockWebTarget = Mockito.mock(WebTarget.class); Mockito.when(mockWebTarget.request(MediaType.APPLICATION_JSON)).thenReturn(builder1); Client mockClient = Mockito.mock(Client.class); Mockito.when(mockClient.target(remoteUrl)).thenReturn(mockWebTarget); SPQRResourceManagerClient client = new SPQRResourceManagerClient("http", "localhost", 7070, mockClient); NodeRegistrationResponse response = client.registerNode("http", "localhost", 8080, 8081); Assert.assertNotNull("The response must not be null", response); Assert.assertEquals("No errors expected", NodeRegistrationState.OK, response.getState()); Mockito.verify(mockClient).target(remoteUrl); Mockito.verify(mockWebTarget).request(MediaType.APPLICATION_JSON); Mockito.verify(builder1).accept(MediaType.APPLICATION_JSON); Mockito.verify(builder2).post(Mockito.isA(Entity.class), Mockito.eq(NodeRegistrationResponse.class)); }