mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
some more tests
This commit is contained in:
parent
16dc45ab46
commit
0bb0f6671d
5 changed files with 131 additions and 19 deletions
|
|
@ -186,7 +186,16 @@ public class ConfigurationBuilder {
|
|||
* @return the scenario configuration builder
|
||||
*/
|
||||
public static ScenarioBuilder scenario(final String name) {
|
||||
return new ScenarioBuilder(name);
|
||||
return new ScenarioBuilder().name(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new scenario configuration.
|
||||
*
|
||||
* @return the scenario configuration builder
|
||||
*/
|
||||
public static ScenarioBuilder scenario() {
|
||||
return scenario(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import net.sf.saxon.s9api.XPathExecutable;
|
|||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
public class ScenarioBuilder implements Builder<Scenario> {
|
||||
|
||||
private static int nameCount = 0;
|
||||
|
|
@ -51,8 +52,7 @@ public class ScenarioBuilder implements Builder<Scenario> {
|
|||
|
||||
private final XPathBuilder acceptConfig = new XPathBuilder("accept");
|
||||
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private final String name;
|
||||
private String name;
|
||||
|
||||
private SchemaBuilder schemaBuilder;
|
||||
|
||||
|
|
@ -273,4 +273,8 @@ public class ScenarioBuilder implements Builder<Scenario> {
|
|||
return type;
|
||||
}
|
||||
|
||||
public ScenarioBuilder name(final String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import java.util.Map;
|
|||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
|
|
@ -32,7 +33,7 @@ import net.sf.saxon.s9api.XPathExecutable;
|
|||
@Slf4j
|
||||
class XPathBuilder implements Builder<XPathExecutable> {
|
||||
|
||||
private static final String[] IGNORED_PREFIXES = new String[] { "xsd" };
|
||||
private static final String[] IGNORED_PREFIXES = new String[] { "xsd", "saxon", "xsl", "xs" };
|
||||
|
||||
private final String name;
|
||||
|
||||
|
|
@ -66,10 +67,9 @@ class XPathBuilder implements Builder<XPathExecutable> {
|
|||
try {
|
||||
if (this.executable == null) {
|
||||
this.executable = repository.createXPath(this.xpath, this.namespaces);
|
||||
|
||||
} else {
|
||||
this.xpath = extractExpression();
|
||||
this.namespaces = extractNamespaces();
|
||||
extractNamespaces();
|
||||
}
|
||||
} catch (final IllegalStateException e) {
|
||||
final String msg = String.format("Error creating %s xpath", this.name, e);
|
||||
|
|
@ -80,16 +80,21 @@ class XPathBuilder implements Builder<XPathExecutable> {
|
|||
return new Result<>(this.executable);
|
||||
}
|
||||
|
||||
private Map<String, String> extractNamespaces() {
|
||||
private void extractNamespaces() {
|
||||
if (this.namespaces == null) {
|
||||
this.namespaces = new HashMap<>();
|
||||
}
|
||||
final Map<String, String> ns = new HashMap<>();
|
||||
final Iterator<String> iterator = this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext()
|
||||
.iteratePrefixes();
|
||||
final Iterable<String> iterable = () -> iterator;
|
||||
StreamSupport.stream(iterable.spliterator(), false).filter(e -> !ArrayUtils.contains(IGNORED_PREFIXES, e)).forEach(e -> {
|
||||
ns.put(e,
|
||||
this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext().getURIForPrefix(e, false));
|
||||
});
|
||||
return ns;
|
||||
StreamSupport.stream(iterable.spliterator(), false).filter(e -> !ArrayUtils.contains(IGNORED_PREFIXES, e))
|
||||
.filter(StringUtils::isNotBlank).forEach(e -> {
|
||||
ns.put(e, this.executable.getUnderlyingExpression().getInternalExpression().getRetainedStaticContext()
|
||||
.getURIForPrefix(e, false));
|
||||
});
|
||||
this.namespaces.putAll(ns);
|
||||
|
||||
}
|
||||
|
||||
private String extractExpression() {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
package de.kosit.validationtool.daemon;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
|
@ -13,7 +16,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import de.kosit.validationtool.api.Configuration;
|
||||
import de.kosit.validationtool.impl.ConversionService;
|
||||
import de.kosit.validationtool.impl.DefaultCheck;
|
||||
import de.kosit.validationtool.model.daemon.HealthType;
|
||||
|
||||
/**
|
||||
* HTTP-Daemon für die Bereitstellung der Prüf-Funktionalität via http.
|
||||
|
|
@ -25,6 +27,12 @@ import de.kosit.validationtool.model.daemon.HealthType;
|
|||
@Slf4j
|
||||
public class Daemon {
|
||||
|
||||
private static final String DEFAULT_HOST = "localhost";
|
||||
|
||||
private static final int DEFAULT_PORT = 8080;
|
||||
|
||||
private static final int DEFAULT_THREAD_COUNT = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
private final String hostName;
|
||||
|
||||
private final int port;
|
||||
|
|
@ -40,18 +48,25 @@ public class Daemon {
|
|||
HttpServer server = null;
|
||||
try {
|
||||
final ConversionService converter = new ConversionService();
|
||||
converter.initialize(HealthType.class.getPackage());
|
||||
|
||||
server = HttpServer.create(new InetSocketAddress(this.hostName, this.port), 0);
|
||||
server = HttpServer.create(getSocket(), 0);
|
||||
final DefaultCheck check = new DefaultCheck(config);
|
||||
server.createContext("/", new CheckHandler(check, config.getContentRepository().getProcessor()));
|
||||
server.createContext("/server/health", new HealthHandler(config, converter));
|
||||
server.createContext("/server/config", new ConfigHandler(config, new ConversionService()));
|
||||
server.setExecutor(Executors.newFixedThreadPool(this.threadCount));
|
||||
server.createContext("/server/config", new ConfigHandler(config, converter));
|
||||
server.setExecutor(createExecutor());
|
||||
server.start();
|
||||
log.info("Server unter Port {} ist erfolgreich gestartet", this.port);
|
||||
log.info("Server {} started", server.getAddress());
|
||||
} catch (final IOException e) {
|
||||
log.error("Fehler beim HttpServer erstellen: {}", e.getMessage(), e);
|
||||
log.error("Error starting HttpServer for Valdidator: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private ExecutorService createExecutor() {
|
||||
return Executors.newFixedThreadPool(this.threadCount > 0 ? this.threadCount : DEFAULT_THREAD_COUNT);
|
||||
}
|
||||
|
||||
private InetSocketAddress getSocket() {
|
||||
return new InetSocketAddress(defaultIfBlank(this.hostName, DEFAULT_HOST), this.port > 0 ? this.port : DEFAULT_PORT);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,13 +3,23 @@ package de.kosit.validationtool.config;
|
|||
import static de.kosit.validationtool.config.SimpleConfigTest.createScenarioConfiguration;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import de.kosit.validationtool.impl.ContentRepository;
|
||||
import de.kosit.validationtool.impl.Helper.Simple;
|
||||
import de.kosit.validationtool.impl.Scenario;
|
||||
import de.kosit.validationtool.impl.model.Result;
|
||||
import de.kosit.validationtool.model.scenarios.NamespaceType;
|
||||
import de.kosit.validationtool.model.scenarios.ScenarioType;
|
||||
|
||||
import net.sf.saxon.s9api.XPathExecutable;
|
||||
|
||||
/**
|
||||
* Test {@link ScenarioBuilder}.
|
||||
|
|
@ -71,4 +81,73 @@ public class ScenarioBuilderTest {
|
|||
assertThat(result.isValid()).isFalse();
|
||||
assertThat(result.getErrors()).anyMatch(e -> e.contains("accept"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombinedNamespaces() {
|
||||
final ContentRepository repository = Simple.createContentRepository();
|
||||
final Map<String, String> ns1 = new HashMap<>();
|
||||
ns1.put("n1", "http://n1.org");
|
||||
final XPathExecutable match = repository.createXPath("//n1:*", ns1);
|
||||
|
||||
final Map<String, String> ns2 = new HashMap<>();
|
||||
ns2.put("n2", "http://n2.org");
|
||||
final XPathExecutable accept = repository.createXPath("//n2:*", ns2);
|
||||
|
||||
final ScenarioBuilder builder = createScenarioConfiguration();
|
||||
builder.getNamespaces().clear();
|
||||
|
||||
builder.match(match).acceptWith(accept).declareNamespace("n3", "http://n3.org");
|
||||
final Result<Scenario, String> result = builder.build(repository);
|
||||
|
||||
assertThat(result.isValid()).isTrue();
|
||||
final Scenario scenario = result.getObject();
|
||||
final List<NamespaceType> namespaces = scenario.getConfiguration().getNamespace();
|
||||
assertThat(namespaces.stream().map(NamespaceType::getPrefix)).containsExactly("n1", "n2", "n3");
|
||||
assertThat(namespaces).hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigureWithExecutable() {
|
||||
final ContentRepository repository = Simple.createContentRepository();
|
||||
final XPathExecutable match = repository.createXPath("//*", null);
|
||||
final XPathExecutable accept = repository.createXPath("//*", null);
|
||||
final ScenarioBuilder builder = createScenarioConfiguration();
|
||||
builder.getNamespaces().clear();
|
||||
|
||||
builder.match(match);
|
||||
builder.acceptWith(accept);
|
||||
final Result<Scenario, String> result = builder.build(repository);
|
||||
assertThat(result.isValid()).isTrue();
|
||||
final ScenarioType configuration = result.getObject().getConfiguration();
|
||||
assertThat(configuration.getMatch()).isNotEmpty();
|
||||
assertThat(configuration.getAcceptMatch()).isNotEmpty();
|
||||
assertThat(configuration.getNamespace()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicAttributes() {
|
||||
final ContentRepository repository = Simple.createContentRepository();
|
||||
final String random = RandomStringUtils.random(5);
|
||||
final ScenarioBuilder builder = createScenarioConfiguration();
|
||||
builder.name(random).description(random);
|
||||
final Result<Scenario, String> result = builder.build(repository);
|
||||
assertThat(result.isValid()).isTrue();
|
||||
final ScenarioType config = result.getObject().getConfiguration();
|
||||
assertThat(config.getName()).isEqualTo(random);
|
||||
assertThat(config.getDescription()).isNotNull();
|
||||
assertThat(config.getDescription().getPOrOlOrUl()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoBasicAttributes() {
|
||||
final ContentRepository repository = Simple.createContentRepository();
|
||||
final ScenarioBuilder builder = createScenarioConfiguration();
|
||||
builder.name(null);
|
||||
final Result<Scenario, String> result = builder.build(repository);
|
||||
assertThat(result.isValid()).isTrue();
|
||||
final ScenarioType config = result.getObject().getConfiguration();
|
||||
assertThat(config.getName()).contains("manually");
|
||||
assertThat(config.getDescription()).isNotNull();
|
||||
assertThat(config.getDescription().getPOrOlOrUl()).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue