Mercurial > dive4elements > river
diff gwt-client/src/test/java/test/AbstractModuleRunner.java @ 9567:86e522bc7f36
jUnit-Tests completed
author | gernotbelger |
---|---|
date | Mon, 05 Nov 2018 13:21:57 +0100 |
parents | d6d5ca6d4af0 |
children | 531a60b7af95 |
line wrap: on
line diff
--- a/gwt-client/src/test/java/test/AbstractModuleRunner.java Mon Nov 05 10:19:12 2018 +0100 +++ b/gwt-client/src/test/java/test/AbstractModuleRunner.java Mon Nov 05 13:21:57 2018 +0100 @@ -1,27 +1,9 @@ package test; -/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde - * Software engineering by - * Björnsen Beratende Ingenieure GmbH - * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt - * - * This file is Free Software under the GNU AGPL (>=v3) - * and comes with ABSOLUTELY NO WARRANTY! Check out the - * documentation coming with Dive4Elements River for details. - */ - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; import java.util.ArrayList; import java.util.List; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.dive4elements.artifacts.common.ArtifactNamespaceContext; import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; @@ -44,6 +26,7 @@ import org.dive4elements.river.client.shared.model.Data; import org.dive4elements.river.client.shared.model.DataItem; import org.dive4elements.river.client.shared.model.DefaultCollection; +import org.dive4elements.river.client.shared.model.DefaultData; import org.dive4elements.river.client.shared.model.DefaultDataItem; import org.dive4elements.river.client.shared.model.OutputMode; import org.dive4elements.river.client.shared.model.StringOptionsData; @@ -67,40 +50,32 @@ private final static String PASSWORD = "belger"; - private static final String lineSeparator = System.getProperty("line.separator"); - private final String serverUrl = "http://localhost:8181"; private final String locale = "de"; private final HttpClient client; - private static final String exportFileDir = "D:" + File.separator; - private static final String IGNORE_ERSTELLDATUM = "# Datum der Erstellung"; - private static final String IGNORE_FLYS_VERSION = "# FLYS-Version:"; - private static final String IGNORE_BEARBEITER = "NEIN_DOCH_NICHT";// "# Bearbeiter:"; - private final Infotype infotype; private static String userUuid; private Collection collection; private Artifact artifact; - private final IFilenameMapper fileName; // common attributes private final ICalcMode iCalcMode; private final double from; private final double to; private final River river; + private final List<ModuleRunnerOutput> outputList; - public AbstractModuleRunner(final Infotype infotype, final ICalcMode calcmode, final IFilenameMapper helloWorldFile, final double from, final double to, - final River beispielfluss) throws ConnectionException, ServerException { + public AbstractModuleRunner(final Infotype infotype, final ICalcMode calcmode, final double from, final double to, final River beispielfluss) + throws ConnectionException, ServerException { // common attributes (evtl. doch in subklassen, evtl. Zwischenhierarchiestufe einführen - + this.outputList = new ArrayList<ModuleRunnerOutput>(); this.iCalcMode = calcmode; this.river = beispielfluss; this.from = from; this.to = to; this.infotype = infotype; - this.fileName = helloWorldFile; // init this.client = new HttpClientImpl(this.serverUrl, this.locale); @@ -150,7 +125,7 @@ this.artifact = artifact; } - public abstract void runTest(final boolean exportToFile) throws ConnectionException, ServerException, IOException; + public abstract void runTest() throws ConnectionException, ServerException, IOException; protected final void describeCollection() throws ConnectionException { @@ -167,7 +142,7 @@ advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein. } - private final String getReachableStateByIndex(final Artifact artifact, final int index) { + protected final String getReachableStateByIndex(final Artifact artifact, final int index) { final String[] states = artifact.getArtifactDescription().getReachableStates(); if (states != null) { @@ -228,7 +203,7 @@ return null; } - private final void advance(final String target) throws ConnectionException, ServerException { + protected final void advance(final String target) throws ConnectionException, ServerException { final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target); final Document description = (Document) this.client.advance( new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), advance, @@ -246,61 +221,6 @@ } /// ExportServiceImpl - protected final void assertAndWriteToFile(final String mode, final boolean exportToFile) throws IOException { - - final String type = "csv"; - - final String enc = "windows-1252";// req.getParameter("encoding"); - - final URL expectedResource = getClass().getResource(this.fileName.getFilename()); - final Document attr = null; - final Document request = ClientProtocolUtils.newOutCollectionDocument(getCollection().identifier(), mode, type, attr); - - final InputStream response = this.client.collectionOut(request, getCollection().identifier(), mode); - - final String actual = deleteErstelldatum(IOUtils.toString(response, "UTF-8")); - - final String expected = deleteErstelldatum(FileUtils.readFileToString(new File(expectedResource.getFile()), enc)); - - // if (!actual.equals(expected)) { - if (exportToFile) { - doGetWriteToDisk(mode); // TODO: WENN der Test negativ ausfällt, Datei abspeichern -> Diskussion - } - - Assert.assertEquals(expected, actual); - } - - private final String deleteErstelldatum(final String input) { - String result = ""; - final String[] lines = input.split(lineSeparator); - for (final String line : lines) { - if (!line.contains(AbstractModuleRunner.IGNORE_ERSTELLDATUM) && !line.contains(AbstractModuleRunner.IGNORE_FLYS_VERSION) - && !line.contains(AbstractModuleRunner.IGNORE_BEARBEITER)) { - result = result + line + lineSeparator; - } - } - return result; - } - - public final void doGetWriteToDisk(final String mode) throws FileNotFoundException, IOException { - - final String name = mode; - final String type = "csv"; - - final String fn = name + System.currentTimeMillis() + "." + type; - final String enc = "windows-1252"; - - final String filepath = exportFileDir + fn; - - final Document attr = null; - final Document request = ClientProtocolUtils.newOutCollectionDocument(getCollection().identifier(), mode, type, attr); - - final InputStream response = this.client.collectionOut(request, getCollection().identifier(), mode); - final InputStreamReader in = new InputStreamReader(response, "UTF-8"); - - IOUtils.copy(in, new FileOutputStream(filepath), enc); - - } protected final void selectCalcMode() throws ConnectionException, ServerException { @@ -312,18 +232,18 @@ } protected final void selectRange() throws ConnectionException, ServerException { - makeKmRange(this.from, this.to, "ld_from", "ld_to"); + makeKmRange(this.getFrom(), this.getTo(), "ld_from", "ld_to"); } protected final void makeKmRange(final double from, final double to, final String from_key, final String to_key) throws ConnectionException, ServerException { - feedAndGo(makeFromToData(from_key, to_key), 0); + feedAndGo(makeFromToData(from_key, to_key, from, to), 0); } - protected final Data[] makeFromToData(final String from_key, final String to_key) { - final String fromStr = String.valueOf(this.from); - final String toStr = String.valueOf(this.to); + protected final Data[] makeFromToData(final String from_key, final String to_key, final double _from, final double _to) { + final String fromStr = String.valueOf(_from); + final String toStr = String.valueOf(_to); final Data dataFrom = new StringOptionsData(from_key, from_key, new DataItem[] { new DefaultDataItem(fromStr, fromStr, fromStr) }); final Data dataTo = new StringOptionsData(to_key, to_key, new DataItem[] { new DefaultDataItem(toStr, toStr, toStr) }); return new Data[] { dataFrom, dataTo }; @@ -335,19 +255,25 @@ feedAndGo(new Data[] { data }, 0); } - protected final void export(final boolean exportToFile) throws IOException, ServerException { + public final void runTests() throws IOException, ServerException { + for (final ModuleRunnerOutput output : this.outputList) { + final String name = output.getOutputModename(); + if (!stateHasRequiredOutputMode(name)) { + Assert.fail("Unbekannter outputmode " + name); + } + output.assertAndWriteToFile(this.client, this.collection.identifier()); + } + } + + private final boolean stateHasRequiredOutputMode(final String outputMode) throws ConnectionException, ServerException { final OutputMode[] modes = getArtifact().getArtifactDescription().getOutputModes(); - boolean exportFacetCreated = false; if (modes != null) { for (final OutputMode mode : modes) { - if (mode.getDescription().contains("_export")) { - assertAndWriteToFile(mode.getName(), exportToFile); - exportFacetCreated = true; - } + if (mode.getName().toLowerCase().equals(outputMode)) + return true; } - } - assert (exportFacetCreated == true); + return false; } protected final String getRecommendationPairString(final SimpleRecommendation rec1, final SimpleRecommendation rec2) @@ -365,7 +291,7 @@ } protected Data getSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { - return new StringOptionsData(key, key, new DataItem[] { new DefaultDataItem(value, value, value) }); + return new DefaultData(key, key, key, new DataItem[] { new DefaultDataItem(value, value, value) }); } protected final Data[] addItemToExistingData(final Data[] existing, final Data add) { @@ -377,4 +303,18 @@ return data; } + public double getTo() { + return this.to; + } + + public double getFrom() { + return this.from; + } + + public final AbstractModuleRunner addOutputTest(final FilenameSupplier filename, final String outputModeName, final String currentKm) + throws ConnectionException { + this.outputList.add(new ModuleRunnerOutput(filename, outputModeName, currentKm)); + return this; + } + } \ No newline at end of file