HostResource implements HostApi { @Override public GetHostResponse getHost(String hostNameString) { HostName hostName = new HostName(hostNameString); try { Host host = orchestrator.getHost(hostName); URI applicationUri = uriInfo.getBaseUriBuilder() .path(InstanceResource.class) .path(host.getApplicationInstanceReference().asString()) .build(); List<HostService> hostServices = host.getServiceInstances().stream() .map(serviceInstance -> new HostService( serviceInstance.getServiceCluster().clusterId().s(), serviceInstance.getServiceCluster().serviceType().s(), serviceInstance.configId().s(), serviceInstance.serviceStatus().name())) .collect(Collectors.toList()); return new GetHostResponse( host.getHostName().s(), host.getHostInfo().status().name(), host.getHostInfo().suspendedSince().map(Instant::toString).orElse(null), applicationUri.toString(), hostServices); } catch (UncheckedTimeoutException e) { log.log(Level.FINE, "Failed to get host " + hostName + ": " + e.getMessage()); throw webExceptionFromTimeout("getHost", hostName, e); } catch (HostNameNotFoundException e) { log.log(Level.FINE, "Host not found: " + hostName); throw new NotFoundException(e); } } @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 getHost_works() throws Exception { Orchestrator orchestrator = mock(Orchestrator.class); HostResource hostResource = new HostResource(orchestrator, uriInfo); HostName hostName = new HostName("hostname"); UriBuilder baseUriBuilder = mock(UriBuilder.class); when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder); when(baseUriBuilder.path(any(String.class))).thenReturn(baseUriBuilder); when(baseUriBuilder.path(any(Class.class))).thenReturn(baseUriBuilder); URI uri = new URI("https: when(baseUriBuilder.build()).thenReturn(uri); ServiceInstance serviceInstance = new ServiceInstance( new ConfigId("configId"), hostName, ServiceStatus.UP); ServiceCluster serviceCluster = new ServiceCluster( new ClusterId("clusterId"), new ServiceType("serviceType"), Collections.singleton(serviceInstance)); serviceInstance.setServiceCluster(serviceCluster); Host host = new Host( hostName, HostInfo.createSuspended(HostStatus.ALLOWED_TO_BE_DOWN, Instant.EPOCH), new ApplicationInstanceReference( new TenantId("tenantId"), new ApplicationInstanceId("applicationId")), Collections.singletonList(serviceInstance)); when(orchestrator.getHost(hostName)).thenReturn(host); GetHostResponse response = hostResource.getHost(hostName.s()); assertEquals("https: assertEquals("hostname", response.hostname()); assertEquals("ALLOWED_TO_BE_DOWN", response.state()); assertEquals("1970-01-01T00:00:00Z", response.suspendedSince()); assertEquals(1, response.services().size()); assertEquals("clusterId", response.services().get(0).clusterId); assertEquals("configId", response.services().get(0).configId); assertEquals("UP", response.services().get(0).serviceStatus); assertEquals("serviceType", response.services().get(0).serviceType); }