9567
|
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 test; |
|
11 |
|
12 import java.io.ByteArrayInputStream; |
|
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.nio.charset.Charset; |
|
21 import java.util.HashMap; |
|
22 import java.util.Map; |
|
23 |
|
24 import org.apache.commons.io.FileUtils; |
|
25 import org.apache.commons.io.IOUtils; |
|
26 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; |
|
27 import org.dive4elements.artifacts.httpclient.http.HttpClient; |
|
28 import org.dive4elements.river.client.server.ChartServiceHelper; |
|
29 import org.junit.Assert; |
|
30 import org.w3c.dom.Document; |
|
31 |
|
32 /** |
|
33 * @author Domenico Nardi Tironi |
|
34 * |
|
35 */ |
|
36 |
|
37 public class ModuleRunnerOutput { |
|
38 |
|
39 private static final String IGNORE_ERSTELLDATUM = "# Datum der Erstellung"; |
|
40 private static final String IGNORE_FLYS_VERSION = "# FLYS-Version:"; |
|
41 private static final String IGNORE_BEARBEITER = "# Bearbeiter:"; |
|
42 |
|
43 private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
|
44 |
|
45 private static final String EXPORT_FILE_DIR = "D:" + File.separator; |
|
46 |
|
47 final FilenameSupplier filename; |
|
48 final String outputModeName; |
|
49 |
|
50 // private final String collectionIdentifier; |
|
51 |
|
52 private final Map<String, String> params; |
|
53 |
|
54 public final String getOutputModename() { |
|
55 return this.outputModeName; |
|
56 } |
|
57 |
|
58 public ModuleRunnerOutput(final FilenameSupplier filename, final String outputModeName, final String currentKm) { |
|
59 this.filename = filename; |
|
60 this.outputModeName = outputModeName; |
|
61 |
|
62 this.params = getChartExportAttributeMap(currentKm, outputModeName); |
|
63 } |
|
64 |
|
65 protected final void assertAndWriteToFile(final HttpClient client, final String collectionId) throws IOException { |
|
66 |
|
67 final String type = "csv"; |
|
68 |
|
69 final String enc = "UTF-8";// req.getParameter("encoding"); |
|
70 |
|
71 final URL expectedResource = getClass().getResource(this.filename.getFilename()); |
|
72 final Document attr = ChartServiceHelper.getChartAttributes(this.params); |
|
73 |
|
74 final Document request = ClientProtocolUtils.newOutCollectionDocument(collectionId, this.outputModeName, type, attr); |
|
75 |
|
76 final InputStream response = client.collectionOut(request, collectionId, this.outputModeName); |
|
77 |
|
78 final String actual = deleteErstelldatum(IOUtils.toString(response, "UTF-8")); |
|
79 |
|
80 final String expected = deleteErstelldatum(FileUtils.readFileToString(new File(expectedResource.getFile()), enc)); |
|
81 |
|
82 // if (!actual.equals(expected)) { |
|
83 // if (this.writeToFile != null && this.writeToFile == true) { |
|
84 doGetWriteToDisk(actual); // TODO: WENN der Test negativ ausfällt, Datei abspeichern -> Diskussion |
|
85 // } |
|
86 |
|
87 try { |
|
88 Assert.assertEquals(expected, actual); |
|
89 } |
|
90 catch (final AssertionError e) { |
|
91 // TODO write (both?) files |
|
92 |
|
93 // doGetWriteToDisk(actual); // TODO: WENN der Test negativ ausfällt, Datei abspeichern -> Diskussion |
|
94 throw e; |
|
95 } |
|
96 } |
|
97 |
|
98 public final void doGetWriteToDisk(final String actual_text) throws FileNotFoundException, IOException { |
|
99 |
|
100 // final String name = mode; |
|
101 final String type = "csv"; |
|
102 |
|
103 final String fn = this.outputModeName + System.currentTimeMillis() + "." + type; |
|
104 final String enc = "windows-1252"; |
|
105 |
|
106 final String filepath = EXPORT_FILE_DIR + fn; |
|
107 // BOM |
|
108 final byte[] BOM = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; |
|
109 final String bomChar = new String(BOM, "UTF-8"); |
|
110 final String actualWithBom = new StringBuilder().append(BOM).append(actual_text).toString(); |
|
111 |
|
112 final InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(Charset.forName(enc).encode(actualWithBom).array()), enc); |
|
113 |
|
114 IOUtils.copy(in, new FileOutputStream(filepath), enc); |
|
115 } |
|
116 |
|
117 private final String deleteErstelldatum(final String input) { |
|
118 String result = ""; |
|
119 final String[] lines = input.split(LINE_SEPARATOR); |
|
120 for (final String line : lines) { |
|
121 if (!line.contains(IGNORE_ERSTELLDATUM) && !line.contains(IGNORE_FLYS_VERSION) && !line.contains(IGNORE_BEARBEITER)) { |
|
122 result = result + line + LINE_SEPARATOR; |
|
123 } |
|
124 } |
|
125 return result; |
|
126 } |
|
127 |
|
128 private final Map<String, String> getChartExportAttributeMap(final String currentKm, final String outputmodeName) { |
|
129 |
|
130 // From ChartOutputServiceImpl |
|
131 final Map<String, String> attr = new HashMap<String, String>(); |
|
132 |
|
133 attr.put("width", "-1"); // req.getParameter("width")); |
|
134 attr.put("height", "-1"); // req.getParameter("height")); |
|
135 // attr.put("minx", null);// req.getParameter("minx")); |
|
136 // attr.put("maxx", req.getParameter("maxx")); |
|
137 // attr.put("miny", req.getParameter("miny")); |
|
138 // attr.put("maxy", req.getParameter("maxy")); |
|
139 attr.put("format", "csv"); // req.getParameter("format")); |
|
140 attr.put("export", "true");// req.getParameter("export")); |
|
141 |
|
142 if (currentKm != null) |
|
143 attr.put("km", currentKm);// req.getParameter("currentKm")); |
|
144 // encoding? |
|
145 |
|
146 // Weiteres (nicht mehr von CHartOutputServiceImpl, sondern von URL-Parametern): |
|
147 attr.put("type", outputmodeName); |
|
148 attr.put("locale", "de"); |
|
149 attr.put("timestamp", String.valueOf(System.currentTimeMillis())); |
|
150 attr.put("encoding", "windows-1252"); |
|
151 |
|
152 return attr; |
|
153 } |
|
154 |
|
155 } |