Mercurial > dive4elements > river
comparison gwt-client/src/test/java/test/SuperTest.java @ 9031:efd2de78d158
work on unit testing
author | gernotbelger |
---|---|
date | Fri, 27 Apr 2018 11:34:04 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9030:4dc047fb3633 | 9031:efd2de78d158 |
---|---|
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.HashMap; | |
22 import java.util.List; | |
23 | |
24 import org.apache.commons.io.FileUtils; | |
25 import org.apache.commons.io.IOUtils; | |
26 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
27 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; | |
28 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; | |
29 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
30 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | |
31 import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; | |
32 import org.dive4elements.artifacts.httpclient.utils.XMLUtils; | |
33 import org.dive4elements.river.client.client.ui.NilDatacageTwinPanelInfo; | |
34 import org.dive4elements.river.client.client.ui.RecommandationUtils; | |
35 import org.dive4elements.river.client.server.AdvanceServiceImpl; | |
36 import org.dive4elements.river.client.server.ArtifactHelper; | |
37 import org.dive4elements.river.client.server.CollectionHelper; | |
38 import org.dive4elements.river.client.server.CreateCollectionServiceImpl; | |
39 import org.dive4elements.river.client.server.FLYSArtifactCreator; | |
40 import org.dive4elements.river.client.server.FeedServiceImpl; | |
41 import org.dive4elements.river.client.server.LoadArtifactServiceImpl; | |
42 import org.dive4elements.river.client.server.auth.DefaultUser; | |
43 import org.dive4elements.river.client.server.auth.User; | |
44 import org.dive4elements.river.client.server.auth.UserClient; | |
45 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
46 import org.dive4elements.river.client.shared.model.Artifact; | |
47 import org.dive4elements.river.client.shared.model.Collection; | |
48 import org.dive4elements.river.client.shared.model.Data; | |
49 import org.dive4elements.river.client.shared.model.DataItem; | |
50 import org.dive4elements.river.client.shared.model.DefaultCollection; | |
51 import org.dive4elements.river.client.shared.model.DefaultDataItem; | |
52 import org.dive4elements.river.client.shared.model.Recommendation; | |
53 import org.dive4elements.river.client.shared.model.StringOptionsData; | |
54 import org.junit.Assert; | |
55 import org.w3c.dom.Document; | |
56 import org.w3c.dom.Element; | |
57 | |
58 /** | |
59 * @author Domenico Nardi Tironi | |
60 * | |
61 */ | |
62 public abstract class SuperTest { | |
63 | |
64 private final String serverUrl = "http://localhost:8181"; | |
65 private final String locale = "de"; | |
66 private final HttpClient client; | |
67 | |
68 private static final String exportFileDir = "D:" + File.separator; | |
69 private static final String erstellDatumSearchString = "# Datum der Erstellung"; | |
70 | |
71 private final String username; | |
72 private final String password; | |
73 private final String infotype; | |
74 private final String userUuid; | |
75 private Collection collection; | |
76 private Artifact artifact; | |
77 | |
78 private final List<String> pairIds = new ArrayList<String>(); | |
79 | |
80 public SuperTest(final String username, final String password, final String infotype) throws ConnectionException, ServerException { | |
81 this.username = username; | |
82 this.password = password; | |
83 this.infotype = infotype; | |
84 | |
85 // init | |
86 this.client = new HttpClientImpl(this.serverUrl, this.locale); | |
87 this.userUuid = makeUserUuid(); | |
88 this.collection = getCollection(); | |
89 this.artifact = getArtifact(); | |
90 } | |
91 | |
92 private String makeUserUuid() throws ConnectionException { | |
93 final User user = new DefaultUser(this.username, this.password, null, false, new ArrayList<String>(), new ArrayList<String>()); | |
94 final UserClient userClient = new UserClient(this.serverUrl); | |
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) { | |
104 this.artifact = ArtifactHelper.createArtifact(this.serverUrl, this.locale, this.infotype, null); | |
105 setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz | |
106 } | |
107 return this.artifact; | |
108 } | |
109 | |
110 private Collection getCollection() throws ConnectionException { | |
111 | |
112 if (this.collection == null) { | |
113 // lazy-Loading | |
114 final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); | |
115 final Document doc = (Document) this.client.createCollection(create, this.userUuid, new DocumentResponseHandler()); | |
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 | |
124 private void setCollection(final Collection collection) { | |
125 this.collection = collection; | |
126 } | |
127 | |
128 private void setArtifact(final Artifact artifact) { | |
129 this.artifact = artifact; | |
130 } | |
131 | |
132 public abstract void runTest(final boolean exportToFile) throws ConnectionException, ServerException, IOException; | |
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); | |
146 advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein... TODO: feedAndgo(data,string) | |
147 // bauen, falls irgendwann erforderlich | |
148 } | |
149 | |
150 private String getReachableStateByIndex(final Artifact artifact, final int index) { | |
151 | |
152 final String[] states = artifact.getArtifactDescription().getReachableStates(); | |
153 if (states != null) { | |
154 if (states.length > index) { | |
155 return states[index]; | |
156 } else { | |
157 return states[0]; | |
158 } | |
159 } else { | |
160 return ""; | |
161 } | |
162 } | |
163 | |
164 private void feed(final Data[] data) throws ServerException, ConnectionException { | |
165 final Document feed = ClientProtocolUtils.newFeedDocument(getArtifact().getUuid(), getArtifact().getHash(), createKVP(data)); | |
166 final Document description = (Document) this.client.feed( | |
167 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), feed, | |
168 new DocumentResponseHandler()); | |
169 | |
170 final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); | |
171 | |
172 if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
173 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | |
174 } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { | |
175 final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); | |
176 throw new ServerException(msg); | |
177 } | |
178 } | |
179 | |
180 private String[][] createKVP(final Data[] data) { | |
181 if (data != null) { | |
182 final String[][] kvp = new String[data.length][]; | |
183 | |
184 int i = 0; | |
185 | |
186 for (final Data d : data) { | |
187 // final DataItem[] items = d.getItems(); | |
188 final String key = d.getLabel(); | |
189 final String value = d.getStringValue(); | |
190 | |
191 kvp[i++] = new String[] { key, value }; | |
192 } | |
193 | |
194 return kvp; | |
195 } | |
196 return null; | |
197 } | |
198 | |
199 private void advance(final String target) throws ConnectionException, ServerException { | |
200 final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target); | |
201 final Document description = (Document) this.client.advance( | |
202 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), advance, | |
203 new DocumentResponseHandler()); | |
204 | |
205 if (description == null) { | |
206 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); | |
207 } | |
208 | |
209 final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); | |
210 | |
211 if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) { | |
212 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); | |
213 } | |
214 } | |
215 | |
216 protected final Artifact[] loadMany(final Recommendation[] recoms, final String factory) throws ServerException, ConnectionException { | |
217 final ArrayList<Artifact> artifacts = new ArrayList<Artifact>(); | |
218 final HashMap<Recommendation, Artifact> cloneMap = new HashMap<Recommendation, Artifact>(); | |
219 | |
220 for (final Recommendation recom : recoms) { | |
221 | |
222 final Artifact prevClone = cloneMap.get(recom); | |
223 if (prevClone != null) { | |
224 | |
225 artifacts.add(prevClone); | |
226 } else { | |
227 // Not already cloned. | |
228 final String realFactory = factory != null ? factory : recom.getFactory(); | |
229 | |
230 final Artifact clone = ArtifactHelper.createArtifact(this.serverUrl, this.locale, realFactory, recom); | |
231 | |
232 if (clone != null) { | |
233 final Collection c = CollectionHelper.addArtifact(getCollection(), clone, this.serverUrl, this.locale); | |
234 | |
235 if (c != null) { | |
236 artifacts.add(clone); | |
237 // Remember we cloned a recommendation like this. | |
238 cloneMap.put(recom, clone); | |
239 } else { | |
240 throw new ServerException(LoadArtifactServiceImpl.ERROR_LOAD_ARTIFACT); | |
241 } | |
242 } | |
243 } | |
244 } | |
245 return artifacts.toArray(new Artifact[artifacts.size()]); | |
246 | |
247 } | |
248 | |
249 /// ExportServiceImpl | |
250 public void doGet(final String mode, final boolean exportToFile) throws IOException { | |
251 | |
252 final String name = mode; | |
253 final String type = "csv"; | |
254 | |
255 final String fn = name + "." + type; // TODO: make filename unique | |
256 final String enc = "windows-1252";// req.getParameter("encoding"); | |
257 | |
258 final URL expectedResource = getClass().getResource("/sinfo/flowdepthminmax/" + fn); | |
259 final Document attr = null; | |
260 final Document request = ClientProtocolUtils.newOutCollectionDocument(getCollection().identifier(), mode, type, attr); | |
261 | |
262 final InputStream response = this.client.collectionOut(request, getCollection().identifier(), mode); | |
263 | |
264 final String actual = deleteErstelldatum(IOUtils.toString(response, "UTF-8")); | |
265 | |
266 final String expected = deleteErstelldatum(FileUtils.readFileToString(new File(expectedResource.getFile()), enc)); | |
267 | |
268 // if (!actual.equals(expected)) { | |
269 if (exportToFile) { | |
270 doGetWriteToDisk(mode); // TODO: WENN der Test negativ ausfällt, Datei abspeichern -> Diskussion | |
271 } | |
272 Assert.assertEquals(actual, expected); | |
273 | |
274 } | |
275 | |
276 private String deleteErstelldatum(final String input) { | |
277 String result = ""; | |
278 final String[] lines = input.split(System.lineSeparator()); | |
279 for (final String line : lines) { | |
280 if (!line.contains(SuperTest.erstellDatumSearchString)) { | |
281 result = result + line + System.lineSeparator(); | |
282 } | |
283 } | |
284 return result; | |
285 } | |
286 | |
287 public void doGetWriteToDisk(final String mode) throws FileNotFoundException, IOException { | |
288 | |
289 final String name = mode; | |
290 final String type = "csv"; | |
291 | |
292 final String fn = name + "." + type; // TODO: make filename unique | |
293 final String enc = "windows-1252"; | |
294 | |
295 final String filepath = exportFileDir + fn; | |
296 | |
297 final Document attr = null; | |
298 final Document request = ClientProtocolUtils.newOutCollectionDocument(getCollection().identifier(), mode, type, attr); | |
299 | |
300 final InputStream response = this.client.collectionOut(request, getCollection().identifier(), mode); | |
301 final InputStreamReader in = new InputStreamReader(response, "UTF-8"); | |
302 | |
303 IOUtils.copy(in, new FileOutputStream(filepath), enc); | |
304 | |
305 } | |
306 | |
307 public void addRecommendationPair(final String[] rec1, final String[] rec2) throws ConnectionException, ServerException { | |
308 final Recommendation recom1 = new Recommendation(rec1[0], rec1[1], rec1[2]); | |
309 final Recommendation recom2 = new Recommendation(rec2[0], rec2[1], rec2[2]); | |
310 final Artifact[] artifacts = loadMany(new Recommendation[] { recom1, recom2 }, null); | |
311 final String rec1String = RecommandationUtils.createDataString(artifacts[0].getUuid(), recom1, new NilDatacageTwinPanelInfo("xxxx")); | |
312 final String rec2String = RecommandationUtils.createDataString(artifacts[1].getUuid(), recom2, new NilDatacageTwinPanelInfo("xxxx")); | |
313 // TODO: check display name recom1.getDisplayName() TODO: makeDisplayName | |
314 final String combinedIdNeu = rec1String + "#" + rec2String; | |
315 this.pairIds.add(combinedIdNeu); | |
316 } | |
317 | |
318 protected Data[] getPairData() { | |
319 final Data[] data = new Data[this.pairIds.size()]; | |
320 int i = 0; | |
321 for (final String pairId : this.pairIds) { | |
322 final Data pair = new StringOptionsData("diffids", "diffids", new DataItem[] { new DefaultDataItem(pairId, pairId, pairId) }); | |
323 data[i] = pair; | |
324 i++; | |
325 } | |
326 return data; | |
327 } | |
328 } |