EndpointHealthcheckResolver { public List<EndpointRule> resolve(Api api) { HealthCheckService rootHealthCheck = api.getServices().get(HealthCheckService.class); boolean hcEnabled = (rootHealthCheck != null && rootHealthCheck.isEnabled()); Stream<HttpEndpoint> httpEndpoints = api.getProxy().getGroups() .stream() .filter(group -> group.getEndpoints() != null) .peek(group -> group.getEndpoints().forEach(endpoint -> { if (HttpEndpoint.class.isAssignableFrom(endpoint.getClass())) { final HttpEndpoint httpEndpoint = ((HttpEndpoint) endpoint); final boolean inherit = endpoint.getInherit() != null && endpoint.getInherit(); if (inherit || httpEndpoint.getHttpClientOptions() == null) { httpEndpoint.setHttpClientOptions(group.getHttpClientOptions()); httpEndpoint.setHttpClientSslOptions(group.getHttpClientSslOptions()); httpEndpoint.setHttpProxy(group.getHttpProxy()); httpEndpoint.setHeaders(group.getHeaders()); } } })) .flatMap(group -> group.getEndpoints().stream()) .filter(endpoint -> HttpEndpoint.class.isAssignableFrom(endpoint.getClass())) .map(endpoint -> (HttpEndpoint) endpoint); if (gatewayConfiguration.tenant().isPresent()) { String tenant = gatewayConfiguration.tenant().get(); httpEndpoints = httpEndpoints .filter(endpoint -> endpoint.getTenants() == null || endpoint.getTenants().isEmpty() || endpoint.getTenants().contains(tenant)); } httpEndpoints = httpEndpoints.filter(endpoint -> !endpoint.isBackup()); httpEndpoints = httpEndpoints.filter(endpoint -> ( (endpoint.getHealthCheck() == null && hcEnabled) || (endpoint.getHealthCheck() != null && endpoint.getHealthCheck().isEnabled() && !endpoint.getHealthCheck().isInherit()) || (endpoint.getHealthCheck() != null && endpoint.getHealthCheck().isEnabled() && endpoint.getHealthCheck().isInherit() && hcEnabled))); return httpEndpoints.map((Function<HttpEndpoint, EndpointRule>) endpoint -> { HealthCheckService healthcheck = (endpoint.getHealthCheck() == null || endpoint.getHealthCheck().isInherit()) ? rootHealthCheck : endpoint.getHealthCheck(); if (endpoint.getType() == EndpointType.GRPC) { return new GrpcEndpointRule(api.getId(), (GrpcEndpoint) endpoint, healthcheck); } else { return new HttpEndpointRule(api.getId(), endpoint, healthcheck); } }).collect(Collectors.toList()); }  List<EndpointRule> resolve(Api api); EndpointRule resolve(Api api, T endpoint);  }
@Test public void shouldNotResolveEndpointWithoutGlobalNorLocal() { Services services = new Services(); services.set(Collections.emptyList()); when(mockApi.getServices()).thenReturn(services); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertTrue(resolve.isEmpty()); }
@Test public void shouldNotResolveEndpointWithGlobalDisabledAndNoLocal() { Services services = new Services(); HealthCheckService healthCheckService = new HealthCheckService(); healthCheckService.setEnabled(false); services.set(Collections.singleton(healthCheckService)); when(mockApi.getServices()).thenReturn(services); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertTrue(resolve.isEmpty()); }
@Test public void shouldNotResolveEndpointWithGlobalDisabledAndLocalDisabled() { Services services = new Services(); HealthCheckService healthCheckService = new HealthCheckService(); healthCheckService.setEnabled(false); services.set(Collections.singleton(healthCheckService)); when(mockApi.getServices()).thenReturn(services); when(mockEndpointHealthcheckService.isEnabled()).thenReturn(false); when(mockEndpoint.getHealthCheck()).thenReturn(mockEndpointHealthcheckService); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertTrue(resolve.isEmpty()); }
@Test public void shouldNotResolveEndpointWithoutGlobalAndLocalDisabled() { Services services = new Services(); services.set(Collections.emptyList()); when(mockApi.getServices()).thenReturn(services); when(mockEndpointHealthcheckService.isEnabled()).thenReturn(false); when(mockEndpoint.getHealthCheck()).thenReturn(mockEndpointHealthcheckService); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertTrue(resolve.isEmpty()); }
@Test public void shouldNotResolveEndpointWithGlobalEnabledAndLocalDisabled() { Services services = new Services(); HealthCheckService healthCheckService = new HealthCheckService(); healthCheckService.setEnabled(true); services.set(Collections.singleton(healthCheckService)); when(mockApi.getServices()).thenReturn(services); when(mockEndpointHealthcheckService.isEnabled()).thenReturn(false); when(mockEndpoint.getHealthCheck()).thenReturn(mockEndpointHealthcheckService); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertTrue(resolve.isEmpty()); }
@Test public void shouldResolveEndpointWithGlobalEnabledAndNoLocal() { Services services = new Services(); HealthCheckService healthCheckService = new HealthCheckService(); healthCheckService.setEnabled(true); services.set(Collections.singleton(healthCheckService)); when(mockApi.getServices()).thenReturn(services); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertEquals(1, resolve.size()); assertEquals("api-id", resolve.get(0).api()); }
@Test public void shouldResolveEndpointWithGlobalEnabledAndLocalEnabled() { Services services = new Services(); HealthCheckService healthCheckService = new HealthCheckService(); healthCheckService.setEnabled(true); services.set(Collections.singleton(healthCheckService)); when(mockApi.getServices()).thenReturn(services); when(mockEndpointHealthcheckService.isEnabled()).thenReturn(true); when(mockEndpoint.getHealthCheck()).thenReturn(mockEndpointHealthcheckService); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertEquals(1, resolve.size()); assertEquals("api-id", resolve.get(0).api()); }
@Test public void shouldResolveEndpointWithGlobalDisabledAndLocalEnabled() { Services services = new Services(); HealthCheckService healthCheckService = new HealthCheckService(); healthCheckService.setEnabled(false); services.set(Collections.singleton(healthCheckService)); when(mockApi.getServices()).thenReturn(services); when(mockEndpointHealthcheckService.isEnabled()).thenReturn(true); when(mockEndpoint.getHealthCheck()).thenReturn(mockEndpointHealthcheckService); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertEquals(1, resolve.size()); assertEquals("api-id", resolve.get(0).api()); }
@Test public void shouldResolveEndpointWithoutGlobalAndLocalEnabled() { Services services = new Services(); services.set(Collections.emptyList()); when(mockApi.getServices()).thenReturn(services); when(mockEndpointHealthcheckService.isEnabled()).thenReturn(true); when(mockEndpoint.getHealthCheck()).thenReturn(mockEndpointHealthcheckService); List<EndpointRule> resolve = endpointHealthcheckResolver.resolve(mockApi); assertNotNull(resolve); assertEquals(1, resolve.size()); assertEquals("api-id", resolve.get(0).api()); }