diff --git a/CHANGELOG.md b/CHANGELOG.md index 870c7409..dd6102da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,7 @@ * Rewrite of Declaration. * Rewrite of Expectation. -* no.difi.vefa.validator.api.Document.getDeclaration() returns a string, not an object. +* no.difi.vefa.validator.model.Document.getDeclaration() returns a string, not an object. * Fixing methods in ValidatorBuilder returning void. * Adding support for Piwik in sample application. Not turned on by default. * Refactoring of validator-build. diff --git a/src/main/java/no/difi/vefa/validator/CheckerCacheLoader.java b/src/main/java/no/difi/vefa/validator/CheckerCacheLoader.java index cc72cb4e..e360b2e7 100644 --- a/src/main/java/no/difi/vefa/validator/CheckerCacheLoader.java +++ b/src/main/java/no/difi/vefa/validator/CheckerCacheLoader.java @@ -5,12 +5,11 @@ import com.google.inject.Singleton; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.annotation.Type; import no.difi.vefa.validator.api.Checker; import no.difi.vefa.validator.api.CheckerFactory; import no.difi.vefa.validator.lang.ValidatorException; -import java.util.List; +import java.util.Map; /** * @author erlend @@ -19,26 +18,22 @@ @Singleton public class CheckerCacheLoader extends CacheLoader { - public static final int DEFAULT_SIZE = 250; - @Inject - private List factories; + private Map factories; @Inject private ValidatorEngine validatorEngine; @Override @NonNull - public Checker load(@NonNull String key) throws Exception { + public Checker load(@NonNull String key) throws ValidatorException { try { - for (CheckerFactory factory : factories) { - for (String extension : factory.getClass().getAnnotation(Type.class).value()) { - if (key.toLowerCase().endsWith(extension)) { - return factory.prepare(validatorEngine.getResource(key), key.split("#")[1]); - } + for (var entry : factories.entrySet()) { + if (key.toLowerCase().endsWith(entry.getKey())) { + return entry.getValue().prepare(validatorEngine.getResource(key), key.split("#")[1]); } } - } catch (Exception e) { + } catch (ValidatorException e) { throw new ValidatorException(String.format("Unable to load checker for '%s'.", key), e); } diff --git a/src/main/java/no/difi/vefa/validator/ValidationInstance.java b/src/main/java/no/difi/vefa/validator/ValidationInstance.java index 3c550f53..642e31d2 100644 --- a/src/main/java/no/difi/vefa/validator/ValidationInstance.java +++ b/src/main/java/no/difi/vefa/validator/ValidationInstance.java @@ -1,11 +1,16 @@ package no.difi.vefa.validator; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.*; +import no.difi.vefa.validator.api.Expectation; +import no.difi.vefa.validator.api.FlagFilterer; +import no.difi.vefa.validator.api.Section; +import no.difi.vefa.validator.api.Validation; import no.difi.vefa.validator.lang.UnknownDocumentTypeException; import no.difi.vefa.validator.lang.ValidatorException; -import no.difi.vefa.validator.properties.CombinedProperties; -import no.difi.vefa.validator.util.DeclarationIdentification; +import no.difi.vefa.validator.model.Detected; +import no.difi.vefa.validator.model.Document; +import no.difi.vefa.validator.model.Prop; +import no.difi.vefa.validator.model.Props; import no.difi.xsd.vefa.validator._1.AssertionType; import no.difi.xsd.vefa.validator._1.FileType; import no.difi.xsd.vefa.validator._1.FlagType; @@ -24,7 +29,7 @@ class ValidationInstance implements Validation { private final ValidatorInstance validatorInstance; - private final Properties properties; + private final Props props; private Configuration configuration; @@ -45,8 +50,8 @@ class ValidationInstance implements Validation { private List children; - public static ValidationInstance of(ValidatorInstance validatorInstance, Document document, Properties properties) { - return new ValidationInstance(validatorInstance, document, properties); + public static ValidationInstance of(ValidatorInstance validatorInstance, Document document, Prop... props) { + return new ValidationInstance(validatorInstance, document, props); } /** @@ -54,11 +59,11 @@ public static ValidationInstance of(ValidatorInstance validatorInstance, Documen * * @param validatorInstance Instance of validator. * @param document Source to validate. - * @param properties Properties for validation + * @param props Properties for validation */ - private ValidationInstance(ValidatorInstance validatorInstance, Document document, Properties properties) { + private ValidationInstance(ValidatorInstance validatorInstance, Document document, Prop... props) { this.validatorInstance = validatorInstance; - this.properties = new CombinedProperties(properties, validatorInstance.getProperties()); + this.props = validatorInstance.getProps().update(props); this.report = new Report(); this.report.setUuid(UUID.randomUUID().toString()); @@ -97,16 +102,16 @@ private ValidationInstance(ValidatorInstance validatorInstance, Document documen } } - private DeclarationIdentification loadDocument(Document document) throws ValidatorException, IOException { + private Detected loadDocument(Document document) throws ValidatorException, IOException { // Use declaration implementations to detect declaration to use. - DeclarationIdentification declarationIdentifier = validatorInstance.detect(document); + Detected declarationIdentifier = validatorInstance.detect(document); - if (declarationIdentifier.equals(DeclarationIdentification.UNKNOWN)) + if (declarationIdentifier.equals(Detected.UNKNOWN)) throw new UnknownDocumentTypeException("Unable to detect type of content."); // Detect expectation Expectation expectation = null; - if (properties.getBoolean("feature.expectation")) { + if (props.getBool("feature.expectation", false)) { expectation = declarationIdentifier.expectations(document); if (expectation != null) @@ -130,7 +135,7 @@ private void loadConfiguration() throws UnknownDocumentTypeException { // Get configuration using declaration this.configuration = validatorInstance.getConfiguration(document.getDeclarations()); - if (!properties.getBoolean("feature.suppress_notloaded")) + if (!props.getBool("feature.suppress_notloaded", false)) for (String notLoaded : configuration.getNotLoaded()) section.add("SYSTEM-007", String.format( "Validation artifact '%s' not loaded.", notLoaded), FlagType.WARNING); @@ -173,11 +178,11 @@ private void validate() { /** * Handling nested validation. */ - private void nestedValidation(DeclarationIdentification declarationIdentification) throws ValidatorException { + private void nestedValidation(Detected detected) throws ValidatorException { if (report.getFlag().compareTo(FlagType.FATAL) < 0) { - if (declarationIdentification.hasChildren() && properties.getBoolean("feature.nesting")) { - for (Document child : declarationIdentification.getChildren()) { - addChildValidation(ValidationInstance.of(validatorInstance, child, null)); + if (detected.hasChildren() && props.getBool("feature.nesting", false)) { + for (Document child : detected.getChildren()) { + addChildValidation(ValidationInstance.of(validatorInstance, child)); } } } diff --git a/src/main/java/no/difi/vefa/validator/Validator.java b/src/main/java/no/difi/vefa/validator/Validator.java index 41bc7838..51ecfa8d 100644 --- a/src/main/java/no/difi/vefa/validator/Validator.java +++ b/src/main/java/no/difi/vefa/validator/Validator.java @@ -3,16 +3,15 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.Document; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.Validation; +import no.difi.vefa.validator.model.Document; +import no.difi.vefa.validator.model.Prop; import no.difi.xsd.vefa.validator._1.PackageType; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; import java.nio.file.Path; import java.util.List; @@ -34,44 +33,45 @@ public class Validator implements Closeable { /** * Validate file. * - * @param file File to validate. + * @param file File to validate. + * @param props Optional properties. * @return Validation result. */ - public Validation validate(File file) throws IOException { - return validate(file.toPath()); + public Validation validate(File file, Prop... props) throws IOException { + return validate(Document.of(file), props); } /** * Validate file. * - * @param file File to validate. + * @param file File to validate. + * @param props Optional properties. * @return Validation result. */ - public Validation validate(Path file) throws IOException { - try (InputStream inputStream = Files.newInputStream(file)) { - return validate(inputStream); - } + public Validation validate(Path file, Prop... props) throws IOException { + return validate(Document.of(file), props); } /** * Validate content of stream. * * @param inputStream Stream containing content. + * @param props Optional properties. * @return Validation result. */ - public Validation validate(InputStream inputStream) throws IOException { - return validate(inputStream, null); + public Validation validate(InputStream inputStream, Prop... props) throws IOException { + return validate(Document.of(inputStream), props); } /** - * Validate content of stream. + * Validate document. * - * @param inputStream Stream containing content. - * @param properties Properties used for individual validation. + * @param document File to validate. + * @param props Optional properties. * @return Validation result. */ - public Validation validate(InputStream inputStream, Properties properties) throws IOException { - return ValidationInstance.of(this.validatorInstance, Document.of(inputStream), properties); + public Validation validate(Document document, Prop... props) { + return validatorInstance.validate(document, props); } /** @@ -80,7 +80,7 @@ public Validation validate(InputStream inputStream, Properties properties) throw * @return List of packages. */ public List getPackages() { - return this.validatorInstance.getPackages(); + return validatorInstance.getPackages(); } @Override diff --git a/src/main/java/no/difi/vefa/validator/ValidatorBuilder.java b/src/main/java/no/difi/vefa/validator/ValidatorBuilder.java index d65a0791..f3fe8971 100644 --- a/src/main/java/no/difi/vefa/validator/ValidatorBuilder.java +++ b/src/main/java/no/difi/vefa/validator/ValidatorBuilder.java @@ -3,8 +3,8 @@ import com.google.inject.Guice; import com.google.inject.Module; import com.google.inject.util.Modules; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.Source; +import no.difi.vefa.validator.model.Prop; import no.difi.vefa.validator.module.PropertiesModule; import no.difi.vefa.validator.module.SourceModule; import no.difi.vefa.validator.module.ValidatorModule; @@ -19,7 +19,7 @@ public class ValidatorBuilder { private Source source; - private Properties properties; + private Prop[] props; /** * Initiate creation of a new validator. Loads default plugins. @@ -40,11 +40,11 @@ private ValidatorBuilder() { /** * Defines configuration to use for validator. * - * @param properties Configuration + * @param props Configuration * @return Builder object */ - public ValidatorBuilder setProperties(Properties properties) { - this.properties = properties; + public ValidatorBuilder setProperties(Prop... props) { + this.props = props; return this; } @@ -66,7 +66,7 @@ public ValidatorBuilder setSource(Source source) { */ public Validator build() { List modules = new ArrayList<>(); - modules.add(new PropertiesModule(properties)); + modules.add(PropertiesModule.with(props)); modules.add(new SourceModule(source)); return Guice.createInjector(Modules.override(new ValidatorModule()).with(modules)).getInstance(Validator.class); diff --git a/src/main/java/no/difi/vefa/validator/ValidatorDefaults.java b/src/main/java/no/difi/vefa/validator/ValidatorDefaults.java deleted file mode 100644 index 9e140d6b..00000000 --- a/src/main/java/no/difi/vefa/validator/ValidatorDefaults.java +++ /dev/null @@ -1,31 +0,0 @@ -package no.difi.vefa.validator; - -import no.difi.vefa.validator.api.Properties; -import no.difi.vefa.validator.properties.SimpleProperties; - -import java.util.concurrent.TimeUnit; - -/** - * Class to hold defaults in validator. - */ -public class ValidatorDefaults { - - /** - * Default configuration. - */ - public static final Properties PROPERTIES = new SimpleProperties() - - // feature - .set("feature.expectation", false) - .set("feature.nesting", false) - .set("feature.suppress_notloaded", false) - .set("feature.infourl", false) - - // pools.checker - .set("pools.checker.size", CheckerCacheLoader.DEFAULT_SIZE) - .set("pools.checker.expire", TimeUnit.DAYS.toMinutes(1)) - - // finish - ; - -} diff --git a/src/main/java/no/difi/vefa/validator/ValidatorEngine.java b/src/main/java/no/difi/vefa/validator/ValidatorEngine.java index fa186ca4..d8d00b3f 100644 --- a/src/main/java/no/difi/vefa/validator/ValidatorEngine.java +++ b/src/main/java/no/difi/vefa/validator/ValidatorEngine.java @@ -3,19 +3,16 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import jakarta.xml.bind.JAXBContext; -import jakarta.xml.bind.JAXBException; -import jakarta.xml.bind.Unmarshaller; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.ArtifactHolder; import no.difi.vefa.validator.api.SourceInstance; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.ArtifactHolder; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.util.JaxbUtils; import no.difi.xsd.vefa.validator._1.*; -import javax.xml.transform.stream.StreamSource; import java.io.Closeable; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -55,8 +52,7 @@ class ValidatorEngine implements Closeable { * Loading a new validator engine loading configurations from current source. */ @Inject - public ValidatorEngine(SourceInstance sourceInstance, List configurations) - throws ValidatorException { + public ValidatorEngine(SourceInstance sourceInstance, List configurations) throws ValidatorException { // Load configurations from ValidatorBuilder. for (Configurations c : configurations) loadConfigurations("", c); @@ -65,9 +61,9 @@ public ValidatorEngine(SourceInstance sourceInstance, List confi for (Map.Entry entry : sourceInstance.getContent().entrySet()) { for (String filename : entry.getValue().getFilenames()) { if (filename.startsWith("config") && filename.endsWith(".xml")) { - try (InputStream inputStream = entry.getValue().getInputStream(filename)) { + try { content.put(entry.getKey(), entry.getValue()); - loadConfigurations(entry.getKey(), inputStream); + loadConfigurations(entry.getKey(), entry.getValue().getDocument(filename)); } catch (ValidatorException e) { throw new IOException(e.getMessage(), e); } @@ -87,16 +83,10 @@ public ValidatorEngine(SourceInstance sourceInstance, List confi * Load configuration from stream of config.xml. * * @param configurationSource Identifier for resource. - * @param inputStream Stream of config.xml. + * @param document Contents of config.xml. */ - private void loadConfigurations(String configurationSource, InputStream inputStream) throws ValidatorException { - try { - Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); - loadConfigurations(configurationSource, - unmarshaller.unmarshal(new StreamSource(inputStream), Configurations.class).getValue()); - } catch (JAXBException e) { - throw new ValidatorException("Unable to read configurations.", e); - } + private void loadConfigurations(String configurationSource, Document document) throws ValidatorException { + loadConfigurations(configurationSource, document.unmarshal(JAXB_CONTEXT, Configurations.class)); } /** diff --git a/src/main/java/no/difi/vefa/validator/ValidatorInstance.java b/src/main/java/no/difi/vefa/validator/ValidatorInstance.java index ce165d9a..d1ee3cab 100644 --- a/src/main/java/no/difi/vefa/validator/ValidatorInstance.java +++ b/src/main/java/no/difi/vefa/validator/ValidatorInstance.java @@ -1,17 +1,19 @@ package no.difi.vefa.validator; -import com.google.common.cache.LoadingCache; import com.google.inject.Inject; import com.google.inject.Singleton; import lombok.extern.slf4j.Slf4j; import no.difi.vefa.validator.api.Checker; -import no.difi.vefa.validator.api.Document; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.Section; +import no.difi.vefa.validator.api.Validation; import no.difi.vefa.validator.lang.UnknownDocumentTypeException; import no.difi.vefa.validator.lang.ValidatorException; -import no.difi.vefa.validator.util.DeclarationDetector; -import no.difi.vefa.validator.util.DeclarationIdentification; +import no.difi.vefa.validator.model.Detected; +import no.difi.vefa.validator.model.Document; +import no.difi.vefa.validator.model.Prop; +import no.difi.vefa.validator.model.Props; +import no.difi.vefa.validator.service.CheckerService; +import no.difi.vefa.validator.service.DetectorService; import no.difi.xsd.vefa.validator._1.ConfigurationType; import no.difi.xsd.vefa.validator._1.FileType; import no.difi.xsd.vefa.validator._1.FlagType; @@ -40,19 +42,23 @@ class ValidatorInstance implements Closeable { * Current validator configuration. */ @Inject - private Properties properties; + private Props props; /** * Declarations to use. */ @Inject - private DeclarationDetector declarationDetector; + private DetectorService declarationDetector; /** * Cache of checkers. */ @Inject - private LoadingCache checkerCache; + private CheckerService checkerService; + + public Validation validate(Document document, Prop... props) { + return ValidationInstance.of(this, document, props); + } /** * Normalized configurations indexed by document declarations. @@ -73,8 +79,8 @@ protected final List getPackages() { * * @return Current properties. */ - protected final Properties getProperties() { - return properties; + protected final Props getProps() { + return props; } /** @@ -108,7 +114,7 @@ protected Configuration getConfiguration(List declarations) throws Unkno "Configuration for '%s' not found.", declarations.get(0))); } - protected DeclarationIdentification detect(Document document) throws IOException { + protected Detected detect(Document document) throws IOException { return declarationDetector.detect(document); } @@ -125,7 +131,7 @@ protected Section check(FileType fileType, Document document, Configuration conf throws ValidatorException { Checker checker; try { - checker = checkerCache.get(fileType.getPath()); + checker = checkerService.get(fileType.getPath()); } catch (Exception e) { log.warn(e.getMessage(), e); throw new ValidatorException(String.format( @@ -135,19 +141,13 @@ protected Section check(FileType fileType, Document document, Configuration conf Section section = new Section(document.getExpectation()); section.setFlag(FlagType.OK); - if (properties.getBoolean("feature.infourl")) - section.setInfoUrl(fileType.getInfoUrl()); - checker.check(document, section); - - section.setInfoUrl(null); return section; } @Override public void close() throws IOException { - checkerCache.invalidateAll(); - checkerCache.cleanUp(); + checkerService.clear(); // This is last statement, allow to propagate. validatorEngine.close(); diff --git a/src/main/java/no/difi/vefa/validator/api/ArtifactHolder.java b/src/main/java/no/difi/vefa/validator/api/ArtifactHolder.java deleted file mode 100644 index c85bb2fb..00000000 --- a/src/main/java/no/difi/vefa/validator/api/ArtifactHolder.java +++ /dev/null @@ -1,69 +0,0 @@ -package no.difi.vefa.validator.api; - -import com.google.common.io.ByteStreams; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -/** - * @author erlend - */ -public interface ArtifactHolder { - - boolean exists(String path); - - byte[] get(String path); - - InputStream getInputStream(String path); - - Set getFilenames(); - - static ArtifactHolder of(InputStream inputStream) throws IOException { - Map content = new HashMap<>(); - - try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) { - ZipEntry zipEntry; - while ((zipEntry = zipInputStream.getNextEntry()) != null) { - content.put(zipEntry.getName(), ByteStreams.toByteArray(zipInputStream)); - zipInputStream.closeEntry(); - } - } - - return new ArtifactHolderImpl(content); - } - - class ArtifactHolderImpl implements ArtifactHolder { - - private final Map content; - - public ArtifactHolderImpl(Map content) { - this.content = content; - } - - @Override - public boolean exists(String path) { - return content.containsKey(path); - } - - @Override - public byte[] get(String path) { - return content.get(path); - } - - @Override - public InputStream getInputStream(String path) { - return new ByteArrayInputStream(content.get(path)); - } - - @Override - public Set getFilenames() { - return content.keySet(); - } - } -} diff --git a/src/main/java/no/difi/vefa/validator/api/Checker.java b/src/main/java/no/difi/vefa/validator/api/Checker.java index 63fd6285..47f21e34 100644 --- a/src/main/java/no/difi/vefa/validator/api/Checker.java +++ b/src/main/java/no/difi/vefa/validator/api/Checker.java @@ -1,6 +1,7 @@ package no.difi.vefa.validator.api; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.Document; /** * Interface for classes performing validation of business documents. diff --git a/src/main/java/no/difi/vefa/validator/api/CheckerFactory.java b/src/main/java/no/difi/vefa/validator/api/CheckerFactory.java index 3a4040ea..cd027ff2 100644 --- a/src/main/java/no/difi/vefa/validator/api/CheckerFactory.java +++ b/src/main/java/no/difi/vefa/validator/api/CheckerFactory.java @@ -1,6 +1,7 @@ package no.difi.vefa.validator.api; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.ArtifactHolder; /** * @author erlend diff --git a/src/main/java/no/difi/vefa/validator/api/Properties.java b/src/main/java/no/difi/vefa/validator/api/Properties.java deleted file mode 100644 index 607f3a32..00000000 --- a/src/main/java/no/difi/vefa/validator/api/Properties.java +++ /dev/null @@ -1,22 +0,0 @@ -package no.difi.vefa.validator.api; - -public interface Properties { - - boolean contains(String key); - - Object get(String key); - - Object get(String key, Object defaultValue); - - boolean getBoolean(String key); - - boolean getBoolean(String key, boolean defaultValue); - - int getInteger(String key); - - int getInteger(String key, int defaultValue); - - String getString(String key); - - String getString(String key, String defaultValue); -} diff --git a/src/main/java/no/difi/vefa/validator/api/Source.java b/src/main/java/no/difi/vefa/validator/api/Source.java index b420856a..c2899bef 100644 --- a/src/main/java/no/difi/vefa/validator/api/Source.java +++ b/src/main/java/no/difi/vefa/validator/api/Source.java @@ -13,6 +13,6 @@ public interface Source { * @throws ValidatorException * @return Instance containing validation artifacts. */ - SourceInstance createInstance(Properties properties) throws ValidatorException; + SourceInstance createInstance() throws ValidatorException; } diff --git a/src/main/java/no/difi/vefa/validator/api/SourceInstance.java b/src/main/java/no/difi/vefa/validator/api/SourceInstance.java index 49f0006e..59477d9b 100644 --- a/src/main/java/no/difi/vefa/validator/api/SourceInstance.java +++ b/src/main/java/no/difi/vefa/validator/api/SourceInstance.java @@ -1,5 +1,7 @@ package no.difi.vefa.validator.api; +import no.difi.vefa.validator.model.ArtifactHolder; + import java.util.Map; /** diff --git a/src/main/java/no/difi/vefa/validator/api/Validation.java b/src/main/java/no/difi/vefa/validator/api/Validation.java index f1f72e1a..43e0d5b9 100644 --- a/src/main/java/no/difi/vefa/validator/api/Validation.java +++ b/src/main/java/no/difi/vefa/validator/api/Validation.java @@ -1,5 +1,6 @@ package no.difi.vefa.validator.api; +import no.difi.vefa.validator.model.Document; import no.difi.xsd.vefa.validator._1.Report; import java.util.List; diff --git a/src/main/java/no/difi/vefa/validator/build/task/BuildTask.java b/src/main/java/no/difi/vefa/validator/build/task/BuildTask.java index 17dfd453..0ad1b4c5 100644 --- a/src/main/java/no/difi/vefa/validator/build/task/BuildTask.java +++ b/src/main/java/no/difi/vefa/validator/build/task/BuildTask.java @@ -13,7 +13,7 @@ import no.difi.vefa.validator.build.model.Build; import no.difi.vefa.validator.build.util.PreparerProvider; import no.difi.vefa.validator.util.JaxbUtils; -import no.difi.vefa.validator.util.ZipArchiver; +import no.difi.vefa.validator.util.ZipArchive; import no.difi.xsd.vefa.validator._1.BuildConfigurations; import no.difi.xsd.vefa.validator._1.ConfigurationType; import no.difi.xsd.vefa.validator._1.Configurations; @@ -116,7 +116,7 @@ public void build(final Build build) throws IOException, JAXBException { marshaller.marshal(configurations, outputStream); } - ZipArchiver.archive( + ZipArchive.archive( build.getTargetFolder().resolve(String.format("%s-%s.zip", build.getSetting("name"), build.getSetting("build"))), contentsPath); diff --git a/src/main/java/no/difi/vefa/validator/builder/ConfigurationBuilder.java b/src/main/java/no/difi/vefa/validator/builder/ConfigurationBuilder.java index a9a34c19..21afd854 100644 --- a/src/main/java/no/difi/vefa/validator/builder/ConfigurationBuilder.java +++ b/src/main/java/no/difi/vefa/validator/builder/ConfigurationBuilder.java @@ -4,6 +4,7 @@ import no.difi.xsd.vefa.validator._1.DeclarationType; import no.difi.xsd.vefa.validator._1.IdentifierType; +@Deprecated public class ConfigurationBuilder { private final ConfigurationType configuration = new ConfigurationType(); diff --git a/src/main/java/no/difi/vefa/validator/builder/ConfigurationsBuilder.java b/src/main/java/no/difi/vefa/validator/builder/ConfigurationsBuilder.java index 68549528..7a06c4f6 100644 --- a/src/main/java/no/difi/vefa/validator/builder/ConfigurationsBuilder.java +++ b/src/main/java/no/difi/vefa/validator/builder/ConfigurationsBuilder.java @@ -4,6 +4,7 @@ import no.difi.xsd.vefa.validator._1.Configurations; import no.difi.xsd.vefa.validator._1.PackageType; +@Deprecated public class ConfigurationsBuilder { private final Configurations configurations = new Configurations(); diff --git a/src/main/java/no/difi/vefa/validator/checker/SchematronCheckerFactory.java b/src/main/java/no/difi/vefa/validator/checker/SchematronCheckerFactory.java index c47199ce..033d87c8 100644 --- a/src/main/java/no/difi/vefa/validator/checker/SchematronCheckerFactory.java +++ b/src/main/java/no/difi/vefa/validator/checker/SchematronCheckerFactory.java @@ -4,17 +4,17 @@ import com.google.inject.Injector; import com.google.inject.Provider; import com.google.inject.name.Named; -import net.sf.saxon.s9api.*; +import net.sf.saxon.s9api.XdmDestination; +import net.sf.saxon.s9api.XsltCompiler; +import net.sf.saxon.s9api.XsltExecutable; +import net.sf.saxon.s9api.XsltTransformer; import no.difi.vefa.validator.annotation.Type; -import no.difi.vefa.validator.api.ArtifactHolder; import no.difi.vefa.validator.api.Checker; import no.difi.vefa.validator.api.CheckerFactory; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.ArtifactHolder; import no.difi.vefa.validator.util.SaxonUtils; -import javax.xml.transform.stream.StreamSource; -import java.io.InputStream; - /** * Implementation performing step 3 (compilation) of Schematron. * @@ -28,27 +28,27 @@ public class SchematronCheckerFactory implements CheckerFactory { private Provider schematronCompiler; @Inject - private Processor processor; + private Provider compilerProvider; @Inject private Injector injector; @Override public Checker prepare(ArtifactHolder artifactHolder, String path) throws ValidatorException { - try (InputStream inputStream = artifactHolder.getInputStream(path)) { + try { XdmDestination destination = new XdmDestination(); XsltTransformer xsltTransformer = schematronCompiler.get().load(); xsltTransformer.setErrorListener(SaxonUtils.ERROR_LISTENER); xsltTransformer.setMessageHandler(SaxonUtils.MESSAGE_HANDLER); - xsltTransformer.setSource(new StreamSource(inputStream)); + xsltTransformer.setSource(artifactHolder.getStream(path)); xsltTransformer.setDestination(destination); xsltTransformer.transform(); - XsltCompiler xsltCompiler = processor.newXsltCompiler(); + XsltCompiler xsltCompiler = compilerProvider.get(); xsltCompiler.setErrorListener(SaxonUtils.ERROR_LISTENER); - Checker checker = new SchematronXsltChecker(processor, xsltCompiler.compile(destination.getXdmNode().asSource())); + Checker checker = new SchematronXsltChecker(xsltCompiler.compile(destination.getXdmNode().asSource())); injector.injectMembers(checker); return checker; } catch (Exception e) { diff --git a/src/main/java/no/difi/vefa/validator/checker/SchematronXsltChecker.java b/src/main/java/no/difi/vefa/validator/checker/SchematronXsltChecker.java index 680c1449..3455a06c 100644 --- a/src/main/java/no/difi/vefa/validator/checker/SchematronXsltChecker.java +++ b/src/main/java/no/difi/vefa/validator/checker/SchematronXsltChecker.java @@ -6,11 +6,10 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.Unmarshaller; import lombok.extern.slf4j.Slf4j; -import net.sf.saxon.s9api.Processor; import net.sf.saxon.s9api.XsltExecutable; import net.sf.saxon.s9api.XsltTransformer; import no.difi.vefa.validator.api.Checker; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.api.Section; import no.difi.vefa.validator.lang.ValidatorException; import no.difi.vefa.validator.util.JaxbUtils; @@ -26,16 +25,13 @@ public class SchematronXsltChecker implements Checker { private static final JAXBContext JAXB_CONTEXT = JaxbUtils.context(SectionType.class); - private final Processor processor; - private final XsltExecutable xsltExecutable; @Inject @Named("schematron-svrl-parser") private Provider parser; - public SchematronXsltChecker(Processor processor, XsltExecutable xsltExecutable) { - this.processor = processor; + public SchematronXsltChecker(XsltExecutable xsltExecutable) { this.xsltExecutable = xsltExecutable; } @@ -55,7 +51,7 @@ public void check(Document document, Section section) throws ValidatorException parser.setErrorListener(SaxonUtils.ERROR_LISTENER); parser.setMessageHandler(SaxonUtils.MESSAGE_HANDLER); - parser.setDestination(processor.newSerializer(baos)); + parser.setDestination(xsltExecutable.getProcessor().newSerializer(baos)); schematron.transform(); diff --git a/src/main/java/no/difi/vefa/validator/checker/SchematronXsltCheckerFactory.java b/src/main/java/no/difi/vefa/validator/checker/SchematronXsltCheckerFactory.java index e6c110a8..7961017e 100644 --- a/src/main/java/no/difi/vefa/validator/checker/SchematronXsltCheckerFactory.java +++ b/src/main/java/no/difi/vefa/validator/checker/SchematronXsltCheckerFactory.java @@ -2,18 +2,15 @@ import com.google.inject.Inject; import com.google.inject.Injector; -import net.sf.saxon.s9api.Processor; +import com.google.inject.Provider; import net.sf.saxon.s9api.XsltCompiler; import no.difi.vefa.validator.annotation.Type; -import no.difi.vefa.validator.api.ArtifactHolder; import no.difi.vefa.validator.api.Checker; import no.difi.vefa.validator.api.CheckerFactory; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.ArtifactHolder; import no.difi.vefa.validator.util.SaxonUtils; -import javax.xml.transform.stream.StreamSource; -import java.io.InputStream; - /** * @author erlend */ @@ -21,21 +18,20 @@ public class SchematronXsltCheckerFactory implements CheckerFactory { @Inject - private Processor processor; + private Provider compilerProvider; @Inject private Injector injector; @Override public Checker prepare(ArtifactHolder artifactHolder, String path) throws ValidatorException { - try (InputStream inputStream = artifactHolder.getInputStream(path)) { - XsltCompiler xsltCompiler = processor.newXsltCompiler(); + try { + XsltCompiler xsltCompiler = compilerProvider.get(); xsltCompiler.setErrorListener(SaxonUtils.ERROR_LISTENER); - Checker checker = new SchematronXsltChecker(processor, xsltCompiler.compile(new StreamSource(inputStream))); + Checker checker = new SchematronXsltChecker(xsltCompiler.compile(artifactHolder.getStream(path))); injector.injectMembers(checker); return checker; - } catch (Exception e) { throw new ValidatorException(e.getMessage(), e); } diff --git a/src/main/java/no/difi/vefa/validator/checker/XsdChecker.java b/src/main/java/no/difi/vefa/validator/checker/XsdChecker.java index 006ee89c..9c473c74 100644 --- a/src/main/java/no/difi/vefa/validator/checker/XsdChecker.java +++ b/src/main/java/no/difi/vefa/validator/checker/XsdChecker.java @@ -1,7 +1,7 @@ package no.difi.vefa.validator.checker; import no.difi.vefa.validator.api.Checker; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.api.Section; import no.difi.xsd.vefa.validator._1.AssertionType; import no.difi.xsd.vefa.validator._1.FlagType; diff --git a/src/main/java/no/difi/vefa/validator/checker/XsdCheckerFactory.java b/src/main/java/no/difi/vefa/validator/checker/XsdCheckerFactory.java index b654df87..a76763bc 100644 --- a/src/main/java/no/difi/vefa/validator/checker/XsdCheckerFactory.java +++ b/src/main/java/no/difi/vefa/validator/checker/XsdCheckerFactory.java @@ -1,16 +1,14 @@ package no.difi.vefa.validator.checker; import no.difi.vefa.validator.annotation.Type; -import no.difi.vefa.validator.api.ArtifactHolder; import no.difi.vefa.validator.api.Checker; import no.difi.vefa.validator.api.CheckerFactory; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.ArtifactHolder; import no.difi.vefa.validator.util.HolderLSResolveResource; import javax.xml.XMLConstants; -import javax.xml.transform.stream.StreamSource; import javax.xml.validation.SchemaFactory; -import java.io.InputStream; /** * @author erlend @@ -20,10 +18,11 @@ public class XsdCheckerFactory implements CheckerFactory { @Override public Checker prepare(ArtifactHolder artifactHolder, String path) throws ValidatorException { - try (InputStream inputStream = artifactHolder.getInputStream(path)) { + try { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - schemaFactory.setResourceResolver(new HolderLSResolveResource(artifactHolder, path)); - return new XsdChecker(schemaFactory.newSchema(new StreamSource(inputStream))); + schemaFactory.setResourceResolver(new HolderLSResolveResource(artifactHolder)); + + return new XsdChecker(schemaFactory.newSchema(artifactHolder.getStream(path))); } catch (Exception e) { throw new ValidatorException(e.getMessage(), e); } diff --git a/src/main/java/no/difi/vefa/validator/configuration/ValidatorTestConfigurationProvider.java b/src/main/java/no/difi/vefa/validator/configuration/ValidatorTestConfigurationProvider.java index 5a6e9d17..f7b467fb 100644 --- a/src/main/java/no/difi/vefa/validator/configuration/ValidatorTestConfigurationProvider.java +++ b/src/main/java/no/difi/vefa/validator/configuration/ValidatorTestConfigurationProvider.java @@ -5,6 +5,7 @@ import no.difi.vefa.validator.builder.ConfigurationsBuilder; import no.difi.xsd.vefa.validator._1.Configurations; +@Deprecated public class ValidatorTestConfigurationProvider implements ConfigurationProvider { @Override diff --git a/src/main/java/no/difi/vefa/validator/expectation/ValidatorTestExpectation.java b/src/main/java/no/difi/vefa/validator/expectation/ValidatorTestExpectation.java index 4ef9943a..9e8a7ae4 100644 --- a/src/main/java/no/difi/vefa/validator/expectation/ValidatorTestExpectation.java +++ b/src/main/java/no/difi/vefa/validator/expectation/ValidatorTestExpectation.java @@ -3,7 +3,7 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.util.JaxbUtils; import no.difi.xsd.vefa.validator._1.AssertElementType; import no.difi.xsd.vefa.validator._1.AssertType; @@ -18,28 +18,34 @@ public class ValidatorTestExpectation extends AbstractExpectation { public ValidatorTestExpectation(Document document) { try { - Test test = jaxbContext.createUnmarshaller().unmarshal( - new StreamSource(document.asInputStream()), Test.class).getValue(); - AssertType assertType = test.getAssert(); - - if (assertType != null) { - description = test.getId() == null ? - assertType.getDescription() : - String.format("%s) %s", test.getId(), assertType.getDescription()); - scopes.addAll(assertType.getScope()); - - for (AssertElementType a : assertType.getFatal()) - fatals.put(a.getValue(), a.getNumber() == null ? 1 : a.getNumber()); - for (AssertElementType a : assertType.getError()) - errors.put(a.getValue(), a.getNumber() == null ? 1 : a.getNumber()); - for (AssertElementType a : assertType.getWarning()) - warnings.put(a.getValue(), a.getNumber() == null ? 1 : a.getNumber()); - for (String s : assertType.getSuccess()) - successes.put(s, 1); - } - + load(jaxbContext.createUnmarshaller().unmarshal( + new StreamSource(document.asInputStream()), Test.class).getValue()); } catch (JAXBException e) { log.warn(e.getMessage(), e); } } + + public ValidatorTestExpectation(Test test) { + load(test); + } + + private void load(Test test) { + AssertType assertType = test.getAssert(); + + if (assertType != null) { + description = test.getId() == null ? + assertType.getDescription() : + String.format("%s) %s", test.getId(), assertType.getDescription()); + scopes.addAll(assertType.getScope()); + + for (AssertElementType a : assertType.getFatal()) + fatals.put(a.getValue(), a.getNumber() == null ? 1 : a.getNumber()); + for (AssertElementType a : assertType.getError()) + errors.put(a.getValue(), a.getNumber() == null ? 1 : a.getNumber()); + for (AssertElementType a : assertType.getWarning()) + warnings.put(a.getValue(), a.getNumber() == null ? 1 : a.getNumber()); + for (String s : assertType.getSuccess()) + successes.put(s, 1); + } + } } diff --git a/src/main/java/no/difi/vefa/validator/expectation/XmlExpectation.java b/src/main/java/no/difi/vefa/validator/expectation/XmlExpectation.java index eabbc536..c01a0018 100644 --- a/src/main/java/no/difi/vefa/validator/expectation/XmlExpectation.java +++ b/src/main/java/no/difi/vefa/validator/expectation/XmlExpectation.java @@ -1,7 +1,7 @@ package no.difi.vefa.validator.expectation; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import java.util.List; import java.util.Map; diff --git a/src/main/java/no/difi/vefa/validator/model/ArtifactHolder.java b/src/main/java/no/difi/vefa/validator/model/ArtifactHolder.java new file mode 100644 index 00000000..762cc273 --- /dev/null +++ b/src/main/java/no/difi/vefa/validator/model/ArtifactHolder.java @@ -0,0 +1,70 @@ +package no.difi.vefa.validator.model; + +import com.google.common.io.ByteStreams; + +import javax.xml.transform.stream.StreamSource; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * @author erlend + */ +public class ArtifactHolder { + + public static ArtifactHolder of(Map content) { + return new ArtifactHolder(content); + } + + public static ArtifactHolder of(InputStream inputStream) throws IOException { + Map content = new HashMap<>(); + + try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) { + ZipEntry zipEntry; + while ((zipEntry = zipInputStream.getNextEntry()) != null) { + content.put(zipEntry.getName(), ByteStreams.toByteArray(zipInputStream)); + zipInputStream.closeEntry(); + } + } + + return of(content); + } + + private final Map content; + + private ArtifactHolder(Map content) { + this.content = content; + } + + public byte[] get(String path) { + return content.get(path); + } + + public InputStream getInputStream(String path) throws IOException { + if (!content.containsKey(path)) + throw new IOException(String.format("Resource not found in holder: %s", path)); + + return new ByteArrayInputStream(content.get(path)); + } + + public StreamSource getStream(String path) throws IOException { + return new StreamSource(getInputStream(path), "holder:" + path); + } + + public Document getDocument(String path) throws IOException { + if (!content.containsKey(path)) + throw new IOException(String.format("Resource not found in holder: %s", path)); + + return Document.of(content.get(path)); + } + + public Set getFilenames() { + return content.keySet(); + } + +} diff --git a/src/main/java/no/difi/vefa/validator/util/DeclarationIdentification.java b/src/main/java/no/difi/vefa/validator/model/Detected.java similarity index 86% rename from src/main/java/no/difi/vefa/validator/util/DeclarationIdentification.java rename to src/main/java/no/difi/vefa/validator/model/Detected.java index 7d62a150..54629ad0 100644 --- a/src/main/java/no/difi/vefa/validator/util/DeclarationIdentification.java +++ b/src/main/java/no/difi/vefa/validator/model/Detected.java @@ -1,12 +1,12 @@ -package no.difi.vefa.validator.util; +package no.difi.vefa.validator.model; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; -import no.difi.vefa.validator.api.Document; import no.difi.vefa.validator.api.Expectation; import no.difi.vefa.validator.expectation.ValidatorTestExpectation; import no.difi.vefa.validator.expectation.XmlExpectation; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.util.JaxbUtils; import no.difi.xsd.vefa.validator._1_0.internal.IdentificationType; import no.difi.xsd.vefa.validator._1_0.internal.PropertyType; import org.w3c.dom.Node; @@ -21,9 +21,9 @@ import java.util.Map; import java.util.stream.Collectors; -public class DeclarationIdentification { +public class Detected { - public static final DeclarationIdentification UNKNOWN = new DeclarationIdentification(); + public static final Detected UNKNOWN = new Detected(); private static final JAXBContext JAXB = JaxbUtils.context(IdentificationType.class); @@ -37,20 +37,20 @@ public class DeclarationIdentification { private final Object converted; - public static DeclarationIdentification of(ByteArrayOutputStream baos) throws ValidatorException { + public static Detected of(ByteArrayOutputStream baos) throws ValidatorException { return of(new ByteArrayInputStream(baos.toByteArray())); } - public static DeclarationIdentification of(InputStream inputStream) throws ValidatorException { + public static Detected of(InputStream inputStream) throws ValidatorException { try { - return new DeclarationIdentification(JAXB.createUnmarshaller() + return new Detected(JAXB.createUnmarshaller() .unmarshal(new StreamSource(inputStream), IdentificationType.class).getValue()); } catch (JAXBException e) { throw new ValidatorException("Unable to parse detector result.", e); } } - private DeclarationIdentification() { + private Detected() { this.type = null; this.identifiers = List.of("unknown"); this.properties = Collections.emptyMap(); @@ -58,7 +58,7 @@ private DeclarationIdentification() { this.converted = null; } - private DeclarationIdentification(IdentificationType it) { + private Detected(IdentificationType it) { this.type = it.getType(); this.identifiers = it.getId(); this.properties = it.getProperty() == null ? Collections.emptyMap() : diff --git a/src/main/java/no/difi/vefa/validator/api/Document.java b/src/main/java/no/difi/vefa/validator/model/Document.java similarity index 96% rename from src/main/java/no/difi/vefa/validator/api/Document.java rename to src/main/java/no/difi/vefa/validator/model/Document.java index 32886f98..b4a6cc29 100644 --- a/src/main/java/no/difi/vefa/validator/api/Document.java +++ b/src/main/java/no/difi/vefa/validator/model/Document.java @@ -1,8 +1,9 @@ -package no.difi.vefa.validator.api; +package no.difi.vefa.validator.model; import com.google.common.io.ByteStreams; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; +import no.difi.vefa.validator.api.Expectation; import no.difi.vefa.validator.lang.ValidatorException; import org.w3c.dom.Node; @@ -48,6 +49,9 @@ public static Document of(String content) { public static Document ofResource(String resource) throws IOException { try (var inputStream = Document.class.getResourceAsStream(resource)) { + if (inputStream == null) + throw new IOException(String.format("Resource '%s' not found.", resource)); + return of(inputStream); } } diff --git a/src/main/java/no/difi/vefa/validator/model/Prop.java b/src/main/java/no/difi/vefa/validator/model/Prop.java new file mode 100644 index 00000000..5600153c --- /dev/null +++ b/src/main/java/no/difi/vefa/validator/model/Prop.java @@ -0,0 +1,49 @@ +package no.difi.vefa.validator.model; + +public class Prop { + + public static Prop of(String key, String value) { + return new Prop(key, value); + } + + public static Prop of(String key, boolean value) { + return new Prop(key, Boolean.toString(value)); + } + + public static Prop of(String key, int value) { + return new Prop(key, Integer.toString(value)); + } + + public static Prop of(String key, long value) { + return new Prop(key, Long.toString(value)); + } + + private final String key; + + private final String value; + + private Prop(String key, String value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public String asString() { + return value; + } + + public int asInt() { + return Integer.parseInt(value); + } + + public long asLong() { + return Long.parseLong(value); + } + + public boolean asBool() { + return Boolean.parseBoolean(value); + } +} diff --git a/src/main/java/no/difi/vefa/validator/model/Props.java b/src/main/java/no/difi/vefa/validator/model/Props.java new file mode 100644 index 00000000..f139967b --- /dev/null +++ b/src/main/java/no/difi/vefa/validator/model/Props.java @@ -0,0 +1,55 @@ +package no.difi.vefa.validator.model; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + +public class Props { + + public static Props init() { + return new Props(); + } + + private final Map map; + + private Props() { + map = Collections.emptyMap(); + } + + private Props(Map updated) { + map = updated; + } + + public String getString(String key, String defaultValue) { + return map.containsKey(key) ? map.get(key).asString() : defaultValue; + } + + public String getString(String key, Supplier defaultSupplier) { + return map.containsKey(key) ? map.get(key).asString() : defaultSupplier.get(); + } + + public int getInt(String key, int defaultValue) { + return map.containsKey(key) ? map.get(key).asInt() : defaultValue; + } + + public long getLong(String key, long defaultValue) { + return map.containsKey(key) ? map.get(key).asLong() : defaultValue; + } + + public boolean getBool(String key, boolean defaultValue) { + return map.containsKey(key) ? map.get(key).asBool() : defaultValue; + } + + public Props update(Prop... props) { + if (props == null || props.length == 0) + return this; + + Map updated = new HashMap<>(map); + + for (var prop : props) + updated.put(prop.getKey(), prop); + + return new Props(updated); + } +} diff --git a/src/main/java/no/difi/vefa/validator/module/CacheModule.java b/src/main/java/no/difi/vefa/validator/module/CacheModule.java deleted file mode 100644 index 6abf0b92..00000000 --- a/src/main/java/no/difi/vefa/validator/module/CacheModule.java +++ /dev/null @@ -1,28 +0,0 @@ -package no.difi.vefa.validator.module; - -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.LoadingCache; -import com.google.inject.AbstractModule; -import com.google.inject.Provides; -import com.google.inject.Singleton; -import no.difi.vefa.validator.CheckerCacheLoader; -import no.difi.vefa.validator.api.Checker; -import no.difi.vefa.validator.api.Properties; - -import java.util.concurrent.TimeUnit; - -/** - * @author erlend - */ -public class CacheModule extends AbstractModule { - - @Provides - @Singleton - public LoadingCache getCheckerCache(Properties properties, CheckerCacheLoader loader) { - return CacheBuilder.newBuilder() - .softValues() - .maximumSize(properties.getInteger("pools.checker.size")) - .expireAfterAccess(properties.getInteger("pools.checker.expire"), TimeUnit.MINUTES) - .build(loader); - } -} diff --git a/src/main/java/no/difi/vefa/validator/module/CheckerModule.java b/src/main/java/no/difi/vefa/validator/module/CheckerModule.java index 4e45ed30..743f68cd 100644 --- a/src/main/java/no/difi/vefa/validator/module/CheckerModule.java +++ b/src/main/java/no/difi/vefa/validator/module/CheckerModule.java @@ -4,12 +4,15 @@ import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.multibindings.Multibinder; +import no.difi.vefa.validator.annotation.Type; import no.difi.vefa.validator.api.CheckerFactory; import no.difi.vefa.validator.checker.SchematronCheckerFactory; import no.difi.vefa.validator.checker.SchematronXsltCheckerFactory; import no.difi.vefa.validator.checker.XsdCheckerFactory; -import java.util.List; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Set; public class CheckerModule extends AbstractModule { @@ -24,7 +27,13 @@ protected void configure() { @Provides @Singleton - public List getCheckerFactories(Set factories) { - return List.copyOf(factories); + public Map getMap(Set factories) { + Map result = new HashMap<>(); + + for (var factory : factories) + for (var type : factory.getClass().getAnnotation(Type.class).value()) + result.put(type, factory); + + return Collections.unmodifiableMap(result); } } diff --git a/src/main/java/no/difi/vefa/validator/module/DetectorModule.java b/src/main/java/no/difi/vefa/validator/module/DetectorModule.java index 4df75875..c3fc98eb 100644 --- a/src/main/java/no/difi/vefa/validator/module/DetectorModule.java +++ b/src/main/java/no/difi/vefa/validator/module/DetectorModule.java @@ -4,23 +4,16 @@ import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.name.Named; -import net.sf.saxon.s9api.Processor; -import net.sf.saxon.s9api.SaxonApiException; import net.sf.saxon.s9api.XsltExecutable; - -import javax.xml.transform.stream.StreamSource; -import java.io.IOException; +import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.util.XsltHelper; public class DetectorModule extends AbstractModule { @Provides @Singleton @Named("detector") - public XsltExecutable getDetectorXslt(Processor processor) throws IOException, SaxonApiException { - var compiler = processor.newXsltCompiler(); - - try (var inputStream = getClass().getResourceAsStream("/vefa-validator/xslt/detector.xslt")) { - return compiler.compile(new StreamSource(inputStream)); - } + public XsltExecutable getDetectorXslt(XsltHelper helper) throws ValidatorException { + return helper.fromResource("/vefa-validator/xslt/detector.xslt"); } } diff --git a/src/main/java/no/difi/vefa/validator/module/PropertiesModule.java b/src/main/java/no/difi/vefa/validator/module/PropertiesModule.java index 0b83daa5..5368ec4a 100644 --- a/src/main/java/no/difi/vefa/validator/module/PropertiesModule.java +++ b/src/main/java/no/difi/vefa/validator/module/PropertiesModule.java @@ -1,31 +1,15 @@ package no.difi.vefa.validator.module; -import com.google.inject.AbstractModule; -import com.google.inject.Provides; -import com.google.inject.Singleton; -import no.difi.vefa.validator.ValidatorDefaults; -import no.difi.vefa.validator.api.Properties; -import no.difi.vefa.validator.properties.CombinedProperties; +import com.google.inject.Module; +import no.difi.vefa.validator.model.Prop; +import no.difi.vefa.validator.model.Props; /** * @author erlend */ -public class PropertiesModule extends AbstractModule { +public interface PropertiesModule { - private final Properties properties; - - public PropertiesModule() { - this(null); - } - - public PropertiesModule(Properties properties) { - this.properties = properties; - } - - @Provides - @Singleton - public Properties getProperties() { - // Create config combined with default values. - return new CombinedProperties(properties, ValidatorDefaults.PROPERTIES); + static Module with(Prop... props) { + return binder -> binder.bind(Props.class).toInstance(Props.init().update(props)); } } diff --git a/src/main/java/no/difi/vefa/validator/module/SaxonModule.java b/src/main/java/no/difi/vefa/validator/module/SaxonModule.java index 181503b5..2e76147b 100644 --- a/src/main/java/no/difi/vefa/validator/module/SaxonModule.java +++ b/src/main/java/no/difi/vefa/validator/module/SaxonModule.java @@ -6,6 +6,8 @@ import net.sf.saxon.Configuration; import net.sf.saxon.lib.Feature; import net.sf.saxon.s9api.Processor; +import net.sf.saxon.s9api.XsltCompiler; +import no.difi.vefa.validator.util.ContentLoader; /** @@ -21,4 +23,12 @@ public Processor getProcessor() { return new Processor(configuration); } + + @Provides + public XsltCompiler getCompiler(Processor processor, ContentLoader resourceResolver) { + var compiler = processor.newXsltCompiler(); + compiler.setResourceResolver(resourceResolver); + + return compiler; + } } diff --git a/src/main/java/no/difi/vefa/validator/module/SchematronModule.java b/src/main/java/no/difi/vefa/validator/module/SchematronModule.java index 245be524..b4108485 100644 --- a/src/main/java/no/difi/vefa/validator/module/SchematronModule.java +++ b/src/main/java/no/difi/vefa/validator/module/SchematronModule.java @@ -5,15 +5,9 @@ import com.google.inject.Singleton; import com.google.inject.name.Named; import lombok.extern.slf4j.Slf4j; -import net.sf.saxon.s9api.Processor; -import net.sf.saxon.s9api.SaxonApiException; -import net.sf.saxon.s9api.XsltCompiler; import net.sf.saxon.s9api.XsltExecutable; -import no.difi.vefa.validator.util.ClasspathURIResolver; - -import javax.xml.transform.stream.StreamSource; -import java.io.IOException; -import java.io.InputStream; +import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.util.XsltHelper; /** * @author erlend @@ -24,25 +18,14 @@ public class SchematronModule extends AbstractModule { @Provides @Named("schematron-step3") @Singleton - public XsltExecutable getSchematronCompiler(Processor processor) { - try (InputStream inputStream = getClass().getResourceAsStream("/iso-schematron-xslt2/iso_svrl_for_xslt2.xsl")) { - XsltCompiler xsltCompiler = processor.newXsltCompiler(); - xsltCompiler.setURIResolver(new ClasspathURIResolver("/iso-schematron-xslt2")); - return xsltCompiler.compile(new StreamSource(inputStream)); - } catch (IOException | SaxonApiException e) { - throw new IllegalStateException("Unable to load parsing of Schematron."); - } + public XsltExecutable getSchematronCompiler(XsltHelper helper) throws ValidatorException { + return helper.fromResource("/iso-schematron-xslt2/iso_svrl_for_xslt2.xsl"); } @Provides @Named("schematron-svrl-parser") @Singleton - public XsltExecutable getSchematronSvrlParser(Processor processor) { - try (InputStream inputStream = getClass().getResourceAsStream("/vefa-validator/xslt/svrl-parser.xslt")) { - XsltCompiler xsltCompiler = processor.newXsltCompiler(); - return xsltCompiler.compile(new StreamSource(inputStream)); - } catch (IOException | SaxonApiException e) { - throw new IllegalStateException("Unable to load parsing of Schematron reports."); - } + public XsltExecutable getSchematronSvrlParser(XsltHelper helper) throws ValidatorException { + return helper.fromResource("/vefa-validator/xslt/svrl-parser.xslt"); } } diff --git a/src/main/java/no/difi/vefa/validator/module/SourceModule.java b/src/main/java/no/difi/vefa/validator/module/SourceModule.java index 20c0f384..aca8104a 100644 --- a/src/main/java/no/difi/vefa/validator/module/SourceModule.java +++ b/src/main/java/no/difi/vefa/validator/module/SourceModule.java @@ -3,10 +3,10 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.Singleton; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.Source; import no.difi.vefa.validator.api.SourceInstance; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.Props; import no.difi.vefa.validator.source.RepositorySource; /** @@ -26,9 +26,8 @@ public SourceModule(Source source) { @Provides @Singleton - public SourceInstance getSource(Properties properties) throws ValidatorException { + public SourceInstance getSource(Props props) throws ValidatorException { // Make sure to default to repository source if no source is set. - return (source != null ? source : RepositorySource.forProduction()) - .createInstance(properties); + return (source != null ? source : RepositorySource.forProduction()).createInstance(); } } diff --git a/src/main/java/no/difi/vefa/validator/module/ValidatorModule.java b/src/main/java/no/difi/vefa/validator/module/ValidatorModule.java index 88f0f6d4..976e919a 100644 --- a/src/main/java/no/difi/vefa/validator/module/ValidatorModule.java +++ b/src/main/java/no/difi/vefa/validator/module/ValidatorModule.java @@ -19,10 +19,9 @@ public class ValidatorModule extends AbstractModule { @Override protected void configure() { - install(new CacheModule()); install(new CheckerModule()); install(new DetectorModule()); - install(new PropertiesModule()); + install(PropertiesModule.with()); install(new SaxonModule()); install(new SourceModule()); install(new SchematronModule()); diff --git a/src/main/java/no/difi/vefa/validator/properties/AbstractProperties.java b/src/main/java/no/difi/vefa/validator/properties/AbstractProperties.java deleted file mode 100644 index 85f5012d..00000000 --- a/src/main/java/no/difi/vefa/validator/properties/AbstractProperties.java +++ /dev/null @@ -1,27 +0,0 @@ -package no.difi.vefa.validator.properties; - -import no.difi.vefa.validator.api.Properties; - -abstract class AbstractProperties implements Properties { - - @Override - public Object get(String key) { - return get(key, null); - } - - @Override - public boolean getBoolean(String key) { - return getBoolean(key, false); - } - - @Override - public int getInteger(String key) { - return getInteger(key, 0); - } - - @Override - public String getString(String key) { - return getString(key, null); - } - -} diff --git a/src/main/java/no/difi/vefa/validator/properties/CombinedProperties.java b/src/main/java/no/difi/vefa/validator/properties/CombinedProperties.java deleted file mode 100644 index 00d084b8..00000000 --- a/src/main/java/no/difi/vefa/validator/properties/CombinedProperties.java +++ /dev/null @@ -1,57 +0,0 @@ -package no.difi.vefa.validator.properties; - -import no.difi.vefa.validator.api.Properties; - -/** - * Implementation of Properties making it easy to access multiple instances of Properties. - */ -public class CombinedProperties extends AbstractProperties { - - private final Properties[] properties; - - /** - * Allow combination of configs, the most specific first. - * - * @param properties List containing instances of Properties to be combined. - */ - public CombinedProperties(Properties... properties) { - this.properties = properties; - } - - private Properties detect(String key) { - for (Properties properties : this.properties) - if (properties != null && properties.contains(key)) - return properties; - return null; - } - - @Override - public boolean contains(String key) { - Properties properties = detect(key); - return properties != null; - } - - @Override - public Object get(String key, Object defaultValue) { - Properties properties = detect(key); - return properties == null ? defaultValue : properties.get(key, defaultValue); - } - - @Override - public boolean getBoolean(String key, boolean defaultValue) { - Properties properties = detect(key); - return properties == null ? defaultValue : properties.getBoolean(key, defaultValue); - } - - @Override - public int getInteger(String key, int defaultValue) { - Properties properties = detect(key); - return properties == null ? defaultValue : properties.getInteger(key, defaultValue); - } - - @Override - public String getString(String key, String defaultValue) { - Properties properties = detect(key); - return properties == null ? defaultValue : properties.getString(key, defaultValue); - } -} diff --git a/src/main/java/no/difi/vefa/validator/properties/SimpleProperties.java b/src/main/java/no/difi/vefa/validator/properties/SimpleProperties.java deleted file mode 100644 index cf204387..00000000 --- a/src/main/java/no/difi/vefa/validator/properties/SimpleProperties.java +++ /dev/null @@ -1,55 +0,0 @@ -package no.difi.vefa.validator.properties; - -import lombok.extern.slf4j.Slf4j; - -import java.util.HashMap; -import java.util.Map; - -/** - * Simple implementation of Properties using a HashMap to store values. - */ -@Slf4j -public class SimpleProperties extends AbstractProperties { - - private Map values; - - public SimpleProperties() { - values = new HashMap<>(); - } - - public SimpleProperties set(String key, Object value) { - values.put(key, value); - return this; - } - - @Override - public boolean contains(String key) { - return values.containsKey(key); - } - - @Override - public Object get(String key, Object defaultValue) { - return values.containsKey(key) ? values.get(key) : defaultValue; - } - - @Override - public boolean getBoolean(String key, boolean defaultValue) { - return values.containsKey(key) ? Boolean.parseBoolean(String.valueOf(values.get(key))) : defaultValue; - } - - @Override - public int getInteger(String key, int defaultValue) { - try { - if (values.containsKey(key)) - return Integer.parseInt(String.valueOf(values.get(key))); - } catch (NumberFormatException e) { - log.error(String.format("Error while casting '%s' to integer for key '%s'.", values.get(key), key)); - } - return defaultValue; - } - - @Override - public String getString(String key, String defaultValue) { - return values.containsKey(key) ? String.valueOf(values.get(key)) : defaultValue; - } -} diff --git a/src/main/java/no/difi/vefa/validator/properties/package-info.java b/src/main/java/no/difi/vefa/validator/properties/package-info.java deleted file mode 100644 index 430681e1..00000000 --- a/src/main/java/no/difi/vefa/validator/properties/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Implementations used for properties. - */ -package no.difi.vefa.validator.properties; \ No newline at end of file diff --git a/src/main/java/no/difi/vefa/validator/service/CheckerService.java b/src/main/java/no/difi/vefa/validator/service/CheckerService.java new file mode 100644 index 00000000..5d7ffba1 --- /dev/null +++ b/src/main/java/no/difi/vefa/validator/service/CheckerService.java @@ -0,0 +1,35 @@ +package no.difi.vefa.validator.service; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.LoadingCache; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import no.difi.vefa.validator.CheckerCacheLoader; +import no.difi.vefa.validator.api.Checker; +import no.difi.vefa.validator.model.Props; + +import java.util.concurrent.TimeUnit; + +@Singleton +public class CheckerService { + + private final LoadingCache cache; + + @Inject + public CheckerService(Props props, CheckerCacheLoader loader) { + cache = CacheBuilder.newBuilder() + .softValues() + .maximumSize(props.getInt("pools.checker.size", 250)) + .expireAfterAccess(props.getLong("pools.checker.expire", TimeUnit.DAYS.toMinutes(1)), TimeUnit.MINUTES) + .build(loader); + } + + public Checker get(String key) throws Exception { + return cache.get(key); + } + + public void clear() { + cache.invalidateAll(); + cache.cleanUp(); + } +} diff --git a/src/main/java/no/difi/vefa/validator/util/DeclarationDetector.java b/src/main/java/no/difi/vefa/validator/service/DetectorService.java similarity index 75% rename from src/main/java/no/difi/vefa/validator/util/DeclarationDetector.java rename to src/main/java/no/difi/vefa/validator/service/DetectorService.java index b921610f..4f72740a 100644 --- a/src/main/java/no/difi/vefa/validator/util/DeclarationDetector.java +++ b/src/main/java/no/difi/vefa/validator/service/DetectorService.java @@ -1,4 +1,4 @@ -package no.difi.vefa.validator.util; +package no.difi.vefa.validator.service; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -6,8 +6,9 @@ import lombok.extern.slf4j.Slf4j; import net.sf.saxon.s9api.SaxonApiException; import net.sf.saxon.s9api.XsltExecutable; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.model.Detected; import javax.xml.transform.stream.StreamSource; import java.io.ByteArrayOutputStream; @@ -15,13 +16,13 @@ @Slf4j @Singleton -public class DeclarationDetector { +public class DetectorService { @Inject @Named("detector") private XsltExecutable detector; - public DeclarationIdentification detect(Document document) throws IOException { + public Detected detect(Document document) throws IOException { try { var baos = new ByteArrayOutputStream(); @@ -30,9 +31,9 @@ public DeclarationIdentification detect(Document document) throws IOException { transformer.setDestination(detector.getProcessor().newSerializer(baos)); transformer.transform(); - return DeclarationIdentification.of(baos); + return Detected.of(baos); } catch (SaxonApiException e) { - return DeclarationIdentification.UNKNOWN; + return Detected.UNKNOWN; } catch (ValidatorException e) { throw new IOException(e.getMessage(), e); } diff --git a/src/main/java/no/difi/vefa/validator/service/TestingService.java b/src/main/java/no/difi/vefa/validator/service/TestingService.java new file mode 100644 index 00000000..9a057dc1 --- /dev/null +++ b/src/main/java/no/difi/vefa/validator/service/TestingService.java @@ -0,0 +1,93 @@ +package no.difi.vefa.validator.service; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import jakarta.xml.bind.JAXBContext; +import no.difi.vefa.validator.Validator; +import no.difi.vefa.validator.model.Document; +import no.difi.vefa.validator.api.Expectation; +import no.difi.vefa.validator.api.Section; +import no.difi.vefa.validator.api.Validation; +import no.difi.vefa.validator.expectation.ValidatorTestExpectation; +import no.difi.vefa.validator.expectation.XmlExpectation; +import no.difi.vefa.validator.lang.ValidatorException; +import no.difi.vefa.validator.util.JaxbUtils; +import no.difi.xsd.vefa.validator._1.Test; +import no.difi.xsd.vefa.validator._1.TestSet; +import org.w3c.dom.Node; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +@Singleton +public class TestingService { + + private static final JAXBContext JAXB = JaxbUtils.context(TestSet.class); + + @Inject + private Validator validator; + + @Inject + private DetectorService detectorService; + + public List verify(Path path) throws IOException, ValidatorException { + return verify(Document.of(path)); + } + + public List verify(Document document) throws IOException, ValidatorException { + var detected = detectorService.detect(document); + + if ("http://difi.no/xsd/vefa/validator/1.0".equals(detected.getProperties().get("xml.namespace"))) { + if ("testSet".equals(detected.getProperties().get("xml.element"))) { + return handleTestSet(document); + } else if ("test".equals(detected.getProperties().get("xml.element"))) { + return List.of(handleTest(document)); + } + } else { + return List.of(handleDocument(document)); + } + + throw new ValidatorException("Unable to use provided document as test."); + } + + private List handleTestSet(Document document) throws ValidatorException { + var validations = new ArrayList(); + + for (var test : document.unmarshal(JAXB, TestSet.class).getTest()) { + validations.add(handleTest(test)); + } + + return validations; + } + + private Validation handleTest(Document document) throws ValidatorException { + return handleTest(document.unmarshal(JAXB, Test.class)); + } + + private Validation handleTest(Test test) throws ValidatorException { + if (test.getAny() instanceof Node node) + return handleGeneric(Document.of(node), new ValidatorTestExpectation(test)); + + throw new ValidatorException("Unable to read example XML."); + } + + private Validation handleDocument(Document document) { + return handleGeneric(document, new XmlExpectation(document)); + } + + private Validation handleGeneric(Document document, Expectation expectation) { + var validation = validator.validate(document); + + for (var section : validation.getReport().getSection()) { + for (var assertion : section.getAssertion()) { + expectation.filterFlag(assertion); + } + + expectation.verify((Section) section); + } + + return validation; + } +} diff --git a/src/main/java/no/difi/vefa/validator/source/AbstractSourceInstance.java b/src/main/java/no/difi/vefa/validator/source/AbstractSourceInstance.java index 58807973..f2fdd01b 100644 --- a/src/main/java/no/difi/vefa/validator/source/AbstractSourceInstance.java +++ b/src/main/java/no/difi/vefa/validator/source/AbstractSourceInstance.java @@ -2,9 +2,9 @@ import jakarta.xml.bind.JAXBContext; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.ArtifactHolder; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.SourceInstance; +import no.difi.vefa.validator.model.ArtifactHolder; +import no.difi.vefa.validator.model.Props; import no.difi.vefa.validator.util.JaxbUtils; import no.difi.xsd.vefa.validator._1.Artifacts; @@ -21,14 +21,8 @@ public abstract class AbstractSourceInstance implements SourceInstance, Closeabl protected static final JAXBContext JAXB_CONTEXT = JaxbUtils.context(Artifacts.class); - protected Properties properties; - protected Map content = new HashMap<>(); - public AbstractSourceInstance(Properties properties) { - this.properties = properties; - } - protected void unpackContainer(InputStream inputStream, String targetName) throws IOException { content.put(targetName, ArtifactHolder.of(inputStream)); } diff --git a/src/test/java/no/difi/vefa/validator/source/ClasspathSource.java b/src/main/java/no/difi/vefa/validator/source/ClasspathSource.java similarity index 65% rename from src/test/java/no/difi/vefa/validator/source/ClasspathSource.java rename to src/main/java/no/difi/vefa/validator/source/ClasspathSource.java index c60529c5..2cbfd8f4 100644 --- a/src/test/java/no/difi/vefa/validator/source/ClasspathSource.java +++ b/src/main/java/no/difi/vefa/validator/source/ClasspathSource.java @@ -1,6 +1,5 @@ package no.difi.vefa.validator.source; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.Source; import no.difi.vefa.validator.api.SourceInstance; import no.difi.vefa.validator.lang.ValidatorException; @@ -14,7 +13,7 @@ public ClasspathSource(String location) { } @Override - public SourceInstance createInstance(Properties properties) throws ValidatorException { - return new ClasspathSourceInstance(properties, location); + public SourceInstance createInstance() throws ValidatorException { + return new ClasspathSourceInstance(location); } } diff --git a/src/test/java/no/difi/vefa/validator/source/ClasspathSourceInstance.java b/src/main/java/no/difi/vefa/validator/source/ClasspathSourceInstance.java similarity index 90% rename from src/test/java/no/difi/vefa/validator/source/ClasspathSourceInstance.java rename to src/main/java/no/difi/vefa/validator/source/ClasspathSourceInstance.java index b437d0ac..ae5b3b8c 100644 --- a/src/test/java/no/difi/vefa/validator/source/ClasspathSourceInstance.java +++ b/src/main/java/no/difi/vefa/validator/source/ClasspathSourceInstance.java @@ -1,7 +1,6 @@ package no.difi.vefa.validator.source; import jakarta.xml.bind.Unmarshaller; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.lang.ValidatorException; import no.difi.xsd.vefa.validator._1.ArtifactType; import no.difi.xsd.vefa.validator._1.Artifacts; @@ -16,9 +15,7 @@ class ClasspathSourceInstance extends AbstractSourceInstance { private static final Logger logger = LoggerFactory.getLogger(ClasspathSourceInstance.class); - public ClasspathSourceInstance(Properties properties, String location) throws ValidatorException { - super(properties); - + public ClasspathSourceInstance(String location) throws ValidatorException { String artifactsUri = location + "artifacts.xml"; try (InputStream inputStream = getClass().getResourceAsStream(artifactsUri)) { diff --git a/src/main/java/no/difi/vefa/validator/source/DirectorySource.java b/src/main/java/no/difi/vefa/validator/source/DirectorySource.java index d145d3ba..b55514ef 100644 --- a/src/main/java/no/difi/vefa/validator/source/DirectorySource.java +++ b/src/main/java/no/difi/vefa/validator/source/DirectorySource.java @@ -1,6 +1,5 @@ package no.difi.vefa.validator.source; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.Source; import no.difi.vefa.validator.api.SourceInstance; import no.difi.vefa.validator.lang.ValidatorException; @@ -24,7 +23,7 @@ public DirectorySource(Path... directories) { } @Override - public SourceInstance createInstance(Properties properties) throws ValidatorException { - return new DirectorySourceInstance(properties, directories); + public SourceInstance createInstance() throws ValidatorException { + return new DirectorySourceInstance(directories); } } diff --git a/src/main/java/no/difi/vefa/validator/source/DirectorySourceInstance.java b/src/main/java/no/difi/vefa/validator/source/DirectorySourceInstance.java index ca568236..7f290efc 100644 --- a/src/main/java/no/difi/vefa/validator/source/DirectorySourceInstance.java +++ b/src/main/java/no/difi/vefa/validator/source/DirectorySourceInstance.java @@ -2,7 +2,6 @@ import jakarta.xml.bind.Unmarshaller; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.lang.ValidatorException; import no.difi.xsd.vefa.validator._1.ArtifactType; import no.difi.xsd.vefa.validator._1.Artifacts; @@ -24,10 +23,7 @@ class DirectorySourceInstance extends AbstractSourceInstance { * * @param directories Directories containing validation artifacts. */ - public DirectorySourceInstance(Properties properties, Path... directories) throws ValidatorException { - // Call #AbstractSourceInstance(). - super(properties); - + public DirectorySourceInstance(Path... directories) throws ValidatorException { try { for (Path directory : directories) { log.info("Directory: {}", directory); diff --git a/src/main/java/no/difi/vefa/validator/source/RepositorySource.java b/src/main/java/no/difi/vefa/validator/source/RepositorySource.java index 7f9cf39b..be9f1fb7 100644 --- a/src/main/java/no/difi/vefa/validator/source/RepositorySource.java +++ b/src/main/java/no/difi/vefa/validator/source/RepositorySource.java @@ -1,6 +1,5 @@ package no.difi.vefa.validator.source; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.api.Source; import no.difi.vefa.validator.api.SourceInstance; import no.difi.vefa.validator.lang.ValidatorException; @@ -58,7 +57,7 @@ public RepositorySource(List uris) { } @Override - public SourceInstance createInstance(Properties properties) throws ValidatorException { - return new RepositorySourceInstance(properties, rootUri); + public SourceInstance createInstance() throws ValidatorException { + return new RepositorySourceInstance(rootUri); } } diff --git a/src/main/java/no/difi/vefa/validator/source/RepositorySourceInstance.java b/src/main/java/no/difi/vefa/validator/source/RepositorySourceInstance.java index 265ad217..dd206410 100644 --- a/src/main/java/no/difi/vefa/validator/source/RepositorySourceInstance.java +++ b/src/main/java/no/difi/vefa/validator/source/RepositorySourceInstance.java @@ -2,7 +2,6 @@ import jakarta.xml.bind.Unmarshaller; import lombok.extern.slf4j.Slf4j; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.lang.ValidatorException; import no.difi.xsd.vefa.validator._1.ArtifactType; import no.difi.xsd.vefa.validator._1.Artifacts; @@ -14,9 +13,7 @@ @Slf4j class RepositorySourceInstance extends AbstractSourceInstance { - public RepositorySourceInstance(Properties properties, List rootUris) throws ValidatorException { - super(properties); - + public RepositorySourceInstance(List rootUris) throws ValidatorException { try { for (URI rootUri : rootUris) { Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); diff --git a/src/main/java/no/difi/vefa/validator/tester/Tester.java b/src/main/java/no/difi/vefa/validator/tester/Tester.java index 1edd0c60..ddae1fca 100644 --- a/src/main/java/no/difi/vefa/validator/tester/Tester.java +++ b/src/main/java/no/difi/vefa/validator/tester/Tester.java @@ -1,36 +1,43 @@ package no.difi.vefa.validator.tester; +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.Module; import com.google.inject.Singleton; +import com.google.inject.util.Modules; import lombok.extern.slf4j.Slf4j; import no.difi.vefa.validator.Validator; -import no.difi.vefa.validator.ValidatorBuilder; +import no.difi.vefa.validator.api.Source; import no.difi.vefa.validator.api.Validation; -import no.difi.vefa.validator.properties.SimpleProperties; +import no.difi.vefa.validator.model.Document; +import no.difi.vefa.validator.model.Prop; +import no.difi.vefa.validator.module.PropertiesModule; +import no.difi.vefa.validator.module.SourceModule; +import no.difi.vefa.validator.module.ValidatorModule; +import no.difi.vefa.validator.service.TestingService; import no.difi.vefa.validator.source.DirectorySource; import no.difi.vefa.validator.source.RepositorySource; import no.difi.xsd.vefa.validator._1.AssertionType; import no.difi.xsd.vefa.validator._1.FlagType; import no.difi.xsd.vefa.validator._1.SectionType; -import java.io.Closeable; -import java.io.File; import java.io.IOException; import java.net.URI; -import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; -import java.util.Collections; import java.util.List; @Slf4j @Singleton -public class Tester implements Closeable { +public class Tester { + @Inject private Validator validator; + @Inject + private TestingService testingService; + private final List validations = new ArrayList<>(); private int tests; @@ -38,63 +45,41 @@ public class Tester implements Closeable { private int failed; public static List perform(Path artifactsPath, List testPaths) throws IOException { - try (Tester tester = new Tester(artifactsPath)) { - for (Path path : testPaths) - tester.perform(path); - return tester.finish(); - } + return perform(new DirectorySource(artifactsPath), testPaths); } public static List perform(URI artifactsUri, List testPaths) throws IOException { - try (Tester tester = new Tester(artifactsUri)) { - for (Path path : testPaths) - tester.perform(path); - return tester.finish(); - } + return perform(new RepositorySource(artifactsUri), testPaths); } - private Tester(Path artifactsPath) { - validator = ValidatorBuilder - .newValidator() - .setProperties(new SimpleProperties() - .set("feature.nesting", true) - .set("feature.expectation", true) - .set("feature.suppress_notloaded", true) - ) - .setSource(new DirectorySource(artifactsPath)) - .build(); + public static List perform(Source source, List testPaths) throws IOException { + Tester tester = new Tester(source); + + for (Path path : testPaths) + tester.perform(path); + + return tester.finish(); } - private Tester(URI artifactsUri) { - validator = ValidatorBuilder - .newValidator() - .setProperties(new SimpleProperties() - .set("feature.nesting", true) - .set("feature.expectation", true) - .set("feature.suppress_notloaded", true) - ) - .setSource(new RepositorySource(artifactsUri)) - .build(); + private Tester(Source source) { + var modules = new ArrayList(); + modules.add(PropertiesModule.with( + Prop.of("feature.nesting", true), + Prop.of("feature.expectation", true), + Prop.of("feature.suppress_notloaded", true) + )); + modules.add(new SourceModule(source)); + + Guice.createInjector(Modules.override(new ValidatorModule()).with(modules)) + .injectMembers(this); } private void perform(Path path) throws IOException { - List files = new ArrayList<>(); - - Files.walkFileTree(path, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (file.endsWith(".xml")) - files.add(file.toFile()); - - return super.visitFile(file, attrs); - } - }); - - Collections.sort(files); - - for (File file : files) - if (!file.getName().equals("buildconfig.xml")) - validate(file); + Files.walk(path) + .filter(p -> p.endsWith(".xml")) + .filter(p -> !"buildconfig.xml".equals(p.toFile().getName())) + .sorted() + .forEach(this::validate); } private List finish() { @@ -103,9 +88,12 @@ private List finish() { return validations; } - private void validate(File file) { + private void validate(Path file) { try { - Validation validation = validator.validate(file); + // Load document + var document = Document.of(file); + + Validation validation = validator.validate(document); validation.getReport().setFilename(file.toString()); if (validation.getDocument().getDeclarations() @@ -148,12 +136,4 @@ public void append(String description, Validation validation, Integer numberInSe log.info("Test '{}'", description); } } - - @Override - public void close() { - if (validator != null) { - validator.close(); - validator = null; - } - } } diff --git a/src/main/java/no/difi/vefa/validator/util/ClasspathURIResolver.java b/src/main/java/no/difi/vefa/validator/util/ClasspathURIResolver.java deleted file mode 100644 index ddaee9f7..00000000 --- a/src/main/java/no/difi/vefa/validator/util/ClasspathURIResolver.java +++ /dev/null @@ -1,22 +0,0 @@ -package no.difi.vefa.validator.util; - -import javax.xml.transform.Source; -import javax.xml.transform.URIResolver; -import javax.xml.transform.stream.StreamSource; - -/** - * @author erlend - */ -public class ClasspathURIResolver implements URIResolver { - - private final String path; - - public ClasspathURIResolver(String path) { - this.path = path; - } - - public Source resolve(String href, String base) { - return !"".equals(base) ? null : new StreamSource( - this.getClass().getResourceAsStream(String.format("%s/%s", this.path, href))); - } -} \ No newline at end of file diff --git a/src/main/java/no/difi/vefa/validator/util/ContentLoader.java b/src/main/java/no/difi/vefa/validator/util/ContentLoader.java new file mode 100644 index 00000000..32ee6825 --- /dev/null +++ b/src/main/java/no/difi/vefa/validator/util/ContentLoader.java @@ -0,0 +1,43 @@ +package no.difi.vefa.validator.util; + +import com.google.inject.Singleton; +import net.sf.saxon.lib.ResourceRequest; +import net.sf.saxon.lib.ResourceResolver; +import net.sf.saxon.trans.XPathException; + +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; +import java.io.InputStream; + +@Singleton +public class ContentLoader implements ResourceResolver { + + @Override + public Source resolve(ResourceRequest request) throws XPathException { + return resolve(request.uri); + } + + public Source resolve(String uri) throws XPathException { + try { + // Loading resource + return new StreamSource(open(uri), uri); + } catch (IOException e) { + // Make sure to not hand over loading to default resolver + throw new XPathException(String.format("Unable to load resource: %s", uri)); + } + } + + public InputStream open(String uri) throws IOException { + // Loading resource from jar files + if (uri.startsWith("res:")) { + var inputStream = getClass().getResourceAsStream(uri.substring(4)); + if (inputStream == null) + throw new IOException(String.format("Resource not found: %s", uri)); + + return inputStream; + } + + throw new IOException(String.format("Unable to load resource: %s", uri)); + } +} diff --git a/src/main/java/no/difi/vefa/validator/util/HolderLSResolveResource.java b/src/main/java/no/difi/vefa/validator/util/HolderLSResolveResource.java index 0e0bc8ac..3aad01ff 100644 --- a/src/main/java/no/difi/vefa/validator/util/HolderLSResolveResource.java +++ b/src/main/java/no/difi/vefa/validator/util/HolderLSResolveResource.java @@ -1,30 +1,23 @@ package no.difi.vefa.validator.util; -import no.difi.vefa.validator.api.ArtifactHolder; +import no.difi.vefa.validator.model.ArtifactHolder; import org.w3c.dom.ls.LSInput; import org.w3c.dom.ls.LSResourceResolver; -import java.nio.file.Path; import java.nio.file.Paths; public class HolderLSResolveResource implements LSResourceResolver { private final ArtifactHolder artifactHolder; - private final Path rootPath; - public HolderLSResolveResource(ArtifactHolder artifactHolder, String rootPath) { + public HolderLSResolveResource(ArtifactHolder artifactHolder) { this.artifactHolder = artifactHolder; - this.rootPath = Paths.get(rootPath).getParent(); } @Override public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { - Path target; - if (baseURI == null) - target = rootPath.resolve(systemId); - else - target = Paths.get(baseURI.substring(7)).getParent().resolve(systemId); + var target = Paths.get(baseURI.substring(7)).getParent().resolve(systemId); String newPath = ("/" + target.toString().replaceAll("\\\\", "/")).replaceAll("/([^/]+?)/\\.\\.", "").substring(1); diff --git a/src/main/java/no/difi/vefa/validator/util/XsltHelper.java b/src/main/java/no/difi/vefa/validator/util/XsltHelper.java new file mode 100644 index 00000000..78e85cd7 --- /dev/null +++ b/src/main/java/no/difi/vefa/validator/util/XsltHelper.java @@ -0,0 +1,28 @@ +package no.difi.vefa.validator.util; + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import net.sf.saxon.s9api.SaxonApiException; +import net.sf.saxon.s9api.XsltCompiler; +import net.sf.saxon.s9api.XsltExecutable; +import no.difi.vefa.validator.lang.ValidatorException; + +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; +import java.io.InputStream; + +@Singleton +public class XsltHelper { + + @Inject + private Provider compilerProvider; + + public XsltExecutable fromResource(String path) throws ValidatorException { + try (InputStream inputStream = getClass().getResourceAsStream(path)) { + return compilerProvider.get().compile(new StreamSource(inputStream, "res:" + path)); + } catch (IOException | SaxonApiException e) { + throw new ValidatorException("Unable to load XSLT from resource."); + } + } +} diff --git a/src/main/java/no/difi/vefa/validator/util/ZipArchiver.java b/src/main/java/no/difi/vefa/validator/util/ZipArchive.java similarity index 97% rename from src/main/java/no/difi/vefa/validator/util/ZipArchiver.java rename to src/main/java/no/difi/vefa/validator/util/ZipArchive.java index 9d7b468b..5fc82696 100644 --- a/src/main/java/no/difi/vefa/validator/util/ZipArchiver.java +++ b/src/main/java/no/difi/vefa/validator/util/ZipArchive.java @@ -16,7 +16,7 @@ /** * @author erlend */ -public interface ZipArchiver { +public interface ZipArchive { static void archive(Path target, Path directory) throws IOException { try (OutputStream outputStream = Files.newOutputStream(target); diff --git a/src/main/xsd/vefa-validator.xsd b/src/main/xsd/vefa-validator.xsd index 3762e182..bd6a8c97 100644 --- a/src/main/xsd/vefa-validator.xsd +++ b/src/main/xsd/vefa-validator.xsd @@ -125,7 +125,6 @@ - @@ -174,7 +173,6 @@ - diff --git a/src/test/java/no/difi/vefa/validator/SimpleTest.java b/src/test/java/no/difi/vefa/validator/SimpleTest.java index 0c024698..b566f33b 100644 --- a/src/test/java/no/difi/vefa/validator/SimpleTest.java +++ b/src/test/java/no/difi/vefa/validator/SimpleTest.java @@ -2,7 +2,7 @@ import lombok.extern.slf4j.Slf4j; import no.difi.vefa.validator.api.Validation; -import no.difi.vefa.validator.properties.SimpleProperties; +import no.difi.vefa.validator.model.Prop; import no.difi.vefa.validator.source.ClasspathSource; import no.difi.xsd.vefa.validator._1.AssertionType; import no.difi.xsd.vefa.validator._1.FlagType; @@ -24,8 +24,7 @@ public class SimpleTest { public void beforeClass() { validator = ValidatorBuilder .newValidator() - .setProperties(new SimpleProperties() - .set("feature.expectation", true)) + .setProperties(Prop.of("feature.expectation", true)) .setSource(new ClasspathSource("/rules/")) .build(); } @@ -78,7 +77,7 @@ public void simpleOk() throws IOException { public void simpleValidatorTest() throws IOException { var validation = validator.validate( getClass().getResourceAsStream("/documents/NOGOV-T10-R014.xml"), - new SimpleProperties().set("feature.nesting", true)); + Prop.of("feature.nesting", true)); assertEquals(validation.getReport().getFlag(), FlagType.OK); assertEquals(validation.getChildren().size(), 3); diff --git a/src/test/java/no/difi/vefa/validator/config/SimplePropertiesTest.java b/src/test/java/no/difi/vefa/validator/config/SimplePropertiesTest.java deleted file mode 100644 index 1fedbbac..00000000 --- a/src/test/java/no/difi/vefa/validator/config/SimplePropertiesTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package no.difi.vefa.validator.config; - -import static org.testng.Assert.*; - -import no.difi.vefa.validator.api.Properties; -import no.difi.vefa.validator.properties.SimpleProperties; -import org.testng.annotations.Test; - -public class SimplePropertiesTest { - - @Test - public void simple() { - Properties properties = new SimpleProperties() - .set("some.string", "Hello World!") - .set("some.number", 1) - .set("some.boolean", true) - .set("some.object", new Long(123)); - - assertTrue(properties.contains("some.string")); - assertTrue(properties.contains("some.number")); - assertTrue(properties.contains("some.boolean")); - assertTrue(properties.contains("some.object")); - - assertFalse(properties.contains("some.other.string")); - assertFalse(properties.contains("some.other.number")); - assertFalse(properties.contains("some.other.boolean")); - assertFalse(properties.contains("some.other.object")); - - assertEquals(properties.getString("some.string"), "Hello World!"); - assertEquals(properties.getInteger("some.number"), 1); - assertEquals(properties.getBoolean("some.boolean"), true); - assertEquals(properties.get("some.object").getClass(), Long.class); - - assertEquals(properties.getString("some.string", "No no!"), "Hello World!"); - assertEquals(properties.getInteger("some.number", 0), 1); - assertEquals(properties.getBoolean("some.boolean", false), true); - assertEquals(properties.get("some.object", null).getClass(), Long.class); - - assertEquals(properties.getString("some.other.string"), null); - assertEquals(properties.getInteger("some.other.number"), 0); - assertEquals(properties.getBoolean("some.other.boolean"), false); - assertEquals(properties.get("some.other.object"), null); - - assertEquals(properties.getString("some.other.string", "No no!"), "No no!"); - assertEquals(properties.getInteger("some.other.number", 0), 0); - assertEquals(properties.getBoolean("some.other.boolean", true), true); - assertEquals(properties.get("some.other.object", null), null); - - assertEquals(properties.getInteger("some.boolean", 100), 100); - } - -} diff --git a/src/test/java/no/difi/vefa/validator/declaration/EspdDeclarationTest.java b/src/test/java/no/difi/vefa/validator/declaration/EspdDeclarationTest.java index 1da29ee0..de1dc7c4 100644 --- a/src/test/java/no/difi/vefa/validator/declaration/EspdDeclarationTest.java +++ b/src/test/java/no/difi/vefa/validator/declaration/EspdDeclarationTest.java @@ -2,9 +2,9 @@ import com.google.inject.Guice; import com.google.inject.Inject; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.module.ValidatorModule; -import no.difi.vefa.validator.util.DeclarationDetector; +import no.difi.vefa.validator.service.DetectorService; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -13,7 +13,7 @@ public class EspdDeclarationTest { @Inject - private DeclarationDetector declarationDetector; + private DetectorService declarationDetector; @BeforeClass public void beforeClass() { diff --git a/src/test/java/no/difi/vefa/validator/declaration/SbdhDeclarationTest.java b/src/test/java/no/difi/vefa/validator/declaration/SbdhDeclarationTest.java index 584543d1..696e8511 100644 --- a/src/test/java/no/difi/vefa/validator/declaration/SbdhDeclarationTest.java +++ b/src/test/java/no/difi/vefa/validator/declaration/SbdhDeclarationTest.java @@ -2,9 +2,9 @@ import com.google.inject.Guice; import com.google.inject.Inject; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.module.ValidatorModule; -import no.difi.vefa.validator.util.DeclarationDetector; +import no.difi.vefa.validator.service.DetectorService; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -13,7 +13,7 @@ public class SbdhDeclarationTest { @Inject - private DeclarationDetector declarationDetector; + private DetectorService declarationDetector; @BeforeClass public void beforeClass() { diff --git a/src/test/java/no/difi/vefa/validator/declaration/UblDeclarationTest.java b/src/test/java/no/difi/vefa/validator/declaration/UblDeclarationTest.java index 60bd88b5..1a696ee4 100644 --- a/src/test/java/no/difi/vefa/validator/declaration/UblDeclarationTest.java +++ b/src/test/java/no/difi/vefa/validator/declaration/UblDeclarationTest.java @@ -2,10 +2,10 @@ import com.google.inject.Guice; import com.google.inject.Inject; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.module.ValidatorModule; -import no.difi.vefa.validator.util.DeclarationDetector; -import no.difi.vefa.validator.util.DeclarationIdentification; +import no.difi.vefa.validator.service.DetectorService; +import no.difi.vefa.validator.model.Detected; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -18,7 +18,7 @@ public class UblDeclarationTest { ""); - assertEquals(declarationDetector.detect(document), DeclarationIdentification.UNKNOWN); + assertEquals(declarationDetector.detect(document), Detected.UNKNOWN); } @Test diff --git a/src/test/java/no/difi/vefa/validator/declaration/UnCefactDeclarationTest.java b/src/test/java/no/difi/vefa/validator/declaration/UnCefactDeclarationTest.java index c2fd7582..00979332 100644 --- a/src/test/java/no/difi/vefa/validator/declaration/UnCefactDeclarationTest.java +++ b/src/test/java/no/difi/vefa/validator/declaration/UnCefactDeclarationTest.java @@ -2,9 +2,9 @@ import com.google.inject.Guice; import com.google.inject.Inject; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.module.ValidatorModule; -import no.difi.vefa.validator.util.DeclarationDetector; +import no.difi.vefa.validator.service.DetectorService; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -12,7 +12,7 @@ public class UnCefactDeclarationTest { @Inject - private DeclarationDetector declarationDetector; + private DetectorService declarationDetector; @BeforeClass public void beforeClass() { diff --git a/src/test/java/no/difi/vefa/validator/api/DocumentTest.java b/src/test/java/no/difi/vefa/validator/model/DocumentTest.java similarity index 86% rename from src/test/java/no/difi/vefa/validator/api/DocumentTest.java rename to src/test/java/no/difi/vefa/validator/model/DocumentTest.java index 0db8066a..4d77a77a 100644 --- a/src/test/java/no/difi/vefa/validator/api/DocumentTest.java +++ b/src/test/java/no/difi/vefa/validator/model/DocumentTest.java @@ -1,5 +1,6 @@ -package no.difi.vefa.validator.api; +package no.difi.vefa.validator.model; +import no.difi.vefa.validator.model.Document; import org.testng.Assert; import org.testng.annotations.Test; diff --git a/src/test/java/no/difi/vefa/validator/util/DeclarationDetectorTest.java b/src/test/java/no/difi/vefa/validator/service/DetectorServiceTest.java similarity index 76% rename from src/test/java/no/difi/vefa/validator/util/DeclarationDetectorTest.java rename to src/test/java/no/difi/vefa/validator/service/DetectorServiceTest.java index 2cdee013..c776c5f2 100644 --- a/src/test/java/no/difi/vefa/validator/util/DeclarationDetectorTest.java +++ b/src/test/java/no/difi/vefa/validator/service/DetectorServiceTest.java @@ -1,21 +1,17 @@ -package no.difi.vefa.validator.util; +package no.difi.vefa.validator.service; -import com.google.common.io.ByteStreams; import com.google.inject.Guice; import com.google.inject.Inject; -import no.difi.vefa.validator.api.Document; +import no.difi.vefa.validator.model.Document; import no.difi.vefa.validator.module.ValidatorModule; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -public class DeclarationDetectorTest { +public class DetectorServiceTest { @Inject - private DeclarationDetector declarationDetector; + private DetectorService declarationDetector; @BeforeClass public void beforeClass() { diff --git a/src/test/java/no/difi/vefa/validator/service/TestingServiceTest.java b/src/test/java/no/difi/vefa/validator/service/TestingServiceTest.java new file mode 100644 index 00000000..bb34eaba --- /dev/null +++ b/src/test/java/no/difi/vefa/validator/service/TestingServiceTest.java @@ -0,0 +1,37 @@ +package no.difi.vefa.validator.service; + +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.util.Modules; +import no.difi.vefa.validator.model.Document; +import no.difi.vefa.validator.module.SourceModule; +import no.difi.vefa.validator.module.ValidatorModule; +import no.difi.vefa.validator.source.ClasspathSource; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class TestingServiceTest { + + @Inject + private TestingService testingService; + + @BeforeClass + public void beforeClass() { + var module = Modules.override(new ValidatorModule()).with(new SourceModule(new ClasspathSource("/rules/"))); + Guice.createInjector(module).injectMembers(this); + } + + @Test + public void testSetTest() throws Exception { + var document = Document.ofResource("/documents/NOGOV-T10-R014.xml"); + + var validations = testingService.verify(document); + } + + // @Test + public void genericTest() throws Exception { + var document = Document.ofResource("/documents/T10-hode-feilkoder.xml"); + + var validations = testingService.verify(document); + } +} diff --git a/src/test/java/no/difi/vefa/validator/source/DirectorySourceTest.java b/src/test/java/no/difi/vefa/validator/source/DirectorySourceTest.java index 67d9e8d1..2b4be07d 100644 --- a/src/test/java/no/difi/vefa/validator/source/DirectorySourceTest.java +++ b/src/test/java/no/difi/vefa/validator/source/DirectorySourceTest.java @@ -1,8 +1,6 @@ package no.difi.vefa.validator.source; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.lang.ValidatorException; -import org.mockito.Mockito; import org.testng.annotations.Test; public class DirectorySourceTest { @@ -10,6 +8,6 @@ public class DirectorySourceTest { @Test(expectedExceptions = ValidatorException.class) public void triggerException() throws ValidatorException { DirectorySource source = new DirectorySource(null); - source.createInstance(Mockito.mock(Properties.class)); + source.createInstance(); } } diff --git a/src/test/java/no/difi/vefa/validator/source/RepositorySourceTest.java b/src/test/java/no/difi/vefa/validator/source/RepositorySourceTest.java index 85a846b2..bc47e1a9 100644 --- a/src/test/java/no/difi/vefa/validator/source/RepositorySourceTest.java +++ b/src/test/java/no/difi/vefa/validator/source/RepositorySourceTest.java @@ -1,8 +1,6 @@ package no.difi.vefa.validator.source; -import no.difi.vefa.validator.api.Properties; import no.difi.vefa.validator.lang.ValidatorException; -import org.mockito.Mockito; import org.testng.Assert; import org.testng.annotations.Test; @@ -20,6 +18,6 @@ public void simple() { @Test(expectedExceptions = ValidatorException.class) public void triggerException() throws ValidatorException { RepositorySource source = new RepositorySource((URI) null); - source.createInstance(Mockito.mock(Properties.class)); + source.createInstance(); } } diff --git a/src/test/resources/rules/artifacts.xml b/src/test/resources/rules/artifacts.xml index d9dae604..9c61d188 100644 --- a/src/test/resources/rules/artifacts.xml +++ b/src/test/resources/rules/artifacts.xml @@ -1,7 +1,6 @@ - diff --git a/src/test/resources/rules/ehf-stylesheet-1c573960978491dff855ae35ec28b582de2405e2.asice b/src/test/resources/rules/ehf-stylesheet-1c573960978491dff855ae35ec28b582de2405e2.asice deleted file mode 100644 index ecdf93d9..00000000 Binary files a/src/test/resources/rules/ehf-stylesheet-1c573960978491dff855ae35ec28b582de2405e2.asice and /dev/null differ