ConfigServerApiImpl implements ConfigServerApi { @Override public <T> T get(String path, Class<T> wantedReturnType, Params paramsOrNull) { Optional<RequestConfig> requestConfig = getRequestConfigOverride(paramsOrNull); return tryAllConfigServers(configServer -> { HttpGet get = new HttpGet(configServer.resolve(path)); requestConfig.ifPresent(get::setConfig); return get; }, wantedReturnType); } private  ConfigServerApiImpl(Collection<URI> configServers,
                                HostnameVerifier verifier,
                                ServiceIdentityProvider identityProvider); private  ConfigServerApiImpl(Collection<URI> configServers, CloseableHttpClient client); static ConfigServerApiImpl create(ConfigServerInfo info,
                                             ServiceIdentityProvider provider,
                                             HostnameVerifier hostnameVerifier); static ConfigServerApiImpl createFor(ConfigServerInfo info,
                                                ServiceIdentityProvider provider,
                                                HostnameVerifier hostnameVerifier,
                                                HostName configServerHostname); static ConfigServerApiImpl createForTesting(List<URI> configServerHosts); @Override T put(String path, Optional<Object> bodyJsonPojo, Class<T> wantedReturnType, Params paramsOrNull); @Override T patch(String path, Object bodyJsonPojo, Class<T> wantedReturnType, Params paramsOrNull); @Override T delete(String path, Class<T> wantedReturnType, Params paramsOrNull); @Override T get(String path, Class<T> wantedReturnType, Params paramsOrNull); @Override T post(String path, Object bodyJsonPojo, Class<T> wantedReturnType, Params paramsOrNull); @Override void close();  }
@Test public void testBasicParsingSingleServer() { TestPojo answer = configServerApi.get("/path", TestPojo.class); assertThat(answer.foo, is("bar")); assertLogStringContainsGETForAHost(); }
@Test(expected = HttpException.class) public void testBasicFailure() { mockReturnCode = 400; TestPojo testPojo = configServerApi.get("/path", TestPojo.class); assertEquals(testPojo.errorCode.intValue(), mockReturnCode); assertLogStringContainsGETForAHost(); }
@Test public void testBasicSuccessWithNoRetries() { mockReturnCode = 201; TestPojo testPojo = configServerApi.get("/path", TestPojo.class); assertEquals(testPojo.errorCode.intValue(), mockReturnCode); assertLogStringContainsGETForAHost(); }
@Test public void testBasicSuccessWithCustomTimeouts() { mockReturnCode = TIMEOUT_RETURN_CODE; var params = new ConfigServerApi.Params(); params.setConnectionTimeout(Duration.ofSeconds(3)); try { configServerApi.get("/path", TestPojo.class, params); fail(); } catch (ConnectionException e) { assertNotNull(e.getCause()); assertEquals("read timed out", e.getCause().getMessage()); } }
@Test public void testRetries() { mockReturnCode = FAIL_RETURN_CODE; try { configServerApi.get("/path", TestPojo.class); fail("Expected failure"); } catch (Exception e) { } String[] log = mockLog.toString().split(" "); assertThat(log, arrayContainingInAnyOrder("GET http: }
@Test public void testNoRetriesOnBadHttpResponseCode() { mockReturnCode = 503; try { configServerApi.get("/path", TestPojo.class); fail("Expected failure"); } catch (Exception e) { } assertLogStringContainsGETForAHost(); }
@Test public void testForbidden() { mockReturnCode = 403; try { configServerApi.get("/path", TestPojo.class); fail("Expected exception"); } catch (HttpException.ForbiddenException e) { } assertLogStringContainsGETForAHost(); }
@Test public void testNotFound() { mockReturnCode = 404; try { configServerApi.get("/path", TestPojo.class); fail("Expected exception"); } catch (HttpException.NotFoundException e) { } assertLogStringContainsGETForAHost(); }
@Test public void testConflict() { mockReturnCode = 409; configServerApi.get("/path", TestPojo.class); assertLogStringContainsGETForAHost(); }