-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate base data file with ilivalidator (#1)
- Loading branch information
Showing
13 changed files
with
477 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/ch/geowerkstatt/interlis/testbed/runner/InterlisValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public final class InterlisValidator implements Validator { | ||
private static final Logger LOGGER = LogManager.getLogger(); | ||
|
||
private final TestOptions options; | ||
|
||
/** | ||
* Creates a new instance of the InterlisValidator class. | ||
* | ||
* @param options the test options. | ||
*/ | ||
public InterlisValidator(TestOptions options) { | ||
this.options = options; | ||
|
||
LOGGER.info("Using ilivalidator at " + options.ilivalidatorPath()); | ||
} | ||
|
||
@Override | ||
public boolean validate(Path filePath) throws ValidatorException { | ||
var relativePath = options.basePath().relativize(filePath.getParent()); | ||
var logDirectory = options.outputPath().resolve(relativePath); | ||
|
||
var filenameWithoutExtension = StringUtils.getFilenameWithoutExtension(filePath.getFileName().toString()); | ||
var logFile = logDirectory.resolve(filenameWithoutExtension + ".log"); | ||
|
||
try { | ||
Files.createDirectories(logDirectory); | ||
|
||
var processBuilder = new ProcessBuilder() | ||
.command( | ||
"java", "-jar", options.ilivalidatorPath().toString(), | ||
"--log", logFile.toString(), | ||
filePath.toString()) | ||
.directory(options.basePath().toFile()); | ||
|
||
var process = processBuilder.start(); | ||
var exitCode = process.waitFor(); | ||
|
||
if (exitCode == 0) { | ||
LOGGER.info("Validation of " + filePath + " completed successfully."); | ||
return true; | ||
} else { | ||
LOGGER.error("Validation of " + filePath + " failed with exit code " + exitCode + ". See " + logFile + " for details."); | ||
return false; | ||
} | ||
} catch (IOException | InterruptedException e) { | ||
throw new ValidatorException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
public final class Runner { | ||
private static final Logger LOGGER = LogManager.getLogger(); | ||
|
||
private final TestOptions options; | ||
private final Validator validator; | ||
|
||
/** | ||
* Creates a new instance of the Runner class. | ||
* | ||
* @param options the test options. | ||
* @param validator the validator to use. | ||
*/ | ||
public Runner(TestOptions options, Validator validator) { | ||
this.options = options; | ||
this.validator = validator; | ||
} | ||
|
||
/** | ||
* Runs the testbed validation. | ||
* @return {@code true} if the validation was successful, {@code false} otherwise. | ||
*/ | ||
public boolean run() { | ||
LOGGER.info("Starting validation of testbed at " + options.basePath()); | ||
|
||
try { | ||
if (!validateBaseData()) { | ||
LOGGER.error("Validation of base data failed."); | ||
return false; | ||
} | ||
} catch (ValidatorException e) { | ||
LOGGER.error("Validation could not run, check the configuration.", e); | ||
return false; | ||
} | ||
|
||
LOGGER.info("Validation of testbed completed."); | ||
return true; | ||
} | ||
|
||
private boolean validateBaseData() throws ValidatorException { | ||
Optional<Path> filePath; | ||
try { | ||
filePath = options.baseDataFilePath(); | ||
} catch (IOException e) { | ||
throw new ValidatorException(e); | ||
} | ||
|
||
if (filePath.isEmpty()) { | ||
LOGGER.error("No base data file found."); | ||
return false; | ||
} | ||
|
||
LOGGER.info("Validating base data file " + filePath.get()); | ||
return validator.validate(filePath.get()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/ch/geowerkstatt/interlis/testbed/runner/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
public final class StringUtils { | ||
private StringUtils() { | ||
} | ||
|
||
/** | ||
* Gets the filename without the extension. | ||
* | ||
* @param filename the filename to get the name from. | ||
* @return the filename without the extension. | ||
*/ | ||
public static String getFilenameWithoutExtension(String filename) { | ||
var lastDotIndex = filename.lastIndexOf('.'); | ||
if (lastDotIndex == -1) { | ||
return filename; | ||
} | ||
|
||
return filename.substring(0, lastDotIndex); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/ch/geowerkstatt/interlis/testbed/runner/TestOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
public record TestOptions(Path basePath, Path ilivalidatorPath) { | ||
private static final String DATA_FILE_EXTENSION = ".xtf"; | ||
private static final String OUTPUT_DIR_NAME = "output"; | ||
|
||
/** | ||
* Creates a new instance of the RunnerOptions class. | ||
* | ||
* @param basePath the base path of the testbed. | ||
*/ | ||
public TestOptions { | ||
basePath = basePath.toAbsolutePath().normalize(); | ||
ilivalidatorPath = ilivalidatorPath.toAbsolutePath().normalize(); | ||
} | ||
|
||
/** | ||
* Gets the path to the data file that is used as the base for all validations. | ||
* | ||
* @return the path to the base data file. | ||
*/ | ||
public Optional<Path> baseDataFilePath() throws IOException { | ||
try (var dataFiles = findDataFiles(basePath)) { | ||
return dataFiles.findFirst(); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the path to the output directory. | ||
* | ||
* @return the path to the output directory. | ||
*/ | ||
public Path outputPath() { | ||
return basePath.resolve(OUTPUT_DIR_NAME); | ||
} | ||
|
||
private static Stream<Path> findDataFiles(Path basePath) throws IOException { | ||
return Files.find(basePath, 1, (path, attributes) -> path.getFileName().toString().toLowerCase().endsWith(DATA_FILE_EXTENSION)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/ch/geowerkstatt/interlis/testbed/runner/Validator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
import java.nio.file.Path; | ||
|
||
public interface Validator { | ||
/** | ||
* Validates the given file. | ||
* | ||
* @param filePath the path to the file to validate. | ||
* @return true if the validation was successful, false otherwise. | ||
* @throws ValidatorException if the validation could not be performed. | ||
*/ | ||
boolean validate(Path filePath) throws ValidatorException; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
/** | ||
* Exception that is thrown when the validation failed to run. | ||
*/ | ||
public final class ValidatorException extends Exception { | ||
/** | ||
* Creates a new instance of the ValidatorException class. | ||
* | ||
* @param message the message of the exception. | ||
*/ | ||
public ValidatorException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Creates a new instance of the ValidatorException class. | ||
* | ||
* @param message the message of the exception. | ||
* @param cause the cause of the exception. | ||
*/ | ||
public ValidatorException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Creates a new instance of the ValidatorException class. | ||
* | ||
* @param cause the cause of the exception. | ||
*/ | ||
public ValidatorException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="WARN"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%-5level %logger{3} - %msg%n"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Logger name="ch.geowerkstatt.interlis.testbed.runner" level="trace" additivity="false"> | ||
<AppenderRef ref="Console"/> | ||
</Logger> | ||
<Root level="error"> | ||
<AppenderRef ref="Console"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ili:transfer xmlns:ili="http://www.interlis.ch/xtf/2.4/INTERLIS"> | ||
<ili:headersection> | ||
<ili:models> | ||
</ili:models> | ||
<ili:sender>interlis-testbed-runner</ili:sender> | ||
</ili:headersection> | ||
<ili:datasection> | ||
</ili:datasection> | ||
</ili:transfer> |
Oops, something went wrong.