This is an automated email from the git hooks/post-receive script. New commit to branch develop-4.x in repository observe. See http://git.codelutin.com/observe.git commit cd698df02d5230f4cc23bac81e4621f3c69dc28f Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Nov 3 14:38:20 2015 +0100 Ajout d'un test pour vérifier la bonne récupération des RTP (See #7628) --- .../LengthWeightParemeterHelperTest.java | 294 +++++++++++++++++++++ 1 file changed, 294 insertions(+) diff --git a/observe-business/src/test/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelperTest.java b/observe-business/src/test/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelperTest.java new file mode 100644 index 0000000..4cba24f --- /dev/null +++ b/observe-business/src/test/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelperTest.java @@ -0,0 +1,294 @@ +/* + * #%L + * ObServe :: Entities + * %% + * Copyright (C) 2008 - 2010 IRD, Codelutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ +package fr.ird.observe.entities.referentiel; + +import fr.ird.observe.DataService; +import fr.ird.observe.IObserveConfig; +import fr.ird.observe.ObserveServiceHelper; +import fr.ird.observe.db.DBTestHelper; +import fr.ird.observe.db.DataSource; +import fr.ird.observe.db.DataSourceException; +import fr.ird.observe.entities.constants.ReferenceStatus; +import fr.ird.observe.test.TestHelper; +import fr.ird.observe.util.Scripts; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.nuiton.topia.TopiaContext; +import org.nuiton.util.DateUtil; + +import java.io.File; +import java.net.URL; +import java.util.Date; + +/** + * Test de la classe {@link LengthWeightParemeterHelper}. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 1.8 + */ +public class LengthWeightParemeterHelperTest { + + /** Logger */ + private static final Log log = LogFactory.getLog(LengthWeightParemeterHelperTest.class); + + + /** La base de test chargée à partir du la sauvegarde. */ + private static DataSource dataSource; + + private DataService dataService; + + @BeforeClass + public static void init() throws Exception { + + TestHelper.initTest(LengthWeightParemeterHelperTest.class); + + TestHelper.createApplicationContext(); + + TestHelper.setConfig(IObserveConfig.DB_VERSION, Scripts.V_LAST.toString()); + + File localDB = TestHelper.newLocalDB(TestHelper.class.getSimpleName()); + + String dbPath = Scripts.getBackupScript(Scripts.V_LAST, "referentiel"); + + URL dbUrl = LengthWeightParemeterHelperTest.class.getResource(dbPath); + + Assert.assertNotNull("could not find resource : " + dbPath, dbUrl); + + dataSource = DBTestHelper.createAndOpenFromDump( + localDB, + dbUrl, + false, + false, + true, + true, + true); + + DataService dataService = ObserveServiceHelper.get().getDataService(); + dataService.registerDataSource(dataSource); + + TopiaContext tx = dataService.beginTransaction(dataSource, "testGetCorrectLengthWeightParameter"); + + try { + + SpeciesDAO speciesDAO = (SpeciesDAO) dataSource.getDAO(tx, Species.class); + OceanDAO oceanDAO = (OceanDAO) dataSource.getDAO(tx, Ocean.class); + SexDAO sexDao = (SexDAO) dataSource.getDAO(tx, Sex.class); + + Species species = speciesDAO.findByFaoCode("DOL"); + Assert.assertNotNull("Could not find species with faoCode: DOL", species); + + Sex undeterminedSex = sexDao.findByCode("0"); + Assert.assertNotNull("Could not find sex with code 0 (Undetermined)", undeterminedSex); + + Sex maleSex = sexDao.findByCode("1"); + Assert.assertNotNull("Could not find sex with code 1 (male)", maleSex); + + Sex femaleSex = sexDao.findByCode("2"); + Assert.assertNotNull("Could not find sex with code 2 (female)", femaleSex); + + Date firstStartDate = DateUtil.createDate(1, 1, 2010); + Date firstEndDate = DateUtil.createDate(31, 12, 2010); + Date secondStartDate = DateUtil.createDate(1, 1, 2011); + + Ocean atlanticOcean = oceanDAO.findByCode("1"); + Assert.assertNotNull("Could not find ocean with code 1 (Atlantic)", atlanticOcean); + + Ocean indianOcean = oceanDAO.findByCode("2"); + Assert.assertNotNull("Could not find ocean with code 2 (Indian)", indianOcean); + + LengthWeightParameterDAO lengthWeightParameterDao = (LengthWeightParameterDAO) dataSource.getDAO(tx, LengthWeightParameter.class); + + // Ajout parametrage Male / Atlantique (2010) + createLengthWeightParameter(lengthWeightParameterDao, species, atlanticOcean, maleSex, firstStartDate, firstEndDate); + // Ajout parametrage Male / Atlantique (après 2010) + createLengthWeightParameter(lengthWeightParameterDao, species, atlanticOcean, maleSex, secondStartDate, null); + // Ajout parametrage Male / Indien (2010) + createLengthWeightParameter(lengthWeightParameterDao, species, indianOcean, maleSex, firstStartDate, firstEndDate); + // Ajout parametrage Male / Indien (Après 2010) + createLengthWeightParameter(lengthWeightParameterDao, species, indianOcean, maleSex, secondStartDate, null); + + tx.commitTransaction(); + + + } finally { + + tx.closeContext(); + + } + + } + + protected static void createLengthWeightParameter(LengthWeightParameterDAO lengthWeightParameterDao, + Species species, + Ocean ocean, + Sex sex, + Date startDate, + Date endDate) { + + LengthWeightParameter entity = lengthWeightParameterDao.newInstance(); + + entity.setSpecies(species); + entity.setSex(sex); + entity.setOcean(ocean); + entity.setStartDate(startDate); + entity.setEndDate(endDate); + entity.setStatus(ReferenceStatus.enabled); + entity.setCoefficients("a=3.8E-5:b=2.78"); + entity.setLengthWeightFormula("a * Math.pow(L, b)"); + entity.setWeightLengthFormula("Math.pow(P/a, 1/b)"); + lengthWeightParameterDao.create(entity); + + + } + + @Before + public void setUp() throws Exception { + + dataService = ObserveServiceHelper.get().getDataService(); + + } + + @After + public final void tearDown() throws Exception { + + try { + dataSource.doClose(false); + } finally { + + ObserveServiceHelper.get().close(); + ObserveServiceHelper.close(); + + } + + } + + /** + * Pour tester que l'on récupère la bonne relation. + * See http://forge.codelutin.com/issues/7628 + */ + @Test + public void testGetCorrectLengthWeightParameter() throws DataSourceException { + + + TopiaContext tx = dataService.beginTransaction(dataSource, "testGetCorrectLengthWeightParameter"); + + try { + + SpeciesDAO speciesDAO = (SpeciesDAO) dataSource.getDAO(tx, Species.class); + OceanDAO oceanDAO = (OceanDAO) dataSource.getDAO(tx, Ocean.class); + SexDAO sexDao = (SexDAO) dataSource.getDAO(tx, Sex.class); + + Species species = speciesDAO.findByFaoCode("DOL"); + Assert.assertNotNull("Could not find species with faoCode: DOL", species); + + Sex undeterminedSex = sexDao.findByCode("0"); + Assert.assertNotNull("Could not find sex with code 0 (Undetermined)", undeterminedSex); + + Sex maleSex = sexDao.findByCode("1"); + Assert.assertNotNull("Could not find sex with code 1 (male)", maleSex); + + Sex femaleSex = sexDao.findByCode("2"); + Assert.assertNotNull("Could not find sex with code 2 (female)", femaleSex); + + Date date1970 = DateUtil.createDate(1, 1, 1970); + Date date2009 = DateUtil.createDate(1, 1, 2009); + Date date2010 = DateUtil.createDate(1, 1, 2010); + Date date2011 = DateUtil.createDate(1, 1, 2011); + + Ocean atlanticOcean = oceanDAO.findByCode("1"); + Assert.assertNotNull("Could not find ocean with code 1 (Atlantic)", atlanticOcean); + + Ocean indianOcean = oceanDAO.findByCode("2"); + Assert.assertNotNull("Could not find ocean with code 2 (Indian)", indianOcean); + + Ocean pacificOcean = oceanDAO.findByCode("3"); + Assert.assertNotNull("Could not find ocean with code 3 (Pacific)", pacificOcean); + + assertFoundLengthWeightParameter(tx, species, atlanticOcean, null, undeterminedSex, date2009, date1970); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, undeterminedSex, undeterminedSex, date2009, date1970); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, maleSex, undeterminedSex, date2009, date1970); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, femaleSex, undeterminedSex, date2009, date1970); + + assertFoundLengthWeightParameter(tx, species, atlanticOcean, null, undeterminedSex, date2010, date1970); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, undeterminedSex, undeterminedSex, date2010, date1970); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, maleSex, maleSex, date2010, date2010); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, femaleSex, undeterminedSex, date2010, date1970); + + assertFoundLengthWeightParameter(tx, species, atlanticOcean, null, undeterminedSex, date2011, date1970); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, undeterminedSex, undeterminedSex, date2011, date1970); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, maleSex, maleSex, date2011, date2011); + assertFoundLengthWeightParameter(tx, species, atlanticOcean, femaleSex, undeterminedSex, date2011, date1970); + + assertFoundLengthWeightParameter(tx, species, indianOcean, null, undeterminedSex, date2009, date1970); + assertFoundLengthWeightParameter(tx, species, indianOcean, undeterminedSex, undeterminedSex, date2009, date1970); + assertFoundLengthWeightParameter(tx, species, indianOcean, maleSex, undeterminedSex, date2009, date1970); + assertFoundLengthWeightParameter(tx, species, indianOcean, femaleSex, undeterminedSex, date2009, date1970); + + assertFoundLengthWeightParameter(tx, species, indianOcean, undeterminedSex, undeterminedSex, date2010, date1970); + assertFoundLengthWeightParameter(tx, species, indianOcean, maleSex, maleSex, date2010, date2010); + assertFoundLengthWeightParameter(tx, species, indianOcean, femaleSex, undeterminedSex, date2010, date1970); + + assertFoundLengthWeightParameter(tx, species, indianOcean, undeterminedSex, undeterminedSex, date2011, date1970); + assertFoundLengthWeightParameter(tx, species, indianOcean, maleSex, maleSex, date2011, date2011); + assertFoundLengthWeightParameter(tx, species, indianOcean, femaleSex, undeterminedSex, date2011, date1970); + + assertNotFoundLengthWeightParameter(tx, species, pacificOcean, undeterminedSex, date2010); + assertNotFoundLengthWeightParameter(tx, species, pacificOcean, maleSex, date2010); + assertNotFoundLengthWeightParameter(tx, species, pacificOcean, femaleSex, date2010); + + } finally { + + tx.closeContext(); + + } + + } + + protected void assertFoundLengthWeightParameter(TopiaContext tx, Species species, Ocean ocean, Sex sex, Sex expectedSex, Date date, Date expectedStartDate) throws DataSourceException { + + if (log.isInfoEnabled()) { + log.info("Try to find length weith parameter for species " + species.getFaoCode() + " - ocean " + ocean.getLabel1() + " - sex " + (sex == null ? "null" : sex.getLabel1()) + " at " + date); + } + LengthWeightParameter lengthWeightParameter = dataService.findLengthWeightParameter(tx, species, ocean, sex, date); + + Assert.assertNotNull("length weith parameter not found for species " + species.getFaoCode() + " - ocean " + ocean.getLabel1() + " - sex " + (sex == null ? "null" : sex.getLabel1()) + " at " + date, lengthWeightParameter); + Assert.assertEquals("Expected sex is " + expectedSex.getLabel1() + " but the one found was " + lengthWeightParameter.getSex().getLabel1(), expectedSex, lengthWeightParameter.getSex()); + Date startDate = new Date(lengthWeightParameter.getStartDate().getTime()); + Assert.assertEquals("Expected startDate is " + expectedStartDate + " but the one found was " + startDate, expectedStartDate, startDate); + } + + protected void assertNotFoundLengthWeightParameter(TopiaContext tx, Species species, Ocean ocean, Sex sex, Date date) throws DataSourceException { + + if (log.isInfoEnabled()) { + log.info("Try to find length weith parameter for species " + species.getFaoCode() + " - ocean " + ocean.getLabel1() + " - sex " + (sex == null ? "null" : sex.getLabel1()) + " at " + date); + } + LengthWeightParameter lengthWeightParameter = dataService.findLengthWeightParameter(tx, species, ocean, sex, date); + + Assert.assertNull("length weith parameter should not ne found for species " + species.getFaoCode() + " - ocean " + ocean.getLabel1() + " - sex " + (sex == null ? "null" : sex.getLabel1()) + " at " + date, lengthWeightParameter); + } + +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.