From 13eb7eebb482f876674073656b72095501ec8ec3 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Tue, 5 Jan 2021 19:22:59 +0100 Subject: [PATCH] RoundRobinRouterPlaner IT to Unit test --- .../keyserver/RoundRobinRouterPlaner.java | 6 ++- ...T.java => RoundRobinRouterPlanerTest.java} | 54 +++++++++++++------ 2 files changed, 41 insertions(+), 19 deletions(-) rename src/test/java/org/simplify4u/plugins/keyserver/{RoundRobinRouterPlanerIT.java => RoundRobinRouterPlanerTest.java} (54%) diff --git a/src/main/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlaner.java b/src/main/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlaner.java index e2a42d3b..aef7a5b5 100644 --- a/src/main/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlaner.java +++ b/src/main/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlaner.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Optional; +import io.vavr.CheckedFunction1; import io.vavr.control.Try; import org.apache.http.HttpException; import org.apache.http.HttpHost; @@ -33,6 +34,7 @@ class RoundRobinRouterPlaner implements HttpRoutePlanner { private HttpRoute lastRoute; private List errorAddresses = new ArrayList<>(); + private CheckedFunction1 resolver = InetAddress::getAllByName; public RoundRobinRouterPlaner() { // default constructor @@ -72,9 +74,9 @@ public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContex * * @return arrays of IP address */ - private static List resolve(String hostName) throws HttpException { + private List resolve(String hostName) throws HttpException { - return Try.of(() -> Arrays.asList(InetAddress.getAllByName(hostName))) + return Try.of(() -> Arrays.asList(resolver.apply(hostName))) .getOrElseThrow(e -> new HttpException("UnknownHostException: " + hostName, e)); } diff --git a/src/test/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlanerIT.java b/src/test/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlanerTest.java similarity index 54% rename from src/test/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlanerIT.java rename to src/test/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlanerTest.java index aba30b1c..24b15f5b 100644 --- a/src/test/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlanerIT.java +++ b/src/test/java/org/simplify4u/plugins/keyserver/RoundRobinRouterPlanerTest.java @@ -18,58 +18,78 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; +import io.vavr.CheckedFunction1; +import io.vavr.control.Try; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.conn.routing.HttpRoute; -import org.testng.Assert; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.testng.MockitoTestNGListener; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Listeners; import org.testng.annotations.Test; -public class RoundRobinRouterPlanerIT { +@Listeners(MockitoTestNGListener.class) +public class RoundRobinRouterPlanerTest { - public static final String TEST_HOST = "hkps.pool.sks-keyservers.net"; + private static final InetAddress[] EXPECTED_ADDRESSES = Try.of(() -> new InetAddress[]{ + InetAddress.getByName("127.0.0.1"), + InetAddress.getByName("127.0.0.2"), + InetAddress.getByName("127.0.0.3"), + }).get(); - @Test - public void shouldReturnTheSameAddressForSequentialCall() throws UnknownHostException, HttpException { + private static final String TEST_HOST = "test.host.example.com"; + + @Mock + private CheckedFunction1 resolver; + + @InjectMocks + private RoundRobinRouterPlaner routerPlaner; - InetAddress[] expected = InetAddress.getAllByName(TEST_HOST); + @BeforeMethod + void setup() throws Throwable { + when(resolver.apply(anyString())).thenReturn(EXPECTED_ADDRESSES); + } + + @Test + public void shouldReturnTheSameAddressForSequentialCall() throws HttpException { - RoundRobinRouterPlaner routerPlaner = new RoundRobinRouterPlaner(); HttpHost httpHost = new HttpHost(TEST_HOST); // first call HttpRoute firstRoute = routerPlaner.determineRoute(httpHost, null, null); - for (int i = 0; i < expected.length; i++) { + for (int i = 0; i < EXPECTED_ADDRESSES.length; i++) { HttpRoute httpRouteNext = routerPlaner.determineRoute(httpHost, null, null); - assertEquals(httpRouteNext.getTargetHost().getAddress(), firstRoute.getTargetHost().getAddress()); + assertThat(httpRouteNext.getTargetHost().getAddress()) + .isEqualTo(firstRoute.getTargetHost().getAddress()); } } @Test public void shouldReturnNextAddressAfterError() throws UnknownHostException, HttpException { - InetAddress[] expected = InetAddress.getAllByName(TEST_HOST); - RoundRobinRouterPlaner routerPlaner = new RoundRobinRouterPlaner(); HttpHost httpHost = new HttpHost(TEST_HOST); List actual = new ArrayList<>(); - for (int i = 0; i < expected.length; i++) { + for (int i = 0; i < EXPECTED_ADDRESSES.length; i++) { HttpRoute httpRoute = routerPlaner.determineRoute(httpHost, null, null); routerPlaner.lastRouteCauseError(); actual.add(httpRoute.getTargetHost().getAddress()); } - Assert.assertEqualsNoOrder(actual.toArray(), expected); + assertThat(actual.toArray()).containsExactlyInAnyOrder(EXPECTED_ADDRESSES); // after all failed next should be returned HttpRoute httpRoute = routerPlaner.determineRoute(httpHost, null, null); - assertTrue(Arrays.asList(expected).contains(httpRoute.getTargetHost().getAddress())); + assertThat(httpRoute.getTargetHost().getAddress()).isIn(EXPECTED_ADDRESSES); } }