Mercurial > dive4elements > river
view gwt-client/src/test/java/test/AbstractModuleRunner.java @ 9504:76c0665888a3
No rounding during calculation (Meilenstein-2 2.4.2 and 2.9), delta-w-cm as double (for historical scenario)
author | mschaefer |
---|---|
date | Fri, 28 Sep 2018 10:13:09 +0200 |
parents | d6d5ca6d4af0 |
children | 86e522bc7f36 |
line wrap: on
line source
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; import org.dive4elements.artifacts.httpclient.http.HttpClient; import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; import org.dive4elements.artifacts.httpclient.utils.XMLUtils; import org.dive4elements.river.client.server.AdvanceServiceImpl; import org.dive4elements.river.client.server.ArtifactHelper; import org.dive4elements.river.client.server.CollectionHelper; import org.dive4elements.river.client.server.CreateCollectionServiceImpl; import org.dive4elements.river.client.server.FLYSArtifactCreator; import org.dive4elements.river.client.server.FeedServiceImpl; import org.dive4elements.river.client.server.auth.DefaultUser; import org.dive4elements.river.client.server.auth.User; import org.dive4elements.river.client.server.auth.UserClient; import org.dive4elements.river.client.shared.exceptions.ServerException; import org.dive4elements.river.client.shared.model.Artifact; import org.dive4elements.river.client.shared.model.Collection; 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.DefaultDataItem; import org.dive4elements.river.client.shared.model.OutputMode; import org.dive4elements.river.client.shared.model.StringOptionsData; import org.junit.Assert; import org.w3c.dom.Document; import org.w3c.dom.Element; import test.AbstractBerechnungsartenTester.River; /** * @author Domenico Nardi Tironi * */ public abstract class AbstractModuleRunner { public enum Infotype { sinfo, uinfo, bundu } private final static String USERNAME = "belger"; 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; public AbstractModuleRunner(final Infotype infotype, final ICalcMode calcmode, final IFilenameMapper helloWorldFile, final double from, final double to, final River beispielfluss) throws ConnectionException, ServerException { // common attributes (evtl. doch in subklassen, evtl. Zwischenhierarchiestufe einführen 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); if (AbstractModuleRunner.userUuid == null) AbstractModuleRunner.userUuid = makeUserUuid(this.serverUrl); this.collection = getCollection(); this.artifact = getArtifact(); } private static String makeUserUuid(final String serverUrl) throws ConnectionException { final User user = new DefaultUser(USERNAME, PASSWORD, null, false, new ArrayList<String>(), new ArrayList<String>(), null); final UserClient userClient = new UserClient(serverUrl); Element userElement; userElement = userClient.findUser(user); return userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid"); } protected final Artifact getArtifact() throws ServerException, ConnectionException { if (this.artifact == null) { this.artifact = ArtifactHelper.createArtifact(this.serverUrl, this.locale, this.infotype.name(), null); setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz } return this.artifact; } protected Collection getCollection() throws ConnectionException { if (this.collection == null) { // lazy-Loading final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); final Document doc = (Document) this.client.createCollection(create, userUuid, new DocumentResponseHandler()); final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE); final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); this.collection = new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid); } return this.collection; } private final void setCollection(final Collection collection) { this.collection = collection; } private final void setArtifact(final Artifact artifact) { this.artifact = artifact; } public abstract void runTest(final boolean exportToFile) throws ConnectionException, ServerException, IOException; protected final void describeCollection() throws ConnectionException { final String uuid = getCollection().identifier(); final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); final Document response = (Document) this.client.doCollectionAction(describe, uuid, new DocumentResponseHandler()); final Collection c = CollectionHelper.parseCollection(response); setCollection(c); } protected final void feedAndGo(final Data[] data, final int reachableStateIndex) throws ConnectionException, ServerException { feed(data); advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein. } private final String getReachableStateByIndex(final Artifact artifact, final int index) { final String[] states = artifact.getArtifactDescription().getReachableStates(); if (states != null) { if (states.length > index) return states[index]; return states[0]; } return ""; } protected final void feed(final Data[] data) throws ServerException, ConnectionException { final Document feed = ClientProtocolUtils.newFeedDocument(getArtifact().getUuid(), getArtifact().getHash(), createKVP(data)); final Document description = (Document) this.client.feed( new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), feed, new DocumentResponseHandler()); final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { setArtifact((Artifact) new FLYSArtifactCreator().create(description)); } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); throw new ServerException(msg); } } protected final Data[] extractPairData(final List<String> pairIds, final String dataName) { final DataItem[] items = new DataItem[pairIds.size()]; int i = 0; final StringBuilder builder = new StringBuilder(); for (final String pairId : pairIds) { builder.append(pairId).append("#"); i++; } final String allPairIds = builder.delete(builder.length() - 1, builder.length()).toString(); // letztes Gatter entfernen final DataItem[] item = new DataItem[] { new DefaultDataItem(allPairIds, allPairIds, allPairIds) }; return new Data[] { new StringOptionsData(dataName, dataName, item) }; } private final String[][] createKVP(final Data[] data) { if (data != null) { final String[][] kvp = new String[data.length][]; int i = 0; for (final Data d : data) { final String key = d.getLabel(); final String value = d.getStringValue(); kvp[i++] = new String[] { key, value }; } return kvp; } return null; } private 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, new DocumentResponseHandler()); if (description == null) { throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); } final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) { setArtifact((Artifact) new FLYSArtifactCreator().create(description)); } } /// 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 { /* Select ICalcMode */ final String calcmodeStr = this.iCalcMode.toString(); final Data dataCalcMode = new StringOptionsData("calculation_mode", "calculation_mode", new DataItem[] { new DefaultDataItem(calcmodeStr, calcmodeStr, calcmodeStr) }); feedAndGo(new Data[] { dataCalcMode }, 0); } protected final void selectRange() throws ConnectionException, ServerException { makeKmRange(this.from, this.to, "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); } 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); 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 }; } protected final void selectRiver() throws ConnectionException, ServerException { final String riverStr = this.river.name(); final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem(riverStr, riverStr, riverStr) }); feedAndGo(new Data[] { data }, 0); } protected final void export(final boolean exportToFile) throws IOException, 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; } } } assert (exportFacetCreated == true); } protected final String getRecommendationPairString(final SimpleRecommendation rec1, final SimpleRecommendation rec2) throws ConnectionException, ServerException { return rec1.getRecommendationPairString(rec2, getCollection(), this.serverUrl, this.locale); } protected final void feedAndGoData(final Data[] data) throws ConnectionException, ServerException { feedAndGo(data, 0); } protected final void feedAndGoSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { feedAndGo(new Data[] { getSimpleTextInput(key, value) }, 0); } protected Data getSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { return new StringOptionsData(key, key, new DataItem[] { new DefaultDataItem(value, value, value) }); } protected final Data[] addItemToExistingData(final Data[] existing, final Data add) { final Data[] data = new Data[existing.length + 1]; for (int i = 0; i < existing.length; i++) { data[i] = existing[i]; } data[existing.length] = add; return data; } }