Skip to content

Commit

Permalink
fix: Repair the extractSourceFileElements function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelsJP committed Oct 20, 2023
1 parent 894170b commit 9d859a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.heigit.ors.util.StringUtility;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import static org.heigit.ors.api.ORSEnvironmentPostProcessor.ORS_CONFIG_LOCATION_ENV;
Expand Down Expand Up @@ -102,22 +104,27 @@ record SourceFileElements(String repoBaseUrlString, String repoName, String loca
}

SourceFileElements extractSourceFileElements(String sourceFilePropertyValue) {
String repoBaseUrlString = null;
String repoName = null;
String localOsmFilePath = "";
String accessScheme = null, repoBaseUrlString = null, repoName = null;
sourceFilePropertyValue = sourceFilePropertyValue.trim();
try {
new URL(sourceFilePropertyValue);
LOGGER.debug("Configuration property 'source_file' contains a URL, using value as URL for a graphs repository");
sourceFilePropertyValue = sourceFilePropertyValue.trim().replaceAll("/$", "");
String[] urlElements = sourceFilePropertyValue.split("/");
URI uri = new URI(sourceFilePropertyValue);
accessScheme = uri.getScheme();
repoBaseUrlString = uri.getHost();
if (accessScheme == null || repoBaseUrlString == null) {
throw new URISyntaxException(sourceFilePropertyValue, "URI does not contain a valid schema or host");
}
repoBaseUrlString = accessScheme + "://" + repoBaseUrlString;

repoName = urlElements[urlElements.length - 1];
repoBaseUrlString = sourceFilePropertyValue.replaceAll("/%s$".formatted(repoName), "");
} catch (MalformedURLException e) {
String[] pathElements = uri.getPath().split("/");
if (pathElements.length == 0) {
throw new URISyntaxException(sourceFilePropertyValue, "URI does not contain a path");
}
repoName = pathElements[pathElements.length - 1];
} catch (URISyntaxException e) {
LOGGER.debug("Configuration property 'source_file' does not contain a URL, using value as local osm file path");
localOsmFilePath = sourceFilePropertyValue;
return new SourceFileElements(repoBaseUrlString, repoName, sourceFilePropertyValue);
}
return new SourceFileElements(repoBaseUrlString, repoName, localOsmFilePath);
return new SourceFileElements(repoBaseUrlString, repoName, "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ void setUp(){
}
@ParameterizedTest
@CsvSource(value = {
"https://example.com/test-repo/planet, https://example.com, test-repo, planet",
"https://example.com/test-repo/planet/, https://example.com, test-repo, planet",
"' https://example.com/test-repo/planet ', https://example.com, test-repo, planet",
"' https://example.com/test-repo/planet/ ', https://example.com, test-repo, planet",
"' http://example.com/test-repo/planet/ ', http://example.com, test-repo, planet"
"https://example.com/foo1/foo2/foo3/test-repo, https://example.com, test-repo",
"https://example.com/test-repo/, https://example.com, test-repo",
"https://example.com/test-repo, https://example.com, test-repo",
"' https://example.com/test-repo ', https://example.com, test-repo",
"https://example.com/test-repos/, https://example.com, test-repos",
"http://example.com/test-repo/, http://example.com, test-repo"
})
void extractSourceFileElements_withURL(String url, String expectedBaseUrl, String expectedRepoName, String expectedCoverage){
void extractSourceFileElements_withURL(String url, String expectedBaseUrl, String expectedRepoName){
ORSInitContextListener.SourceFileElements elements = orsInitContextListener.extractSourceFileElements(url);
assertEquals(expectedBaseUrl, elements.repoBaseUrlString());
assertEquals(expectedRepoName, elements.repoName());
// TODO fix this function
// assertEquals(expectedCoverage, elements.repoCoverage());
}
}

0 comments on commit 9d859a2

Please sign in to comment.