View Javadoc
1   /*
2    * #%L
3    * Coser :: Business
4    * %%
5    * Copyright (C) 2011 Codelutin, Chatellier Eric
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
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   * Test du service de l'interface web.
48   * 
49   * @author chatellier
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       * Simule la publication de nouveaux resultats (uplaod client lourd).
76       * 
77       * @param path result to upload
78       * @throws CoserBusinessException 
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       * Test de lecture du fichier des indicateurs des resources de test.
88       * 
89       * @throws CoserBusinessException 
90       */
91      @Test
92      public void testGetIndicators() throws CoserBusinessException {
93          MultiKeyMap indicators = webService.getIndicatorsMap();
94          Assert.assertEquals(200, indicators.size());
95      }
96  
97      /**
98       * Test de lecture du fichier de zones de resources de test.
99       * 
100      * @throws CoserBusinessException 
101      */
102     @Test
103     public void testGetZones() throws CoserBusinessException {
104         DataStorage zones = webService.getZonesMap();
105         Assert.assertEquals(25, zones.size());
106     }
107 
108     /**
109      * Test de lecture du fichier de zones de resources de test.
110      * 
111      * @throws CoserBusinessException 
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      * Test de lecture du fichier de zones de resources de test.
121      * 
122      * @throws CoserBusinessException 
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      * Test que les upload successible merge bien les resultats en fonction
138      * des idenfiants des zones auquels ils sont affectés et que les resulats
139      * en conflit sont bien supprimé (avec si besoins leurs selections et projets).
140      * @throws CoserBusinessException 
141      */
142     @Test
143     public void testUploadDirectoryMerge() throws CoserBusinessException {
144         registerUploadedResult("/web/upload1.zip");
145 
146         // second upload with merge
147         registerUploadedResult("/web/upload2.zip");
148 
149         // some tests (from first)
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         // some tests (from second)
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      * Test du contenu du zip de téléchargement des sources avec erreur
161      * can le projet ne le permet pas.
162      * 
163      * @throws CoserBusinessException 
164      * @throws IOException 
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      * Test du contenu du zip de téléchargement des sources.
175      * 
176      * Generation du zip, freemarker, et pdf.
177      * 
178      * @throws CoserBusinessException 
179      * @throws IOException 
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         // test que le reftax est dans le zip
197         Assert.assertTrue(new File(tempDir, "RSUFI_DATA_projet1" + File.separator + "reftaxSpecies.csv").isFile());
198         FileUtils.deleteDirectory(tempDir);
199     }
200 
201     /**
202      * Test la récupération d'une image parmit les resultat de type "mapReference".
203      * 
204      * @throws CoserBusinessException
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      * Test que la generation d'un graph fonctionne.
215      * 
216      * @throws CoserBusinessException
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      * Test que la generation d'un graph fonctionne.
227      * 
228      * @throws CoserBusinessException
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      * Test que la generation d'un sous fichier csv à partir du fichier
239      * estcomind fonctionne.
240      * 
241      * @throws CoserBusinessException
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      * Test que la generation d'un sous fichier csv à partir du fichier
252      * estcomind fonctionne.
253      * 
254      * @throws CoserBusinessException
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      * Test que la recuperation des noms de list pour un indicateur fonctionne.
265      * 
266      * @throws CoserBusinessException
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      * Test des modifications de methodes suite à l'intoduction du
284      * moteur de recherche pour extraire les données.
285      * 
286      * @throws CoserBusinessException 
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      * Test une validation de formulaire web pour extraire parmis toutes les
311      * données celle choisie sous forme de zip.
312      * 
313      * @throws CoserBusinessException
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