mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-26 01:05:38 +00:00
Verzeichnisstruktur erweitert, um Prüftool und Konfiguration XRechnung aufzunehmen.
This commit is contained in:
parent
0fd87014b4
commit
4eab984c01
161 changed files with 106853 additions and 11 deletions
|
|
@ -1,132 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
|
||||
* one or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. KoSIT licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package de.kosit.validationtool.cmd;
|
||||
|
||||
import de.kosit.validationtool.cmd.assertions.AssertionType;
|
||||
import de.kosit.validationtool.cmd.assertions.Assertions;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.saxon.s9api.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Überprüft den Report mittels bereitgestellter Assertions. Diese {@link CheckAction} dient der Überprüfung der von der
|
||||
* KoSIT bereitgestellten Prüfszenarien und den darin enthaltenen Artefakten.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class CheckAssertionAction implements CheckAction {
|
||||
|
||||
private final Assertions assertions;
|
||||
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
private final Processor processor;
|
||||
|
||||
private Map<String, List<AssertionType>> mappedAssertions;
|
||||
|
||||
private static boolean matches(String key, String name) {
|
||||
return key.startsWith(name) || (name + ".xml").endsWith(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void check(Bag results) {
|
||||
log.info("Checking assertions for {}", results.getInput().getName());
|
||||
final List<AssertionType> toCheck = findAssertions(results.getName());
|
||||
final List<String> errors = new ArrayList<>();
|
||||
if (toCheck != null && !toCheck.isEmpty()) {
|
||||
final XdmNode node = loadDocument(results.getReport());
|
||||
toCheck.forEach(a -> {
|
||||
if (!check(node, a)) {
|
||||
log.error("Assertion mismatch: {}", a.getValue());
|
||||
errors.add(a.getValue());
|
||||
}
|
||||
});
|
||||
if (errors.isEmpty()) {
|
||||
log.info("{} assertions successfully verified for {}", toCheck.size(), results.getName());
|
||||
} else {
|
||||
log.warn("{} assertion of {} failed while checking {}", errors.size(), toCheck.size(), results.getName());
|
||||
}
|
||||
results.setAssertionResult(new Result<>(toCheck.size(), errors));
|
||||
} else {
|
||||
log.warn("Can not find assertions for {}", results.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private List<AssertionType> findAssertions(String name) {
|
||||
return getMapped().entrySet().stream().filter(e -> matches(e.getKey(), name)).map(Map.Entry::getValue).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private XdmNode loadDocument(Document d) {
|
||||
DocumentBuilder documentBuilder = getProcessor().newDocumentBuilder();
|
||||
try {
|
||||
return documentBuilder.build(new DOMSource(d));
|
||||
} catch (SaxonApiException e) {
|
||||
log.error("Can not load result document. Therefore can not run defined assertions", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean check(XdmNode document, AssertionType assertion) {
|
||||
try {
|
||||
final XPathSelector selector = createSelector(assertion);
|
||||
selector.setContextItem(document);
|
||||
return selector.effectiveBooleanValue();
|
||||
} catch (SaxonApiException e) {
|
||||
log.error("Error evaluating assertion {} for {}", assertion.getTest(), assertion.getReportDoc(), e);
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
private XPathSelector createSelector(AssertionType assertion) throws SaxonApiException {
|
||||
try {
|
||||
final XPathCompiler compiler = getProcessor().newXPathCompiler();
|
||||
assertions.getNamespace().forEach(ns -> compiler.declareNamespace(ns.getPrefix(), ns.getValue()));
|
||||
return compiler.compile(assertion.getTest()).load();
|
||||
} catch (SaxonApiException e) {
|
||||
throw new IllegalStateException(String.format("Can not compile xpath match expression '%s'",
|
||||
StringUtils.isNotBlank(assertion.getTest()) ? assertion.getTest() : "EMPTY EXPRESSION"), e);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, List<AssertionType>> getMapped() {
|
||||
if (mappedAssertions == null) {
|
||||
mappedAssertions = new HashMap<>();
|
||||
for (AssertionType assertionType : assertions.getAssertion()) {
|
||||
List<AssertionType> list = mappedAssertions.computeIfAbsent(assertionType.getReportDoc(), k -> new ArrayList<>());
|
||||
list.add(assertionType);
|
||||
}
|
||||
}
|
||||
return mappedAssertions;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,290 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
|
||||
* one or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. KoSIT licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package de.kosit.validationtool.cmd;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.cli.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import de.kosit.validationtool.api.CheckConfiguration;
|
||||
import de.kosit.validationtool.api.Input;
|
||||
import de.kosit.validationtool.api.InputFactory;
|
||||
import de.kosit.validationtool.cmd.assertions.Assertions;
|
||||
import de.kosit.validationtool.impl.ConversionService;
|
||||
import de.kosit.validationtool.impl.ObjectFactory;
|
||||
|
||||
/**
|
||||
* Commandline Version des Prüftools. Parsed die Kommandozeile und führt die konfigurierten Aktionen aus.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
@Slf4j
|
||||
public class CommandLineApplication {
|
||||
|
||||
private static final Option HELP = Option.builder("?").longOpt("help").argName("Help").desc("Displays this help").build();
|
||||
|
||||
private static final Option SCENARIOS = Option.builder("s").required().longOpt("scenarios").hasArg()
|
||||
.desc("Location of scenarios.xml e.g.").build();
|
||||
|
||||
private static final Option REPOSITORY = Option.builder("r").longOpt("repository").hasArg()
|
||||
.desc("Directory containing scenario content").build();
|
||||
|
||||
private static final Option PRINT = Option.builder("p").longOpt("print").desc("Prints the check result to stdout").build();
|
||||
|
||||
private static final Option OUTPUT = Option.builder("o").longOpt("output-directory")
|
||||
.desc("Defines the out directory for results. Defaults to cwd").hasArg().build();
|
||||
|
||||
private static final Option EXTRACT_HTML = Option.builder("h").longOpt("html")
|
||||
.desc("Extract and save any html content within result as a separate file ").build();
|
||||
|
||||
private static final Option DEBUG = Option.builder("d").longOpt("debug").desc("Prints some more debug information").build();
|
||||
|
||||
private static final Option CHECK_ASSERTIONS = Option.builder("c").longOpt("check-assertions").hasArg()
|
||||
.desc("Check the result using defined assertions").argName("assertions-file").build();
|
||||
|
||||
private CommandLineApplication() {
|
||||
// main class -> hide constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Main-Funktion für die Kommandozeilen-Applikation.
|
||||
*
|
||||
* @param args die Eingabe-Argumente
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
final int resultStatus = mainProgram(args);
|
||||
System.exit(resultStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hauptprogramm für die Kommandozeilen-Applikation.
|
||||
*
|
||||
* @param args die Eingabe-Argumente
|
||||
*/
|
||||
static int mainProgram(String[] args) {
|
||||
Options options = createOptions();
|
||||
if (isHelpRequested(args)) {
|
||||
printHelp(options);
|
||||
} else {
|
||||
try {
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
final CommandLine cmd = parser.parse(options, args);
|
||||
if (cmd.getArgList().isEmpty()) {
|
||||
printHelp(createOptions());
|
||||
} else {
|
||||
return processActions(cmd);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
log.error("Error processing command line arguments: " + e.getMessage());
|
||||
printHelp(options);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static boolean isHelpRequested(String[] args) {
|
||||
Options helpOptions = createHelpOptions();
|
||||
try {
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
CommandLine cmd = parser.parse(helpOptions, args, true);
|
||||
if (cmd.hasOption(HELP.getOpt()) || args.length == 0) {
|
||||
return true;
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
// we can ignore that, we just look for the help parameters
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int processActions(CommandLine cmd) {
|
||||
try {
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
CheckConfiguration d = new CheckConfiguration(determineDefinition(cmd));
|
||||
d.setScenarioRepository(determineRepository(cmd));
|
||||
InternalCheck check = new InternalCheck(d);
|
||||
Path outputDirectory = determineOutputDirectory(cmd);
|
||||
|
||||
if (cmd.hasOption(EXTRACT_HTML.getOpt())) {
|
||||
check.getCheckSteps().add(new ExtractHtmlContentAction(check.getContentRepository(), outputDirectory));
|
||||
}
|
||||
check.getCheckSteps().add(new SerializeReportAction(outputDirectory));
|
||||
if (cmd.hasOption(PRINT.getOpt())) {
|
||||
check.getCheckSteps().add(new PrintReportAction());
|
||||
}
|
||||
if (cmd.hasOption(CHECK_ASSERTIONS.getOpt())) {
|
||||
Assertions assertions = loadAssertions(cmd.getOptionValue(CHECK_ASSERTIONS.getOpt()));
|
||||
check.getCheckSteps().add(new CheckAssertionAction(assertions, ObjectFactory.createProcessor()));
|
||||
}
|
||||
|
||||
log.info("Setup completed in {}ms\n", System.currentTimeMillis() - start);
|
||||
|
||||
final Collection<Path> targets = determineTestTargets(cmd);
|
||||
start = System.currentTimeMillis();
|
||||
final List<Input> input = targets.stream().map(InputFactory::read).collect(Collectors.toList());
|
||||
boolean result = check.checkInput(input);
|
||||
log.info("Processing {} object(s) completed in {}ms", input.size(), System.currentTimeMillis() - start);
|
||||
return result ? 0 : 1;
|
||||
|
||||
} catch (Exception e) {
|
||||
if (cmd.hasOption(DEBUG.getOpt())) {
|
||||
log.error(e.getMessage(), e);
|
||||
} else {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private static Assertions loadAssertions(String optionValue) {
|
||||
Path p = Paths.get(optionValue);
|
||||
Assertions a = null;
|
||||
if (Files.exists(p)) {
|
||||
ConversionService c = new ConversionService();
|
||||
c.initialize(de.kosit.validationtool.cmd.assertions.ObjectFactory.class.getPackage());
|
||||
a = c.readXml(p.toUri(), Assertions.class);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
private static Path determineOutputDirectory(CommandLine cmd) {
|
||||
final String value = cmd.getOptionValue(OUTPUT.getOpt());
|
||||
Path fir;
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
fir = Paths.get(value);
|
||||
if ((!Files.exists(fir) && !fir.toFile().mkdirs()) || !Files.isDirectory(fir)) {
|
||||
throw new IllegalStateException(String.format("Invalid target directory %s specified", value));
|
||||
}
|
||||
} else {
|
||||
fir = Paths.get(""/* cwd */);
|
||||
}
|
||||
return fir;
|
||||
}
|
||||
|
||||
private static Collection<Path> determineTestTargets(CommandLine cmd) {
|
||||
Collection<Path> targets = new ArrayList<>();
|
||||
if (!cmd.getArgList().isEmpty()) {
|
||||
cmd.getArgList().forEach(e -> targets.addAll(determineTestTarget(e)));
|
||||
}
|
||||
if (targets.isEmpty()) {
|
||||
throw new IllegalStateException("No test targets found. Nothing to check. Will quit now!");
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
private static Collection<Path> determineTestTarget(String s) {
|
||||
Path d = Paths.get(s);
|
||||
if (Files.isDirectory(d)) {
|
||||
return listDirectoryTargets(d);
|
||||
} else if (Files.exists(d)) {
|
||||
return Collections.singleton(d);
|
||||
}
|
||||
log.warn("The specified test target {} does not exist. Will be ignored", s);
|
||||
return Collections.emptyList();
|
||||
|
||||
}
|
||||
|
||||
private static Collection<Path> listDirectoryTargets(Path d) {
|
||||
try {
|
||||
return Files.list(d).filter(path -> path.toString().endsWith(".xml")).collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("IOException while liste directory content. Can not determine test targets.", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static URI determineRepository(CommandLine cmd) throws MalformedURLException {
|
||||
if (checkOptionWithValue(REPOSITORY, cmd)) {
|
||||
Path d = Paths.get(cmd.getOptionValue(REPOSITORY.getOpt()));
|
||||
if (Files.isDirectory(d)) {
|
||||
return d.toUri();
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Not a valid path for scenario definition specified: '%s'", d.toAbsolutePath()));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static URI determineDefinition(CommandLine cmd) throws MalformedURLException {
|
||||
checkOptionWithValue(SCENARIOS, cmd);
|
||||
Path f = Paths.get(cmd.getOptionValue(SCENARIOS.getOpt()));
|
||||
if (Files.isRegularFile(f)) {
|
||||
return f.toAbsolutePath().toUri();
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Not a valid path for scenario definition specified: '%s'", f.toAbsolutePath()));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean checkOptionWithValue(Option option, CommandLine cmd) {
|
||||
String opt = option.getOpt();
|
||||
if (cmd.hasOption(opt)) {
|
||||
String value = cmd.getOptionValue(opt);
|
||||
if (StringUtils.isNoneBlank(value)) {
|
||||
return true;
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format("Option value required for Option '%s'", option.getLongOpt()));
|
||||
}
|
||||
} else if (option.isRequired()) {
|
||||
|
||||
throw new IllegalArgumentException(String.format("Option '%s' required ", option.getLongOpt()));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void printHelp(Options options) {
|
||||
// automatically generate the help statement
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
formatter.printHelp("check-tool -s <scenario-config-file> [OPTIONS] [FILE]... ", options, false);
|
||||
}
|
||||
|
||||
private static Options createHelpOptions() {
|
||||
Options options = new Options();
|
||||
options.addOption(HELP);
|
||||
return options;
|
||||
}
|
||||
|
||||
private static Options createOptions() {
|
||||
Options options = new Options();
|
||||
options.addOption(HELP);
|
||||
options.addOption(SCENARIOS);
|
||||
options.addOption(REPOSITORY);
|
||||
options.addOption(PRINT);
|
||||
options.addOption(OUTPUT);
|
||||
options.addOption(EXTRACT_HTML);
|
||||
options.addOption(DEBUG);
|
||||
options.addOption(CHECK_ASSERTIONS);
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
|
||||
* one or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. KoSIT licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package de.kosit.validationtool.cmd;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import de.kosit.validationtool.impl.ContentRepository;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction;
|
||||
|
||||
import net.sf.saxon.s9api.*;
|
||||
|
||||
/**
|
||||
* Extrahiert HTML-Dokumente aus dem Report und persistiert diese im konfigurierten Ausgabe-Verzeichnis.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class ExtractHtmlContentAction implements CheckAction {
|
||||
|
||||
private static final QName NAME_ATTRIBUTE = new QName("data-report-type");
|
||||
|
||||
private final ContentRepository repository;
|
||||
|
||||
private final Path outputDirectory;
|
||||
|
||||
private XPathExecutable executable;
|
||||
|
||||
@Override
|
||||
public void check(Bag results) {
|
||||
try {
|
||||
final XPathSelector selector = getSelector();
|
||||
DocumentBuilder documentBuilder = repository.getProcessor().newDocumentBuilder();
|
||||
|
||||
final XdmNode xdmSource = documentBuilder.build(new DOMSource(results.getReport()));
|
||||
selector.setContextItem(xdmSource);
|
||||
selector.forEach(m -> print(results.getName(), m));
|
||||
|
||||
} catch (SaxonApiException e) {
|
||||
throw new IllegalStateException("Can not extract html content", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void print(String origName, XdmItem xdmItem) {
|
||||
XdmNode node = (XdmNode) xdmItem;
|
||||
final String name = origName + "-" + node.getAttributeValue(NAME_ATTRIBUTE);
|
||||
final Path file = outputDirectory.resolve(name + ".html");
|
||||
final Serializer serializer = repository.getProcessor().newSerializer(file.toFile());
|
||||
try {
|
||||
log.info("Writing report html '{}' to {}", name, file.toAbsolutePath());
|
||||
serializer.serializeNode(node);
|
||||
} catch (SaxonApiException e) {
|
||||
log.info("Error extracting html content to {}", file.toAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private XPathSelector getSelector() {
|
||||
if (executable == null) {
|
||||
Map<String, String> ns = new HashMap<>();
|
||||
ns.put("html", "http://www.w3.org/1999/xhtml");
|
||||
executable = repository.createXPath("//html:html", ns);
|
||||
}
|
||||
return executable.load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSkipped(Bag results) {
|
||||
if (results.getReport() == null) {
|
||||
log.warn("Can not extract html content. No report document found");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,83 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
|
||||
* one or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. KoSIT licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package de.kosit.validationtool.cmd;
|
||||
|
||||
import de.kosit.validationtool.api.CheckConfiguration;
|
||||
import de.kosit.validationtool.api.Input;
|
||||
import de.kosit.validationtool.impl.DefaultCheck;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Simple Erweiterung der Klasse {@link DefaultCheck} um das Ergebnis der Assertion-Prüfung auszwerten und auszugeben.
|
||||
* Diese Klasse stellt keine fachlicher Erweiterung des eigentlichen Prüfvorganges dar!
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
@Slf4j
|
||||
class InternalCheck extends DefaultCheck {
|
||||
|
||||
/**
|
||||
* Erzeugt eine neue Instanz mit der angegebenen Konfiguration.
|
||||
*
|
||||
* @param configuration die Konfiguration
|
||||
*/
|
||||
public InternalCheck(CheckConfiguration configuration) {
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft die Prüflinge und gibt Informationen über etwaige Assertions aus.
|
||||
*
|
||||
* @param input die Prüflinge
|
||||
* @return false wenn es Assertion-Fehler gibt, sonst true
|
||||
*/
|
||||
public boolean checkInput(List<Input> input) {
|
||||
List<CheckAction.Bag> results = new ArrayList<>();
|
||||
input.forEach(i -> {
|
||||
CheckAction.Bag bag = new CheckAction.Bag(i, createReport());
|
||||
runCheckInternal(bag);
|
||||
results.add(bag);
|
||||
});
|
||||
|
||||
return printAndEvaluate(results);
|
||||
|
||||
}
|
||||
|
||||
private boolean printAndEvaluate(List<CheckAction.Bag> results) {
|
||||
final List<Result<Integer, String>> asserts = results.stream().filter(r -> r.getAssertionResult() != null)
|
||||
.map(CheckAction.Bag::getAssertionResult).collect(Collectors.toList());
|
||||
int checkAssertions = asserts.stream().mapToInt(e -> e.getObject()).sum();
|
||||
int failedAssertions = asserts.stream().mapToInt(e -> e.getErrors().size()).sum();
|
||||
|
||||
if (failedAssertions > 0) {
|
||||
log.error("Assertion check failed.\n\nAssertions run: {}, Assertions failed: {}\n", checkAssertions, failedAssertions);
|
||||
} else if (checkAssertions > 0) {
|
||||
log.info("Assertion check successful.\n\nAssertions run: {}, Assertions failed: {}\n", checkAssertions, failedAssertions);
|
||||
}
|
||||
return failedAssertions == 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
|
||||
* one or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. KoSIT licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package de.kosit.validationtool.cmd;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import de.kosit.validationtool.impl.ObjectFactory;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction;
|
||||
|
||||
/**
|
||||
* Gibt das Ergebnis-Document auf std-out aus.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
@Slf4j
|
||||
public class PrintReportAction implements CheckAction {
|
||||
|
||||
@Override
|
||||
public void check(Bag results) {
|
||||
try {
|
||||
Transformer transformer = ObjectFactory.createTransformer(true);
|
||||
final StringWriter writer = new StringWriter();
|
||||
Result output = new StreamResult(writer);
|
||||
Source input = new DOMSource(results.getReport());
|
||||
transformer.transform(input, output);
|
||||
System.out.print(writer.toString());
|
||||
} catch (TransformerException e) {
|
||||
log.error("Error while printing result to stdout", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Koordinierungsstelle für IT-Standards (KoSIT) under
|
||||
* one or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. KoSIT licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package de.kosit.validationtool.cmd;
|
||||
|
||||
import de.kosit.validationtool.impl.ObjectFactory;
|
||||
import de.kosit.validationtool.impl.tasks.CheckAction;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Schreibt das Prüfergebnis als XML-Dokument an eine definierte Stelle.
|
||||
*
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class SerializeReportAction implements CheckAction {
|
||||
|
||||
private final Path outputDirectory;
|
||||
|
||||
@Override
|
||||
public void check(Bag results) {
|
||||
final Path file = outputDirectory.resolve(results.getName() + "-report.xml");
|
||||
try {
|
||||
log.info("Serializing result to {}", file.toAbsolutePath());
|
||||
Transformer transformer = ObjectFactory.createTransformer(true);
|
||||
Result output = new StreamResult(file.toFile());
|
||||
Source input = new DOMSource(results.getReport());
|
||||
transformer.transform(input, output);
|
||||
} catch (TransformerException e) {
|
||||
log.error("Can not serialize result report to {}", file.toAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSkipped(Bag results) {
|
||||
if (results.getReport() == null) {
|
||||
log.warn("Can not serialize result report. No document found");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue