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