Skip to content

Commit 1562be0

Browse files
authored
Merge pull request #46410 from gian1200/feature/testhttpendpoint-inheritance
Make URL inherit TestHTTPEndpoint from class level
2 parents 878285c + fc1ea68 commit 1562be0

File tree

3 files changed

+151
-20
lines changed

3 files changed

+151
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
@TestHTTPResource("")
21+
URL testRootEndpoint;
22+
23+
@TestHTTPResource("/")
24+
URL testRootEndpoint2;
25+
26+
String testRootExpectedResponse = "TEST";
27+
28+
@TestHTTPResource("int/10")
29+
URL testPathEndpoint;
30+
31+
int testPathExpectedResponse = 11;
32+
33+
@TestHTTPEndpoint(GreetingEndpoint.class)
34+
@TestHTTPResource("")
35+
URL overridenGreetingEndpoint;
36+
37+
@TestHTTPEndpoint(GreetingEndpoint.class)
38+
@TestHTTPResource("name")
39+
URL overridenGreetingNameEndpoint;
40+
41+
String overridenGreetingNameExpectedResponse = "Hello name";
42+
43+
@Test
44+
public void shouldConfigURLWithTestHTTPEndpointFromClass() {
45+
given()
46+
.when().get(testRootEndpoint).then()
47+
.body(is(testRootExpectedResponse));
48+
49+
given()
50+
.when().get(testRootEndpoint2).then()
51+
.body(is(testRootExpectedResponse));
52+
53+
given()
54+
.when().get(testPathEndpoint).then()
55+
.body(is(String.valueOf(testPathExpectedResponse)));
56+
}
57+
58+
@Test
59+
public void shouldConfigURLWithTestHTTPEndpointFromField() {
60+
given()
61+
.when().get(overridenGreetingEndpoint.toString() + "/anotherName").then()
62+
.body(is("Hello anotherName"));
63+
64+
given()
65+
.when().get(overridenGreetingNameEndpoint).then()
66+
.body(is(overridenGreetingNameExpectedResponse));
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
@TestHTTPResource("")
20+
URL testRootEndpoint;
21+
22+
@TestHTTPEndpoint(TestResource.class)
23+
@TestHTTPResource("/")
24+
URL testRootEndpoint2;
25+
26+
String testRootExpectedResponse = "TEST";
27+
28+
@TestHTTPEndpoint(TestResource.class)
29+
@TestHTTPResource("int/10")
30+
URL testPathEndpoint;
31+
32+
int testPathExpectedResponse = 11;
33+
34+
@Test
35+
public void shouldConfigURLWithTestHTTPEndpointFromField() {
36+
given()
37+
.when().get(testRootEndpoint).then()
38+
.body(is(testRootExpectedResponse));
39+
40+
given()
41+
.when().get(testRootEndpoint2).then()
42+
.body(is(testRootExpectedResponse));
43+
}
44+
45+
@Test
46+
public void shouldConfigURLWithTestHTTPEndpointAndTestHTTPResourceFromField() {
47+
given()
48+
.when().get(testPathEndpoint).then()
49+
.body(is(String.valueOf(testPathExpectedResponse)));
50+
}
51+
52+
}

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

+30-20
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,6 +59,7 @@ 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);
6465
if (resource != null) {
@@ -68,27 +69,22 @@ public static void inject(Object testCase, List<Function<Class<?>, String>> endp
6869
"Unable to inject TestHTTPResource field " + f + " as no provider exists for the type");
6970
}
7071
String path = resource.value();
72+
if (path.startsWith("/")) {
73+
path = path.substring(1);
74+
}
7175
String endpointPath = null;
7276
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-
}
77+
TestHTTPEndpoint fieldEndpointAnnotation = f.getAnnotation(TestHTTPEndpoint.class);
78+
if (fieldEndpointAnnotation != null) {
79+
endpointPath = getEndpointPath(endpointProviders, f, fieldEndpointAnnotation);
80+
} else if (classEndpointAnnotation != null) {
81+
endpointPath = getEndpointPath(endpointProviders, f, classEndpointAnnotation);
8682
}
8783
if (!path.isEmpty() && endpointPath != null) {
88-
if (!endpointPath.endsWith("/")) {
89-
path = endpointPath + "/" + path;
90-
} else {
84+
if (endpointPath.endsWith("/")) {
9185
path = endpointPath + path;
86+
} else {
87+
path = endpointPath + "/" + path;
9288
}
9389
} else if (endpointPath != null) {
9490
path = endpointPath;
@@ -143,4 +139,18 @@ private static Map<Class<?>, TestHTTPResourceProvider<?>> getProviders() {
143139
}
144140
return Collections.unmodifiableMap(map);
145141
}
142+
143+
private static String getEndpointPath(List<Function<Class<?>, String>> endpointProviders, Field field,
144+
TestHTTPEndpoint endpointAnnotation) {
145+
for (Function<Class<?>, String> func : endpointProviders) {
146+
String endpointPath = func.apply(endpointAnnotation.value());
147+
if (endpointPath != null) {
148+
return endpointPath;
149+
}
150+
}
151+
throw new RuntimeException(
152+
"Could not determine the endpoint path for " + endpointAnnotation.value()
153+
+ " to inject " + field);
154+
}
155+
146156
}

0 commit comments

Comments
 (0)