HostResource implements HostApi { @Override public UpdateHostResponse suspend(String hostNameString) { HostName hostName = new HostName(hostNameString); try { orchestrator.suspend(hostName); } catch (HostNameNotFoundException e) { log.log(Level.FINE, "Host not found: " + hostName); throw new NotFoundException(e); } catch (UncheckedTimeoutException e) { log.log(Level.FINE, "Failed to suspend " + hostName + ": " + e.getMessage()); throw webExceptionFromTimeout("suspend", hostName, e); } catch (HostStateChangeDeniedException e) { log.log(Level.FINE, "Failed to suspend " + hostName + ": " + e.getMessage()); throw webExceptionWithDenialReason("suspend", hostName, e); } return new UpdateHostResponse(hostName.s(), null); } @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 public void returns_200_on_success() { HostResource hostResource = new HostResource(alwaysAllowOrchestrator, uriInfo); final String hostName = "hostname"; UpdateHostResponse response = hostResource.suspend(hostName); assertEquals(hostName, response.hostname()); }
@Test public void throws_404_when_host_unknown() { try { HostResource hostResource = new HostResource(hostNotFoundOrchestrator, uriInfo); hostResource.suspend("hostname"); fail(); } catch (WebApplicationException w) { assertEquals(404, w.getResponse().getStatus()); } }
@Test public void throws_409_when_request_rejected_by_policies() { final OrchestratorImpl alwaysRejectResolver = new OrchestratorImpl( new AlwaysFailPolicy(), new ClusterControllerClientFactoryMock(), EVERY_HOST_IS_UP_HOST_STATUS_SERVICE, serviceMonitor, SERVICE_MONITOR_CONVERGENCE_LATENCY_SECONDS, clock, applicationApiFactory, flagSource); try { HostResource hostResource = new HostResource(alwaysRejectResolver, uriInfo); hostResource.suspend("hostname"); fail(); } catch (WebApplicationException w) { assertEquals(409, w.getResponse().getStatus()); } }