Initial Implementation

This commit is contained in:
apenski 2017-10-26 09:50:58 +02:00
parent 2950785e25
commit beeb104007
98 changed files with 29308 additions and 0 deletions

View file

@ -0,0 +1,77 @@
/*
* 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.net.URISyntaxException;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import de.kosit.validationtool.api.InputFactory;
import de.kosit.validationtool.cmd.assertions.Assertions;
import de.kosit.validationtool.impl.Helper;
import de.kosit.validationtool.impl.ObjectFactory;
import de.kosit.validationtool.impl.tasks.CheckAction;
import de.kosit.validationtool.model.reportInput.CreateReportInput;
/**
* Testet das Assertion-Feature.
*
* @author Andreas Penski
*/
public class CheckAssertionActionTest {
private static final URL SAMPLE = CheckAssertionActionTest.class.getResource("/examples/assertions/ubl-0001.xml");
private static final URL SAMPLE_REPORT = CheckAssertionActionTest.class.getResource("/examples/assertions/ubl-0001-report.xml");
private static final URL SAMPLE_ASSERTIONS = CheckAssertionActionTest.class.getResource("/examples/assertions/tests-xrechnung.xml");
private CommandLine commandLine;
@Before
public void setup() throws IOException {
commandLine = new CommandLine();
commandLine.activate();
}
@Test
public void testEmptyInput() {
CheckAssertionAction a = new CheckAssertionAction(new Assertions(), ObjectFactory.createProcessor());
a.check(new CheckAction.Bag(InputFactory.read(SAMPLE), new CreateReportInput()));
assertThat(commandLine.getErrorOutput()).contains("Can not find assertions for");
}
@Test
public void testSimple() throws URISyntaxException {
final CheckAction.Bag bag = new CheckAction.Bag(InputFactory.read(SAMPLE), new CreateReportInput());
bag.setReport(Helper.load(SAMPLE_REPORT).getObject());
final Assertions assertions = Helper.load(SAMPLE_ASSERTIONS, Assertions.class);
CheckAssertionAction a = new CheckAssertionAction(assertions, ObjectFactory.createProcessor());
a.check(bag);
assertThat(commandLine.getErrorOutput()).contains("Assertion mismatch");
}
}

View file

@ -0,0 +1,130 @@
/*
* 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.*;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.TeeOutputStream;
import lombok.Getter;
import lombok.Setter;
/**
* Helferlein um Ausgaben auf der Kommandozeile zu testen.
*
* @author Andreas Penski
*/
public class CommandLine {
private static ReplaceableOutputStream<ByteArrayOutputStream> out = new ReplaceableOutputStream<>();
private static ReplaceableOutputStream<ByteArrayOutputStream> error = new ReplaceableOutputStream<>();
static {
// Initialisierung muss vor SL4J's SimpleLogger erfolgen, sonst sind logs nicht erfasst.
// deshalb darf diese Klasse kein Log haben
System.setOut(new PrintStream(new TeeOutputStream(System.out, out)));
System.setErr(new PrintStream(new TeeOutputStream(System.err, error)));
}
public String getOutput() {
return new String(out.getOut().toByteArray());
}
public String getErrorOutput() {
return new String(error.getOut().toByteArray());
}
public List<String> getOutputLines() {
return readLines(out.getOut().toByteArray());
}
public List<String> getErrorLines() {
return readLines(error.getOut().toByteArray());
}
private List<String> readLines(byte[] bytes) {
try ( ByteArrayInputStream in = new ByteArrayInputStream(bytes);
Reader r = new InputStreamReader(in) ) {
return IOUtils.readLines(r);
} catch (IOException e) {
throw new IllegalStateException("Can not read input");
}
}
public void activate() {
out.setOut(new ByteArrayOutputStream());
error.setOut(new ByteArrayOutputStream());
}
public void deactivate() {
out.setOut(null);
error.setOut(null);
}
/**
* Simpler Proxy für {@link OutputStream}, dessen target ausgetauscht werden kann.
*
* @param <O> Typ des eigentlichen {@link OutputStream}
*/
private static class ReplaceableOutputStream<O extends OutputStream> extends OutputStream {
@Getter
@Setter
private O out;
public void write(int idx) throws IOException {
if (out != null) {
this.out.write(idx);
}
}
public void write(byte[] bts) throws IOException {
if (out != null) {
this.out.write(bts);
}
}
public void write(byte[] bts, int st, int end) throws IOException {
if (out != null) {
this.out.write(bts, st, end);
}
}
public void flush() throws IOException {
if (out != null) {
this.out.flush();
}
}
public void close() throws IOException {
if (out != null) {
this.out.close();
}
}
}
}

View file

@ -0,0 +1,178 @@
/*
* 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 org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static de.kosit.validationtool.impl.Helper.*;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Testet die Parameter des Kommandozeilen-Tools.
*
* @author Andreas Penski
*/
public class CommandlineApplicationTest {
public static final String RESULT_OUTPUT = "Processing 1 object(s) completed";
private CommandLine commandLine;
@Before
public void setup() throws IOException {
commandLine = new CommandLine();
commandLine.activate();
}
@Test
public void testHelp() {
String[] args = new String[] { "-?" };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).isEmpty();
checkForHelp(commandLine.getOutputLines());
}
private void checkForHelp(List<String> outputLines) {
assertThat(outputLines.size()).isGreaterThan(0);
outputLines.subList(1, outputLines.size() - 1).forEach(l -> assertThat(l.startsWith(" -") || l.startsWith(" ")));
}
@Test
public void testRequiredScenarioFile() {
String[] args = new String[] { "-d", "arguments", "egal welche", "argument drin sind" };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).isNotEmpty();
assertThat(commandLine.getErrorOutput()).contains("Missing required option: s");
}
@Test
public void testNotExistingScenarioFile() {
String[] args = new String[] { "-s", Paths.get(NOT_EXISTING).toString(), Paths.get(NOT_EXISTING).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).isNotEmpty();
assertThat(commandLine.getErrorOutput()).contains("Not a valid path for scenario definition specified");
}
@Test
public void testIncorrectRepository() {
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), Paths.get(NOT_EXISTING).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).isNotEmpty();
assertThat(commandLine.getErrorOutput()).contains("Can not load schema from sources");
}
@Test
public void testNotExistingTestTarget() {
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-r", Paths.get(REPOSITORY).toString(),
Paths.get(NOT_EXISTING).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).isNotEmpty();
assertThat(commandLine.getErrorOutput()).contains("No test targets found");
}
@Test
public void testValidMinimalConfiguration() {
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-r", Paths.get(REPOSITORY).toString(),
Paths.get(SAMPLE).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
}
@Test
public void testValidMultipleInput() {
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-r", Paths.get(REPOSITORY).toString(),
Paths.get(SAMPLE).toString(), Paths.get(SAMPLE2).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains("Processing 2 object(s) completed");
}
@Test
public void testValidDirectoryInput() {
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-r", Paths.get(REPOSITORY).toString(),
Paths.get(SAMPLE_DIR).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains("Processing 4 object(s) completed");
}
@Test
public void testValidOutputConfiguration() throws IOException {
Path output = Paths.get("output");
// assertThat(output).doesNotExist();
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-o", output.getFileName().toString(), "-r",
Paths.get(REPOSITORY).toString(), Paths.get(SAMPLE).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
assertThat(output).exists();
assertThat(Files.list(output)).hasSize(1);
}
@Test
public void testNoInput() {
// assertThat(output).doesNotExist();
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-r", Paths.get(REPOSITORY).toString(), };
CommandLineApplication.mainProgram(args);
checkForHelp(commandLine.getOutputLines());
}
@Test
public void testPrint() {
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-p", "-r", Paths.get(REPOSITORY).toString(),
Paths.get(SAMPLE).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
assertThat(commandLine.getOutputLines()).contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
@Test
public void testHtmlExtraktion() throws IOException {
Path output = Files.createTempDirectory("pruef-tool-test");
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-h", "-o", output.toAbsolutePath().toString(), "-r",
Paths.get(REPOSITORY).toString(), Paths.get(SAMPLE).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
assertThat(Files.list(output).filter(f -> f.toString().endsWith(".html")).count()).isGreaterThan(0);
}
@Test
public void testAssertionsExtraktion() throws IOException {
String[] args = new String[] { "-d", "-s", Paths.get(SCENARIO_FILE).toString(), "-r", Paths.get(REPOSITORY).toString(), "-c",
Paths.get(ASSERTIONS).toString(), Paths.get(REPOSITORY).toString(), Paths.get(SAMPLE).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains(RESULT_OUTPUT);
assertThat(commandLine.getErrorOutput()).contains("Can not find assertions for ");
}
@Test
public void testDebugFlag() throws IOException {
String[] args = new String[] { "-s", Paths.get(SCENARIO_FILE).toString(), "-r", "unknown", "-d", Paths.get(ASSERTIONS).toString() };
CommandLineApplication.mainProgram(args);
assertThat(commandLine.getErrorOutput()).contains("at de.kosit.validationtool");
}
}

View file

@ -0,0 +1,74 @@
/*
* 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.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
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.Helper;
import de.kosit.validationtool.impl.tasks.CheckAction;
/**
* Testet die HTML-Extrkation des Kommondazeilenprogramms.
*
* @author Andreas Penski
*/
public class ExtractHtmlActionTest {
private static final URL REPORT = SerializeReportActionTest.class.getResource("/examples/results/report.xml");
private ExtractHtmlContentAction action;
private Path tmpDirectory;
@Before
public void setup() throws IOException {
tmpDirectory = Files.createTempDirectory("checktool");
action = new ExtractHtmlContentAction(Helper.loadTestRepository(), tmpDirectory);
}
@After
public void tearDown() throws IOException {
FileUtils.deleteDirectory(tmpDirectory.toFile());
}
@Test
public void testSimple() throws IOException {
CheckAction.Bag b = new CheckAction.Bag(InputFactory.read(REPORT));
assertThat(action.isSkipped(b)).isTrue();
b.setReport(Helper.load(REPORT).getObject());
action.check(b);
assertThat(action.isSkipped(b)).isFalse();
action.check(b);
assertThat(b.isStopped()).isFalse();
assertThat(Files.list(tmpDirectory).collect(Collectors.toList())).hasSize(1);
}
}

View file

@ -0,0 +1,70 @@
/*
* 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.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import de.kosit.validationtool.api.InputFactory;
import de.kosit.validationtool.impl.Helper;
import de.kosit.validationtool.impl.tasks.CheckAction;
/**
* @author Andreas Penski
*/
public class PrintReportActionTest {
private static final URL REPORT = SerializeReportActionTest.class.getResource("/examples/results/report.xml");
private CommandLine commandLine;
private PrintReportAction action;
@Before
public void setup() throws IOException {
commandLine = new CommandLine();
commandLine.activate();
action = new PrintReportAction();
}
@After
public void tearDownd() throws IOException {
commandLine.deactivate();
}
@Test
public void testSimpleSerialize() {
CheckAction.Bag b = new CheckAction.Bag(InputFactory.read(REPORT));
b.setReport(Helper.load(REPORT).getObject());
assertThat(action.isSkipped(b)).isFalse();
action.check(b);
assertThat(b.isStopped()).isFalse();
assertThat(commandLine.getOutput()).isNotEmpty();
assertThat(commandLine.getOutput()).startsWith("<?xml version=\"1.0\" ");
assertThat(commandLine.getErrorOutput()).isEmpty();
}
}

View file

@ -0,0 +1,78 @@
/*
* 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.InputFactory;
import de.kosit.validationtool.impl.Helper;
import de.kosit.validationtool.impl.tasks.CheckAction;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Andreas Penski
*/
public class SerializeReportActionTest {
private static final URL REPORT = SerializeReportActionTest.class.getResource("/examples/results/report.xml");
private Path tmpDirectory;
private SerializeReportAction action;
@Before
public void setup() throws IOException {
tmpDirectory = Files.createTempDirectory("checktool");
action = new SerializeReportAction(tmpDirectory);
}
@After
public void tearDown() throws IOException {
FileUtils.deleteDirectory(tmpDirectory.toFile());
}
@Test
public void testSimpleSerialize() {
CheckAction.Bag b = new CheckAction.Bag(InputFactory.read(REPORT));
assertThat(action.isSkipped(b)).isTrue();
b.setReport(Helper.load(REPORT).getObject());
assertThat(action.isSkipped(b)).isFalse();
action.check(b);
assertThat(b.isStopped()).isFalse();
assertThat(tmpDirectory.toFile().listFiles()).hasSize(1);
}
//ERPT-83
@Test
public void testName(){
final String name = "some.name.with.dots";
CheckAction.Bag b = new CheckAction.Bag(InputFactory.read("ega".getBytes(), name + ".xml"));
assertThat(b.getName()).isEqualTo(name);
}
}