ChainedHostResolver implements AdvancedHostResolver { @Override public Collection<InetAddress> resolve(String host) { readLock.lock(); try { return resolvers.stream() .map(resolver -> resolver.resolve(host)) .filter(results -> !results.isEmpty()) .findFirst() .orElse(emptyList()); } finally { readLock.unlock(); } } ChainedHostResolver(Collection<? extends AdvancedHostResolver> resolvers); Collection<? extends AdvancedHostResolver> getResolvers(); @Override void remapHosts(Map<String, String> hostRemappings); @Override void remapHost(String originalHost, String remappedHost); @Override void removeHostRemapping(String originalHost); @Override void clearHostRemappings(); @Override Map<String, String> getHostRemappings(); @Override Collection<String> getOriginalHostnames(String remappedHost); @Override void clearDNSCache(); @Override void setPositiveDNSCacheTimeout(int timeout, TimeUnit timeUnit); @Override void setNegativeDNSCacheTimeout(int timeout, TimeUnit timeUnit); @Override Collection<InetAddress> resolve(String host);  }
@Test public void testResolveReturnsFirstResults() { AdvancedHostResolver firstResolver = mock(AdvancedHostResolver.class); AdvancedHostResolver secondResolver = mock(AdvancedHostResolver.class); ChainedHostResolver chainResolver = new ChainedHostResolver(ImmutableList.of(firstResolver, secondResolver)); when(firstResolver.resolve("1.1.1.1")).thenReturn(TestConstants.addressOnesList); when(secondResolver.resolve("1.1.1.1")).thenReturn(Collections.<InetAddress>emptyList()); Collection<InetAddress> results = chainResolver.resolve("1.1.1.1"); assertNotNull("Resolver should not return null results", results); assertThat("Expected resolver to return a result", results, not(empty())); Assert.assertEquals("Resolver returned unexpected result", TestConstants.addressOnes, Iterables.get(results, 0)); verify(secondResolver, never()).resolve("1.1.1.1"); reset(firstResolver); reset(secondResolver); when(firstResolver.resolve("2.2.2.2")).thenReturn(Collections.<InetAddress>emptyList()); when(secondResolver.resolve("2.2.2.2")).thenReturn(TestConstants.addressTwosList); results = chainResolver.resolve("2.2.2.2"); assertNotNull("Resolver should not return null results", results); assertThat("Expected resolver to return a result", results, not(empty())); Assert.assertEquals("Resolver returned unexpected result", TestConstants.addressTwos, Iterables.get(results, 0)); verify(firstResolver).resolve("2.2.2.2"); verify(secondResolver).resolve("2.2.2.2"); }