Mercurial > dive4elements > river
annotate gwt-client/src/test/java/test/AbstractModuleRunner.java @ 9497:d6d5ca6d4af0
Enabled logging of saml-group-name in log-ing logfile.
Some cleanup/refaktoring.
author | gernotbelger |
---|---|
date | Thu, 27 Sep 2018 17:40:39 +0200 |
parents | e75afd693f78 |
children | 86e522bc7f36 |
rev | line source |
---|---|
9031 | 1 package test; |
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; | |
17 import java.io.InputStream; | |
18 import java.io.InputStreamReader; | |
19 import java.net.URL; | |
20 import java.util.ArrayList; | |
21 import java.util.List; | |
22 | |
23 import org.apache.commons.io.FileUtils; | |
24 import org.apache.commons.io.IOUtils; | |
25 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
26 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; | |
27 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; | |
28 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
29 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | |
30 import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; | |
31 import org.dive4elements.artifacts.httpclient.utils.XMLUtils; | |
32 import org.dive4elements.river.client.server.AdvanceServiceImpl; | |
33 import org.dive4elements.river.client.server.ArtifactHelper; | |
34 import org.dive4elements.river.client.server.CollectionHelper; | |
35 import org.dive4elements.river.client.server.CreateCollectionServiceImpl; | |
36 import org.dive4elements.river.client.server.FLYSArtifactCreator; | |
37 import org.dive4elements.river.client.server.FeedServiceImpl; | |
38 import org.dive4elements.river.client.server.auth.DefaultUser; | |
39 import org.dive4elements.river.client.server.auth.User; | |
40 import org.dive4elements.river.client.server.auth.UserClient; | |
41 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
42 import org.dive4elements.river.client.shared.model.Artifact; | |
43 import org.dive4elements.river.client.shared.model.Collection; | |
44 import org.dive4elements.river.client.shared.model.Data; | |
45 import org.dive4elements.river.client.shared.model.DataItem; | |
46 import org.dive4elements.river.client.shared.model.DefaultCollection; | |
47 import org.dive4elements.river.client.shared.model.DefaultDataItem; | |
9127 | 48 import org.dive4elements.river.client.shared.model.OutputMode; |
9031 | 49 import org.dive4elements.river.client.shared.model.StringOptionsData; |
50 import org.junit.Assert; | |
51 import org.w3c.dom.Document; | |
52 import org.w3c.dom.Element; | |
53 | |
9422 | 54 import test.AbstractBerechnungsartenTester.River; |
9127 | 55 |
9031 | 56 /** |
57 * @author Domenico Nardi Tironi | |
58 * | |
59 */ | |
9148 | 60 public abstract class AbstractModuleRunner { |
9127 | 61 |
62 public enum Infotype { | |
9422 | 63 sinfo, uinfo, bundu |
9127 | 64 } |
9031 | 65 |
9422 | 66 private final static String USERNAME = "belger"; |
67 | |
68 private final static String PASSWORD = "belger"; | |
69 | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
70 private static final String lineSeparator = System.getProperty("line.separator"); |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
71 |
9031 | 72 private final String serverUrl = "http://localhost:8181"; |
73 private final String locale = "de"; | |
74 private final HttpClient client; | |
75 | |
76 private static final String exportFileDir = "D:" + File.separator; | |
9127 | 77 private static final String IGNORE_ERSTELLDATUM = "# Datum der Erstellung"; |
78 private static final String IGNORE_FLYS_VERSION = "# FLYS-Version:"; | |
9226 | 79 private static final String IGNORE_BEARBEITER = "NEIN_DOCH_NICHT";// "# Bearbeiter:"; |
9031 | 80 |
9127 | 81 private final Infotype infotype; |
9424 | 82 private static String userUuid; |
9031 | 83 private Collection collection; |
84 private Artifact artifact; | |
9422 | 85 private final IFilenameMapper fileName; |
9127 | 86 |
87 // common attributes | |
9422 | 88 private final ICalcMode iCalcMode; |
9127 | 89 private final double from; |
90 private final double to; | |
91 private final River river; | |
9031 | 92 |
9422 | 93 public AbstractModuleRunner(final Infotype infotype, final ICalcMode calcmode, final IFilenameMapper helloWorldFile, final double from, final double to, |
94 final River beispielfluss) throws ConnectionException, ServerException { | |
9127 | 95 // common attributes (evtl. doch in subklassen, evtl. Zwischenhierarchiestufe einführen |
96 | |
9422 | 97 this.iCalcMode = calcmode; |
9127 | 98 this.river = beispielfluss; |
99 this.from = from; | |
100 this.to = to; | |
101 | |
9031 | 102 this.infotype = infotype; |
9127 | 103 this.fileName = helloWorldFile; |
9031 | 104 |
105 // init | |
106 this.client = new HttpClientImpl(this.serverUrl, this.locale); | |
9424 | 107 if (AbstractModuleRunner.userUuid == null) |
108 AbstractModuleRunner.userUuid = makeUserUuid(this.serverUrl); | |
9031 | 109 this.collection = getCollection(); |
110 this.artifact = getArtifact(); | |
111 } | |
112 | |
9424 | 113 private static String makeUserUuid(final String serverUrl) throws ConnectionException { |
9497
d6d5ca6d4af0
Enabled logging of saml-group-name in log-ing logfile.
gernotbelger
parents:
9441
diff
changeset
|
114 final User user = new DefaultUser(USERNAME, PASSWORD, null, false, new ArrayList<String>(), new ArrayList<String>(), null); |
9424 | 115 final UserClient userClient = new UserClient(serverUrl); |
9031 | 116 Element userElement; |
117 | |
118 userElement = userClient.findUser(user); | |
119 return userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid"); | |
120 | |
121 } | |
122 | |
123 protected final Artifact getArtifact() throws ServerException, ConnectionException { | |
124 if (this.artifact == null) { | |
9127 | 125 this.artifact = ArtifactHelper.createArtifact(this.serverUrl, this.locale, this.infotype.name(), null); |
9031 | 126 setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz |
127 } | |
128 return this.artifact; | |
129 } | |
130 | |
9148 | 131 protected Collection getCollection() throws ConnectionException { |
9031 | 132 |
133 if (this.collection == null) { | |
134 // lazy-Loading | |
135 final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); | |
9424 | 136 final Document doc = (Document) this.client.createCollection(create, userUuid, new DocumentResponseHandler()); |
9031 | 137 final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE); |
138 final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); | |
139 this.collection = new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid); | |
140 } | |
141 return this.collection; | |
142 | |
143 } | |
144 | |
9127 | 145 private final void setCollection(final Collection collection) { |
9031 | 146 this.collection = collection; |
147 } | |
148 | |
9127 | 149 private final void setArtifact(final Artifact artifact) { |
9031 | 150 this.artifact = artifact; |
151 } | |
152 | |
153 public abstract void runTest(final boolean exportToFile) throws ConnectionException, ServerException, IOException; | |
154 | |
155 protected final void describeCollection() throws ConnectionException { | |
156 | |
157 final String uuid = getCollection().identifier(); | |
158 final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); | |
159 final Document response = (Document) this.client.doCollectionAction(describe, uuid, new DocumentResponseHandler()); | |
160 final Collection c = CollectionHelper.parseCollection(response); | |
161 setCollection(c); | |
162 | |
163 } | |
164 | |
165 protected final void feedAndGo(final Data[] data, final int reachableStateIndex) throws ConnectionException, ServerException { | |
166 feed(data); | |
9127 | 167 advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein. |
9031 | 168 } |
169 | |
9127 | 170 private final String getReachableStateByIndex(final Artifact artifact, final int index) { |
9031 | 171 |
172 final String[] states = artifact.getArtifactDescription().getReachableStates(); | |
173 if (states != null) { | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
174 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
175 if (states.length > index) |
9031 | 176 return states[index]; |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
177 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
178 return states[0]; |
9031 | 179 } |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
180 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
181 return ""; |
9031 | 182 } |
183 | |
9226 | 184 protected final void feed(final Data[] data) throws ServerException, ConnectionException { |
9031 | 185 final Document feed = ClientProtocolUtils.newFeedDocument(getArtifact().getUuid(), getArtifact().getHash(), createKVP(data)); |
186 final Document description = (Document) this.client.feed( | |
187 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), feed, | |
188 new DocumentResponseHandler()); | |
189 | |
190 final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); | |
191 | |
192 if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
193 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | |
194 } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
195 final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); | |
196 throw new ServerException(msg); | |
197 } | |
198 } | |
199 | |
9148 | 200 protected final Data[] extractPairData(final List<String> pairIds, final String dataName) { |
9228 | 201 final DataItem[] items = new DataItem[pairIds.size()]; |
9148 | 202 int i = 0; |
9228 | 203 final StringBuilder builder = new StringBuilder(); |
9148 | 204 for (final String pairId : pairIds) { |
9228 | 205 builder.append(pairId).append("#"); |
9148 | 206 i++; |
207 } | |
9228 | 208 final String allPairIds = builder.delete(builder.length() - 1, builder.length()).toString(); // letztes Gatter entfernen |
209 final DataItem[] item = new DataItem[] { new DefaultDataItem(allPairIds, allPairIds, allPairIds) }; | |
210 return new Data[] { new StringOptionsData(dataName, dataName, item) }; | |
9148 | 211 } |
212 | |
9127 | 213 private final String[][] createKVP(final Data[] data) { |
9031 | 214 if (data != null) { |
215 final String[][] kvp = new String[data.length][]; | |
216 | |
217 int i = 0; | |
218 | |
219 for (final Data d : data) { | |
220 final String key = d.getLabel(); | |
221 final String value = d.getStringValue(); | |
222 | |
223 kvp[i++] = new String[] { key, value }; | |
224 } | |
225 | |
226 return kvp; | |
227 } | |
228 return null; | |
229 } | |
230 | |
9127 | 231 private final void advance(final String target) throws ConnectionException, ServerException { |
9031 | 232 final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target); |
233 final Document description = (Document) this.client.advance( | |
234 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), advance, | |
235 new DocumentResponseHandler()); | |
236 | |
237 if (description == null) { | |
238 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); | |
239 } | |
240 | |
241 final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); | |
242 | |
243 if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) { | |
244 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | |
245 } | |
246 } | |
247 | |
248 /// ExportServiceImpl | |
9148 | 249 protected final void assertAndWriteToFile(final String mode, final boolean exportToFile) throws IOException { |
9031 | 250 |
251 final String type = "csv"; | |
252 | |
253 final String enc = "windows-1252";// req.getParameter("encoding"); | |
254 | |
9127 | 255 final URL expectedResource = getClass().getResource(this.fileName.getFilename()); |
9031 | 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 | |
9133
af73fdd96920
Title current/historical diff pair; limit on entries in diff'pair twin_panel
gernotbelger
parents:
9127
diff
changeset
|
270 Assert.assertEquals(expected, actual); |
9031 | 271 } |
272 | |
9127 | 273 private final String deleteErstelldatum(final String input) { |
9031 | 274 String result = ""; |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
275 final String[] lines = input.split(lineSeparator); |
9031 | 276 for (final String line : lines) { |
9148 | 277 if (!line.contains(AbstractModuleRunner.IGNORE_ERSTELLDATUM) && !line.contains(AbstractModuleRunner.IGNORE_FLYS_VERSION) |
278 && !line.contains(AbstractModuleRunner.IGNORE_BEARBEITER)) { | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
279 result = result + line + lineSeparator; |
9031 | 280 } |
281 } | |
282 return result; | |
283 } | |
284 | |
9127 | 285 public final void doGetWriteToDisk(final String mode) throws FileNotFoundException, IOException { |
9031 | 286 |
287 final String name = mode; | |
288 final String type = "csv"; | |
289 | |
9127 | 290 final String fn = name + System.currentTimeMillis() + "." + type; |
9031 | 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 | |
9127 | 305 protected final void selectCalcMode() throws ConnectionException, ServerException { |
306 | |
9422 | 307 /* Select ICalcMode */ |
308 final String calcmodeStr = this.iCalcMode.toString(); | |
9127 | 309 final Data dataCalcMode = new StringOptionsData("calculation_mode", "calculation_mode", |
310 new DataItem[] { new DefaultDataItem(calcmodeStr, calcmodeStr, calcmodeStr) }); | |
311 feedAndGo(new Data[] { dataCalcMode }, 0); | |
312 } | |
313 | |
314 protected final void selectRange() throws ConnectionException, ServerException { | |
9424 | 315 makeKmRange(this.from, this.to, "ld_from", "ld_to"); |
316 | |
317 } | |
318 | |
319 protected final void makeKmRange(final double from, final double to, final String from_key, final String to_key) | |
320 throws ConnectionException, ServerException { | |
9441 | 321 feedAndGo(makeFromToData(from_key, to_key), 0); |
322 } | |
323 | |
324 protected final Data[] makeFromToData(final String from_key, final String to_key) { | |
325 final String fromStr = String.valueOf(this.from); | |
326 final String toStr = String.valueOf(this.to); | |
9424 | 327 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) }); | |
9441 | 329 return new Data[] { dataFrom, dataTo }; |
9127 | 330 } |
331 | |
332 protected final void selectRiver() throws ConnectionException, ServerException { | |
333 final String riverStr = this.river.name(); | |
334 final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem(riverStr, riverStr, riverStr) }); | |
335 feedAndGo(new Data[] { data }, 0); | |
336 } | |
337 | |
338 protected final void export(final boolean exportToFile) throws IOException, ServerException { | |
9148 | 339 final OutputMode[] modes = getArtifact().getArtifactDescription().getOutputModes(); |
9226 | 340 boolean exportFacetCreated = false; |
9127 | 341 if (modes != null) { |
9148 | 342 for (final OutputMode mode : modes) { |
9226 | 343 if (mode.getDescription().contains("_export")) { |
9148 | 344 assertAndWriteToFile(mode.getName(), exportToFile); |
9226 | 345 exportFacetCreated = true; |
346 } | |
9148 | 347 } |
9226 | 348 |
9127 | 349 } |
9226 | 350 assert (exportFacetCreated == true); |
9127 | 351 } |
352 | |
9148 | 353 protected final String getRecommendationPairString(final SimpleRecommendation rec1, final SimpleRecommendation rec2) |
354 throws ConnectionException, ServerException { | |
355 | |
356 return rec1.getRecommendationPairString(rec2, getCollection(), this.serverUrl, this.locale); | |
357 } | |
9424 | 358 |
9441 | 359 protected final void feedAndGoData(final Data[] data) throws ConnectionException, ServerException { |
360 feedAndGo(data, 0); | |
361 } | |
362 | |
363 protected final void feedAndGoSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { | |
364 feedAndGo(new Data[] { getSimpleTextInput(key, value) }, 0); | |
365 } | |
366 | |
367 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) }); | |
369 } | |
370 | |
371 protected final Data[] addItemToExistingData(final Data[] existing, final Data add) { | |
372 final Data[] data = new Data[existing.length + 1]; | |
373 for (int i = 0; i < existing.length; i++) { | |
374 data[i] = existing[i]; | |
375 } | |
376 data[existing.length] = add; | |
377 return data; | |
9424 | 378 } |
379 | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
380 } |