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 0a8b9db
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ all_modules:= $(go_modules) db scheduler
MVN_OPTS="-Dmaven.test.skip=true -Dmaven.plugin.validation=VERBOSE"
OS:=$(shell . /etc/lsb-release &>/dev/null && echo $${DISTRIB_ID} || uname )
db_type:=postgres
DB_HOST:=localhost
DBURL := $(shell case "${db_type}" in\
(postgres) printf "postgres://postgres:postgres@${DB_HOST}/autoscaler?sslmode=disable"; ;; \
(mysql) printf "root@tcp(${DB_HOST})/autoscaler?tls=false"; ;; esac)
Expand All @@ -25,6 +24,7 @@ DEST?=build
export BUILDIN_MODE?=false
export DEBUG?=false
export ACCEPTANCE_TESTS_FILE?=${DEST}/app-autoscaler-acceptance-tests-v${VERSION}.tgz
export DB_HOST:=localhost

$(shell mkdir -p target)
$(shell mkdir -p build)
Expand Down Expand Up @@ -131,7 +131,6 @@ test-autoscaler-suite: check-db_type init init-db test-certs autoscaler schedule
@make -C src/autoscaler testsuite TEST=${TEST} DBURL="${DBURL}" OPTS="${OPTS}"

test-scheduler: check-db_type init init-db test-certs
@export DB_HOST=${DB_HOST}; \
cd src && mvn test --no-transfer-progress -Dspring.profiles.include=${db_type} && cd ..
test-changelog: init
@make -C src/changelog test
Expand Down
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 0a8b9db

Please sign in to comment.