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 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
57
58
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
91
92
93
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
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
127 String fileContent = FileUtils.readFileToString(htmlExport);
128 Assert.assertTrue(fileContent.indexOf("9999") > 0);
129 Assert.assertTrue(fileContent.indexOf("<img src=") > 0);
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
136 htmlExport.delete();
137 }
138
139
140
141
142
143
144
145 @Test
146 public void testControlLogExportHtml() throws CoserBusinessException, IOException {
147
148 Project project = createTestProject(projectService, false);
149
150
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
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
174 htmlExport.delete();
175 }
176
177
178
179
180
181
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
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
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
213
214 }
215 }