Skip to content

Commit ff06083

Browse files
committed
Make URL inherit TestHTTPEndpoint from class level
1 parent fe911f9 commit ff06083

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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("service")
23+
URL testServiceEndpoint;
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(testServiceEndpoint).then()
43+
.body(is("external"));
44+
}
45+
46+
@Test
47+
public void shouldConfigURLAndOverrideTestHTTPEndpointOnClass() {
48+
given()
49+
.pathParam("name", "some-random-name")
50+
.when().get(externalGreetingEndpoint).then()
51+
.body(is("some-random-name"));
52+
53+
given()
54+
.when().get(externalGreetingNameEndpoint).then()
55+
.body(is("name"));
56+
}
57+
58+
}
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("service")
23+
URL testServiceEndpoint;
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(testServiceEndpoint).then()
36+
.body(is("external"));
37+
}
38+
39+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
5252
Map<Class<?>, TestHTTPResourceProvider<?>> providers = getProviders();
5353
Class<?> c = testCase.getClass();
5454
while (c != Object.class) {
55+
TestHTTPEndpoint classEndpointAnnotation = c.getAnnotation(TestHTTPEndpoint.class);
5556
for (Field f : c.getDeclaredFields()) {
5657
TestHTTPResource resource = f.getAnnotation(TestHTTPResource.class);
5758
if (resource != null) {
@@ -75,6 +76,18 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
7576
"Could not determine the endpoint path for " + endpointAnnotation.value() + " to inject "
7677
+ f);
7778
}
79+
} else if (classEndpointAnnotation != null) {
80+
for (Function<Class<?>, String> func : endpointProviders) {
81+
endpointPath = func.apply(classEndpointAnnotation.value());
82+
if (endpointPath != null) {
83+
break;
84+
}
85+
}
86+
if (endpointPath == null) {
87+
throw new RuntimeException(
88+
"Could not determine the endpoint path for " + classEndpointAnnotation.value()
89+
+ " to inject " + f);
90+
}
7891
}
7992
if (!path.isEmpty() && endpointPath != null) {
8093
if (!endpointPath.endsWith("/")) {

0 commit comments

Comments
 (0)