Skip to content

Commit c75ead2

Browse files
committed
feat(TestHTTPEndpoint): make URL inherit TestHTTPEndpoint from class level
1 parent 5c1f904 commit c75ead2

File tree

3 files changed

+124
-21
lines changed

3 files changed

+124
-21
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.quarkus.it.main;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.Matchers.is;
5+
6+
import java.net.URL;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import io.quarkus.it.rest.GreetingEndpoint;
11+
import io.quarkus.it.rest.TestResource;
12+
import io.quarkus.test.common.http.TestHTTPEndpoint;
13+
import io.quarkus.test.common.http.TestHTTPResource;
14+
import io.quarkus.test.junit.QuarkusTest;
15+
16+
@QuarkusTest
17+
@TestHTTPEndpoint(TestResource.class)
18+
public class TestHTTPResourceClassTestCase {
19+
20+
URL testRootEndpoint;
21+
22+
@TestHTTPResource("int/10")
23+
URL testPathEndpoint;
24+
25+
@TestHTTPEndpoint(GreetingEndpoint.class)
26+
URL externalGreetingEndpoint;
27+
28+
@TestHTTPEndpoint(GreetingEndpoint.class)
29+
@TestHTTPResource("name")
30+
URL externalGreetingNameEndpoint;
31+
32+
@Test
33+
public void shouldConfigURLWithTestHTTPEndpointOnClass() {
34+
given()
35+
.when().get(testRootEndpoint).then()
36+
.body(is("TEST"));
37+
}
38+
39+
@Test
40+
public void shouldConfigURLWithTestHTTPEndpointOnClassAndTestHTTPResourceOnField() {
41+
given()
42+
.when().get(testPathEndpoint).then()
43+
.body(is("11"));
44+
}
45+
46+
@Test
47+
public void shouldConfigURLAndOverrideTestHTTPEndpointOnClass() {
48+
given()
49+
.when().get(externalGreetingEndpoint.toString() + "/anotherName").then()
50+
.body(is("Hello anotherName"));
51+
52+
given()
53+
.when().get(externalGreetingNameEndpoint).then()
54+
.body(is("Hello name"));
55+
}
56+
57+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.quarkus.it.main;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.Matchers.is;
5+
6+
import java.net.URL;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import io.quarkus.it.rest.TestResource;
11+
import io.quarkus.test.common.http.TestHTTPEndpoint;
12+
import io.quarkus.test.common.http.TestHTTPResource;
13+
import io.quarkus.test.junit.QuarkusTest;
14+
15+
@QuarkusTest
16+
public class TestHTTPResourceFieldTestCase {
17+
18+
@TestHTTPEndpoint(TestResource.class)
19+
URL testRootEndpoint;
20+
21+
@TestHTTPEndpoint(TestResource.class)
22+
@TestHTTPResource("int/10")
23+
URL testPathEndpoint;
24+
25+
@Test
26+
public void shouldConfigURLWithTestHTTPEndpointOnField() {
27+
given()
28+
.when().get(testRootEndpoint).then()
29+
.body(is("TEST"));
30+
}
31+
32+
@Test
33+
public void shouldConfigURLWithTestHTTPEndpointAndTestHTTPResourceOnField() {
34+
given()
35+
.when().get(testPathEndpoint).then()
36+
.body(is("11"));
37+
}
38+
39+
}

test-framework/common/src/main/java/io/quarkus/test/common/http/TestHTTPResourceManager.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static String getUri() {
1919
try {
2020
return ConfigProvider.getConfig().getValue("test.url", String.class);
2121
} catch (IllegalStateException e) {
22-
//massive hack for dev mode tests, dev mode has not started yet
23-
//so we don't have any way to load this correctly from config
22+
// massive hack for dev mode tests, dev mode has not started yet
23+
// so we don't have any way to load this correctly from config
2424
return "http://localhost:8080";
2525
}
2626
}
@@ -29,8 +29,8 @@ public static String getManagementUri() {
2929
try {
3030
return ConfigProvider.getConfig().getValue("test.management.url", String.class);
3131
} catch (IllegalStateException e) {
32-
//massive hack for dev mode tests, dev mode has not started yet
33-
//so we don't have any way to load this correctly from config
32+
// massive hack for dev mode tests, dev mode has not started yet
33+
// so we don't have any way to load this correctly from config
3434
return "http://localhost:9000";
3535
}
3636
}
@@ -59,30 +59,23 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
5959
Map<Class<?>, TestHTTPResourceProvider<?>> providers = getProviders();
6060
Class<?> c = testCase.getClass();
6161
while (c != Object.class) {
62+
TestHTTPEndpoint classEndpointAnnotation = c.getAnnotation(TestHTTPEndpoint.class);
6263
for (Field f : c.getDeclaredFields()) {
6364
TestHTTPResource resource = f.getAnnotation(TestHTTPResource.class);
64-
if (resource != null) {
65+
TestHTTPEndpoint fieldEndpointAnnotation = f.getAnnotation(TestHTTPEndpoint.class);
66+
if (resource != null || classEndpointAnnotation != null || fieldEndpointAnnotation != null) {
6567
TestHTTPResourceProvider<?> provider = providers.get(f.getType());
6668
if (provider == null) {
6769
throw new RuntimeException(
6870
"Unable to inject TestHTTPResource field " + f + " as no provider exists for the type");
6971
}
70-
String path = resource.value();
72+
String path = resource != null ? resource.value() : "";
7173
String endpointPath = null;
72-
boolean management = resource.management();
73-
TestHTTPEndpoint endpointAnnotation = f.getAnnotation(TestHTTPEndpoint.class);
74-
if (endpointAnnotation != null) {
75-
for (Function<Class<?>, String> func : endpointProviders) {
76-
endpointPath = func.apply(endpointAnnotation.value());
77-
if (endpointPath != null) {
78-
break;
79-
}
80-
}
81-
if (endpointPath == null) {
82-
throw new RuntimeException(
83-
"Could not determine the endpoint path for " + endpointAnnotation.value() + " to inject "
84-
+ f);
85-
}
74+
boolean management = resource != null && resource.management();
75+
if (fieldEndpointAnnotation != null) {
76+
endpointPath = getEndpointPath(endpointProviders, f, fieldEndpointAnnotation);
77+
} else if (classEndpointAnnotation != null) {
78+
endpointPath = getEndpointPath(endpointProviders, f, classEndpointAnnotation);
8679
}
8780
if (!path.isEmpty() && endpointPath != null) {
8881
if (!endpointPath.endsWith("/")) {
@@ -94,7 +87,7 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
9487
path = endpointPath;
9588
}
9689
String val;
97-
if (resource.ssl() || resource.tls()) {
90+
if (resource != null && (resource.ssl() || resource.tls())) {
9891
if (management) {
9992
if (path.startsWith("/")) {
10093
val = getManagementSslUri() + path;
@@ -143,4 +136,18 @@ private static Map<Class<?>, TestHTTPResourceProvider<?>> getProviders() {
143136
}
144137
return Collections.unmodifiableMap(map);
145138
}
139+
140+
private static String getEndpointPath(List<Function<Class<?>, String>> endpointProviders, Field field,
141+
TestHTTPEndpoint endpointAnnotation) {
142+
for (Function<Class<?>, String> func : endpointProviders) {
143+
String endpointPath = func.apply(endpointAnnotation.value());
144+
if (endpointPath != null) {
145+
return endpointPath;
146+
}
147+
}
148+
throw new RuntimeException(
149+
"Could not determine the endpoint path for " + endpointAnnotation.value()
150+
+ " to inject " + field);
151+
}
152+
146153
}

0 commit comments

Comments
 (0)