-
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.
Merge and validate XTF files to test constraints (#3)
- Loading branch information
Showing
12 changed files
with
346 additions
and
106 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
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
76 changes: 63 additions & 13 deletions
76
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 |
---|---|---|
@@ -1,64 +1,114 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
import ch.geowerkstatt.interlis.testbed.runner.xtf.XtfMerger; | ||
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; | ||
import java.util.List; | ||
|
||
public final class Runner { | ||
private static final Logger LOGGER = LogManager.getLogger(); | ||
|
||
private final TestOptions options; | ||
private final Validator validator; | ||
private final XtfMerger xtfMerger; | ||
private Path baseFilePath; | ||
|
||
/** | ||
* Creates a new instance of the Runner class. | ||
* | ||
* @param options the test options. | ||
* @param options the test options. | ||
* @param validator the validator to use. | ||
* @param xtfMerger the XTF merger to use. | ||
*/ | ||
public Runner(TestOptions options, Validator validator) { | ||
public Runner(TestOptions options, Validator validator, XtfMerger xtfMerger) { | ||
this.options = options; | ||
this.validator = validator; | ||
this.xtfMerger = xtfMerger; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
if (!mergeAndValidateTransferFiles()) { | ||
return false; | ||
} | ||
|
||
LOGGER.info("Validation of testbed completed."); | ||
return true; | ||
} 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(); | ||
var baseFilePath = options.baseDataFilePath(); | ||
if (baseFilePath.isEmpty()) { | ||
LOGGER.error("No base data file found."); | ||
return false; | ||
} | ||
this.baseFilePath = baseFilePath.get(); | ||
} catch (IOException e) { | ||
throw new ValidatorException(e); | ||
} | ||
|
||
LOGGER.info("Validating base data file " + baseFilePath); | ||
var filenameWithoutExtension = StringUtils.getFilenameWithoutExtension(baseFilePath.getFileName().toString()); | ||
var logFile = options.resolveOutputFilePath(baseFilePath, filenameWithoutExtension + ".log"); | ||
|
||
var valid = validator.validate(baseFilePath, logFile); | ||
if (valid) { | ||
LOGGER.info("Validation of " + baseFilePath + " completed successfully."); | ||
} else { | ||
LOGGER.error("Validation of " + baseFilePath + " failed. See " + logFile + " for details."); | ||
} | ||
return valid; | ||
} | ||
|
||
private boolean mergeAndValidateTransferFiles() throws ValidatorException { | ||
List<Path> patchFiles; | ||
try { | ||
patchFiles = options.patchDataFiles(); | ||
} catch (IOException e) { | ||
throw new ValidatorException(e); | ||
} | ||
|
||
if (filePath.isEmpty()) { | ||
LOGGER.error("No base data file found."); | ||
if (patchFiles.isEmpty()) { | ||
LOGGER.error("No patch files found."); | ||
return false; | ||
} | ||
|
||
LOGGER.info("Validating base data file " + filePath.get()); | ||
return validator.validate(filePath.get()); | ||
var valid = true; | ||
for (var patchFile : patchFiles) { | ||
var patchFileNameWithoutExtension = StringUtils.getFilenameWithoutExtension(patchFile.getFileName().toString()); | ||
var mergedFile = options.resolveOutputFilePath(patchFile, patchFileNameWithoutExtension + "_merged.xtf"); | ||
if (!xtfMerger.merge(baseFilePath, patchFile, mergedFile)) { | ||
valid = false; | ||
continue; | ||
} | ||
|
||
var logFile = mergedFile.getParent().resolve(patchFileNameWithoutExtension + ".log"); | ||
var mergedFileValid = validator.validate(mergedFile, logFile); | ||
if (mergedFileValid) { | ||
LOGGER.error("Validation of " + mergedFile + " was expected to fail but completed successfully."); | ||
valid = false; | ||
} | ||
} | ||
|
||
return valid; | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/test/data/testbed-with-patches/constraintA/testcase-1.xtf
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ili:transfer xmlns:ili="http://www.interlis.ch/xtf/2.4/INTERLIS"> | ||
<ili:datasection> | ||
</ili:datasection> | ||
</ili:transfer> |
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> |
24 changes: 24 additions & 0 deletions
24
src/test/java/ch/geowerkstatt/interlis/testbed/runner/MockitoTestBase.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,24 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoSession; | ||
import org.mockito.quality.Strictness; | ||
|
||
public abstract class MockitoTestBase { | ||
private MockitoSession mockito; | ||
|
||
@BeforeEach | ||
public final void startMockitoSession() { | ||
mockito = Mockito.mockitoSession() | ||
.initMocks(this) | ||
.strictness(Strictness.STRICT_STUBS) | ||
.startMocking(); | ||
} | ||
|
||
@AfterEach | ||
public final void finishMockitoSession() { | ||
mockito.finishMocking(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerBaseDataTest.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,86 @@ | ||
package ch.geowerkstatt.interlis.testbed.runner; | ||
|
||
import ch.geowerkstatt.interlis.testbed.runner.xtf.XtfMerger; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertIterableEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public final class RunnerBaseDataTest extends MockitoTestBase { | ||
private static final Path BASE_PATH = Path.of("src/test/data/testbed").toAbsolutePath().normalize(); | ||
private static final Path BASE_DATA_FILE = BASE_PATH.resolve("data.xtf"); | ||
|
||
private TestOptions options; | ||
private TestLogAppender appender; | ||
@Mock | ||
private Validator validatorMock; | ||
@Mock | ||
private XtfMerger mergerMock; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
options = new TestOptions(BASE_PATH, Path.of("ilivalidator.jar")); | ||
appender = TestLogAppender.registerAppender(Runner.class); | ||
} | ||
|
||
@AfterEach | ||
public void teardown() { | ||
appender.stop(); | ||
appender.unregister(); | ||
} | ||
|
||
@Test | ||
public void runValidatesBaseData() throws IOException, ValidatorException { | ||
when(validatorMock.validate(any(), any())).thenReturn(true); | ||
|
||
var runner = new Runner(options, validatorMock, mergerMock); | ||
|
||
var runResult = runner.run(); | ||
|
||
assertFalse(runResult, "Testbed run should have failed without patch files."); | ||
|
||
var errors = appender.getErrorMessages(); | ||
assertIterableEquals(List.of("No patch files found."), errors); | ||
|
||
var baseDataFile = options.baseDataFilePath(); | ||
assertFalse(baseDataFile.isEmpty(), "Base data file should have been found."); | ||
assertEquals(BASE_DATA_FILE, baseDataFile.get()); | ||
|
||
verify(validatorMock).validate(eq(BASE_DATA_FILE), any()); | ||
} | ||
|
||
@Test | ||
public void runLogsValidationError() throws ValidatorException { | ||
when(validatorMock.validate(any(), any())).thenReturn(false); | ||
|
||
var runner = new Runner(options, validatorMock, mergerMock); | ||
|
||
var runResult = runner.run(); | ||
|
||
assertFalse(runResult, "Testbed run should have failed."); | ||
|
||
var errors = appender.getErrorMessages(); | ||
assertEquals(1, errors.size(), "One error should have been logged."); | ||
var errorMessage = errors.getFirst(); | ||
assertTrue( | ||
Pattern.matches("Validation of .*? failed\\..*", errorMessage), | ||
"Error message should start with 'Validation of <base data file> failed.', actual value: '" + errorMessage + "'." | ||
); | ||
|
||
verify(validatorMock).validate(eq(BASE_DATA_FILE), any()); | ||
} | ||
} |
Oops, something went wrong.