9028
|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by |
|
3 * Björnsen Beratende Ingenieure GmbH |
|
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt |
|
5 * |
|
6 * This file is Free Software under the GNU AGPL (>=v3) |
|
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
8 * documentation coming with Dive4Elements River for details. |
|
9 */ |
|
10 package org.dive4elements.river.client.test; |
|
11 |
|
12 import java.io.File; |
|
13 import java.io.FileOutputStream; |
|
14 import java.io.IOException; |
|
15 import java.io.InputStreamReader; |
|
16 import java.io.OutputStream; |
|
17 import java.io.OutputStreamWriter; |
|
18 import java.util.ArrayList; |
|
19 import java.util.HashMap; |
|
20 |
|
21 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; |
|
22 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; |
|
23 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; |
|
24 import org.dive4elements.artifacts.httpclient.http.HttpClient; |
|
25 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; |
|
26 import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; |
|
27 import org.dive4elements.artifacts.httpclient.utils.XMLUtils; |
|
28 import org.dive4elements.river.client.server.AdvanceServiceImpl; |
|
29 import org.dive4elements.river.client.server.ArtifactHelper; |
|
30 import org.dive4elements.river.client.server.CollectionHelper; |
|
31 import org.dive4elements.river.client.server.CreateCollectionServiceImpl; |
|
32 import org.dive4elements.river.client.server.FLYSArtifactCreator; |
|
33 import org.dive4elements.river.client.server.FeedServiceImpl; |
|
34 import org.dive4elements.river.client.server.LoadArtifactServiceImpl; |
|
35 import org.dive4elements.river.client.server.auth.DefaultUser; |
|
36 import org.dive4elements.river.client.server.auth.User; |
|
37 import org.dive4elements.river.client.server.auth.UserClient; |
|
38 import org.dive4elements.river.client.shared.exceptions.ServerException; |
|
39 import org.dive4elements.river.client.shared.model.Artifact; |
|
40 import org.dive4elements.river.client.shared.model.Collection; |
|
41 import org.dive4elements.river.client.shared.model.Data; |
|
42 import org.dive4elements.river.client.shared.model.DefaultCollection; |
|
43 import org.dive4elements.river.client.shared.model.Recommendation; |
|
44 import org.w3c.dom.Document; |
|
45 import org.w3c.dom.Element; |
|
46 |
|
47 /** |
|
48 * @author Domenico Nardi Tironi |
|
49 * |
|
50 */ |
|
51 public abstract class SuperProof { |
|
52 |
|
53 private final String serverUrl = "http://localhost:8181"; |
|
54 private final String locale = "de"; |
|
55 private final HttpClient client; |
|
56 |
|
57 private final String username; |
|
58 private final String password; |
|
59 private final String infotype; |
|
60 private final String userUuid; |
|
61 private Collection collection; |
|
62 private Artifact artifact; |
|
63 |
|
64 public SuperProof(final String username, final String password, final String infotype) { |
|
65 this.username = username; |
|
66 this.password = password; |
|
67 this.infotype = infotype; |
|
68 |
|
69 // init |
|
70 this.client = new HttpClientImpl(this.serverUrl, this.locale); |
|
71 this.userUuid = makeUserUuid(); |
|
72 this.collection = getCollection(); |
|
73 this.artifact = getArtifact(); |
|
74 } |
|
75 |
|
76 private String makeUserUuid() { |
|
77 final User user = new DefaultUser(this.username, this.password, null, false, new ArrayList<String>(), new ArrayList<String>()); |
|
78 final UserClient userClient = new UserClient(this.serverUrl); |
|
79 Element userElement; |
|
80 try { |
|
81 userElement = userClient.findUser(user); |
|
82 return userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid"); |
|
83 } |
|
84 catch (final ConnectionException e) { |
|
85 e.printStackTrace(); |
|
86 } |
|
87 return ""; |
|
88 } |
|
89 |
|
90 protected final Artifact getArtifact() { |
|
91 |
|
92 /* Init Collection */ |
|
93 if (this.artifact == null) |
|
94 try { |
|
95 |
|
96 this.artifact = ArtifactHelper.createArtifact(this.serverUrl, this.locale, this.infotype, null); |
|
97 setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz |
|
98 } |
|
99 catch (final ServerException e) { |
|
100 // TODO Auto-generated catch block |
|
101 e.printStackTrace(); |
|
102 } |
|
103 return this.artifact; |
|
104 } |
|
105 |
|
106 private Collection getCollection() { |
|
107 |
|
108 if (this.collection == null) { |
|
109 try { |
|
110 // lazy-Loading |
|
111 final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); |
|
112 final Document doc = (Document) this.client.createCollection(create, this.userUuid, new DocumentResponseHandler()); |
|
113 final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE); |
|
114 final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); |
|
115 this.collection = new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid); |
|
116 } |
|
117 catch (final ConnectionException e) { |
|
118 // TODO Auto-generated catch block |
|
119 e.printStackTrace(); |
|
120 } |
|
121 } |
|
122 return this.collection; |
|
123 } |
|
124 |
|
125 private void setCollection(final Collection collection) { |
|
126 this.collection = collection; |
|
127 } |
|
128 |
|
129 private void setArtifact(final Artifact artifact) { |
|
130 this.artifact = artifact; |
|
131 } |
|
132 |
|
133 // TODO: MAKE THIS CLASS ABSTRACT AND OVERRIDE runTest in children |
|
134 public abstract void runTest(); |
|
135 |
|
136 protected final void describeCollection() { |
|
137 try { |
|
138 final String uuid = getCollection().identifier(); |
|
139 final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); |
|
140 final Document response = (Document) this.client.doCollectionAction(describe, uuid, new DocumentResponseHandler()); |
|
141 final Collection c = CollectionHelper.parseCollection(response); |
|
142 setCollection(c); |
|
143 } |
|
144 catch (final ConnectionException e) { |
|
145 e.printStackTrace(); |
|
146 } |
|
147 } |
|
148 |
|
149 protected final void feedAndGo(final Data[] data, final int reachableStateIndex) { |
|
150 try { |
|
151 feed(data); |
|
152 advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein... TODO: feedAndgo(data,string) |
|
153 // bauen |
|
154 } |
|
155 catch (final ConnectionException e) { |
|
156 e.printStackTrace(); |
|
157 } |
|
158 catch (final ServerException e) { |
|
159 e.printStackTrace(); |
|
160 } |
|
161 } |
|
162 |
|
163 private String getReachableStateByIndex(final Artifact artifact, final int index) { |
|
164 |
|
165 final String[] states = artifact.getArtifactDescription().getReachableStates(); |
|
166 if (states != null) { |
|
167 if (states.length > index) { |
|
168 return states[index]; |
|
169 } else { |
|
170 return states[0]; |
|
171 } |
|
172 } else { |
|
173 return ""; |
|
174 } |
|
175 } |
|
176 |
|
177 private void feed(final Data[] data) throws ServerException, ConnectionException { |
|
178 final Document feed = ClientProtocolUtils.newFeedDocument(getArtifact().getUuid(), getArtifact().getHash(), createKVP(data)); |
|
179 |
|
180 final Document description = (Document) this.client.feed( |
|
181 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), feed, |
|
182 new DocumentResponseHandler()); |
|
183 |
|
184 final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); |
|
185 |
|
186 if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { |
|
187 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); |
|
188 } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { |
|
189 final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); |
|
190 throw new ServerException(msg); |
|
191 } |
|
192 |
|
193 // throw new ServerException(FeedServiceImpl.ERROR_FEED_DATA); |
|
194 } |
|
195 |
|
196 private String[][] createKVP(final Data[] data) { |
|
197 if (data != null) { |
|
198 final String[][] kvp = new String[data.length][]; |
|
199 |
|
200 int i = 0; |
|
201 |
|
202 for (final Data d : data) { |
|
203 // final DataItem[] items = d.getItems(); |
|
204 final String key = d.getLabel(); |
|
205 final String value = d.getStringValue(); |
|
206 |
|
207 kvp[i++] = new String[] { key, value }; |
|
208 } |
|
209 |
|
210 return kvp; |
|
211 } |
|
212 return null; |
|
213 } |
|
214 |
|
215 private void advance(final String target) throws ConnectionException, ServerException { |
|
216 final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target); |
|
217 // final HttpClient client = new HttpClientImpl(url, locale); |
|
218 |
|
219 final Document description = (Document) this.client.advance( |
|
220 new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), advance, |
|
221 new DocumentResponseHandler()); |
|
222 |
|
223 if (description == null) { |
|
224 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); |
|
225 } |
|
226 |
|
227 final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); |
|
228 |
|
229 if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) { |
|
230 setArtifact((Artifact) new FLYSArtifactCreator().create(description)); |
|
231 } |
|
232 |
|
233 // throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); |
|
234 } |
|
235 |
|
236 protected final Artifact[] loadMany(final Recommendation[] recoms, final String factory) { |
|
237 try { |
|
238 final ArrayList<Artifact> artifacts = new ArrayList<Artifact>(); |
|
239 final HashMap<Recommendation, Artifact> cloneMap = new HashMap<Recommendation, Artifact>(); |
|
240 |
|
241 for (final Recommendation recom : recoms) { |
|
242 |
|
243 final Artifact prevClone = cloneMap.get(recom); |
|
244 if (prevClone != null) { |
|
245 |
|
246 artifacts.add(prevClone); |
|
247 } else { |
|
248 // Not already cloned. |
|
249 final String realFactory = factory != null ? factory : recom.getFactory(); |
|
250 |
|
251 final Artifact clone = ArtifactHelper.createArtifact(this.serverUrl, this.locale, realFactory, recom); |
|
252 |
|
253 if (clone != null) { |
|
254 final Collection c = CollectionHelper.addArtifact(getCollection(), clone, this.serverUrl, this.locale); |
|
255 |
|
256 if (c != null) { |
|
257 artifacts.add(clone); |
|
258 // Remember we cloned a recommendation like this. |
|
259 cloneMap.put(recom, clone); |
|
260 } else { |
|
261 throw new ServerException(LoadArtifactServiceImpl.ERROR_LOAD_ARTIFACT); |
|
262 } |
|
263 } |
|
264 } |
|
265 } |
|
266 return artifacts.toArray(new Artifact[artifacts.size()]); |
|
267 } |
|
268 catch (final ServerException e) { |
|
269 e.printStackTrace(); |
|
270 } |
|
271 return null; |
|
272 } |
|
273 |
|
274 /// ExportServiceImpl |
|
275 public void doGet(final String mode) { |
|
276 try { |
|
277 |
|
278 final String name = mode; |
|
279 final String type = "csv"; |
|
280 |
|
281 final String fn = name + "." + type; // TODO: make filename unique |
|
282 final String enc = "windows-1252";// req.getParameter("encoding"); |
|
283 |
|
284 final OutputStream out = new FileOutputStream(new File("D:" + File.separator + fn)); |
|
285 final Document attr = null; |
|
286 final Document request = ClientProtocolUtils.newOutCollectionDocument(getCollection().identifier(), mode, type, attr); |
|
287 // final HttpClient client = new HttpClientImpl(serverUrl, locale); |
|
288 |
|
289 if (enc != null) { |
|
290 final InputStreamReader in = new InputStreamReader(this.client.collectionOut(request, getCollection().identifier(), mode), "UTF-8"); |
|
291 try { |
|
292 final OutputStreamWriter encOut = new OutputStreamWriter(out, enc); |
|
293 final char buf[] = new char[4096]; |
|
294 int c; |
|
295 while ((c = in.read(buf, 0, buf.length)) >= 0) { |
|
296 encOut.write(buf, 0, c); |
|
297 } |
|
298 encOut.flush(); |
|
299 encOut.close(); |
|
300 } finally { |
|
301 in.close(); |
|
302 } |
|
303 } |
|
304 } |
|
305 catch (final IOException ioe) { |
|
306 ioe.printStackTrace(); |
|
307 } |
|
308 } |
|
309 |
|
310 } |