Mercurial > dive4elements > river
annotate gwt-client/src/test/java/test/AbstractModuleRunner.java @ 9210:de55d9a94796
Fixed: was using java 7 features in java 6 code
author | gernotbelger |
---|---|
date | Tue, 03 Jul 2018 13:00:56 +0200 |
parents | 48d87af1243e |
children | 83aee0942eae |
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 | |
9127 | 54 import test.BerechnungsartenTester.CalcMode; |
55 import test.BerechnungsartenTester.FilenameMapper; | |
56 import test.BerechnungsartenTester.River; | |
57 | |
9031 | 58 /** |
59 * @author Domenico Nardi Tironi | |
60 * | |
61 */ | |
9148 | 62 public abstract class AbstractModuleRunner { |
9127 | 63 |
64 public enum Infotype { | |
65 sinfo | |
66 } | |
9031 | 67 |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
68 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
|
69 |
9031 | 70 private final String serverUrl = "http://localhost:8181"; |
71 private final String locale = "de"; | |
72 private final HttpClient client; | |
73 | |
74 private static final String exportFileDir = "D:" + File.separator; | |
9127 | 75 private static final String IGNORE_ERSTELLDATUM = "# Datum der Erstellung"; |
76 private static final String IGNORE_FLYS_VERSION = "# FLYS-Version:"; | |
77 private static final String IGNORE_BEARBEITER = "# Bearbeiter:"; | |
9031 | 78 |
79 private final String username; | |
80 private final String password; | |
9127 | 81 private final Infotype infotype; |
9031 | 82 private final String userUuid; |
83 private Collection collection; | |
84 private Artifact artifact; | |
9127 | 85 private final FilenameMapper fileName; |
86 | |
87 // common attributes | |
88 private final CalcMode calcMode; | |
89 private final double from; | |
90 private final double to; | |
91 private final River river; | |
9031 | 92 |
9148 | 93 public AbstractModuleRunner(final String username, final String password, final Infotype infotype, final CalcMode sinfoCalcFlowDepth, |
9127 | 94 final FilenameMapper helloWorldFile, final double from, final double to, final River beispielfluss) throws ConnectionException, ServerException { |
95 // common attributes (evtl. doch in subklassen, evtl. Zwischenhierarchiestufe einführen | |
96 | |
97 this.calcMode = sinfoCalcFlowDepth; | |
98 this.river = beispielfluss; | |
99 this.from = from; | |
100 this.to = to; | |
101 | |
9031 | 102 this.username = username; |
103 this.password = password; | |
104 this.infotype = infotype; | |
9127 | 105 this.fileName = helloWorldFile; |
9031 | 106 |
107 // init | |
108 this.client = new HttpClientImpl(this.serverUrl, this.locale); | |
109 this.userUuid = makeUserUuid(); | |
110 this.collection = getCollection(); | |
111 this.artifact = getArtifact(); | |
112 } | |
113 | |
114 private String makeUserUuid() throws ConnectionException { | |
115 final User user = new DefaultUser(this.username, this.password, null, false, new ArrayList<String>(), new ArrayList<String>()); | |
116 final UserClient userClient = new UserClient(this.serverUrl); | |
117 Element userElement; | |
118 | |
119 userElement = userClient.findUser(user); | |
120 return userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid"); | |
121 | |
122 } | |
123 | |
124 protected final Artifact getArtifact() throws ServerException, ConnectionException { | |
125 if (this.artifact == null) { | |
9127 | 126 this.artifact = ArtifactHelper.createArtifact(this.serverUrl, this.locale, this.infotype.name(), null); |
9031 | 127 setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz |
128 } | |
129 return this.artifact; | |
130 } | |
131 | |
9148 | 132 protected Collection getCollection() throws ConnectionException { |
9031 | 133 |
134 if (this.collection == null) { | |
135 // lazy-Loading | |
136 final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); | |
137 final Document doc = (Document) this.client.createCollection(create, this.userUuid, new DocumentResponseHandler()); | |
138 final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE); | |
139 final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); | |
140 this.collection = new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid); | |
141 } | |
142 return this.collection; | |
143 | |
144 } | |
145 | |
9127 | 146 private final void setCollection(final Collection collection) { |
9031 | 147 this.collection = collection; |
148 } | |
149 | |
9127 | 150 private final void setArtifact(final Artifact artifact) { |
9031 | 151 this.artifact = artifact; |
152 } | |
153 | |
154 public abstract void runTest(final boolean exportToFile) throws ConnectionException, ServerException, IOException; | |
155 | |
156 protected final void describeCollection() throws ConnectionException { | |
157 | |
158 final String uuid = getCollection().identifier(); | |
159 final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); | |
160 final Document response = (Document) this.client.doCollectionAction(describe, uuid, new DocumentResponseHandler()); | |
161 final Collection c = CollectionHelper.parseCollection(response); | |
162 setCollection(c); | |
163 | |
164 } | |
165 | |
166 protected final void feedAndGo(final Data[] data, final int reachableStateIndex) throws ConnectionException, ServerException { | |
167 feed(data); | |
9127 | 168 advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein. |
169 | |
9031 | 170 } |
171 | |
9127 | 172 private final String getReachableStateByIndex(final Artifact artifact, final int index) { |
9031 | 173 |
174 final String[] states = artifact.getArtifactDescription().getReachableStates(); | |
175 if (states != null) { | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
176 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
177 if (states.length > index) |
9031 | 178 return states[index]; |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
179 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
180 return states[0]; |
9031 | 181 } |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
182 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
183 return ""; |
9031 | 184 } |
185 | |
9127 | 186 private final void feed(final Data[] data) throws ServerException, ConnectionException { |
9031 | 187 final Document feed = ClientProtocolUtils.newFeedDocument(getArtifact().getUuid(), getArtifact().getHash(), createKVP(data)); |
188 final Document description = (Document) this.client.feed( | |
189 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), feed, | |
190 new DocumentResponseHandler()); | |
191 | |
192 final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); | |
193 | |
194 if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
195 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | |
196 } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
197 final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); | |
198 throw new ServerException(msg); | |
199 } | |
200 } | |
201 | |
9148 | 202 protected final Data[] extractPairData(final List<String> pairIds, final String dataName) { |
203 final Data[] data = new Data[pairIds.size()]; | |
204 int i = 0; | |
205 for (final String pairId : pairIds) { | |
206 final Data pair = new StringOptionsData(dataName, dataName, new DataItem[] { new DefaultDataItem(pairId, pairId, pairId) }); | |
207 data[i] = pair; | |
208 i++; | |
209 } | |
210 return data; | |
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 | |
307 /* Select CalcMode */ | |
308 final String calcmodeStr = this.calcMode.name(); | |
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 { | |
315 final String fromStr = String.valueOf(this.from); | |
316 final String toStr = String.valueOf(this.to); | |
317 final Data dataFrom = new StringOptionsData("ld_from", "ld_from", new DataItem[] { new DefaultDataItem(fromStr, fromStr, fromStr) }); | |
318 final Data dataTo = new StringOptionsData("ld_to", "ld_to", new DataItem[] { new DefaultDataItem(toStr, toStr, toStr) }); | |
319 final Data[] rangeFromToDetermined = new Data[] { dataFrom, dataTo }; | |
320 | |
321 feedAndGo(rangeFromToDetermined, 0); | |
322 } | |
323 | |
324 protected final void selectRiver() throws ConnectionException, ServerException { | |
325 final String riverStr = this.river.name(); | |
326 final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem(riverStr, riverStr, riverStr) }); | |
327 feedAndGo(new Data[] { data }, 0); | |
328 } | |
329 | |
330 protected final void export(final boolean exportToFile) throws IOException, ServerException { | |
9148 | 331 final OutputMode[] modes = getArtifact().getArtifactDescription().getOutputModes(); |
9127 | 332 if (modes != null) { |
9148 | 333 for (final OutputMode mode : modes) { |
334 if (mode.getDescription().contains("_export")) | |
335 assertAndWriteToFile(mode.getName(), exportToFile); | |
336 } | |
9127 | 337 } |
338 } | |
339 | |
9148 | 340 protected final String getRecommendationPairString(final SimpleRecommendation rec1, final SimpleRecommendation rec2) |
341 throws ConnectionException, ServerException { | |
342 | |
343 return rec1.getRecommendationPairString(rec2, getCollection(), this.serverUrl, this.locale); | |
344 } | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
345 } |