View Javadoc
1   /*
2    * #%L
3    * Coser :: Business
4    * %%
5    * Copyright (C) 2010 Ifremer, 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 static org.nuiton.i18n.I18n.t;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.swing.JDialog;
35  
36  import org.apache.commons.io.FileUtils;
37  import org.jfree.chart.ChartPanel;
38  import org.jfree.chart.JFreeChart;
39  import org.junit.Assert;
40  import org.junit.Before;
41  import org.junit.Ignore;
42  import org.junit.Test;
43  
44  import fr.ifremer.coser.CoserBusinessException;
45  import fr.ifremer.coser.CoserConstants.Category;
46  import fr.ifremer.coser.CoserConstants.ValidationLevel;
47  import fr.ifremer.coser.bean.Project;
48  import fr.ifremer.coser.bean.Selection;
49  import fr.ifremer.coser.command.DeleteLineCommand;
50  import fr.ifremer.coser.command.MergeSpeciesCommand;
51  import fr.ifremer.coser.command.ModifyFieldCommand;
52  import fr.ifremer.coser.control.DiffCatchLengthControlError;
53  import fr.ifremer.coser.control.ControlError;
54  
55  /**
56   * Publication service tests
57   * 
58   * @author chatellier
59  
60   * 
61  
62  
63   */
64  public class PublicationServiceTest extends CoserTestAbstract {
65  
66      protected ProjectService projectService;
67      protected PublicationService publicationService;
68      protected CommandService commandService;
69  
70      @Before
71      public void initService() {
72          projectService = new ProjectService(config);
73          publicationService = new PublicationService(config);
74          commandService = new CommandService(config);
75      }
76  
77      @Ignore
78      public void testCatchChart() throws CoserBusinessException {
79          Project project = createTestProject(projectService, false);
80          Map<String, JFreeChart> charts = publicationService.getCompareCatchLengthGraph(project, project.getControl(), null);
81          JFreeChart chart = charts.get("COSER_SPECIES1");
82          JDialog f = new JDialog();
83          f.setModal(true);
84          f.add(new ChartPanel(chart));
85          f.pack();
86          f.setVisible(true);
87      }
88  
89      /**
90       * Test la géneration des rapports html.
91       * 
92       * @throws CoserBusinessException
93       * @throws IOException 
94       */
95      @Test
96      public void testErrorExportHtml() throws CoserBusinessException, IOException {
97          Project project = createTestProject(projectService, false);
98  
99          List<ControlError> validationErrors = new ArrayList<ControlError>();
100 
101         // error 1 :
102         ControlError error1 = new ControlError();
103         error1.setLevel(ValidationLevel.FATAL);
104         error1.setMessage("Test fatal");
105         error1.addLineNumber("15");
106         error1.setCategory(Category.CATCH);
107         validationErrors.add(error1);
108 
109         DiffCatchLengthControlError error2 = new DiffCatchLengthControlError();
110         error2.setLevel(ValidationLevel.WARNING);
111         error2.setMessage("Test warning and graph");
112         error2.setSpecies("COSER_SPECIES1");
113         error2.setTipMessage("Explication sur l'erreur");
114         validationErrors.add(error2);
115 
116         ControlError error3 = new ControlError();
117         error3.setLevel(ValidationLevel.ERROR);
118         error3.setMessage("Test error");
119         error3.addLineNumber("12");
120         error3.addLineNumber("9999");
121         error3.setCategory(Category.CATCH);
122         validationErrors.add(error3);
123 
124         File htmlExport = publicationService.exportErrorsAsHTML(project, project.getControl(), validationErrors);
125         
126         // some asserts
127         String fileContent = FileUtils.readFileToString(htmlExport);
128         Assert.assertTrue(fileContent.indexOf("9999") > 0);
129         Assert.assertTrue(fileContent.indexOf("<img src=") > 0); // one chart
130         Assert.assertTrue(fileContent.indexOf("Test fatal") > 0);
131         Assert.assertTrue(fileContent.indexOf("Test warning and graph") > 0);
132         Assert.assertTrue(fileContent.indexOf("Test error") > 0);
133         Assert.assertTrue(fileContent.indexOf(t("coser.business.control.error.allCategories")) > 0);
134 
135         // clean all
136         htmlExport.delete();
137     }
138 
139     /**
140      * Test le rapport de control en html.
141      * 
142      * @throws CoserBusinessException
143      * @throws IOException
144      */
145     @Test
146     public void testControlLogExportHtml() throws CoserBusinessException, IOException {
147         
148         Project project = createTestProject(projectService, false);
149 
150         // delete line 2
151         DeleteLineCommand command = new DeleteLineCommand();
152         command.setLineNumber("2");
153         command.setCategory(Category.LENGTH);
154         commandService.doAction(command, project, project.getControl());
155         
156         ModifyFieldCommand command2 = new ModifyFieldCommand();
157         command2.setLineNumber("4");
158         command2.setCategory(Category.CATCH);
159         command2.setFieldName("Number");
160         command2.setCurrentValue("251.86");
161         command2.setNewValue("392.98");
162 
163         commandService.doAction(command2, project, project.getControl());
164         
165         File htmlExport = publicationService.extractControlLogAsHTML(project, project.getControl());
166 
167         // some asserts
168         String fileContent = FileUtils.readFileToString(htmlExport);
169         Assert.assertTrue(fileContent.indexOf("line 2") > 0);
170         Assert.assertTrue(fileContent.indexOf("line 4") > 0);
171         Assert.assertTrue(fileContent.indexOf("from \"251.86\" to \"392.98\"") > 0);
172 
173         // clean all
174         htmlExport.delete();
175     }
176     
177     /**
178      * Test le rapport de selection en html.
179      * 
180      * @throws CoserBusinessException
181      * @throws IOException
182      */
183     @Test
184     public void testSelectionLogExportHtml() throws CoserBusinessException, IOException {
185         
186         Project project = createTestProject(projectService, true);
187         Selection selection = projectService.initProjectSelection(project);
188         selection.setSelectedYears(Collections.singletonList("2010"));
189         selection.setSelectedStrata(Collections.singletonList("EG34EB"));
190         selection.setSelectedSpecies(Collections.singletonList("COSER_SPECIES1"));
191 
192         // merge species
193         MergeSpeciesCommand command = new MergeSpeciesCommand();
194         command.setComment("marged because i wanted to !");
195         command.setNewSpecyName("TESTMERGE");
196         command.setSpeciesNames(new String[]{"COSER_SPECIES2","COSER_SPECIES3"});
197         commandService.doAction(command, project, selection);
198 
199         commandService.doAction(command, project, selection);
200         
201         File htmlExport = publicationService.extractSelectionLogAsHTML(project, selection);
202 
203         // some asserts
204         String fileContent = FileUtils.readFileToString(htmlExport);
205         Assert.assertTrue(fileContent.indexOf("COSER_SPECIES1") > 0);
206         Assert.assertTrue(fileContent.indexOf("2010") > 0);
207         Assert.assertTrue(fileContent.indexOf("EG34EB") > 0);
208         Assert.assertTrue(fileContent.indexOf("TESTMERGE") > 0);
209         Assert.assertTrue(fileContent.indexOf("COSER_SPECIES2") > 0);
210         Assert.assertTrue(fileContent.indexOf("COSER_SPECIES3") > 0);
211 
212         // clean all
213         //htmlExport.delete();
214     }
215 }