Skip to content

Commit e634fe8

Browse files
committed
[engine] Fix problem with follow parameter when api prefix with version removed wrongly
Signed-off-by: Stanislav Melnichuk <[email protected]>
1 parent 58c74a4 commit e634fe8

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

Diff for: backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/ResourceLocator.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.lang.reflect.Method;
44
import java.util.Arrays;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
57

68
import org.ovirt.engine.api.rsdl.ServiceTreeCrawler;
79
import org.ovirt.engine.api.rsdl.ServiceTreeNode;
@@ -13,6 +15,8 @@
1315
*/
1416
public class ResourceLocator {
1517

18+
private static final Pattern ULR_VERSION_PART_PATTERN = Pattern.compile("/api/(v\\d+/)?");
19+
1620
private static ResourceLocator instance;
1721

1822
private ResourceLocator() {
@@ -58,12 +62,15 @@ public BaseBackendResource locateResource(String href) throws Exception {
5862
* http://localhost:8080/ovirt-engine/api/
5963
* Remain with:
6064
* datacenters/1034e9ba-c1a4-442c-8bc9-f7c1c997652b
65+
*
66+
* Api definition with version also can be truncated (e.g. /api/v3/ or /api/v4/)
6167
*/
62-
private String removePrefix(String href) {
63-
int index = href.indexOf("/api/");
64-
if (index>0) {
65-
href = href.substring(index+5);
68+
static String removePrefix(String href) {
69+
Matcher matcher = ULR_VERSION_PART_PATTERN.matcher(href);
70+
if (matcher.find()) {
71+
href = href.substring(matcher.end());
6672
}
73+
6774
return href;
6875
}
6976
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.ovirt.engine.api.restapi.resource;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
class ResourceLocatorTest {
12+
13+
@ParameterizedTest
14+
@MethodSource("providerUrlsToCheckSuffixExtraction")
15+
void testResourceLocatorGetPrefix(String source, String expectedResult) {
16+
assertEquals(expectedResult, ResourceLocator.removePrefix(source));
17+
}
18+
19+
private static Stream<Arguments> providerUrlsToCheckSuffixExtraction() {
20+
var basePart = "http://localhost:8080/ovirt-engine";
21+
var suffix = "datacenters/1034e9ba-c1a4-442c-8bc9-f7c1c997652b";
22+
23+
return Stream.of(
24+
Arguments.of(
25+
basePart + "/api/v12/" + suffix,
26+
suffix
27+
),
28+
Arguments.of(
29+
basePart + "/api/" + suffix,
30+
suffix
31+
),
32+
Arguments.of(
33+
basePart + "/api/v4/" + suffix,
34+
suffix
35+
),
36+
Arguments.of( // Without pattern, method should return the same string.
37+
basePart + "/" + suffix,
38+
basePart + "/" + suffix
39+
)
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)