HostResource implements HostApi { @Override public PatchHostResponse patch(String hostNameString, PatchHostRequest request) { HostName hostName = new HostName(hostNameString); if (request.state != null) { HostStatus state; try { state = HostStatus.valueOf(request.state); } catch (IllegalArgumentException dummy) { throw new BadRequestException("Bad state in request: '" + request.state + "'"); } try { orchestrator.setNodeStatus(hostName, state); } catch (HostNameNotFoundException e) { log.log(Level.FINE, "Host not found: " + hostName); throw new NotFoundException(e); } catch (UncheckedTimeoutException e) { log.log(Level.FINE, "Failed to patch " + hostName + ": " + e.getMessage()); throw webExceptionFromTimeout("patch", hostName, e); } catch (OrchestrationException e) { String message = "Failed to set " + hostName + " to " + state + ": " + e.getMessage(); log.log(Level.FINE, message, e); throw new InternalServerErrorException(message); } } PatchHostResponse response = new PatchHostResponse(); response.description = "ok"; return response; } @Inject  HostResource(@Component Orchestrator orchestrator, @Context UriInfo uriInfo); @Override GetHostResponse getHost(String hostNameString); @Override PatchHostResponse patch(String hostNameString, PatchHostRequest request); @Override UpdateHostResponse suspend(String hostNameString); @Override UpdateHostResponse resume(final String hostNameString);  }
@Test(expected = BadRequestException.class) public void patch_state_may_throw_bad_request() { Orchestrator orchestrator = mock(Orchestrator.class); HostResource hostResource = new HostResource(orchestrator, uriInfo); String hostNameString = "hostname"; PatchHostRequest request = new PatchHostRequest(); request.state = "bad state"; hostResource.patch(hostNameString, request); }
@Test public void patch_works() throws OrchestrationException { Orchestrator orchestrator = mock(Orchestrator.class); HostResource hostResource = new HostResource(orchestrator, uriInfo); String hostNameString = "hostname"; PatchHostRequest request = new PatchHostRequest(); request.state = "NO_REMARKS"; PatchHostResponse response = hostResource.patch(hostNameString, request); assertEquals(response.description, "ok"); verify(orchestrator, times(1)).setNodeStatus(new HostName(hostNameString), HostStatus.NO_REMARKS); }
@Test(expected = InternalServerErrorException.class) public void patch_handles_exception_in_orchestrator() throws OrchestrationException { Orchestrator orchestrator = mock(Orchestrator.class); HostResource hostResource = new HostResource(orchestrator, uriInfo); String hostNameString = "hostname"; PatchHostRequest request = new PatchHostRequest(); request.state = "NO_REMARKS"; doThrow(new OrchestrationException("error")).when(orchestrator).setNodeStatus(new HostName(hostNameString), HostStatus.NO_REMARKS); hostResource.patch(hostNameString, request); }