Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.ovirt.engine.api.rsdl.ServiceTreeCrawler;
import org.ovirt.engine.api.rsdl.ServiceTreeNode;
Expand All @@ -13,6 +15,8 @@
*/
public class ResourceLocator {

private static final Pattern ULR_VERSION_PART_PATTERN = Pattern.compile("/api/(v\\d+/)?");

private static ResourceLocator instance;

private ResourceLocator() {
Expand Down Expand Up @@ -58,12 +62,15 @@ public BaseBackendResource locateResource(String href) throws Exception {
* http://localhost:8080/ovirt-engine/api/
* Remain with:
* datacenters/1034e9ba-c1a4-442c-8bc9-f7c1c997652b
*
* Api definition with version also can be truncated (e.g. /api/v3/ or /api/v4/)
*/
private String removePrefix(String href) {
int index = href.indexOf("/api/");
if (index>0) {
href = href.substring(index+5);
static String removePrefix(String href) {
Matcher matcher = ULR_VERSION_PART_PATTERN.matcher(href);
if (matcher.find()) {
href = href.substring(matcher.end());
}

return href;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.ovirt.engine.api.restapi.resource;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class ResourceLocatorTest {

@ParameterizedTest
@MethodSource("providerUrlsToCheckSuffixExtraction")
void testResourceLocatorGetPrefix(String source, String expectedResult) {
assertEquals(expectedResult, ResourceLocator.removePrefix(source));
}

private static Stream<Arguments> providerUrlsToCheckSuffixExtraction() {
var basePart = "http://localhost:8080/ovirt-engine";
var suffix = "datacenters/1034e9ba-c1a4-442c-8bc9-f7c1c997652b";

return Stream.of(
Arguments.of(
basePart + "/api/v12/" + suffix,
suffix
),
Arguments.of(
basePart + "/api/" + suffix,
suffix
),
Arguments.of(
basePart + "/api/v4/" + suffix,
suffix
),
Arguments.of( // Without pattern, method should return the same string.
basePart + "/" + suffix,
basePart + "/" + suffix
)
);
}
}
Loading