Skip to content

Commit b61cee6

Browse files
committed
Make URL inherit TestHTTPEndpoint from class level
1 parent 591eed0 commit b61cee6

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
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 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("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 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("11"));
37+
}
38+
39+
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ 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);
57-
if (resource != null) {
58+
TestHTTPEndpoint endpointAnnotation = f.getAnnotation(TestHTTPEndpoint.class);
59+
if (resource != null || classEndpointAnnotation != null || endpointAnnotation != null) {
5860
TestHTTPResourceProvider<?> provider = providers.get(f.getType());
5961
if (provider == null) {
6062
throw new RuntimeException(
6163
"Unable to inject TestHTTPResource field " + f + " as no provider exists for the type");
6264
}
63-
String path = resource.value();
65+
String path = resource != null ? resource.value() : "";
6466
String endpointPath = null;
65-
TestHTTPEndpoint endpointAnnotation = f.getAnnotation(TestHTTPEndpoint.class);
6667
if (endpointAnnotation != null) {
6768
for (Function<Class<?>, String> func : endpointProviders) {
6869
endpointPath = func.apply(endpointAnnotation.value());
@@ -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("/")) {
@@ -86,7 +99,7 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
8699
path = endpointPath;
87100
}
88101
String val;
89-
if (resource.ssl()) {
102+
if (resource != null && resource.ssl()) {
90103
if (path.startsWith("/")) {
91104
val = getSslUri() + path;
92105
} else {

0 commit comments

Comments
 (0)