Skip to content

Commit

Permalink
add missing java files
Browse files Browse the repository at this point in the history
  • Loading branch information
asalan316 committed Jun 15, 2023
1 parent 3aeb2ae commit 5c84978
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.cloudfoundry.autoscaler.scheduler.util.health;

import java.util.Set;

public enum EndpointsEnum {
PROMETHEUS("/health/prometheus"),
LIVENESS("/health/liveness");

private final String url;

EndpointsEnum(String url) {
this.url = url;
}

public String getUrl() {
return url;
}

public static EndpointsEnum valueOfEndpoint(String url) {
for (EndpointsEnum endpoint : values()) {
if (endpoint.url.equals(url)) {
return endpoint;
}
}
throw new IllegalArgumentException("Enum for " + url);
}

public static boolean isValidEndpoint(String url) {
EndpointsEnum endpointsEnum = valueOfEndpoint(url);
if (endpointsEnum != null) {
return true;
}
return false;
}

public static boolean configuredEndpointsExists(Set<String> userDefindHealthEndpoints) {

for (String configuredEndpoint : userDefindHealthEndpoints) {
return isValidEndpoint(configuredEndpoint);
}
return false;
}

public static String displayAllEndpointValues() {
String endpointValues = EndpointsEnum.values()[0].getUrl();
for (int i = 1; i < EndpointsEnum.values().length; i++) {
endpointValues += "," + EndpointsEnum.values()[i].getUrl();
}
return endpointValues;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.cloudfoundry.autoscaler.scheduler.rest.healthControllerTest;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import org.cloudfoundry.autoscaler.scheduler.conf.HealthServerConfiguration;
import org.cloudfoundry.autoscaler.scheduler.util.HealthUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
@ActiveProfiles("HealthAuth")
@TestPropertySource(
properties = "scheduler.healthserver.unprotectedEndpoints=" + "/health/liveness")
public class HealthEndpointMixedAuthTest {
@Autowired private TestRestTemplate restTemplate;
@Autowired private HealthServerConfiguration healthServerConfig;

/*
// case 2.1 ; config ["/health/liveness"]
here is – by configuration – one protected endpoint "/health/prometheus" and one unprotected "/health/liveness".
The user is authenticated.
The user queries on "/health/prometheus".
Expected behaviour: The request will be handled successfully.
*/
@Test
public void givenLivenessUnprotectedAndUserIsAuthenticatedShouldReturnPrometheusWith200()
throws MalformedURLException, URISyntaxException {

ResponseEntity<String> prometheusResponse =
this.restTemplate
.withBasicAuth("prometheus", "someHash")
.getForEntity(HealthUtils.prometheusMetricsUrl().toURI(), String.class);

assertThat(prometheusResponse.getStatusCode().value())
.describedAs("Http status code should be OK")
.isEqualTo(200);
assertThat(prometheusResponse.getHeaders().getContentType())
.isEqualTo(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8));
assertThat(prometheusResponse.getBody())
.contains("autoscaler_scheduler_data_source_initial_size 0.0");
}
}

0 comments on commit 5c84978

Please sign in to comment.