ConsulRepository implements EntryPointRepository, PortRepository, HaproxyRepository { @Override public boolean isAutoreloaded(EntryPointKey entryPointKey) { boolean isAutoreloaded = false; HttpGet getEntryPointAutoreloadKey = new HttpGet("http: try { Optional<Boolean> autoreload = client.execute(getEntryPointAutoreloadKey, response -> consulReader.parseHttpResponseAccepting404(response, consulReader::parseBooleanFromHttpEntity)); if (autoreload.isPresent() && autoreload.get()) { LOGGER.debug("The entrypoint {} will be autoreloaded. Uri {} returns true content.", entryPointKey, getEntryPointAutoreloadKey.getRequestLine().getUri()); isAutoreloaded = true; } } catch (IOException e) { LOGGER.error("a problem occurs during call to consul. The entrypoint " + entryPointKey + " won't be autoreloaded. Http get on: http: } return isAutoreloaded; } ConsulRepository(String host, int port, int minGeneratedPort, int maxGeneratedPort);   ConsulRepository(String host, int port, int minGeneratedPort, int maxGeneratedPort, ObjectMapper mapper, ConsulReader consulReader, CloseableHttpClient client); @Override boolean lock(EntryPointKey entryPointKey); @Override boolean isAutoreloaded(EntryPointKey entryPointKey); @Override void release(EntryPointKey key); @Override Optional<EntryPoint> getCurrentConfiguration(EntryPointKey key); @Override Set<String> getEntryPointsId(); @Override Optional<EntryPoint> getPendingConfiguration(EntryPointKey key); @Override Optional<EntryPoint> getCommittingConfiguration(EntryPointKey key); @Override void setPendingConfiguration(EntryPointKey key, EntryPoint configuration); @Override void removePendingConfiguration(EntryPointKey key); @Override boolean setCommittingConfiguration(String correlationId, EntryPointKey entryPointKey, EntryPoint configuration, int ttl); @Override void removeCommittingConfiguration(EntryPointKey key); @Override Optional<Boolean> removeEntrypoint(EntryPointKey entryPointKey); @Override void setCurrentConfiguration(EntryPointKey key, EntryPoint configuration); @Override void init(); @Override Optional<Map<String, Integer>> getPorts(); @Override Optional<Integer> getPort(String key); @Override Integer newPort(String key); @Override Optional<Map<String, String>> getHaproxyProperties(String haproxyId); @Override Optional<Map<String, String>> getHaproxyPropertiesAndAccepting404(String haproxyId); @Override Set<String> getHaproxyVersions(); @Override void addVersion(String haproxyVersion); @Override List<Map<String, String>> getHaproxyProperties(); @Override boolean isAutoreload(String haproxyId); @Override Set<String> getHaproxyIds(); @Override void setHaproxyProperty(String haproxyId, String key, String value); @Override Optional<String> getHaproxyProperty(String haproxyId, String key); @Override Optional<String> getCommitCorrelationId(EntryPointKey key); @Override void setAutoreload(EntryPointKey entryPointKey, Boolean autoreload); void shutdown();  }
@Test public void should_return_autoreload_false_if_entrypoint_disabled_key_is_absent() throws IOException { ConsulReader consulReader = mock(ConsulReader.class); CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); ConsulRepository consulRepository = new ConsulRepository("localhost", 50080, 32_000, 64_000, null, consulReader, closeableHttpClient); when(closeableHttpClient.execute(any(HttpGet.class), any(ResponseHandler.class))).thenReturn(Optional.empty()); boolean result = consulRepository.isAutoreloaded(new EntryPointKeyDefaultImpl("test")); assertThat(result).isFalse(); }
@Test public void should_return_autoreload_false_if_entrypoint_disabled_key_valued_at_true() throws IOException { ConsulReader consulReader = mock(ConsulReader.class); CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); ConsulRepository consulRepository = new ConsulRepository("localhost", 50080, 32_000, 64_000, null, consulReader, closeableHttpClient); when(closeableHttpClient.execute(any(HttpGet.class), any(ResponseHandler.class))).thenReturn(Optional.of(Boolean.FALSE)); boolean result = consulRepository.isAutoreloaded(new EntryPointKeyDefaultImpl("test")); assertThat(result).isFalse(); }
@Test public void should_return_autoreload_false_if_consul_request_fails() throws IOException { ConsulReader consulReader = mock(ConsulReader.class); CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); ConsulRepository consulRepository = new ConsulRepository("localhost", 50080, 32_000, 64_000, null, consulReader, closeableHttpClient); when(closeableHttpClient.execute(any(HttpGet.class), any(ResponseHandler.class))).thenThrow(new IOException()); boolean result = consulRepository.isAutoreloaded(new EntryPointKeyDefaultImpl("test")); assertThat(result).isFalse(); }
@Test public void should_return_autoreload_true_if_autoreload_key_valued_at_true() throws IOException { ConsulReader consulReader = mock(ConsulReader.class); CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); ConsulRepository consulRepository = new ConsulRepository("localhost", 50080, 32_000, 64_000, null, consulReader, closeableHttpClient); when(closeableHttpClient.execute(any(HttpGet.class), any(ResponseHandler.class))).thenReturn(Optional.of(Boolean.TRUE)); boolean result = consulRepository.isAutoreloaded(new EntryPointKeyDefaultImpl("test")); assertThat(result).isTrue(); }