File tree 2 files changed +53
-4
lines changed
backend/manager/modules/restapi/jaxrs/src
main/java/org/ovirt/engine/api/restapi/resource
test/java/org/ovirt/engine/api/restapi/resource
2 files changed +53
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import java .lang .reflect .Method ;
4
4
import java .util .Arrays ;
5
+ import java .util .regex .Matcher ;
6
+ import java .util .regex .Pattern ;
5
7
6
8
import org .ovirt .engine .api .rsdl .ServiceTreeCrawler ;
7
9
import org .ovirt .engine .api .rsdl .ServiceTreeNode ;
13
15
*/
14
16
public class ResourceLocator {
15
17
18
+ private static final Pattern ULR_VERSION_PART_PATTERN = Pattern .compile ("/api/(v\\ d+/)?" );
19
+
16
20
private static ResourceLocator instance ;
17
21
18
22
private ResourceLocator () {
@@ -58,12 +62,15 @@ public BaseBackendResource locateResource(String href) throws Exception {
58
62
* http://localhost:8080/ovirt-engine/api/
59
63
* Remain with:
60
64
* datacenters/1034e9ba-c1a4-442c-8bc9-f7c1c997652b
65
+ *
66
+ * Api definition with version also can be truncated (e.g. /api/v3/ or /api/v4/)
61
67
*/
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 () );
66
72
}
73
+
67
74
return href ;
68
75
}
69
76
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments