View Javadoc
1   /*
2    * #%L
3    * IsisFish
4    * 
5    * $Id: SimulationServiceIT.java 4213 2015-05-04 15:18:17Z echatellier $
6    * $HeadURL: https://svn.codelutin.com/isis-fish/tags/isis-fish-4.4.0.2/src/test/java/fr/ifremer/isisfish/simulator/launcher/SimulationServiceIT.java $
7    * %%
8    * Copyright (C) 2006 - 2010 Ifremer, Code Lutin
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/gpl-3.0.html>.
23   * #L%
24   */
25  
26  package fr.ifremer.isisfish.simulator.launcher;
27  
28  import java.io.File;
29  import java.net.URL;
30  import java.util.List;
31  
32  import org.apache.commons.lang3.StringUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.junit.Assert;
36  import org.junit.Ignore;
37  import org.junit.Test;
38  import org.nuiton.topia.TopiaContext;
39  
40  import fr.ifremer.isisfish.AbstractIsisFishTest;
41  import fr.ifremer.isisfish.datastore.ResultStorage;
42  import fr.ifremer.isisfish.datastore.SimulationStorage;
43  import fr.ifremer.isisfish.entities.Result;
44  import fr.ifremer.isisfish.simulator.SimulationControl;
45  import fr.ifremer.isisfish.types.TimeStep;
46  
47  /**
48   * Integration tests for {@link SimulationService}.
49   * 
50   * Dans ces deux tests, les simulations ont été effectuées sur isis-fish-3.2.0.9.
51   * Les resultats ont été exporté dans le fichier data-backup.sql.gz. Le zip
52   * contient cet export et le fichier de parametres.
53   * 
54   * Il ne contient pas les scripts pour utiliser ceux de la base de test.
55   * 
56   * @author chatellier
57   * @version $Revision: 4213 $
58   * 
59   * Last update : $Date: 2015-05-04 17:18:17 +0200 (Mon, 04 May 2015) $ By : $Author: echatellier $
60   */
61  @Ignore
62  public class SimulationServiceIT extends AbstractIsisFishTest {
63  
64      /** Class logger. */
65      private static Log log = LogFactory.getLog(SimulationServiceIT.class);
66  
67      /**
68       * Read a zipped simulation file.
69       * Redo simulation with same parameters, and check that result are all equals.
70       * 
71       * @param zipName
72       * @throws Exception
73       */
74      protected void testSimulate(String zipName) throws Exception {
75  
76          int resultChecked = 0;
77          
78          // recuperation du fichier zip de la simulation de test
79          File zip = new File(zipName);
80  
81          // import de cette simulation
82          String name = "simulation-test-" + System.currentTimeMillis();
83          SimulationStorage simRef = SimulationStorage.importAndRenameZip(zip, name + "-ref");
84  
85          // la simulation testée ici
86          SimulationStorage sim = SimulationStorage.importAndRenameZip(zip, name + "-temp");
87          
88          try {
89              // suppression des resultats de cette simulation
90              TopiaContext tx = sim.getStorage().beginTransaction();
91              tx.execute("DELETE from " + Result.class.getName());
92              tx.commitTransaction();
93              tx.closeContext();
94  
95              // lancement de la simulation
96              SimulationControl control = new SimulationControl(sim.getName());
97              control.setStep(new TimeStep());
98              InProcessSimulatorLauncher launcher = new InProcessSimulatorLauncher();
99              sim = launcher.localSimulate(control, sim);
100 
101             // if simulation ends with exception, no need to continue
102             if (!StringUtils.isEmpty(sim.getInformation().getException())) {
103                 Assert.fail(sim.getInformation().getException());
104             }
105 
106             // verification qu'on retrouve les memes resulats pour les deux simulations
107             ResultStorage resultRef = simRef.getResultStorage();
108             ResultStorage result = sim.getResultStorage();
109 
110             List<String> resultNames = resultRef.getResultName();
111             Assert.assertEquals(resultNames, result.getResultName());
112 
113             TimeStep lastStep = resultRef.getLastStep();
114 
115             
116             if (log.isInfoEnabled()) {
117                 log.info("Check result ...");
118             }
119 
120             for (TimeStep step = new TimeStep(0); step.beforeOrEquals(lastStep); step = step.next()) {
121                 for (String resultName : resultNames) {
122                     if (log.isDebugEnabled()) {
123                         log.debug("Check result " + step + " " + resultName);
124                     }
125                     Assert.assertEquals("Date " + step.getStep() + " result " + resultName,
126                             resultRef.getMatrix(step, resultName),
127                             result.getMatrix(step, resultName));
128                     
129                     resultChecked++;
130                 }
131             }
132             
133             // nombre de résultat sur 12 mois, sur 5 ans
134             Assert.assertEquals("All expected results have not been checked !", resultNames.size() * 12 * 5, resultChecked);
135 
136         } finally {
137             // fermeture des bases
138             simRef.getStorage().closeContext();
139             sim.getStorage().closeContext();
140             // suppresion des deux simulations
141             simRef.delete(false);
142             sim.delete(false);
143         }
144         
145         // Test qu'au moins quelques resultats ont été verifié
146         Assert.assertTrue("At least some result must be done", resultChecked > 100);
147     }
148 
149     /**
150      * Do simulation test on gdg database.
151      * 
152      * Simulation properties :
153      *  - 10 years
154      *  - all strategies
155      *  - one population
156      * 
157      * @throws Exception
158      */
159     @Test
160     public void testSimlateGdG() throws Exception {
161         URL zipURL = this.getClass().getResource("sim_test-gdg-3.2-3.3.zip");
162         testSimulate(zipURL.getFile());
163     }
164     
165     /**
166      * Do simulation test on ICA database.
167      * 
168      * Simulation properties :
169      *  - 10 years
170      *  - all strategies
171      *  - one population
172      * 
173      * @throws Exception
174      */
175     @Test
176     public void testSimlateICA() throws Exception {
177         URL zipURL = this.getClass().getResource("sim_test-ica-3.2-3.3.zip");
178         testSimulate(zipURL.getFile());
179     }
180 }