mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
#41 Testing processing errors while running schema or schematron validations
This commit is contained in:
parent
382e1a5a72
commit
0d7aeb5de7
5 changed files with 143 additions and 31 deletions
8
pom.xml
8
pom.xml
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!-- License below -->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<name>KoSIT XML Prüftool Implementierung</name>
|
||||
|
|
@ -117,6 +117,12 @@
|
|||
<version>3.3.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.kosit.validationtool</groupId>
|
||||
<artifactId>packaged-test-scenarios</artifactId>
|
||||
|
|
|
|||
|
|
@ -69,9 +69,15 @@ public abstract class BaseScenario {
|
|||
private XPathExecutable matchExecutable;
|
||||
|
||||
private XPathExecutable acceptExecutable;
|
||||
|
||||
@Setter
|
||||
private Schema schema;
|
||||
|
||||
@Setter
|
||||
private List<Transformation> schematronValidations;
|
||||
|
||||
private ContentRepository repository;
|
||||
|
||||
private Transformation reportTransformation;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -19,34 +19,36 @@
|
|||
|
||||
package de.kosit.validationtool.impl.tasks;
|
||||
|
||||
import static de.kosit.validationtool.impl.tasks.TestBagBuilder.createBag;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import javax.xml.validation.Schema;
|
||||
import javax.xml.validation.Validator;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import de.kosit.validationtool.api.Input;
|
||||
import de.kosit.validationtool.api.InputFactory;
|
||||
import de.kosit.validationtool.impl.ContentRepository;
|
||||
import de.kosit.validationtool.impl.Helper;
|
||||
import de.kosit.validationtool.impl.Helper.Simple;
|
||||
import de.kosit.validationtool.impl.ObjectFactory;
|
||||
import de.kosit.validationtool.impl.input.SourceInput;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction.Bag;
|
||||
import de.kosit.validationtool.model.reportInput.CreateReportInput;
|
||||
import de.kosit.validationtool.model.scenarios.ResourceType;
|
||||
import de.kosit.validationtool.model.scenarios.ScenarioType;
|
||||
import de.kosit.validationtool.model.scenarios.ValidateWithXmlSchema;
|
||||
|
||||
/**
|
||||
* Tests die {@link SchemaValidationAction}.
|
||||
|
|
@ -59,15 +61,11 @@ public class SchemaValidatorActionTest {
|
|||
|
||||
private SchemaValidationAction service;
|
||||
|
||||
private ContentRepository repository;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.service = new SchemaValidationAction();
|
||||
this.repository = new ContentRepository(ObjectFactory.createProcessor(), Simple.REPOSITORY);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSimple() throws MalformedURLException {
|
||||
final CheckAction.Bag bag = createBag(InputFactory.read(Simple.SIMPLE_VALID.toURL()));
|
||||
|
|
@ -83,33 +81,15 @@ public class SchemaValidatorActionTest {
|
|||
final CheckAction.Bag bag = createBag(input);
|
||||
this.service.check(bag);
|
||||
assertThat(bag.getSchemaValidationResult().isValid()).isFalse();
|
||||
bag.getSchemaValidationResult().getErrors().forEach(e->{
|
||||
bag.getSchemaValidationResult().getErrors().forEach(e -> {
|
||||
assertThat(e.getRowNumber()).isGreaterThan(0);
|
||||
assertThat(e.getColumnNumber()).isGreaterThan(0);
|
||||
});
|
||||
}
|
||||
|
||||
private Bag createBag(final Input input) {
|
||||
final Bag bag = new Bag(input, new CreateReportInput());
|
||||
bag.setScenarioSelectionResult(new Result<>(createScenario(Helper.Simple.getSchemaLocation())));
|
||||
return bag;
|
||||
}
|
||||
|
||||
private ScenarioType createScenario(final URI schemafile) {
|
||||
final ScenarioType t = new ScenarioType();
|
||||
final ValidateWithXmlSchema v = new ValidateWithXmlSchema();
|
||||
final ResourceType r = new ResourceType();
|
||||
r.setLocation(schemafile.getRawPath());
|
||||
r.setName("invoice");
|
||||
v.getResource().add(r);
|
||||
t.setValidateWithXmlSchema(v);
|
||||
t.initialize(this.repository, true);
|
||||
return t;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchemaReferences() {
|
||||
final Schema reportInputSchema = this.repository.getReportInputSchema();
|
||||
final Schema reportInputSchema = new ContentRepository(ObjectFactory.createProcessor(), Simple.REPOSITORY).getReportInputSchema();
|
||||
assertThat(reportInputSchema).isNotNull();
|
||||
}
|
||||
|
||||
|
|
@ -171,4 +151,17 @@ public class SchemaValidatorActionTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessingError() throws IOException, SAXException {
|
||||
final CheckAction.Bag bag = createBag(InputFactory.read(Simple.SIMPLE_VALID.toURL()));
|
||||
final ScenarioType scenario = bag.getScenarioSelectionResult().getObject();
|
||||
final Schema schema = mock(Schema.class);
|
||||
final Validator validator = mock(Validator.class);
|
||||
when(schema.newValidator()).thenReturn(validator);
|
||||
doThrow(SAXException.class).when(validator).validate(any());
|
||||
scenario.setSchema(schema);
|
||||
this.service.check(bag);
|
||||
assertThat(bag.getReportInput().getProcessingError().getError()).isNotEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
package de.kosit.validationtool.impl.tasks;
|
||||
|
||||
import static de.kosit.validationtool.impl.tasks.TestBagBuilder.createBag;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import de.kosit.validationtool.api.InputFactory;
|
||||
import de.kosit.validationtool.impl.ContentRepository;
|
||||
import de.kosit.validationtool.impl.ConversionService;
|
||||
import de.kosit.validationtool.impl.Helper.Simple;
|
||||
import de.kosit.validationtool.impl.ObjectFactory;
|
||||
import de.kosit.validationtool.impl.model.BaseScenario.Transformation;
|
||||
import de.kosit.validationtool.model.scenarios.ResourceType;
|
||||
import de.kosit.validationtool.model.scenarios.ScenarioType;
|
||||
|
||||
import net.sf.saxon.s9api.SaxonApiException;
|
||||
import net.sf.saxon.s9api.XsltExecutable;
|
||||
import net.sf.saxon.s9api.XsltTransformer;
|
||||
|
||||
/**
|
||||
* Tests {@link SchematronValidationAction}.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
public class SchematronValidationActionTest {
|
||||
|
||||
private SchematronValidationAction action;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final ContentRepository repository = new ContentRepository(ObjectFactory.createProcessor(), Simple.REPOSITORY);
|
||||
this.action = new SchematronValidationAction(repository, new ConversionService());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessingError() throws IOException, SaxonApiException {
|
||||
final CheckAction.Bag bag = createBag(InputFactory.read(Simple.SIMPLE_VALID.toURL()), true);
|
||||
|
||||
final ScenarioType scenario = bag.getScenarioSelectionResult().getObject();
|
||||
final XsltExecutable exec = mock(XsltExecutable.class);
|
||||
final XsltTransformer transformer = mock(XsltTransformer.class);
|
||||
doThrow(new SaxonApiException("invalid")).when(transformer).transform();
|
||||
when(exec.load()).thenReturn(transformer);
|
||||
final ResourceType resourceType = new ResourceType();
|
||||
resourceType.setName("invalid internal");
|
||||
scenario.setSchematronValidations(Collections.singletonList(new Transformation(exec, resourceType)));
|
||||
this.action.check(bag);
|
||||
assertThat(bag.getReportInput().getProcessingError().getError()).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package de.kosit.validationtool.impl.tasks;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import de.kosit.validationtool.api.Input;
|
||||
import de.kosit.validationtool.impl.ContentRepository;
|
||||
import de.kosit.validationtool.impl.Helper;
|
||||
import de.kosit.validationtool.impl.Helper.Simple;
|
||||
import de.kosit.validationtool.impl.ObjectFactory;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction.Bag;
|
||||
import de.kosit.validationtool.model.reportInput.CreateReportInput;
|
||||
import de.kosit.validationtool.model.scenarios.ResourceType;
|
||||
import de.kosit.validationtool.model.scenarios.ScenarioType;
|
||||
import de.kosit.validationtool.model.scenarios.ValidateWithXmlSchema;
|
||||
|
||||
/**
|
||||
* Utilities for creating test objects.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
public class TestBagBuilder {
|
||||
|
||||
public static Bag createBag(final Input input) {
|
||||
return createBag(input, false);
|
||||
}
|
||||
|
||||
public static Bag createBag(final Input input, final boolean parse) {
|
||||
final Bag bag = new Bag(input, new CreateReportInput());
|
||||
if (parse) {
|
||||
bag.setParserResult(DocumentParseAction.parseDocument(bag.getInput()));
|
||||
}
|
||||
bag.setScenarioSelectionResult(new Result<>(createScenario(Helper.Simple.getSchemaLocation())));
|
||||
return bag;
|
||||
}
|
||||
|
||||
private static ScenarioType createScenario(final URI schemafile) {
|
||||
final ContentRepository repository = new ContentRepository(ObjectFactory.createProcessor(), Simple.REPOSITORY);
|
||||
final ScenarioType t = new ScenarioType();
|
||||
final ValidateWithXmlSchema v = new ValidateWithXmlSchema();
|
||||
final ResourceType r = new ResourceType();
|
||||
r.setLocation(schemafile.getRawPath());
|
||||
r.setName("invoice");
|
||||
v.getResource().add(r);
|
||||
t.setValidateWithXmlSchema(v);
|
||||
t.initialize(repository, true);
|
||||
return t;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue