Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
9566:9826b465b751 | 9567:86e522bc7f36 |
---|---|
1 package test; | 1 package test; |
2 | 2 |
3 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde | |
4 * Software engineering by | |
5 * Björnsen Beratende Ingenieure GmbH | |
6 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
7 * | |
8 * This file is Free Software under the GNU AGPL (>=v3) | |
9 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
10 * documentation coming with Dive4Elements River for details. | |
11 */ | |
12 | |
13 import java.io.File; | |
14 import java.io.FileNotFoundException; | |
15 import java.io.FileOutputStream; | |
16 import java.io.IOException; | 3 import java.io.IOException; |
17 import java.io.InputStream; | |
18 import java.io.InputStreamReader; | |
19 import java.net.URL; | |
20 import java.util.ArrayList; | 4 import java.util.ArrayList; |
21 import java.util.List; | 5 import java.util.List; |
22 | 6 |
23 import org.apache.commons.io.FileUtils; | |
24 import org.apache.commons.io.IOUtils; | |
25 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | 7 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; |
26 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; | 8 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; |
27 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; | 9 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; |
28 import org.dive4elements.artifacts.httpclient.http.HttpClient; | 10 import org.dive4elements.artifacts.httpclient.http.HttpClient; |
29 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | 11 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; |
42 import org.dive4elements.river.client.shared.model.Artifact; | 24 import org.dive4elements.river.client.shared.model.Artifact; |
43 import org.dive4elements.river.client.shared.model.Collection; | 25 import org.dive4elements.river.client.shared.model.Collection; |
44 import org.dive4elements.river.client.shared.model.Data; | 26 import org.dive4elements.river.client.shared.model.Data; |
45 import org.dive4elements.river.client.shared.model.DataItem; | 27 import org.dive4elements.river.client.shared.model.DataItem; |
46 import org.dive4elements.river.client.shared.model.DefaultCollection; | 28 import org.dive4elements.river.client.shared.model.DefaultCollection; |
29 import org.dive4elements.river.client.shared.model.DefaultData; | |
47 import org.dive4elements.river.client.shared.model.DefaultDataItem; | 30 import org.dive4elements.river.client.shared.model.DefaultDataItem; |
48 import org.dive4elements.river.client.shared.model.OutputMode; | 31 import org.dive4elements.river.client.shared.model.OutputMode; |
49 import org.dive4elements.river.client.shared.model.StringOptionsData; | 32 import org.dive4elements.river.client.shared.model.StringOptionsData; |
50 import org.junit.Assert; | 33 import org.junit.Assert; |
51 import org.w3c.dom.Document; | 34 import org.w3c.dom.Document; |
65 | 48 |
66 private final static String USERNAME = "belger"; | 49 private final static String USERNAME = "belger"; |
67 | 50 |
68 private final static String PASSWORD = "belger"; | 51 private final static String PASSWORD = "belger"; |
69 | 52 |
70 private static final String lineSeparator = System.getProperty("line.separator"); | |
71 | |
72 private final String serverUrl = "http://localhost:8181"; | 53 private final String serverUrl = "http://localhost:8181"; |
73 private final String locale = "de"; | 54 private final String locale = "de"; |
74 private final HttpClient client; | 55 private final HttpClient client; |
75 | |
76 private static final String exportFileDir = "D:" + File.separator; | |
77 private static final String IGNORE_ERSTELLDATUM = "# Datum der Erstellung"; | |
78 private static final String IGNORE_FLYS_VERSION = "# FLYS-Version:"; | |
79 private static final String IGNORE_BEARBEITER = "NEIN_DOCH_NICHT";// "# Bearbeiter:"; | |
80 | 56 |
81 private final Infotype infotype; | 57 private final Infotype infotype; |
82 private static String userUuid; | 58 private static String userUuid; |
83 private Collection collection; | 59 private Collection collection; |
84 private Artifact artifact; | 60 private Artifact artifact; |
85 private final IFilenameMapper fileName; | |
86 | 61 |
87 // common attributes | 62 // common attributes |
88 private final ICalcMode iCalcMode; | 63 private final ICalcMode iCalcMode; |
89 private final double from; | 64 private final double from; |
90 private final double to; | 65 private final double to; |
91 private final River river; | 66 private final River river; |
92 | 67 private final List<ModuleRunnerOutput> outputList; |
93 public AbstractModuleRunner(final Infotype infotype, final ICalcMode calcmode, final IFilenameMapper helloWorldFile, final double from, final double to, | 68 |
94 final River beispielfluss) throws ConnectionException, ServerException { | 69 public AbstractModuleRunner(final Infotype infotype, final ICalcMode calcmode, final double from, final double to, final River beispielfluss) |
70 throws ConnectionException, ServerException { | |
95 // common attributes (evtl. doch in subklassen, evtl. Zwischenhierarchiestufe einführen | 71 // common attributes (evtl. doch in subklassen, evtl. Zwischenhierarchiestufe einführen |
96 | 72 this.outputList = new ArrayList<ModuleRunnerOutput>(); |
97 this.iCalcMode = calcmode; | 73 this.iCalcMode = calcmode; |
98 this.river = beispielfluss; | 74 this.river = beispielfluss; |
99 this.from = from; | 75 this.from = from; |
100 this.to = to; | 76 this.to = to; |
101 | 77 |
102 this.infotype = infotype; | 78 this.infotype = infotype; |
103 this.fileName = helloWorldFile; | |
104 | 79 |
105 // init | 80 // init |
106 this.client = new HttpClientImpl(this.serverUrl, this.locale); | 81 this.client = new HttpClientImpl(this.serverUrl, this.locale); |
107 if (AbstractModuleRunner.userUuid == null) | 82 if (AbstractModuleRunner.userUuid == null) |
108 AbstractModuleRunner.userUuid = makeUserUuid(this.serverUrl); | 83 AbstractModuleRunner.userUuid = makeUserUuid(this.serverUrl); |
148 | 123 |
149 private final void setArtifact(final Artifact artifact) { | 124 private final void setArtifact(final Artifact artifact) { |
150 this.artifact = artifact; | 125 this.artifact = artifact; |
151 } | 126 } |
152 | 127 |
153 public abstract void runTest(final boolean exportToFile) throws ConnectionException, ServerException, IOException; | 128 public abstract void runTest() throws ConnectionException, ServerException, IOException; |
154 | 129 |
155 protected final void describeCollection() throws ConnectionException { | 130 protected final void describeCollection() throws ConnectionException { |
156 | 131 |
157 final String uuid = getCollection().identifier(); | 132 final String uuid = getCollection().identifier(); |
158 final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); | 133 final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); |
165 protected final void feedAndGo(final Data[] data, final int reachableStateIndex) throws ConnectionException, ServerException { | 140 protected final void feedAndGo(final Data[] data, final int reachableStateIndex) throws ConnectionException, ServerException { |
166 feed(data); | 141 feed(data); |
167 advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein. | 142 advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein. |
168 } | 143 } |
169 | 144 |
170 private final String getReachableStateByIndex(final Artifact artifact, final int index) { | 145 protected final String getReachableStateByIndex(final Artifact artifact, final int index) { |
171 | 146 |
172 final String[] states = artifact.getArtifactDescription().getReachableStates(); | 147 final String[] states = artifact.getArtifactDescription().getReachableStates(); |
173 if (states != null) { | 148 if (states != null) { |
174 | 149 |
175 if (states.length > index) | 150 if (states.length > index) |
226 return kvp; | 201 return kvp; |
227 } | 202 } |
228 return null; | 203 return null; |
229 } | 204 } |
230 | 205 |
231 private final void advance(final String target) throws ConnectionException, ServerException { | 206 protected final void advance(final String target) throws ConnectionException, ServerException { |
232 final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target); | 207 final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target); |
233 final Document description = (Document) this.client.advance( | 208 final Document description = (Document) this.client.advance( |
234 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), advance, | 209 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), advance, |
235 new DocumentResponseHandler()); | 210 new DocumentResponseHandler()); |
236 | 211 |
244 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | 219 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); |
245 } | 220 } |
246 } | 221 } |
247 | 222 |
248 /// ExportServiceImpl | 223 /// ExportServiceImpl |
249 protected final void assertAndWriteToFile(final String mode, final boolean exportToFile) throws IOException { | |
250 | |
251 final String type = "csv"; | |
252 | |
253 final String enc = "windows-1252";// req.getParameter("encoding"); | |
254 | |
255 final URL expectedResource = getClass().getResource(this.fileName.getFilename()); | |
256 final Document attr = null; | |
257 final Document request = ClientProtocolUtils.newOutCollectionDocument(getCollection().identifier(), mode, type, attr); | |
258 | |
259 final InputStream response = this.client.collectionOut(request, getCollection().identifier(), mode); | |
260 | |
261 final String actual = deleteErstelldatum(IOUtils.toString(response, "UTF-8")); | |
262 | |
263 final String expected = deleteErstelldatum(FileUtils.readFileToString(new File(expectedResource.getFile()), enc)); | |
264 | |
265 // if (!actual.equals(expected)) { | |
266 if (exportToFile) { | |
267 doGetWriteToDisk(mode); // TODO: WENN der Test negativ ausfällt, Datei abspeichern -> Diskussion | |
268 } | |
269 | |
270 Assert.assertEquals(expected, actual); | |
271 } | |
272 | |
273 private final String deleteErstelldatum(final String input) { | |
274 String result = ""; | |
275 final String[] lines = input.split(lineSeparator); | |
276 for (final String line : lines) { | |
277 if (!line.contains(AbstractModuleRunner.IGNORE_ERSTELLDATUM) && !line.contains(AbstractModuleRunner.IGNORE_FLYS_VERSION) | |
278 && !line.contains(AbstractModuleRunner.IGNORE_BEARBEITER)) { | |
279 result = result + line + lineSeparator; | |
280 } | |
281 } | |
282 return result; | |
283 } | |
284 | |
285 public final void doGetWriteToDisk(final String mode) throws FileNotFoundException, IOException { | |
286 | |
287 final String name = mode; | |
288 final String type = "csv"; | |
289 | |
290 final String fn = name + System.currentTimeMillis() + "." + type; | |
291 final String enc = "windows-1252"; | |
292 | |
293 final String filepath = exportFileDir + fn; | |
294 | |
295 final Document attr = null; | |
296 final Document request = ClientProtocolUtils.newOutCollectionDocument(getCollection().identifier(), mode, type, attr); | |
297 | |
298 final InputStream response = this.client.collectionOut(request, getCollection().identifier(), mode); | |
299 final InputStreamReader in = new InputStreamReader(response, "UTF-8"); | |
300 | |
301 IOUtils.copy(in, new FileOutputStream(filepath), enc); | |
302 | |
303 } | |
304 | 224 |
305 protected final void selectCalcMode() throws ConnectionException, ServerException { | 225 protected final void selectCalcMode() throws ConnectionException, ServerException { |
306 | 226 |
307 /* Select ICalcMode */ | 227 /* Select ICalcMode */ |
308 final String calcmodeStr = this.iCalcMode.toString(); | 228 final String calcmodeStr = this.iCalcMode.toString(); |
310 new DataItem[] { new DefaultDataItem(calcmodeStr, calcmodeStr, calcmodeStr) }); | 230 new DataItem[] { new DefaultDataItem(calcmodeStr, calcmodeStr, calcmodeStr) }); |
311 feedAndGo(new Data[] { dataCalcMode }, 0); | 231 feedAndGo(new Data[] { dataCalcMode }, 0); |
312 } | 232 } |
313 | 233 |
314 protected final void selectRange() throws ConnectionException, ServerException { | 234 protected final void selectRange() throws ConnectionException, ServerException { |
315 makeKmRange(this.from, this.to, "ld_from", "ld_to"); | 235 makeKmRange(this.getFrom(), this.getTo(), "ld_from", "ld_to"); |
316 | 236 |
317 } | 237 } |
318 | 238 |
319 protected final void makeKmRange(final double from, final double to, final String from_key, final String to_key) | 239 protected final void makeKmRange(final double from, final double to, final String from_key, final String to_key) |
320 throws ConnectionException, ServerException { | 240 throws ConnectionException, ServerException { |
321 feedAndGo(makeFromToData(from_key, to_key), 0); | 241 feedAndGo(makeFromToData(from_key, to_key, from, to), 0); |
322 } | 242 } |
323 | 243 |
324 protected final Data[] makeFromToData(final String from_key, final String to_key) { | 244 protected final Data[] makeFromToData(final String from_key, final String to_key, final double _from, final double _to) { |
325 final String fromStr = String.valueOf(this.from); | 245 final String fromStr = String.valueOf(_from); |
326 final String toStr = String.valueOf(this.to); | 246 final String toStr = String.valueOf(_to); |
327 final Data dataFrom = new StringOptionsData(from_key, from_key, new DataItem[] { new DefaultDataItem(fromStr, fromStr, fromStr) }); | 247 final Data dataFrom = new StringOptionsData(from_key, from_key, new DataItem[] { new DefaultDataItem(fromStr, fromStr, fromStr) }); |
328 final Data dataTo = new StringOptionsData(to_key, to_key, new DataItem[] { new DefaultDataItem(toStr, toStr, toStr) }); | 248 final Data dataTo = new StringOptionsData(to_key, to_key, new DataItem[] { new DefaultDataItem(toStr, toStr, toStr) }); |
329 return new Data[] { dataFrom, dataTo }; | 249 return new Data[] { dataFrom, dataTo }; |
330 } | 250 } |
331 | 251 |
333 final String riverStr = this.river.name(); | 253 final String riverStr = this.river.name(); |
334 final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem(riverStr, riverStr, riverStr) }); | 254 final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem(riverStr, riverStr, riverStr) }); |
335 feedAndGo(new Data[] { data }, 0); | 255 feedAndGo(new Data[] { data }, 0); |
336 } | 256 } |
337 | 257 |
338 protected final void export(final boolean exportToFile) throws IOException, ServerException { | 258 public final void runTests() throws IOException, ServerException { |
259 for (final ModuleRunnerOutput output : this.outputList) { | |
260 final String name = output.getOutputModename(); | |
261 if (!stateHasRequiredOutputMode(name)) { | |
262 Assert.fail("Unbekannter outputmode " + name); | |
263 } | |
264 output.assertAndWriteToFile(this.client, this.collection.identifier()); | |
265 } | |
266 } | |
267 | |
268 private final boolean stateHasRequiredOutputMode(final String outputMode) throws ConnectionException, ServerException { | |
339 final OutputMode[] modes = getArtifact().getArtifactDescription().getOutputModes(); | 269 final OutputMode[] modes = getArtifact().getArtifactDescription().getOutputModes(); |
340 boolean exportFacetCreated = false; | |
341 if (modes != null) { | 270 if (modes != null) { |
342 for (final OutputMode mode : modes) { | 271 for (final OutputMode mode : modes) { |
343 if (mode.getDescription().contains("_export")) { | 272 if (mode.getName().toLowerCase().equals(outputMode)) |
344 assertAndWriteToFile(mode.getName(), exportToFile); | 273 return true; |
345 exportFacetCreated = true; | |
346 } | |
347 } | 274 } |
348 | 275 } |
349 } | 276 return false; |
350 assert (exportFacetCreated == true); | |
351 } | 277 } |
352 | 278 |
353 protected final String getRecommendationPairString(final SimpleRecommendation rec1, final SimpleRecommendation rec2) | 279 protected final String getRecommendationPairString(final SimpleRecommendation rec1, final SimpleRecommendation rec2) |
354 throws ConnectionException, ServerException { | 280 throws ConnectionException, ServerException { |
355 | 281 |
363 protected final void feedAndGoSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { | 289 protected final void feedAndGoSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { |
364 feedAndGo(new Data[] { getSimpleTextInput(key, value) }, 0); | 290 feedAndGo(new Data[] { getSimpleTextInput(key, value) }, 0); |
365 } | 291 } |
366 | 292 |
367 protected Data getSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { | 293 protected Data getSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { |
368 return new StringOptionsData(key, key, new DataItem[] { new DefaultDataItem(value, value, value) }); | 294 return new DefaultData(key, key, key, new DataItem[] { new DefaultDataItem(value, value, value) }); |
369 } | 295 } |
370 | 296 |
371 protected final Data[] addItemToExistingData(final Data[] existing, final Data add) { | 297 protected final Data[] addItemToExistingData(final Data[] existing, final Data add) { |
372 final Data[] data = new Data[existing.length + 1]; | 298 final Data[] data = new Data[existing.length + 1]; |
373 for (int i = 0; i < existing.length; i++) { | 299 for (int i = 0; i < existing.length; i++) { |
375 } | 301 } |
376 data[existing.length] = add; | 302 data[existing.length] = add; |
377 return data; | 303 return data; |
378 } | 304 } |
379 | 305 |
306 public double getTo() { | |
307 return this.to; | |
308 } | |
309 | |
310 public double getFrom() { | |
311 return this.from; | |
312 } | |
313 | |
314 public final AbstractModuleRunner addOutputTest(final FilenameSupplier filename, final String outputModeName, final String currentKm) | |
315 throws ConnectionException { | |
316 this.outputList.add(new ModuleRunnerOutput(filename, outputModeName, currentKm)); | |
317 return this; | |
318 } | |
319 | |
380 } | 320 } |