Resolve "CLI-Switch to serialize the 'CreateReportInput' as a file"

This commit is contained in:
Andreas Penski 2019-06-21 14:58:08 +02:00
parent 3bc715ae85
commit b10a5da352
5 changed files with 136 additions and 17 deletions

View file

@ -28,7 +28,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
@ -46,7 +45,6 @@ import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.api.CheckConfiguration; import de.kosit.validationtool.api.CheckConfiguration;
import de.kosit.validationtool.api.Input; import de.kosit.validationtool.api.Input;
import de.kosit.validationtool.api.InputFactory; import de.kosit.validationtool.api.InputFactory;
import de.kosit.validationtool.api.Result;
import de.kosit.validationtool.cmd.assertions.Assertions; import de.kosit.validationtool.cmd.assertions.Assertions;
import de.kosit.validationtool.impl.ConversionService; import de.kosit.validationtool.impl.ConversionService;
import de.kosit.validationtool.impl.ObjectFactory; import de.kosit.validationtool.impl.ObjectFactory;
@ -59,15 +57,14 @@ import de.kosit.validationtool.impl.ObjectFactory;
@Slf4j @Slf4j
public class CommandLineApplication { public class CommandLineApplication {
private static final Option HELP = Option.builder("?").longOpt("help").argName("Help").desc("Displays this help").build(); 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() private static final Option SCENARIOS = Option.builder("s").required().longOpt("scenarios").hasArg()
.desc("Location of scenarios.xml e.g.").build(); .desc("Location of scenarios.xml e.g.").build();
private static final Option REPOSITORY = Option.builder("r").longOpt("repository").hasArg() private static final Option REPOSITORY = Option.builder("r").longOpt("repository").hasArg()
.desc("Directory containing scenario content").build(); .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 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") private static final Option OUTPUT = Option.builder("o").longOpt("output-directory")
@ -78,20 +75,26 @@ public class CommandLineApplication {
private static final Option DEBUG = Option.builder("d").longOpt("debug").desc("Prints some more debug information").build(); private static final Option DEBUG = Option.builder("d").longOpt("debug").desc("Prints some more debug information").build();
private static final Option SERIALIZE_REPORT_INPUT = Option.builder("c").longOpt("serialize-report-input")
.desc("Serializes the report input to the cwd").build();
private static final Option CHECK_ASSERTIONS = Option.builder("c").longOpt("check-assertions").hasArg() private static final Option CHECK_ASSERTIONS = Option.builder("c").longOpt("check-assertions").hasArg()
.desc("Check the result using defined assertions").argName("assertions-file").build(); .desc("Check the result using defined assertions").argName("assertions-file").build();
private static final Option SERVER = Option.builder("D").longOpt("daemon").desc("Starts a daemon listing for validation requests").build();
private static final Option SERVER = Option.builder("D").longOpt("daemon").desc("Starts a daemon listing for validation requests")
.build();
private static final Option HOST = Option.builder("H").longOpt("host").hasArg() private static final Option HOST = Option.builder("H").longOpt("host").hasArg()
.desc("The hostname / IP address to bind the daemon. Default is localhost").build(); .desc("The hostname / IP address to bind the daemon. Default is localhost").build();
private static final Option PORT = Option.builder("P").longOpt("port").hasArg() private static final Option PORT = Option.builder("P").longOpt("port").hasArg().desc("The port to bind the daemon. Default is 8080")
.desc("The port to bind the daemon. Default is 8080").build(); .build();
private static final Option WORKER_COUNT = Option.builder("T").longOpt("threads").hasArg()
.desc("Number of threads processing validation requests").build();
private static final Option WORKER_COUNT = Option.builder("T").longOpt("threads").hasArg().desc("Number of threads processing validation requests").build();
public static final int DAEMON_SIGNAL = 100; public static final int DAEMON_SIGNAL = 100;
private static final Option PRINT_MEM_STATS = Option.builder("m").longOpt("memory-stats").desc("Prints some memory stats").build(); private static final Option PRINT_MEM_STATS = Option.builder("m").longOpt("memory-stats").desc("Prints some memory stats").build();
private CommandLineApplication() { private CommandLineApplication() {
@ -110,7 +113,6 @@ public class CommandLineApplication {
} }
} }
/** /**
* Hauptprogramm für die Kommandozeilen-Applikation. * Hauptprogramm für die Kommandozeilen-Applikation.
* *
@ -167,19 +169,20 @@ public class CommandLineApplication {
private static int startDaemonMode(final CommandLine cmd) { private static int startDaemonMode(final CommandLine cmd) {
final Option[] unavailable = new Option[] { PRINT, CHECK_ASSERTIONS, DEBUG, OUTPUT, EXTRACT_HTML }; final Option[] unavailable = new Option[] { PRINT, CHECK_ASSERTIONS, DEBUG, OUTPUT, EXTRACT_HTML };
warnUnusedOptions(cmd, unavailable, true); warnUnusedOptions(cmd, unavailable, true);
final Daemon validDaemon = new Daemon(determineDefinition(cmd), determineRepository(cmd), determineHost(cmd), determinePort(cmd), determineThreads(cmd)); final Daemon validDaemon = new Daemon(determineDefinition(cmd), determineRepository(cmd), determineHost(cmd), determinePort(cmd),
determineThreads(cmd));
validDaemon.startServer(); validDaemon.startServer();
return DAEMON_SIGNAL; return DAEMON_SIGNAL;
} }
private static void warnUnusedOptions(final CommandLine cmd, final Option[] unavailable, final boolean daemon) { private static void warnUnusedOptions(final CommandLine cmd, final Option[] unavailable, final boolean daemon) {
Arrays.stream(cmd.getOptions()).filter(o -> ArrayUtils.contains(unavailable, o)).map(o -> "The option " + o.getLongOpt() + " is not available in daemon mode").forEach(log::error); Arrays.stream(cmd.getOptions()).filter(o -> ArrayUtils.contains(unavailable, o))
.map(o -> "The option " + o.getLongOpt() + " is not available in daemon mode").forEach(log::error);
if (daemon && !cmd.getArgList().isEmpty()) { if (daemon && !cmd.getArgList().isEmpty()) {
log.info("Ignoring test targets in daemon mode"); log.info("Ignoring test targets in daemon mode");
} }
} }
private static boolean isHelpRequested(final String[] args) { private static boolean isHelpRequested(final String[] args) {
final Options helpOptions = createHelpOptions(); final Options helpOptions = createHelpOptions();
try { try {
@ -209,6 +212,9 @@ public class CommandLineApplication {
check.getCheckSteps().add(new ExtractHtmlContentAction(check.getContentRepository(), outputDirectory)); check.getCheckSteps().add(new ExtractHtmlContentAction(check.getContentRepository(), outputDirectory));
} }
check.getCheckSteps().add(new SerializeReportAction(outputDirectory)); check.getCheckSteps().add(new SerializeReportAction(outputDirectory));
if (cmd.hasOption(SERIALIZE_REPORT_INPUT.getOpt())) {
check.getCheckSteps().add(new SerializeReportInputAction(outputDirectory, check.getConversionService()));
}
if (cmd.hasOption(PRINT.getOpt())) { if (cmd.hasOption(PRINT.getOpt())) {
check.getCheckSteps().add(new PrintReportAction()); check.getCheckSteps().add(new PrintReportAction());
} }
@ -224,11 +230,10 @@ public class CommandLineApplication {
log.info("Setup completed in {}ms\n", System.currentTimeMillis() - start); log.info("Setup completed in {}ms\n", System.currentTimeMillis() - start);
final Collection<Path> targets = determineTestTargets(cmd); final Collection<Path> targets = determineTestTargets(cmd);
final List<Result> results = new ArrayList<>();
start = System.currentTimeMillis(); start = System.currentTimeMillis();
for (final Path p : targets) { for (final Path p : targets) {
final Input input = InputFactory.read(p); final Input input = InputFactory.read(p);
results.add(check.checkInput(input)); check.checkInput(input);
} }
final boolean result = check.printAndEvaluate(); final boolean result = check.printAndEvaluate();
log.info("Processing {} object(s) completed in {}ms", targets.size(), System.currentTimeMillis() - start); log.info("Processing {} object(s) completed in {}ms", targets.size(), System.currentTimeMillis() - start);

View file

@ -0,0 +1,46 @@
package de.kosit.validationtool.cmd;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import de.kosit.validationtool.impl.ConversionService;
import de.kosit.validationtool.impl.tasks.CheckAction;
/**
* Serializes the {@link de.kosit.validationtool.model.reportInput.CreateReportInput report input} document.
*
* @author Andreas Penski
*/
@RequiredArgsConstructor
@Slf4j
public class SerializeReportInputAction implements CheckAction {
private final Path outputDirectory;
private final ConversionService conversionService;
@Override
public void check(final Bag results) {
final Path file = this.outputDirectory.resolve(results.getName() + "-reportInput.xml");
try {
log.info("Serializing result to {}", file.toAbsolutePath());
final String xml = this.conversionService.writeXml(results.getReportInput());
Files.write(file, xml.getBytes());
} catch (final IOException e) {
log.error("Can not serialize result report to {}", file.toAbsolutePath(), e);
}
}
@Override
public boolean isSkipped(final Bag results) {
if (results.getReportInput() == null) {
log.warn("Can not serialize report input. No object found");
return true;
}
return false;
}
}

View file

@ -68,6 +68,7 @@ public class DefaultCheck implements Check {
@Getter @Getter
private final ContentRepository contentRepository; private final ContentRepository contentRepository;
@Getter
private final ConversionService conversionService; private final ConversionService conversionService;
@Getter @Getter

View file

@ -22,6 +22,7 @@ package de.kosit.validationtool.impl.tasks;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -53,6 +54,7 @@ public interface CheckAction {
private Result<ScenarioType, String> scenarioSelectionResult; private Result<ScenarioType, String> scenarioSelectionResult;
@Setter(AccessLevel.NONE)
private CreateReportInput reportInput; private CreateReportInput reportInput;
/** Das finale Ergebnis */ /** Das finale Ergebnis */

View file

@ -0,0 +1,65 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import de.kosit.validationtool.api.InputFactory;
import de.kosit.validationtool.impl.ConversionService;
import de.kosit.validationtool.impl.tasks.CheckAction;
/**
* @author Andreas Penski
*/
public class SerializeReportInputActionTest {
private Path tmpDirectory;
private SerializeReportInputAction action;
@Before
public void setup() throws IOException {
this.tmpDirectory = Files.createTempDirectory("validator");
this.action = new SerializeReportInputAction(this.tmpDirectory, new ConversionService());
}
@After
public void tearDown() throws IOException {
FileUtils.deleteDirectory(this.tmpDirectory.toFile());
}
@Test
public void testSimpleSerialize() {
final CheckAction.Bag b = new CheckAction.Bag(InputFactory.read("someBytes".getBytes(), "someDoc"));
assertThat(this.action.isSkipped(b)).isFalse();
this.action.check(b);
assertThat(this.tmpDirectory.toFile().listFiles()).hasSize(1);
}
}