Mercurial > dive4elements > river
annotate gwt-client/src/test/java/test/AbstractModuleRunner.java @ 9709:b74f817435fe
comment removed
author | dnt_bjoernsen <d.tironi@bjoernsen.de> |
---|---|
date | Wed, 27 Jan 2021 11:47:38 +0100 |
parents | 531a60b7af95 |
children |
rev | line source |
---|---|
9031 | 1 package test; |
2 | |
9570 | 3 import java.io.File; |
9031 | 4 import java.io.IOException; |
5 import java.util.ArrayList; | |
6 import java.util.List; | |
7 | |
8 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
9 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; | |
10 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; | |
11 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
12 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | |
13 import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; | |
14 import org.dive4elements.artifacts.httpclient.utils.XMLUtils; | |
15 import org.dive4elements.river.client.server.AdvanceServiceImpl; | |
16 import org.dive4elements.river.client.server.ArtifactHelper; | |
17 import org.dive4elements.river.client.server.CollectionHelper; | |
18 import org.dive4elements.river.client.server.CreateCollectionServiceImpl; | |
19 import org.dive4elements.river.client.server.FLYSArtifactCreator; | |
20 import org.dive4elements.river.client.server.FeedServiceImpl; | |
21 import org.dive4elements.river.client.server.auth.DefaultUser; | |
22 import org.dive4elements.river.client.server.auth.User; | |
23 import org.dive4elements.river.client.server.auth.UserClient; | |
24 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
25 import org.dive4elements.river.client.shared.model.Artifact; | |
26 import org.dive4elements.river.client.shared.model.Collection; | |
27 import org.dive4elements.river.client.shared.model.Data; | |
28 import org.dive4elements.river.client.shared.model.DataItem; | |
29 import org.dive4elements.river.client.shared.model.DefaultCollection; | |
9567 | 30 import org.dive4elements.river.client.shared.model.DefaultData; |
9031 | 31 import org.dive4elements.river.client.shared.model.DefaultDataItem; |
9127 | 32 import org.dive4elements.river.client.shared.model.OutputMode; |
9031 | 33 import org.dive4elements.river.client.shared.model.StringOptionsData; |
34 import org.junit.Assert; | |
35 import org.w3c.dom.Document; | |
36 import org.w3c.dom.Element; | |
37 | |
9422 | 38 import test.AbstractBerechnungsartenTester.River; |
9127 | 39 |
9031 | 40 /** |
41 * @author Domenico Nardi Tironi | |
42 * | |
43 */ | |
9148 | 44 public abstract class AbstractModuleRunner { |
9127 | 45 |
46 public enum Infotype { | |
9422 | 47 sinfo, uinfo, bundu |
9127 | 48 } |
9031 | 49 |
9422 | 50 private final static String USERNAME = "belger"; |
51 | |
52 private final static String PASSWORD = "belger"; | |
53 | |
9031 | 54 private final String serverUrl = "http://localhost:8181"; |
55 private final String locale = "de"; | |
56 private final HttpClient client; | |
57 | |
9127 | 58 private final Infotype infotype; |
9424 | 59 private static String userUuid; |
9031 | 60 private Collection collection; |
61 private Artifact artifact; | |
9127 | 62 |
63 // common attributes | |
9422 | 64 private final ICalcMode iCalcMode; |
9127 | 65 private final double from; |
66 private final double to; | |
67 private final River river; | |
9567 | 68 private final List<ModuleRunnerOutput> outputList; |
9031 | 69 |
9570 | 70 protected final File testFolder; |
71 | |
72 public AbstractModuleRunner(final File testFolder, final Infotype infotype, final ICalcMode calcmode, final double from, final double to, | |
73 final River beispielfluss) throws ConnectionException, ServerException { | |
9127 | 74 // common attributes (evtl. doch in subklassen, evtl. Zwischenhierarchiestufe einführen |
9567 | 75 this.outputList = new ArrayList<ModuleRunnerOutput>(); |
9422 | 76 this.iCalcMode = calcmode; |
9127 | 77 this.river = beispielfluss; |
78 this.from = from; | |
79 this.to = to; | |
9570 | 80 this.testFolder = testFolder; |
9127 | 81 |
9031 | 82 this.infotype = infotype; |
83 | |
84 // init | |
85 this.client = new HttpClientImpl(this.serverUrl, this.locale); | |
9424 | 86 if (AbstractModuleRunner.userUuid == null) |
87 AbstractModuleRunner.userUuid = makeUserUuid(this.serverUrl); | |
9031 | 88 this.collection = getCollection(); |
89 this.artifact = getArtifact(); | |
90 } | |
91 | |
9424 | 92 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
|
93 final User user = new DefaultUser(USERNAME, PASSWORD, null, false, new ArrayList<String>(), new ArrayList<String>(), null); |
9424 | 94 final UserClient userClient = new UserClient(serverUrl); |
9031 | 95 Element userElement; |
96 | |
97 userElement = userClient.findUser(user); | |
98 return userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid"); | |
99 | |
100 } | |
101 | |
102 protected final Artifact getArtifact() throws ServerException, ConnectionException { | |
103 if (this.artifact == null) { | |
9127 | 104 this.artifact = ArtifactHelper.createArtifact(this.serverUrl, this.locale, this.infotype.name(), null); |
9031 | 105 setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz |
106 } | |
107 return this.artifact; | |
108 } | |
109 | |
9148 | 110 protected Collection getCollection() throws ConnectionException { |
9031 | 111 |
112 if (this.collection == null) { | |
113 // lazy-Loading | |
114 final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); | |
9424 | 115 final Document doc = (Document) this.client.createCollection(create, userUuid, new DocumentResponseHandler()); |
9031 | 116 final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE); |
117 final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); | |
118 this.collection = new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid); | |
119 } | |
120 return this.collection; | |
121 | |
122 } | |
123 | |
9127 | 124 private final void setCollection(final Collection collection) { |
9031 | 125 this.collection = collection; |
126 } | |
127 | |
9127 | 128 private final void setArtifact(final Artifact artifact) { |
9031 | 129 this.artifact = artifact; |
130 } | |
131 | |
9567 | 132 public abstract void runTest() throws ConnectionException, ServerException, IOException; |
9031 | 133 |
134 protected final void describeCollection() throws ConnectionException { | |
135 | |
136 final String uuid = getCollection().identifier(); | |
137 final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); | |
138 final Document response = (Document) this.client.doCollectionAction(describe, uuid, new DocumentResponseHandler()); | |
139 final Collection c = CollectionHelper.parseCollection(response); | |
140 setCollection(c); | |
141 | |
142 } | |
143 | |
144 protected final void feedAndGo(final Data[] data, final int reachableStateIndex) throws ConnectionException, ServerException { | |
145 feed(data); | |
9127 | 146 advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein. |
9031 | 147 } |
148 | |
9567 | 149 protected final String getReachableStateByIndex(final Artifact artifact, final int index) { |
9031 | 150 |
151 final String[] states = artifact.getArtifactDescription().getReachableStates(); | |
152 if (states != null) { | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
153 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
154 if (states.length > index) |
9031 | 155 return states[index]; |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
156 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
157 return states[0]; |
9031 | 158 } |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
159 |
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
160 return ""; |
9031 | 161 } |
162 | |
9226 | 163 protected final void feed(final Data[] data) throws ServerException, ConnectionException { |
9031 | 164 final Document feed = ClientProtocolUtils.newFeedDocument(getArtifact().getUuid(), getArtifact().getHash(), createKVP(data)); |
165 final Document description = (Document) this.client.feed( | |
166 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), feed, | |
167 new DocumentResponseHandler()); | |
168 | |
169 final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); | |
170 | |
171 if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
172 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | |
173 } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
174 final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); | |
175 throw new ServerException(msg); | |
176 } | |
177 } | |
178 | |
9148 | 179 protected final Data[] extractPairData(final List<String> pairIds, final String dataName) { |
9228 | 180 final DataItem[] items = new DataItem[pairIds.size()]; |
9148 | 181 int i = 0; |
9228 | 182 final StringBuilder builder = new StringBuilder(); |
9148 | 183 for (final String pairId : pairIds) { |
9228 | 184 builder.append(pairId).append("#"); |
9148 | 185 i++; |
186 } | |
9228 | 187 final String allPairIds = builder.delete(builder.length() - 1, builder.length()).toString(); // letztes Gatter entfernen |
188 final DataItem[] item = new DataItem[] { new DefaultDataItem(allPairIds, allPairIds, allPairIds) }; | |
189 return new Data[] { new StringOptionsData(dataName, dataName, item) }; | |
9148 | 190 } |
191 | |
9127 | 192 private final String[][] createKVP(final Data[] data) { |
9031 | 193 if (data != null) { |
194 final String[][] kvp = new String[data.length][]; | |
195 | |
196 int i = 0; | |
197 | |
198 for (final Data d : data) { | |
199 final String key = d.getLabel(); | |
200 final String value = d.getStringValue(); | |
201 | |
202 kvp[i++] = new String[] { key, value }; | |
203 } | |
204 | |
205 return kvp; | |
206 } | |
207 return null; | |
208 } | |
209 | |
9567 | 210 protected final void advance(final String target) throws ConnectionException, ServerException { |
9031 | 211 final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target); |
212 final Document description = (Document) this.client.advance( | |
213 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), advance, | |
214 new DocumentResponseHandler()); | |
215 | |
216 if (description == null) { | |
217 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); | |
218 } | |
219 | |
220 final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); | |
221 | |
222 if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) { | |
223 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | |
224 } | |
225 } | |
226 | |
227 /// ExportServiceImpl | |
228 | |
9127 | 229 protected final void selectCalcMode() throws ConnectionException, ServerException { |
230 | |
9422 | 231 /* Select ICalcMode */ |
232 final String calcmodeStr = this.iCalcMode.toString(); | |
9127 | 233 final Data dataCalcMode = new StringOptionsData("calculation_mode", "calculation_mode", |
234 new DataItem[] { new DefaultDataItem(calcmodeStr, calcmodeStr, calcmodeStr) }); | |
235 feedAndGo(new Data[] { dataCalcMode }, 0); | |
236 } | |
237 | |
238 protected final void selectRange() throws ConnectionException, ServerException { | |
9567 | 239 makeKmRange(this.getFrom(), this.getTo(), "ld_from", "ld_to"); |
9424 | 240 |
241 } | |
242 | |
243 protected final void makeKmRange(final double from, final double to, final String from_key, final String to_key) | |
244 throws ConnectionException, ServerException { | |
9567 | 245 feedAndGo(makeFromToData(from_key, to_key, from, to), 0); |
9441 | 246 } |
247 | |
9567 | 248 protected final Data[] makeFromToData(final String from_key, final String to_key, final double _from, final double _to) { |
249 final String fromStr = String.valueOf(_from); | |
250 final String toStr = String.valueOf(_to); | |
9424 | 251 final Data dataFrom = new StringOptionsData(from_key, from_key, new DataItem[] { new DefaultDataItem(fromStr, fromStr, fromStr) }); |
252 final Data dataTo = new StringOptionsData(to_key, to_key, new DataItem[] { new DefaultDataItem(toStr, toStr, toStr) }); | |
9441 | 253 return new Data[] { dataFrom, dataTo }; |
9127 | 254 } |
255 | |
256 protected final void selectRiver() throws ConnectionException, ServerException { | |
257 final String riverStr = this.river.name(); | |
258 final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem(riverStr, riverStr, riverStr) }); | |
259 feedAndGo(new Data[] { data }, 0); | |
260 } | |
261 | |
9567 | 262 public final void runTests() throws IOException, ServerException { |
263 for (final ModuleRunnerOutput output : this.outputList) { | |
264 final String name = output.getOutputModename(); | |
265 if (!stateHasRequiredOutputMode(name)) { | |
266 Assert.fail("Unbekannter outputmode " + name); | |
267 } | |
9570 | 268 output.assertAndWriteToFile(this.client, this.collection.identifier(), this.testFolder); |
9567 | 269 } |
270 } | |
271 | |
272 private final boolean stateHasRequiredOutputMode(final String outputMode) throws ConnectionException, ServerException { | |
9148 | 273 final OutputMode[] modes = getArtifact().getArtifactDescription().getOutputModes(); |
9127 | 274 if (modes != null) { |
9148 | 275 for (final OutputMode mode : modes) { |
9567 | 276 if (mode.getName().toLowerCase().equals(outputMode)) |
277 return true; | |
9148 | 278 } |
9127 | 279 } |
9567 | 280 return false; |
9127 | 281 } |
282 | |
9148 | 283 protected final String getRecommendationPairString(final SimpleRecommendation rec1, final SimpleRecommendation rec2) |
284 throws ConnectionException, ServerException { | |
285 | |
286 return rec1.getRecommendationPairString(rec2, getCollection(), this.serverUrl, this.locale); | |
287 } | |
9424 | 288 |
9441 | 289 protected final void feedAndGoData(final Data[] data) throws ConnectionException, ServerException { |
290 feedAndGo(data, 0); | |
291 } | |
292 | |
293 protected final void feedAndGoSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { | |
294 feedAndGo(new Data[] { getSimpleTextInput(key, value) }, 0); | |
295 } | |
296 | |
297 protected Data getSimpleTextInput(final String key, final String value) throws ConnectionException, ServerException { | |
9567 | 298 return new DefaultData(key, key, key, new DataItem[] { new DefaultDataItem(value, value, value) }); |
9441 | 299 } |
300 | |
301 protected final Data[] addItemToExistingData(final Data[] existing, final Data add) { | |
302 final Data[] data = new Data[existing.length + 1]; | |
303 for (int i = 0; i < existing.length; i++) { | |
304 data[i] = existing[i]; | |
305 } | |
306 data[existing.length] = add; | |
307 return data; | |
9424 | 308 } |
309 | |
9567 | 310 public double getTo() { |
311 return this.to; | |
312 } | |
313 | |
314 public double getFrom() { | |
315 return this.from; | |
316 } | |
317 | |
318 public final AbstractModuleRunner addOutputTest(final FilenameSupplier filename, final String outputModeName, final String currentKm) | |
319 throws ConnectionException { | |
320 this.outputList.add(new ModuleRunnerOutput(filename, outputModeName, currentKm)); | |
321 return this; | |
322 } | |
323 | |
9210
de55d9a94796
Fixed: was using java 7 features in java 6 code
gernotbelger
parents:
9148
diff
changeset
|
324 } |