Tony CHEMIT pushed to branch develop at ultreiaio / ird-t3 Commits: 179ad7bb by Tony CHEMIT at 2018-03-23T15:01:26Z add more tests for cache + fix some harbour oceans and fix his request) - - - - - 10 changed files: - t3-actions/src/main/java/fr/ird/t3/FakeT3ServiceContext.java - t3-domain/pom.xml - t3-domain/src/main/java/fr/ird/t3/entities/cache/WeightCategoryTreatmentCache.java - t3-domain/src/main/java/fr/ird/t3/entities/cache/ZoneStratumCache.java - t3-domain/src/main/java/fr/ird/t3/entities/reference/AbstractOceanTopiaDao.java - t3-domain/src/main/java/fr/ird/t3/test/T3H2Database.java - t3-domain/src/main/resources/db/migration/V2_1_09_fill-Harbour-ocean.sql - + t3-domain/src/test/java/fr/ird/t3/entities/cache/WeightCategoryTreatmentCacheTest.java - + t3-domain/src/test/java/fr/ird/t3/entities/cache/ZoneStratumCacheTest.java - t3-domain/src/test/java/fr/ird/t3/entities/reference/OceanTopiaDaoPgTest.java Changes: ===================================== t3-actions/src/main/java/fr/ird/t3/FakeT3ServiceContext.java ===================================== --- a/t3-actions/src/main/java/fr/ird/t3/FakeT3ServiceContext.java +++ b/t3-actions/src/main/java/fr/ird/t3/FakeT3ServiceContext.java @@ -21,28 +21,16 @@ package fr.ird.t3; import fr.ird.t3.entities.T3TopiaApplicationContext; -import fr.ird.t3.entities.T3TopiaApplicationContextBuilder; import fr.ird.t3.entities.T3TopiaPersistenceContext; import fr.ird.t3.entities.user.T3Users; import fr.ird.t3.services.T3Service; import fr.ird.t3.services.T3ServiceContext; import fr.ird.t3.services.T3ServiceFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.Assume; -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; -import org.mockito.Mockito; +import fr.ird.t3.test.T3H2Database; import org.nuiton.topia.persistence.TopiaException; -import org.nuiton.util.FileUtil; -import org.nuiton.version.Version; -import java.io.Closeable; -import java.io.File; import java.util.Date; import java.util.Locale; -import java.util.Properties; import java.util.function.Supplier; /** @@ -51,25 +39,19 @@ import java.util.function.Supplier; * @author Tony Chemit - dev@tchemit.fr * @since 1.1.1 */ -public class FakeT3ServiceContext implements TestRule, T3ServiceContext, Closeable { - - private static final Log log = LogFactory.getLog(FakeT3ServiceContext.class); +public class FakeT3ServiceContext extends T3H2Database implements T3ServiceContext { private final T3ServiceFactory serviceFactory = new T3ServiceFactory(); - private final boolean injectReferential; protected T3TopiaPersistenceContext transaction; - private T3TopiaApplicationContext rootContext; - private File testDir; - private T3Config applicationConfiguration; private Locale locale; - private boolean initOk; + public FakeT3ServiceContext() { this(false); } public FakeT3ServiceContext(boolean injectReferential) { - this.injectReferential = injectReferential; + super(injectReferential); setLocale(Locale.FRANCE); } @@ -78,10 +60,6 @@ public class FakeT3ServiceContext implements TestRule, T3ServiceContext, Closeab return null; } - public boolean isInitOk() { - return initOk; - } - /** May be used in test to get a fresh transaction. */ @Override public Supplier<T3TopiaPersistenceContext> getT3TopiaPersistenceContext() { @@ -90,11 +68,11 @@ public class FakeT3ServiceContext implements TestRule, T3ServiceContext, Closeab @Override public T3TopiaApplicationContext getApplicationContext() { - return rootContext; + return topiaApplicationContext; } - public T3TopiaPersistenceContext newTransaction() throws TopiaException { - return rootContext.newPersistenceContext(); + private T3TopiaPersistenceContext newTransaction() throws TopiaException { + return topiaApplicationContext.newPersistenceContext(); } @Override @@ -108,11 +86,6 @@ public class FakeT3ServiceContext implements TestRule, T3ServiceContext, Closeab } @Override - public T3Config getApplicationConfiguration() { - return applicationConfiguration; - } - - @Override public T3ServiceFactory getServiceFactory() { return serviceFactory; } @@ -132,58 +105,4 @@ public class FakeT3ServiceContext implements TestRule, T3ServiceContext, Closeab return new Date(); } - public File getTestDir() { - return testDir; - } - - private void starting(Description description) { - testDir = T3IOUtil.getTestSpecificDirectory(description.getTestClass(), description.getMethodName()); - log.info(String.format("Test dir = %s", testDir)); - Properties defaultProps = new Properties(); - defaultProps.put(T3ConfigOption.DATA_DIRECTORY.getKey(), testDir); - File treatmentDirectory = new File(testDir, "treatment"); - FileUtil.createDirectoryIfNecessary(treatmentDirectory); - T3Config realConfiguration = new T3Config(defaultProps) { - @Override - public void init() { - parse(); - } - }; - realConfiguration.init(); - Version t3DataVersion = realConfiguration.getT3DataVersion(); - applicationConfiguration = Mockito.mock(T3Config.class); - Mockito.when(applicationConfiguration.getDataDirectory()).thenReturn(testDir); - Mockito.when(applicationConfiguration.getTreatmentWorkingDirectory()).thenReturn(treatmentDirectory); - Mockito.when(applicationConfiguration.getT3DataVersion()).thenReturn(t3DataVersion); - Mockito.when(applicationConfiguration.getTreatmentWorkingDirectory(Mockito.anyString(), Mockito.anyBoolean())).thenCallRealMethod(); - Mockito.when(applicationConfiguration.getApplicationVersion()).thenReturn(realConfiguration.getApplicationVersion()); - rootContext = T3TopiaApplicationContextBuilder.forH2Referential(testDir.toPath(), injectReferential ? t3DataVersion : null).build(); - } - - @Override - public void close() { - if (transaction != null) { - transaction.close(); - } - } - - @Override - public Statement apply(Statement base, Description description) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - try { - starting(description); - initOk=true; - } catch (Exception e) { - Assume.assumeNoException("Can't start test: " + e.getMessage(), e); - } - try { - base.evaluate(); - } finally { - close(); - } - } - }; - } } ===================================== t3-domain/pom.xml ===================================== --- a/t3-domain/pom.xml +++ b/t3-domain/pom.xml @@ -178,6 +178,12 @@ <!--we have a test api in source--> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <!--We don't want to create yet another module for tests, so just embedded them inside sources--> + <scope>provided</scope> + </dependency> <dependency> <groupId>com.h2database</groupId> ===================================== t3-domain/src/main/java/fr/ird/t3/entities/cache/WeightCategoryTreatmentCache.java ===================================== --- a/t3-domain/src/main/java/fr/ird/t3/entities/cache/WeightCategoryTreatmentCache.java +++ b/t3-domain/src/main/java/fr/ird/t3/entities/cache/WeightCategoryTreatmentCache.java @@ -56,24 +56,29 @@ public class WeightCategoryTreatmentCache { return forOceanAndSchoolType(activity.getOcean(), activity.getSchoolType()); } - private List<WeightCategoryTreatment> forOceanAndSchoolType(Ocean ocean, SchoolType schoolType) { + List<WeightCategoryTreatment> forOceanAndSchoolType(Ocean ocean, SchoolType schoolType) { String key = ocean.getLabel1() + "#" + schoolType.getLabel1(); - List<WeightCategoryTreatment> weightCategoryTreatments = cache.get(key); - if (weightCategoryTreatments == null) { - weightCategoryTreatments = dao.forOceanEquals(ocean).addEquals(WeightCategoryTreatment.PROPERTY_SCHOOL_TYPE, schoolType).findAll(); - WeightCategories.sort(weightCategoryTreatments); - cache.putAll(key, weightCategoryTreatments); + List<WeightCategoryTreatment> result; + if (cache.containsKey(key)) { + result = cache.get(key); + } else { + result = dao.forOceanEquals(ocean).addEquals(WeightCategoryTreatment.PROPERTY_SCHOOL_TYPE, schoolType).findAll(); + WeightCategories.sort(result); + cache.putAll(key, result); + log.info(String.format("Cache WeightCategoryTreatment [%d] for %s (%d categories)", cache.keySet().size(), key, result.size())); } - return weightCategoryTreatments; + return result; } public List<WeightCategoryTreatment> forOceansAndSchoolType(Collection<Ocean> oceans, SchoolType schoolType) { String key = Joiner.on("#").join(oceans.stream().map(Ocean::getLabel1).collect(Collectors.toList())) + "#" + schoolType.getLabel1(); - List<WeightCategoryTreatment> result = cache.get(key); - if (result == null) { + List<WeightCategoryTreatment> result; + if (cache.containsKey(key)) { + result = cache.get(key); + } else { result = dao.forOceanIn(oceans).addEquals(WeightCategoryTreatment.PROPERTY_SCHOOL_TYPE, schoolType).findAll(); WeightCategories.sort(result); - log.info(String.format("Cache WeightCategoryTreatment [%d] for %s (%d categories)", cache.size(), key, result.size())); + log.info(String.format("Cache WeightCategoryTreatment [%d] for %s (%d categories)", cache.keySet().size(), key, result.size())); cache.putAll(key, result); } return result; ===================================== t3-domain/src/main/java/fr/ird/t3/entities/cache/ZoneStratumCache.java ===================================== --- a/t3-domain/src/main/java/fr/ird/t3/entities/cache/ZoneStratumCache.java +++ b/t3-domain/src/main/java/fr/ird/t3/entities/cache/ZoneStratumCache.java @@ -1,5 +1,26 @@ package fr.ird.t3.entities.cache; +/*- + * #%L + * T3 :: Domain + * %% + * Copyright (C) 2010 - 2018 IRD, Code Lutin, Ultreia.io + * %% + * 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% + */ + import com.google.common.base.Joiner; import com.google.common.collect.ArrayListMultimap; import fr.ird.t3.entities.T3TopiaPersistenceContext; @@ -39,10 +60,12 @@ public class ZoneStratumCache { public List<ZoneStratumAware> forOceansAndSchoolType(Collection<Ocean> oceans, SchoolType schoolType) { String key = Joiner.on("#").join(oceans.stream().map(Ocean::getLabel1).collect(Collectors.toList())) + "#" + schoolType.getLabel1(); - List<ZoneStratumAware> result = cache.get(key); - if (result == null) { + List<ZoneStratumAware> result; + if (cache.containsKey(key)) { + result = cache.get(key); + } else { result = zoneMeta.getZones(oceans, schoolType, zoneVersion, persistenceContext); - log.info(String.format("Cache Zone [%d] for %s (%d zones)", cache.size(), key, result.size())); + log.info(String.format("Cache Zone [%d] for %s (%d zones)", cache.keySet().size(), key, result.size())); cache.putAll(key, result); } return result; ===================================== t3-domain/src/main/java/fr/ird/t3/entities/reference/AbstractOceanTopiaDao.java ===================================== --- a/t3-domain/src/main/java/fr/ird/t3/entities/reference/AbstractOceanTopiaDao.java +++ b/t3-domain/src/main/java/fr/ird/t3/entities/reference/AbstractOceanTopiaDao.java @@ -84,7 +84,7 @@ public class AbstractOceanTopiaDao<E extends Ocean> extends GeneratedOceanTopiaD public PreparedStatement prepareQuery(Connection connection) throws SQLException { @SuppressWarnings("SqlResolve") PreparedStatement ps = connection.prepareStatement( - String.format("SELECT o.topiaid FROM %s a, Ocean o WHERE a.topiaId = ? AND ST_Contains(o.the_geom, a.%s)", tableName, geoColumnName)); + String.format("SELECT o.topiaid FROM %s a, Ocean o WHERE a.topiaId = ? AND ST_DWithin(a.the_geom, o.%s, 1)", tableName, geoColumnName)); ps.setString(1, id); return ps; } ===================================== t3-domain/src/main/java/fr/ird/t3/test/T3H2Database.java ===================================== --- a/t3-domain/src/main/java/fr/ird/t3/test/T3H2Database.java +++ b/t3-domain/src/main/java/fr/ird/t3/test/T3H2Database.java @@ -21,18 +21,27 @@ package fr.ird.t3.test; * #L% */ +import fr.ird.t3.T3Config; +import fr.ird.t3.T3ConfigOption; import fr.ird.t3.T3IOUtil; import fr.ird.t3.entities.T3EntityHelper; import fr.ird.t3.entities.T3TopiaApplicationContext; import fr.ird.t3.entities.T3TopiaApplicationContextBuilder; import fr.ird.t3.entities.T3TopiaPersistenceContext; -import org.junit.rules.TestWatcher; +import org.junit.Assume; +import org.junit.rules.TestRule; import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.mockito.Mockito; import org.nuiton.topia.persistence.TopiaException; +import org.nuiton.util.FileUtil; +import org.nuiton.version.Version; +import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Properties; /** * A new database created for each test. @@ -40,21 +49,48 @@ import java.io.InputStream; * @author Tony Chemit - dev@tchemit.fr * @since 1.0 */ -public class T3H2Database extends TestWatcher { +public class T3H2Database implements TestRule, Closeable { + private final boolean injectReferential; private File testBasedir; + protected T3TopiaApplicationContext topiaApplicationContext; + private T3Config applicationConfiguration; + private boolean initOk; + public T3H2Database() { + this(false); + } - private T3TopiaApplicationContext topiaApplicationContext; + public T3H2Database(boolean injectReferential) { + this.injectReferential = injectReferential; + } - @Override - protected void starting(Description description) { + private void starting(Description description) { testBasedir = T3IOUtil.getTestSpecificDirectory(description.getTestClass(), description.getMethodName()); - topiaApplicationContext = T3TopiaApplicationContextBuilder.forH2(testBasedir.toPath()).build(); - } - @Override - public void finished(Description description) { - T3EntityHelper.releaseRootContext(topiaApplicationContext); + Properties defaultProps = new Properties(); + defaultProps.put(T3ConfigOption.DATA_DIRECTORY.getKey(), testBasedir); + File treatmentDirectory = new File(testBasedir, "treatment"); + FileUtil.createDirectoryIfNecessary(treatmentDirectory); + T3Config realConfiguration = new T3Config(defaultProps) { + @Override + public void init() { + parse(); + } + }; + realConfiguration.init(); + Version t3DataVersion = realConfiguration.getT3DataVersion(); + applicationConfiguration = Mockito.mock(T3Config.class); + Mockito.when(applicationConfiguration.getDataDirectory()).thenReturn(testBasedir); + Mockito.when(applicationConfiguration.getTreatmentWorkingDirectory()).thenReturn(treatmentDirectory); + Mockito.when(applicationConfiguration.getT3DataVersion()).thenReturn(t3DataVersion); + Mockito.when(applicationConfiguration.getTreatmentWorkingDirectory(Mockito.anyString(), Mockito.anyBoolean())).thenCallRealMethod(); + Mockito.when(applicationConfiguration.getApplicationVersion()).thenReturn(realConfiguration.getApplicationVersion()); + + if (injectReferential) { + topiaApplicationContext = T3TopiaApplicationContextBuilder.forH2Referential(testBasedir.toPath(), t3DataVersion).build(); + } else { + topiaApplicationContext = T3TopiaApplicationContextBuilder.forH2(testBasedir.toPath()).build(); + } } public T3TopiaPersistenceContext beginTransaction() throws TopiaException { @@ -69,7 +105,43 @@ public class T3H2Database extends TestWatcher { } } + @SuppressWarnings("unused") public File getTestBasedir() { return testBasedir; } + + public T3Config getApplicationConfiguration() { + return applicationConfiguration; + } + + @Override + public void close() { + if (topiaApplicationContext != null) { + T3EntityHelper.releaseRootContext(topiaApplicationContext); + } + } + + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + try { + starting(description); + initOk=true; + } catch (Exception e) { + Assume.assumeNoException("Can't start test: " + e.getMessage(), e); + } + try { + base.evaluate(); + } finally { + close(); + } + } + }; + } + + public boolean isInitOk() { + return initOk; + } } ===================================== t3-domain/src/main/resources/db/migration/V2_1_09_fill-Harbour-ocean.sql ===================================== --- a/t3-domain/src/main/resources/db/migration/V2_1_09_fill-Harbour-ocean.sql +++ b/t3-domain/src/main/resources/db/migration/V2_1_09_fill-Harbour-ocean.sql @@ -53,3 +53,27 @@ UPDATE Harbour SET ocean='fr.ird.t3.entities.reference.Ocean#1297580528924#0.024 UPDATE Harbour SET ocean='fr.ird.t3.entities.reference.Ocean#1297580528924#0.02462443299831396' WHERE topiaId = 'fr.ird.t3.entities.reference.Harbour#1297580528933#0.4894751387384305'; UPDATE Harbour SET ocean='fr.ird.t3.entities.reference.Ocean#1297580528924#0.02462443299831396' WHERE topiaId = 'fr.ird.t3.entities.reference.Harbour#1297580528934#0.17822915599428157'; UPDATE Harbour SET ocean='fr.ird.t3.entities.reference.Ocean#1297580528924#0.02462443299831396' WHERE topiaId = 'fr.ird.t3.entities.reference.Harbour#1297580528932#0.21164070994041773'; + +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.09848580496276282'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.4358849181824964'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.5213952546445324'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528932#0.05217800949850426'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528932#0.09458236901561745'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1464000000000#0.00115115'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1464000000000#0.00116116'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1464000000000#0.00117117'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528935#0.5032524703497515'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.3128607487452171'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.7077375683910743'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.37146946328714225'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.1776110119020825'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528930#0.10672736119748483'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528929#0.29263430155373604'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528932#0.23227601066196246'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528934#0.5223852489420004'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528929#0.037238377699795766'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528934#0.9698949751160373'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528933#0.2600646486222412'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528934#0.35235641408050444'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1297580528934#0.45885264373047663'; +UPDATE Harbour SET ocean ='fr.ird.t3.entities.reference.Ocean#1297580528924#0.4917298410119624' WHERE topiaId ='fr.ird.t3.entities.reference.Harbour#1460000000000#0.19'; ===================================== t3-domain/src/test/java/fr/ird/t3/entities/cache/WeightCategoryTreatmentCacheTest.java ===================================== --- /dev/null +++ b/t3-domain/src/test/java/fr/ird/t3/entities/cache/WeightCategoryTreatmentCacheTest.java @@ -0,0 +1,83 @@ +package fr.ird.t3.entities.cache; + +/*- + * #%L + * T3 :: Domain + * %% + * Copyright (C) 2010 - 2018 IRD, Code Lutin, Ultreia.io + * %% + * 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% + */ + +import com.google.common.collect.ImmutableSet; +import fr.ird.t3.entities.T3TopiaPersistenceContext; +import fr.ird.t3.entities.reference.Ocean; +import fr.ird.t3.entities.reference.SchoolType; +import fr.ird.t3.entities.reference.WeightCategoryTreatment; +import fr.ird.t3.test.T3H2Database; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + +import java.util.List; +import java.util.Set; + +/** + * Created by tchemit on 23/03/2018. + * + * @author Tony Chemit - dev@tchemit.fr + */ +public class WeightCategoryTreatmentCacheTest { + + @ClassRule + public static final T3H2Database db = new T3H2Database(true); + + private WeightCategoryTreatmentCache cache; + private T3TopiaPersistenceContext persistenceContext; + + @Before + public void setUp() { + persistenceContext = db.beginTransaction(); + cache = persistenceContext.newWeightCategoryTreatmentCache(); + } + + @Test + public void forOceanAndSchoolType() { + List<Ocean> oceans = persistenceContext.getOceanDao().findAll(); + List<SchoolType> schoolTypes = persistenceContext.getSchoolTypeDao().findAll(); + Set<Integer> oceanCodesWithCategories = ImmutableSet.of(1, 2); + for (Ocean ocean : oceans) { + for (SchoolType schoolType : schoolTypes) { + List<WeightCategoryTreatment> weightCategoryTreatments = cache.forOceanAndSchoolType(ocean, schoolType); + Assert.assertNotNull(weightCategoryTreatments); + if (oceanCodesWithCategories.contains(ocean.getCode())) { + Assert.assertFalse(weightCategoryTreatments.isEmpty()); + } + } + } + } + + @Test + public void forOceansAndSchoolType() { + List<Ocean> oceans = persistenceContext.getOceanDao().findAll(); + List<SchoolType> schoolTypes = persistenceContext.getSchoolTypeDao().findAll(); + for (SchoolType schoolType : schoolTypes) { + List<WeightCategoryTreatment> weightCategoryTreatments = cache.forOceansAndSchoolType(oceans, schoolType); + Assert.assertNotNull(weightCategoryTreatments); + Assert.assertFalse(weightCategoryTreatments.isEmpty()); + } + } +} ===================================== t3-domain/src/test/java/fr/ird/t3/entities/cache/ZoneStratumCacheTest.java ===================================== --- /dev/null +++ b/t3-domain/src/test/java/fr/ird/t3/entities/cache/ZoneStratumCacheTest.java @@ -0,0 +1,66 @@ +package fr.ird.t3.entities.cache; + +/*- + * #%L + * T3 :: Domain + * %% + * Copyright (C) 2010 - 2018 IRD, Code Lutin, Ultreia.io + * %% + * 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% + */ + +import fr.ird.t3.entities.T3TopiaPersistenceContext; +import fr.ird.t3.entities.reference.Ocean; +import fr.ird.t3.entities.reference.SchoolType; +import fr.ird.t3.entities.reference.zone.ZoneETMeta; +import fr.ird.t3.entities.reference.zone.ZoneStratumAware; +import fr.ird.t3.test.T3PostgresqlDatabase; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + +import java.util.List; + +/** + * Created by tchemit on 23/03/2018. + * + * @author Tony Chemit - dev@tchemit.fr + */ +public class ZoneStratumCacheTest { + + @ClassRule + public static final T3PostgresqlDatabase db = new T3PostgresqlDatabase(); + + private ZoneStratumCache cache; + private T3TopiaPersistenceContext persistenceContext; + + @Before + public void setUp() { + persistenceContext = db.beginTransaction(); + cache = persistenceContext.newZoneStratumCache(new ZoneETMeta(), "v2011"); + } + + @Test + public void forOceansAndSchoolType() { + List<Ocean> oceans = persistenceContext.getOceanDao().findAll(); + List<SchoolType> schoolTypes = persistenceContext.getSchoolTypeDao().findAll(); + for (SchoolType schoolType : schoolTypes) { + List<ZoneStratumAware> zones = cache.forOceansAndSchoolType(oceans, schoolType); + Assert.assertNotNull(zones); + Assert.assertFalse(zones.isEmpty()); + } + } +} ===================================== t3-domain/src/test/java/fr/ird/t3/entities/reference/OceanTopiaDaoPgTest.java ===================================== --- a/t3-domain/src/test/java/fr/ird/t3/entities/reference/OceanTopiaDaoPgTest.java +++ b/t3-domain/src/test/java/fr/ird/t3/entities/reference/OceanTopiaDaoPgTest.java @@ -43,6 +43,8 @@ public class OceanTopiaDaoPgTest { @Test public void findOceanByHarbour() { + int wrongOcean = 0; + int fixOcean = 0; StringBuilder queryBuilder = new StringBuilder(); try (T3TopiaPersistenceContext persistenceContext = db.beginTransaction()) { OceanTopiaDao oceanDao = persistenceContext.getOceanDao(); @@ -55,11 +57,13 @@ public class OceanTopiaDaoPgTest { if (harbour.getOcean() == null) { log.warn(String.format("Harbour %s [%s] with no ocean, can use now %s [%s]", harbour.getLabel1(), harbour.getTopiaId(), oceanByHarbour.getLabel1(), oceanByHarbour.getTopiaId())); queryBuilder.append(String.format("UPDATE Harbour SET ocean ='%s' WHERE topiaId ='%s';\n", oceanByHarbour.getTopiaId(), harbour.getTopiaId())); + fixOcean++; } else { try { Assert.assertEquals(String.format("Wrong ocean for %s", harbour.getLabel1()), harbour.getOcean().getLabel1(), oceanByHarbour.getLabel1()); } catch (Throwable e) { log.error(e.getMessage()); + wrongOcean++; } } } @@ -68,5 +72,7 @@ public class OceanTopiaDaoPgTest { if (queryBuilder.length() > 0) { log.warn("You can update your database with thoses queries to fill missing harbour ocean:\n" + queryBuilder.toString()); } + log.info("Number of wrong oceans: " + wrongOcean); + log.info("Number of fixed oceans: " + fixOcean); } } View it on GitLab: https://gitlab.com/ultreiaio/ird-t3/commit/179ad7bbf9a63bad33d568c32c232cee2... --- View it on GitLab: https://gitlab.com/ultreiaio/ird-t3/commit/179ad7bbf9a63bad33d568c32c232cee2... You're receiving this email because of your account on gitlab.com.