Mercurial > dive4elements > river
diff gwt-client/src/test/java/test/SuperTest.java @ 9031:efd2de78d158
work on unit testing
author | gernotbelger |
---|---|
date | Fri, 27 Apr 2018 11:34:04 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gwt-client/src/test/java/test/SuperTest.java Fri Apr 27 11:34:04 2018 +0200 @@ -0,0 +1,328 @@ +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.HashMap; +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.client.ui.NilDatacageTwinPanelInfo; +import org.dive4elements.river.client.client.ui.RecommandationUtils; +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.LoadArtifactServiceImpl; +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.Recommendation; +import org.dive4elements.river.client.shared.model.StringOptionsData; +import org.junit.Assert; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +/** + * @author Domenico Nardi Tironi + * + */ +public abstract class SuperTest { + + 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 erstellDatumSearchString = "# Datum der Erstellung"; + + private final String username; + private final String password; + private final String infotype; + private final String userUuid; + private Collection collection; + private Artifact artifact; + + private final List<String> pairIds = new ArrayList<String>(); + + public SuperTest(final String username, final String password, final String infotype) throws ConnectionException, ServerException { + this.username = username; + this.password = password; + this.infotype = infotype; + + // init + this.client = new HttpClientImpl(this.serverUrl, this.locale); + this.userUuid = makeUserUuid(); + this.collection = getCollection(); + this.artifact = getArtifact(); + } + + private String makeUserUuid() throws ConnectionException { + final User user = new DefaultUser(this.username, this.password, null, false, new ArrayList<String>(), new ArrayList<String>()); + final UserClient userClient = new UserClient(this.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, null); + setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz + } + return this.artifact; + } + + private Collection getCollection() throws ConnectionException { + + if (this.collection == null) { + // lazy-Loading + final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); + final Document doc = (Document) this.client.createCollection(create, this.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 void setCollection(final Collection collection) { + this.collection = collection; + } + + private 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... TODO: feedAndgo(data,string) + // bauen, falls irgendwann erforderlich + } + + private String getReachableStateByIndex(final Artifact artifact, final int index) { + + final String[] states = artifact.getArtifactDescription().getReachableStates(); + if (states != null) { + if (states.length > index) { + return states[index]; + } else { + return states[0]; + } + } else { + return ""; + } + } + + private 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); + } + } + + private String[][] createKVP(final Data[] data) { + if (data != null) { + final String[][] kvp = new String[data.length][]; + + int i = 0; + + for (final Data d : data) { + // final DataItem[] items = d.getItems(); + final String key = d.getLabel(); + final String value = d.getStringValue(); + + kvp[i++] = new String[] { key, value }; + } + + return kvp; + } + return null; + } + + private 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)); + } + } + + protected final Artifact[] loadMany(final Recommendation[] recoms, final String factory) throws ServerException, ConnectionException { + final ArrayList<Artifact> artifacts = new ArrayList<Artifact>(); + final HashMap<Recommendation, Artifact> cloneMap = new HashMap<Recommendation, Artifact>(); + + for (final Recommendation recom : recoms) { + + final Artifact prevClone = cloneMap.get(recom); + if (prevClone != null) { + + artifacts.add(prevClone); + } else { + // Not already cloned. + final String realFactory = factory != null ? factory : recom.getFactory(); + + final Artifact clone = ArtifactHelper.createArtifact(this.serverUrl, this.locale, realFactory, recom); + + if (clone != null) { + final Collection c = CollectionHelper.addArtifact(getCollection(), clone, this.serverUrl, this.locale); + + if (c != null) { + artifacts.add(clone); + // Remember we cloned a recommendation like this. + cloneMap.put(recom, clone); + } else { + throw new ServerException(LoadArtifactServiceImpl.ERROR_LOAD_ARTIFACT); + } + } + } + } + return artifacts.toArray(new Artifact[artifacts.size()]); + + } + + /// ExportServiceImpl + public void doGet(final String mode, final boolean exportToFile) throws IOException { + + final String name = mode; + final String type = "csv"; + + final String fn = name + "." + type; // TODO: make filename unique + final String enc = "windows-1252";// req.getParameter("encoding"); + + final URL expectedResource = getClass().getResource("/sinfo/flowdepthminmax/" + 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 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(actual, expected); + + } + + private String deleteErstelldatum(final String input) { + String result = ""; + final String[] lines = input.split(System.lineSeparator()); + for (final String line : lines) { + if (!line.contains(SuperTest.erstellDatumSearchString)) { + result = result + line + System.lineSeparator(); + } + } + return result; + } + + public void doGetWriteToDisk(final String mode) throws FileNotFoundException, IOException { + + final String name = mode; + final String type = "csv"; + + final String fn = name + "." + type; // TODO: make filename unique + 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); + + } + + public void addRecommendationPair(final String[] rec1, final String[] rec2) throws ConnectionException, ServerException { + final Recommendation recom1 = new Recommendation(rec1[0], rec1[1], rec1[2]); + final Recommendation recom2 = new Recommendation(rec2[0], rec2[1], rec2[2]); + final Artifact[] artifacts = loadMany(new Recommendation[] { recom1, recom2 }, null); + final String rec1String = RecommandationUtils.createDataString(artifacts[0].getUuid(), recom1, new NilDatacageTwinPanelInfo("xxxx")); + final String rec2String = RecommandationUtils.createDataString(artifacts[1].getUuid(), recom2, new NilDatacageTwinPanelInfo("xxxx")); + // TODO: check display name recom1.getDisplayName() TODO: makeDisplayName + final String combinedIdNeu = rec1String + "#" + rec2String; + this.pairIds.add(combinedIdNeu); + } + + protected Data[] getPairData() { + final Data[] data = new Data[this.pairIds.size()]; + int i = 0; + for (final String pairId : this.pairIds) { + final Data pair = new StringOptionsData("diffids", "diffids", new DataItem[] { new DefaultDataItem(pairId, pairId, pairId) }); + data[i] = pair; + i++; + } + return data; + } +}