r319 - trunk/echobase-services/src/test/java/fr/ifremer/echobase/services
Author: tchemit Date: 2012-02-17 00:50:12 +0100 (Fri, 17 Feb 2012) New Revision: 319 Url: http://forge.codelutin.com/repositories/revision/echobase/319 Log: add import data service test (without data won't commit from my phone :() Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/FakeEchoBaseServiceContext.java trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java 2012-02-16 23:50:12 UTC (rev 319) @@ -0,0 +1,98 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import fr.ifremer.echobase.entities.data.Cell; +import fr.ifremer.echobase.entities.data.Data; +import fr.ifremer.echobase.entities.data.DataAcquisition; +import fr.ifremer.echobase.entities.data.DataProcessing; +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.services.configurations.AcousticDataImportConfiguration; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link AcousticDataImportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class AcousticDataImportServiceTest extends EchoBaseTestServiceSupport { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext("/import-data/echobase-0.3-commonData-result.h2.db"); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "acousticData", filename}; + } + + @Test + public void doImport() throws Exception { + + assertNbEntities(Voyage.class, 1); + assertNbEntities(Transit.class, 3); + assertNbEntities(Transect.class, 181); + + // import operations + + assertNbEntities(DataAcquisition.class, 0); + assertNbEntities(DataProcessing.class, 0); + assertNbEntities(Cell.class, 0); + assertNbEntities(Data.class, 0); + + AcousticDataImportConfiguration conf = + new AcousticDataImportConfiguration(getLocale()); + + conf.setVoyageId("fr.ifremer.echobase.entities.data.Voyage#1329429559711#0.2622586586792377"); + conf.setVesselId("fr.ifremer.echobase.entities.references.Vessel#1323196672049#0.9790502711645855"); + + prepareInputFile(conf.getMoviesFile(), getImportPath("movies.csv")); + + AcousticDataImportService service = + getService(AcousticDataImportService.class); + + List<CsvImportResult> result = + service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(4, result.size()); + Iterator<CsvImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), DataAcquisition.class, 61, 0, 61); + assertCsvImportResult(iterator.next(), DataProcessing.class, 183, 0, 183); + assertCsvImportResult(iterator.next(), Cell.class, 427, 0, 427); + assertCsvImportResult(iterator.next(), Data.class, 427, 0, 427); + + assertNbEntities(DataAcquisition.class, 0); + assertNbEntities(DataProcessing.class, 0); + assertNbEntities(Cell.class, 0); + assertNbEntities(Data.class, 0); + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/AcousticDataImportServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java 2012-02-16 23:50:12 UTC (rev 319) @@ -0,0 +1,93 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import fr.ifremer.echobase.entities.data.Operation; +import fr.ifremer.echobase.entities.data.Sample; +import fr.ifremer.echobase.entities.data.SampleData; +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.services.configurations.CatchesDataImportConfiguration; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link CatchesDataImportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CatchesDataImportServiceTest extends EchoBaseTestServiceSupport { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext("/import-data/echobase-0.3-operation-result.h2.db"); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "catchesData", filename}; + } + + @Test + public void doImport() throws Exception { + + assertNbEntities(Voyage.class, 1); + assertNbEntities(Transit.class, 3); + assertNbEntities(Transect.class, 181); + assertNbEntities(Operation.class, 61); + + // import operations + + assertNbEntities(Sample.class, 0); + assertNbEntities(SampleData.class, 0); + + CatchesDataImportConfiguration conf = + new CatchesDataImportConfiguration(getLocale()); + + conf.setVoyageId("fr.ifremer.echobase.entities.data.Voyage#1329429559711#0.2622586586792377"); + conf.setVesselId("fr.ifremer.echobase.entities.references.Vessel#1323196672049#0.9790502711645855"); + + prepareInputFile(conf.getTotalSampleFile(), getImportPath("totalsample.csv")); + prepareInputFile(conf.getSubSampleFile(), getImportPath("subsample.csv")); +// prepareInputFile(conf.getBiometrySampleFile(), getImportPath("biometrysample.csv")); + + CatchesDataImportService service = + getService(CatchesDataImportService.class); + + List<CsvImportResult> result = service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(2, result.size()); + Iterator<CsvImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), Sample.class, 619, 0, 4680); + assertCsvImportResult(iterator.next(), Sample.class, 4061, 0, 4680); + + assertNbEntities(Sample.class, 4680); + assertNbEntities(SampleData.class, 0); + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CatchesDataImportServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java 2012-02-16 23:50:12 UTC (rev 319) @@ -0,0 +1,92 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; +import fr.ifremer.echobase.services.configurations.CommonDataImportMode; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link CommonDataImportService} with {@link CommonDataImportMode#TRANSECT} mode. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CommonDataImportService2Test extends EchoBaseTestServiceSupport { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext("/import-data/echobase-0.3-commonData-result.h2.db"); + } + + @Test + public void doImport() throws Exception { + + // import with mode transect + + assertNbEntities(Voyage.class, 1); + assertNbEntities(Transit.class, 3); + assertNbEntities(Transect.class, 181); + + CommonDataImportConfiguration conf = + new CommonDataImportConfiguration(getLocale()); + + conf.setAreaOfOperationId("fr.ifremer.echobase.entities.references.AreaOfOperation#1323128474277#0.011341599655098622"); + conf.setDatum("datum"); + conf.setVoyageId("fr.ifremer.echobase.entities.data.Voyage#1329429559711#0.2622586586792377"); + conf.setTransectBinUnitsPingAxis("transectBinUnitsPingAxis"); + conf.setTransectGeospatialVerticalPositive("transectGeospatialVerticalPositive"); + conf.setTransectLicence("transectLicence"); + conf.setVesselId("fr.ifremer.echobase.entities.references.Vessel#1323196672049#0.9790502711645855"); + + prepareInputFile(conf.getTransectFile(), getImportPath("transect.csv")); + + conf.setImportMode(CommonDataImportMode.TRANSECT); + + CommonDataImportService service = + getService(CommonDataImportService.class); + + + List<CsvImportResult> result = service.doImport(conf, createFakeUser()); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + Iterator<CsvImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), Transect.class, 181, 0, 362); + + assertNbEntities(Voyage.class, 1); + assertNbEntities(Transit.class, 3); + assertNbEntities(Transect.class, 362); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "commonData", filename}; + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportService2Test.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java 2012-02-16 23:50:12 UTC (rev 319) @@ -0,0 +1,97 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.services.configurations.CommonDataImportConfiguration; +import fr.ifremer.echobase.services.configurations.CommonDataImportMode; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link CommonDataImportService} with {@link CommonDataImportMode#ALL} mode. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class CommonDataImportServiceTest extends EchoBaseTestServiceSupport { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext("/import-data/echobase-0.3-nodata.h2.db"); + } + + @Test + public void doImport() throws Exception { + + // no data in db + + assertNbEntities(Voyage.class, 0); + assertNbEntities(Transit.class, 0); + assertNbEntities(Transect.class, 0); + + // import with mode all (voyage / transit / transect) + + CommonDataImportConfiguration conf = + new CommonDataImportConfiguration(getLocale()); + + conf.setAreaOfOperationId("fr.ifremer.echobase.entities.references.AreaOfOperation#1323128474277#0.011341599655098622"); + conf.setDatum("datum"); + conf.setMissionId("fr.ifremer.echobase.entities.references.Mission#1323127544274#0.7939481378378231"); + conf.setTransectBinUnitsPingAxis("transectBinUnitsPingAxis"); + conf.setTransectGeospatialVerticalPositive("transectGeospatialVerticalPositive"); + conf.setTransectLicence("transectLicence"); + conf.setTransitRelatedActivity("transitRelatedActivity"); + conf.setVoyageDescription("voyageDescription"); + conf.setVesselId("fr.ifremer.echobase.entities.references.Vessel#1323196672049#0.9790502711645855"); + + prepareInputFile(conf.getVoyageFile(), getImportPath("voyage.csv")); + prepareInputFile(conf.getTransitFile(), getImportPath("transit.csv")); + prepareInputFile(conf.getTransectFile(), getImportPath("transect.csv")); + + conf.setImportMode(CommonDataImportMode.ALL); + + CommonDataImportService service = + getService(CommonDataImportService.class); + + List<CsvImportResult> result = + service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(3, result.size()); + Iterator<CsvImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), Voyage.class, 1, 0, 1); + assertCsvImportResult(iterator.next(), Transit.class, 3, 0, 3); + assertCsvImportResult(iterator.next(), Transect.class, 181, 0, 181); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "commonData", filename}; + } + +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/CommonDataImportServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java 2012-02-16 23:50:12 UTC (rev 319) @@ -0,0 +1,117 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import com.google.common.base.Preconditions; +import fr.ifremer.echobase.InputFile; +import fr.ifremer.echobase.entities.EchoBaseEntityEnum; +import fr.ifremer.echobase.entities.EchoBaseUser; +import fr.ifremer.echobase.entities.EchoBaseUserImpl; +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.util.StringUtil; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +/** + * Nice test service support. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public abstract class EchoBaseTestServiceSupport extends EchoBaseServiceSupport { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(EchoBaseTestServiceSupport.class); + + protected abstract FakeEchoBaseServiceContext initContext(); + + @Rule + public FakeEchoBaseServiceContext fakeServiceContext = initContext(); + + @Before + public void setUp() throws Exception { + fakeServiceContext.setServiceFactory(new EchoBaseServiceFactory()); + setServiceContext(fakeServiceContext); + } + + protected EchoBaseUser createFakeUser() { + EchoBaseUser echoBaseUser = new EchoBaseUserImpl(); + echoBaseUser.setEmail("echobase-test@ifremer.fr"); + return echoBaseUser; + } + + protected File getTestDir() { + return fakeServiceContext.getTestDir(); + } + + protected void prepareInputFile(InputFile inputFile, + String... path) throws IOException { + String filename = path[path.length - 1]; + String resourcePath = StringUtil.join(Arrays.asList(path), "/", true); + InputStream inputStream = + getClass().getResourceAsStream(resourcePath); + Preconditions.checkNotNull(inputStream, "Could not find resource at " + resourcePath); + File testDir = getTestDir(); + File newFile = new File(testDir, filename); + FileUtils.copyInputStreamToFile( + inputStream, + newFile + ); + inputFile.setFileName(filename); + inputFile.setFile(newFile); + + if (log.isDebugEnabled()) { + log.debug("Copy file to " + newFile); + } + } + + protected <E extends TopiaEntity> void assertCsvImportResult(CsvImportResult actual, + Class<E> entityType, + int numberCreated, + int numberUpdated, + int nbCount) throws TopiaException { + Assert.assertNotNull(actual); + Assert.assertEquals(EchoBaseEntityEnum.valueOf(entityType), actual.getEntityType()); + Assert.assertEquals(numberCreated, actual.getNumberCreated()); + Assert.assertEquals(numberUpdated, actual.getNumberUpdated()); + assertNbEntities(entityType, nbCount); + } + + protected <E extends TopiaEntity> void assertNbEntities(Class<E> entityType, + int expectedNumber) throws TopiaException { + long count = getDAO(entityType).count(); + Assert.assertEquals(count, expectedNumber); + } +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/EchoBaseTestServiceSupport.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/FakeEchoBaseServiceContext.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/FakeEchoBaseServiceContext.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/FakeEchoBaseServiceContext.java 2012-02-16 23:50:12 UTC (rev 319) @@ -0,0 +1,212 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import com.google.common.base.Preconditions; +import fr.ifremer.echobase.EchoBaseConfiguration; +import fr.ifremer.echobase.EchoBaseConfigurationOption; +import fr.ifremer.echobase.EchoBaseTechnicalException; +import fr.ifremer.echobase.EchoBaseTopiaRootContextFactory; +import fr.ifremer.echobase.entities.meta.DbMeta; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaRuntimeException; +import org.nuiton.util.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; +import java.util.Locale; +import java.util.Properties; + +/** + * Provide an implementation of {@link EchoBaseServiceContext} suitable for repeatable, + * isolated tests requiring a database. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class FakeEchoBaseServiceContext extends TestWatcher implements EchoBaseServiceContext { + + private static final Log log = + LogFactory.getLog(FakeEchoBaseServiceContext.class); + + /** A time-stamp, allow to make multiple build and keep the tests data. */ + protected static final String TIMESTAMP = String.valueOf(System.nanoTime()); + + protected final String initDb; + + protected EchoBaseServiceFactory serviceFactory; + + protected TopiaContext rootContext; + + protected TopiaContext transaction; + + protected Date fakeCurrentTime; + + protected File testDir; + + protected EchoBaseConfiguration configuration; + + public FakeEchoBaseServiceContext(String initDb) { + this.initDb = initDb; + } + + protected File getTestSpecificDirectory(Description description) { + // Trying to look for the temporary folder to store data for the test + String tempDirPath = System.getProperty("java.io.tmpdir"); + if (tempDirPath == null) { + // can this really occur ? + tempDirPath = ""; + if (log.isWarnEnabled()) { + log.warn("'\"java.io.tmpdir\" not defined"); + } + } + File tempDirFile = new File(tempDirPath); + + // create the directory to store database data + String dataBasePath = description.getClassName() + + File.separator // a directory with the test class name + + description.getMethodName()// a sub-directory with the method name + + '_' + + TIMESTAMP; // and a timestamp + File databaseFile = new File(tempDirFile, dataBasePath); + return databaseFile; + } + + @Override + protected void starting(Description description) { + super.starting(description); + EchoBaseTopiaRootContextFactory factory = + new EchoBaseTopiaRootContextFactory(); + testDir = getTestSpecificDirectory(description); + if (log.isInfoEnabled()) { + log.info("Test dir = " + testDir); + } + + if (initDb != null) { + + // let's use this path as the initial db. + + File destinationDb = new File(testDir, "db" + + File.separator + + "echobase.h2.db"); + InputStream inputStream = getClass().getResourceAsStream(initDb); + Preconditions.checkNotNull(inputStream, "Could not find resource from " + initDb); + try { + FileUtil.createDirectoryIfNecessary( + destinationDb.getParentFile()); + + FileUtils.copyInputStreamToFile( + inputStream, + destinationDb + ); + } catch (IOException e) { + throw new EchoBaseTechnicalException("Could not copy db", e); + } finally { + IOUtils.closeQuietly(inputStream); + } + } + + Properties defaultProps = new Properties(); + defaultProps.put(EchoBaseConfigurationOption.DATA_DIRECTORY.getKey(), testDir); + configuration = new EchoBaseConfiguration(defaultProps); + + rootContext = factory.newEmbeddedDatabase(testDir); +// rootContext = factory.newEmbeddedDatabase(testDir, "/" + CONFIGURATION_PATH); + } + + @Override + protected void finished(Description description) { + super.finished(description); + if (!rootContext.isClosed()) { + try { + rootContext.closeContext(); + } catch (TopiaException e) { + throw new TopiaRuntimeException(e); + } + } + } + + /** May be used in test to get a fresh transaction. */ + @Override + public TopiaContext getTransaction() { + if (transaction == null) { + try { + transaction = rootContext.beginTransaction(); + } catch (TopiaException e) { + throw new TopiaRuntimeException(e); + } + } + return transaction; + } + + public TopiaContext newTransaction() throws TopiaException { + return rootContext.beginTransaction(); + } + + @Override + public void setTransaction(TopiaContext transaction) { + } + + @Override + public Locale getLocale() { + return Locale.FRANCE; + } + + @Override + public DbMeta getDbMeta() { + return DbMeta.newDbMeta(); + } + + @Override + public EchoBaseConfiguration getConfiguration() { + return configuration; + } + + @Override + public EchoBaseServiceFactory getServiceFactory() { + return serviceFactory; + } + + @Override + public <E extends EchoBaseService> E newService(Class<E> clazz) { + return serviceFactory.newService(clazz, this); + } + + public void setServiceFactory(EchoBaseServiceFactory serviceFactory) { + this.serviceFactory = serviceFactory; + } + + public File getTestDir() { + return testDir; + } +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/FakeEchoBaseServiceContext.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java =================================================================== --- trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java (rev 0) +++ trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java 2012-02-16 23:50:12 UTC (rev 319) @@ -0,0 +1,94 @@ +/* + * #%L + * EchoBase :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 Ifremer, Codelutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package fr.ifremer.echobase.services; + +import fr.ifremer.echobase.entities.data.GearMetadataValue; +import fr.ifremer.echobase.entities.data.Operation; +import fr.ifremer.echobase.entities.data.OperationMetadataValue; +import fr.ifremer.echobase.entities.data.Transect; +import fr.ifremer.echobase.entities.data.Transit; +import fr.ifremer.echobase.entities.data.Voyage; +import fr.ifremer.echobase.services.configurations.OperationImportConfiguration; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; + +/** + * Tests {@link OperationImportService}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.3 + */ +public class OperationImportServiceTest extends EchoBaseTestServiceSupport { + + protected FakeEchoBaseServiceContext initContext() { + return new FakeEchoBaseServiceContext("/import-data/echobase-0.3-commonData-result.h2.db"); + } + + protected String[] getImportPath(String filename) { + return new String[]{"/import-data", "operation", filename}; + } + + @Test + public void doImport() throws Exception { + + assertNbEntities(Voyage.class, 1); + assertNbEntities(Transit.class, 3); + assertNbEntities(Transect.class, 181); + + // import operations + + assertNbEntities(Operation.class, 0); + assertNbEntities(OperationMetadataValue.class, 0); + assertNbEntities(GearMetadataValue.class, 0); + + OperationImportConfiguration conf = + new OperationImportConfiguration(getLocale()); + + conf.setVoyageId("fr.ifremer.echobase.entities.data.Voyage#1329429559711#0.2622586586792377"); + conf.setVesselId("fr.ifremer.echobase.entities.references.Vessel#1323196672049#0.9790502711645855"); + + prepareInputFile(conf.getOperationFile(), getImportPath("operation.csv")); + prepareInputFile(conf.getOperationMetadataFile(), getImportPath("operationmetadatavalue.csv")); + prepareInputFile(conf.getGearMetadataFile(), getImportPath("gearmetadatavalue.csv")); + + OperationImportService service = + getService(OperationImportService.class); + + List<CsvImportResult> result = service.doImport(conf, createFakeUser()); + + Assert.assertNotNull(result); + Assert.assertEquals(3, result.size()); + Iterator<CsvImportResult> iterator = result.iterator(); + assertCsvImportResult(iterator.next(), Operation.class, 61, 0, 61); + assertCsvImportResult(iterator.next(), OperationMetadataValue.class, 183, 0, 183); + assertCsvImportResult(iterator.next(), GearMetadataValue.class, 427, 0, 427); + + assertNbEntities(Operation.class, 61); + assertNbEntities(OperationMetadataValue.class, 183); + assertNbEntities(GearMetadataValue.class, 427); + } +} Property changes on: trunk/echobase-services/src/test/java/fr/ifremer/echobase/services/OperationImportServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.forge.codelutin.com