1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package fr.ifremer.coser.services;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Map;
33
34 import org.apache.commons.collections4.map.MultiKeyMap;
35 import org.apache.commons.io.FileUtils;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.nuiton.util.FileUtil;
40 import org.nuiton.util.ZipUtil;
41
42 import fr.ifremer.coser.CoserBusinessException;
43 import fr.ifremer.coser.storage.DataStorage;
44 import fr.ifremer.coser.util.DataType;
45
46
47
48
49
50
51
52
53
54
55 public class WebServiceTest extends CoserTestAbstract {
56
57 protected WebService webService;
58
59 @Before
60 public void initServices() {
61 webService = new WebService(config);
62 }
63
64 protected void assertFileExists(String filepath) {
65 File file = new File(filepath.replace('/', File.separatorChar));
66 Assert.assertTrue(file.exists());
67 }
68
69 protected void assertFileNotExists(String filepath) {
70 File file = new File(filepath.replace('/', File.separatorChar));
71 Assert.assertFalse(file.exists());
72 }
73
74
75
76
77
78
79
80 protected void registerUploadedResult(String path) throws CoserBusinessException {
81 URL firstUpload = WebServiceTest.class.getResource(path);
82 File firstUploadFile = new File(firstUpload.getFile());
83 webService.registerNewUploadedResults("admin", firstUploadFile);
84 }
85
86
87
88
89
90
91 @Test
92 public void testGetIndicators() throws CoserBusinessException {
93 MultiKeyMap indicators = webService.getIndicatorsMap();
94 Assert.assertEquals(200, indicators.size());
95 }
96
97
98
99
100
101
102 @Test
103 public void testGetZones() throws CoserBusinessException {
104 DataStorage zones = webService.getZonesMap();
105 Assert.assertEquals(25, zones.size());
106 }
107
108
109
110
111
112
113 @Test
114 public void testGetZonePictures() throws CoserBusinessException {
115 Map<String, String> zonesAndPictures = webService.getZonePictures();
116 Assert.assertEquals("C_GdG-MC.png", zonesAndPictures.get("gdgmc"));
117 }
118
119
120
121
122
123
124 @Test
125 public void testGetZoneMetaInfo() throws CoserBusinessException {
126 Map<String, String> zonesMetaInfos = webService.getZoneMetaInfo(Locale.FRENCH);
127 Assert.assertEquals("Ensemble de la couverture Evhoe", zonesMetaInfos.get("gdgmc"));
128
129 zonesMetaInfos = webService.getZoneMetaInfo(Locale.ENGLISH);
130 Assert.assertEquals("Area covered by Evhoe survey", zonesMetaInfos.get("gdgmc"));
131
132 zonesMetaInfos = webService.getZoneMetaInfo(null);
133 Assert.assertEquals("Area covered by Evhoe survey", zonesMetaInfos.get("gdgmc"));
134 }
135
136
137
138
139
140
141
142 @Test
143 public void testUploadDirectoryMerge() throws CoserBusinessException {
144 registerUploadedResult("/web/upload1.zip");
145
146
147 registerUploadedResult("/web/upload2.zip");
148
149
150 assertFileNotExists(config.getWebMapsProjectsDirectory() + "/projet1/selections/selection11/results/result111");
151 assertFileExists(config.getWebIndicatorsProjectsDirectory() + "/projet1/selections/selection11/results/result112");
152 assertFileNotExists(config.getWebIndicatorsProjectsDirectory() + "/projet1/selections/selection12");
153
154 assertFileExists(config.getWebIndicatorsProjectsDirectory() + "/projet1/selections/selection11/results/result113");
155 assertFileExists(config.getWebIndicatorsProjectsDirectory() + "/projet1/selections/selection11/results/result114");
156 assertFileExists(config.getWebMapsProjectsDirectory() + "/projet3/selections/selection31/results/result311");
157 }
158
159
160
161
162
163
164
165
166 @Test(expected=CoserBusinessException.class)
167 public void testSourceZipError() throws CoserBusinessException, IOException {
168 registerUploadedResult("/web/upload2.zip");
169
170 webService.getSourceZip("ecorse", Locale.ENGLISH);
171 }
172
173
174
175
176
177
178
179
180
181 @Test
182 public void testSourceZip() throws CoserBusinessException, IOException {
183 registerUploadedResult("/web/upload2.zip");
184
185 File zip = webService.getSourceZip("testzone1", Locale.ENGLISH);
186 File tempDir = FileUtil.createTempDirectory("coser-source-", "-tmp");
187 ZipUtil.uncompress(zip, tempDir);
188 Assert.assertTrue(new File(tempDir, "RSUFI_DATA_projet1" + File.separator + "testcatch.csv").isFile());
189 Assert.assertTrue(new File(tempDir, "RSUFI_DATA_projet1" + File.separator + "DataDisclaimer.pdf").isFile());
190 FileUtils.deleteDirectory(tempDir);
191
192 zip = webService.getSourceZip("testzone1", Locale.FRENCH);
193 tempDir = FileUtil.createTempDirectory("coser-source-", "-tmp");
194 ZipUtil.uncompress(zip, tempDir);
195 Assert.assertTrue(new File(tempDir, "RSUFI_DATA_projet1" + File.separator + "DechargeDonnees.pdf").isFile());
196
197 Assert.assertTrue(new File(tempDir, "RSUFI_DATA_projet1" + File.separator + "reftaxSpecies.csv").isFile());
198 FileUtils.deleteDirectory(tempDir);
199 }
200
201
202
203
204
205
206 @Test
207 public void testGetMapFile() throws CoserBusinessException {
208 registerUploadedResult("/web/upload2.zip");
209 File file = webService.getMapFile("ecorse", "SPECIES1");
210 Assert.assertTrue(file.isFile());
211 }
212
213
214
215
216
217
218 @Test
219 public void testGetChartCom() throws CoserBusinessException {
220 registerUploadedResult("/web/upload2.zip");
221 File file = webService.getChart("ecorse", null, "Lbcomm", null, Locale.FRENCH);
222 Assert.assertTrue(file.isFile());
223 }
224
225
226
227
228
229
230 @Test
231 public void testGetChartPop() throws CoserBusinessException {
232 registerUploadedResult("/web/upload2.zip");
233 File file = webService.getChartData("ecorse", "COSER_SPECIES2", "lnN", null, Locale.FRENCH);
234 Assert.assertTrue(file.isFile());
235 }
236
237
238
239
240
241
242
243 @Test
244 public void testGetChartDataCom() throws CoserBusinessException {
245 registerUploadedResult("/web/upload2.zip");
246 File file = webService.getChartData("ecorse", null, "lnN", null, Locale.FRENCH);
247 Assert.assertTrue(file.isFile());
248 }
249
250
251
252
253
254
255
256 @Test
257 public void testGetChartDataPop() throws CoserBusinessException {
258 registerUploadedResult("/web/upload2.zip");
259 File file = webService.getChartData("ecorse", "COSER_SPECIES2", "lnN", null, Locale.FRENCH);
260 Assert.assertTrue(file.isFile());
261 }
262
263
264
265
266
267
268 @Test
269 public void testGetIndicatorLists() throws CoserBusinessException {
270 registerUploadedResult("/web/upload2.zip");
271 Map<String, String> lists = webService.getIndicatorLists("ecorse", "Delta", Locale.FRENCH);
272 Assert.assertEquals(2, lists.size());
273 Assert.assertEquals("Type2 Liste 1", lists.get("m1"));
274 Assert.assertEquals("Type2 Liste 2", lists.get("m2"));
275
276 lists = webService.getIndicatorLists("ecorse", "Delta", null);
277 Assert.assertEquals(2, lists.size());
278 Assert.assertEquals("Type2 List 1", lists.get("m1"));
279 Assert.assertEquals("Type2 List 2", lists.get("m2"));
280 }
281
282
283
284
285
286
287
288 @Test
289 public void testGetAllZoneSpeciesAndIndicators() throws CoserBusinessException {
290 registerUploadedResult("/web/upload2.zip");
291
292 Map<String, String> zones = webService.getZoneForFacade(null, false, false);
293 List<String> zoneIds = new ArrayList<String>(zones.keySet());
294 Assert.assertEquals(Arrays.asList("ecorse"), zoneIds);
295
296 Map<String, String> species = webService.getSpecies(zoneIds, false);
297 List<String> speciesIds = new ArrayList<String>(species.keySet());
298 Assert.assertEquals(Arrays.asList("COSER_SPECIES1", "COSER_SPECIES2"), speciesIds);
299
300 Map<String, String> indicators = webService.getIndicators(zoneIds, DataType.COMMUNITY, Locale.FRENCH);
301 List<String> indicatorIds = new ArrayList<String>(indicators.keySet());
302 Assert.assertEquals(Arrays.asList("Delta", "Lbcomm", "biomSmall"), indicatorIds);
303
304 indicators = webService.getIndicators(zoneIds, DataType.POPULATION, Locale.FRENCH);
305 indicatorIds = new ArrayList<String>(indicators.keySet());
306 Assert.assertEquals(Arrays.asList("Abundance", "Biomass", "Wbar", "lnN"), indicatorIds);
307 }
308
309
310
311
312
313
314
315 @Test
316 public void testExtractDataAsZip() throws CoserBusinessException {
317 registerUploadedResult("/web/upload2.zip");
318
319 List<String> zones = Arrays.asList("ecorse");
320 List<DataType> types = Arrays.asList(DataType.MAP, DataType.POPULATION, DataType.COMMUNITY, DataType.SOURCE);
321 List<String> species = Arrays.asList("COSER_SPECIES1", "COSER_SPECIES2");
322 List<String> comIndicators = Arrays.asList("Delta", "Lbcomm", "biomSmall");
323 List<String> popIndicators = Arrays.asList("Abundance", "Biomass", "Wbar", "lnN");
324
325 File file = webService.extractData(zones, types, species, comIndicators, popIndicators, Locale.FRENCH);
326 Assert.assertTrue(file.length() > 0);
327 }
328 }
329