Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.it.main;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

import java.net.URL;

import org.junit.jupiter.api.Test;

import io.quarkus.it.rest.GreetingEndpoint;
import io.quarkus.it.rest.TestResource;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@TestHTTPEndpoint(TestResource.class)
public class TestHTTPResourceClassTestCase {

URL testRootEndpoint;

@TestHTTPResource("int/10")
URL testPathEndpoint;

@TestHTTPEndpoint(GreetingEndpoint.class)
URL externalGreetingEndpoint;

@TestHTTPEndpoint(GreetingEndpoint.class)
@TestHTTPResource("name")
URL externalGreetingNameEndpoint;

@Test
public void shouldConfigURLWithTestHTTPEndpointOnClass() {
given()
.when().get(testRootEndpoint).then()
.body(is("TEST"));
}

@Test
public void shouldConfigURLWithTestHTTPEndpointOnClassAndTestHTTPResourceOnField() {
given()
.when().get(testPathEndpoint).then()
.body(is("11"));
}

@Test
public void shouldConfigURLAndOverrideTestHTTPEndpointOnClass() {
given()
.when().get(externalGreetingEndpoint.toString() + "/anotherName").then()
.body(is("Hello anotherName"));

given()
.when().get(externalGreetingNameEndpoint).then()
.body(is("Hello name"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkus.it.main;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

import java.net.URL;

import org.junit.jupiter.api.Test;

import io.quarkus.it.rest.TestResource;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class TestHTTPResourceFieldTestCase {

@TestHTTPEndpoint(TestResource.class)
URL testRootEndpoint;

@TestHTTPEndpoint(TestResource.class)
@TestHTTPResource("int/10")
URL testPathEndpoint;

@Test
public void shouldConfigURLWithTestHTTPEndpointOnField() {
given()
.when().get(testRootEndpoint).then()
.body(is("TEST"));
}

@Test
public void shouldConfigURLWithTestHTTPEndpointAndTestHTTPResourceOnField() {
given()
.when().get(testPathEndpoint).then()
.body(is("11"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static String getUri() {
try {
return ConfigProvider.getConfig().getValue("test.url", String.class);
} catch (IllegalStateException e) {
//massive hack for dev mode tests, dev mode has not started yet
//so we don't have any way to load this correctly from config
// massive hack for dev mode tests, dev mode has not started yet
// so we don't have any way to load this correctly from config
return "http://localhost:8080";
}
}
Expand All @@ -29,8 +29,8 @@ public static String getManagementUri() {
try {
return ConfigProvider.getConfig().getValue("test.management.url", String.class);
} catch (IllegalStateException e) {
//massive hack for dev mode tests, dev mode has not started yet
//so we don't have any way to load this correctly from config
// massive hack for dev mode tests, dev mode has not started yet
// so we don't have any way to load this correctly from config
return "http://localhost:9000";
}
}
Expand Down Expand Up @@ -59,30 +59,23 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
Map<Class<?>, TestHTTPResourceProvider<?>> providers = getProviders();
Class<?> c = testCase.getClass();
while (c != Object.class) {
TestHTTPEndpoint classEndpointAnnotation = c.getAnnotation(TestHTTPEndpoint.class);
for (Field f : c.getDeclaredFields()) {
TestHTTPResource resource = f.getAnnotation(TestHTTPResource.class);
if (resource != null) {
TestHTTPEndpoint fieldEndpointAnnotation = f.getAnnotation(TestHTTPEndpoint.class);
if (resource != null || classEndpointAnnotation != null || fieldEndpointAnnotation != null) {
TestHTTPResourceProvider<?> provider = providers.get(f.getType());
if (provider == null) {
throw new RuntimeException(
"Unable to inject TestHTTPResource field " + f + " as no provider exists for the type");
}
String path = resource.value();
String path = resource != null ? resource.value() : "";
String endpointPath = null;
boolean management = resource.management();
TestHTTPEndpoint endpointAnnotation = f.getAnnotation(TestHTTPEndpoint.class);
if (endpointAnnotation != null) {
for (Function<Class<?>, String> func : endpointProviders) {
endpointPath = func.apply(endpointAnnotation.value());
if (endpointPath != null) {
break;
}
}
if (endpointPath == null) {
throw new RuntimeException(
"Could not determine the endpoint path for " + endpointAnnotation.value() + " to inject "
+ f);
}
boolean management = resource != null && resource.management();
if (fieldEndpointAnnotation != null) {
endpointPath = getEndpointPath(endpointProviders, f, fieldEndpointAnnotation);
} else if (classEndpointAnnotation != null) {
endpointPath = getEndpointPath(endpointProviders, f, classEndpointAnnotation);
}
if (!path.isEmpty() && endpointPath != null) {
if (!endpointPath.endsWith("/")) {
Expand All @@ -94,7 +87,7 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
path = endpointPath;
}
String val;
if (resource.ssl() || resource.tls()) {
if (resource != null && (resource.ssl() || resource.tls())) {
if (management) {
if (path.startsWith("/")) {
val = getManagementSslUri() + path;
Expand Down Expand Up @@ -143,4 +136,18 @@ private static Map<Class<?>, TestHTTPResourceProvider<?>> getProviders() {
}
return Collections.unmodifiableMap(map);
}

private static String getEndpointPath(List<Function<Class<?>, String>> endpointProviders, Field field,
TestHTTPEndpoint endpointAnnotation) {
for (Function<Class<?>, String> func : endpointProviders) {
String endpointPath = func.apply(endpointAnnotation.value());
if (endpointPath != null) {
return endpointPath;
}
}
throw new RuntimeException(
"Could not determine the endpoint path for " + endpointAnnotation.value()
+ " to inject " + field);
}

}
Loading