Skip to content

Commit

Permalink
Allow lookup on URLs (#301)
Browse files Browse the repository at this point in the history
Complies with metafacture-core.

- increase max value of ClassDataAbstractionCoupling in checkstyle
- implement test
  • Loading branch information
dr0i committed Jun 23, 2023
1 parent 2294a74 commit 48ee14b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
4 changes: 3 additions & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
<module name="CatchParameterName">
<property name="format" value="^e$"/>
</module>
<module name="ClassDataAbstractionCoupling"/>
<module name="ClassDataAbstractionCoupling">
<property name="max" value="8"/>
</module>
<module name="ClassFanOutComplexity"/>
<module name="ClassTypeParameterName"/>
<module name="ConstantName"/>
Expand Down
41 changes: 30 additions & 11 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.io.Reader;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -148,26 +150,43 @@ public void literal(final String name, final String value) {
}

public String resolvePath(final String path) {
final Path basePath;
String resolvedPath = path;

if (path.startsWith(".")) {
if (fixFile != null) {
basePath = getPath(fixFile).getParent();
if (isValidUrl(path)) {
LOG.debug("Resolved path: url = '{}'", resolvedPath);
}
else {
final Path basePath;

if (path.startsWith(".")) {
if (fixFile != null) {
basePath = getPath(fixFile).getParent();
}
else {
throw new IllegalArgumentException("Cannot resolve relative path: " + path);
}
}
else {
throw new IllegalArgumentException("Cannot resolve relative path: " + path);
basePath = getPath("");
}
}
else {
basePath = getPath("");
}

final String resolvedPath = basePath.resolve(path).normalize().toString();
LOG.debug("Resolved path: base = '{}', path = '{}', result = '{}'", basePath, path, resolvedPath);
resolvedPath = basePath.resolve(path).normalize().toString();
LOG.debug("Resolved path: base = '{}', path = '{}', result = '{}'", basePath, path, resolvedPath);
}

return resolvedPath;
}

private boolean isValidUrl(final String url) {
try {
new URL(url);
return true;
}
catch (final MalformedURLException e) {
return false;
}
}

private Path getPath(final String path) {
return Paths.get(path).toAbsolutePath().normalize();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand All @@ -46,6 +47,8 @@
@ExtendWith(MockitoExtension.class)
public class MetafixLookupTest {
private static final String CSV_MAP = "src/test/resources/org/metafacture/metafix/maps/test.csv";
private static final String CSV_PATH = "/maps/test.csv";
private static final String CSV_URL = "%s" + CSV_PATH;
private static final String RDF_MAP = "src/test/resources/org/metafacture/metafix/maps/test.ttl";
private static final String HCRT_RDF_MAP = "src/test/resources/org/metafacture/metafix/maps/hcrt.ttl";
private static final String RDF_PATH = "/maps/rpb.ttl";
Expand All @@ -67,20 +70,27 @@ public MetafixLookupTest() {
private static void setStubForWireMock() {
WIRE_MOCK_SERVER.start();

final UrlPattern urlPattern = WireMock.urlPathEqualTo(RDF_PATH);
// stubs for RDF
UrlPattern urlPattern = WireMock.urlPathEqualTo(RDF_PATH);
final String redirectToUrl = "/redirect" + RDF_PATH;
final UrlPattern urlPatternRedirectToUrl = WireMock.urlPathEqualTo(redirectToUrl);

WIRE_MOCK_SERVER.stubFor(WireMock.get(urlPattern)
.willReturn(WireMock.temporaryRedirect(redirectToUrl)));

final String responseBody = new BufferedReader(new InputStreamReader(
MetafixLookupTest.class.getResourceAsStream("." + RDF_PATH))).lines().collect(Collectors.joining("\n"));

String responseBody = loadFile(RDF_PATH);
WIRE_MOCK_SERVER.stubFor(WireMock.get(urlPatternRedirectToUrl)
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "text/turtle")
.withBody(responseBody)));

//stub for CSV
urlPattern = WireMock.urlPathEqualTo(CSV_PATH);
responseBody = loadFile(CSV_PATH);
WIRE_MOCK_SERVER.stubFor(WireMock.get(urlPattern).willReturn(WireMock.aResponse().withBody(responseBody)));
}

private static String loadFile(final String path) {
return new BufferedReader(new InputStreamReader(
Objects.requireNonNull(MetafixLookupTest.class.getResourceAsStream("." + path)))).lines().collect(Collectors.joining("\n"));
}

@AfterAll
Expand Down Expand Up @@ -419,6 +429,15 @@ public void csv() {
);
}

@Test
public void csvViaUrl() {
final String baseUrl = WIRE_MOCK_SERVER.baseUrl();
final String mockedCsvUrl = String.format(CSV_URL, baseUrl);
assertMap(
LOOKUP + " '" + mockedCsvUrl + "')"
);
}

@Test
public void tsv() {
assertMap(
Expand Down

0 comments on commit 48ee14b

Please sign in to comment.