HttpProxy { public HttpResponse get(Application application, String hostName, String serviceType, String relativePath) { HostInfo host = application.getModel().getHosts().stream() .filter(hostInfo -> hostInfo.getHostname().equals(hostName)) .findFirst() .orElseThrow(() -> new NotFoundException("Failed to find host " + hostName)); ServiceInfo service = host.getServices().stream() .filter(serviceInfo -> serviceType.equals(serviceInfo.getServiceType())) .findFirst() .orElseThrow(() -> new NotFoundException("Failed to find any service of type " + serviceType + " on host " + hostName)); PortInfo port = service.getPorts().stream() .filter(portInfo -> portInfo.getTags().stream().collect(Collectors.toSet()).containsAll( Stream.of("http", "state").collect(Collectors.toSet()))) .findFirst() .orElseThrow(() -> new NotFoundException("Failed to find HTTP state port")); return internalGet(host.getHostname(), port.getPort(), relativePath); } @Inject  HttpProxy();  HttpProxy(HttpFetcher fetcher); HttpResponse get(Application application, String hostName, String serviceType, String relativePath);  }
@Test public void testNormalGet() throws Exception { ArgumentCaptor<HttpFetcher.Params> actualParams = ArgumentCaptor.forClass(HttpFetcher.Params.class); ArgumentCaptor<URL> actualUrl = ArgumentCaptor.forClass(URL.class); HttpResponse response = new StaticResponse(200, "application/json", "body"); when(fetcher.get(actualParams.capture(), actualUrl.capture())).thenReturn(response); HttpResponse actualResponse = proxy.get(applicationMock, hostname, CLUSTERCONTROLLER_CONTAINER.serviceName, "clustercontroller-status/v1/clusterName"); assertEquals(1, actualParams.getAllValues().size()); assertEquals(2000, actualParams.getValue().readTimeoutMs); assertEquals(1, actualUrl.getAllValues().size()); assertEquals(new URL("http: actualUrl.getValue()); assertSame(actualResponse, response); }
@Test(expected = RequestTimeoutException.class) public void testFetchException() { when(fetcher.get(any(), any())).thenThrow(new RequestTimeoutException("timed out")); proxy.get(applicationMock, hostname, CLUSTERCONTROLLER_CONTAINER.serviceName, "clustercontroller-status/v1/clusterName"); }