This is an automated email from the git hooks/post-receive script. New commit to branch feature/7470_mise_en_place_de_la_date_de_derniere_mise_a_jour in repository tutti. See http://git.codelutin.com/tutti.git commit 026b9a6b880b3776b33e313f3795d4d454162905 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Dec 29 01:18:50 2015 +0100 Ajout d'un tes de replication de données --- .../replication/ObserveReplicationEngineTest.java | 314 +++++++++++++++++++++ 1 file changed, 314 insertions(+) diff --git a/observe-services-topia/src/test/java/fr/ird/observe/entities/replication/ObserveReplicationEngineTest.java b/observe-services-topia/src/test/java/fr/ird/observe/entities/replication/ObserveReplicationEngineTest.java new file mode 100644 index 0000000..bde6b91 --- /dev/null +++ b/observe-services-topia/src/test/java/fr/ird/observe/entities/replication/ObserveReplicationEngineTest.java @@ -0,0 +1,314 @@ +package fr.ird.observe.entities.replication; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import fr.ird.observe.ObserveTopiaApplicationContext; +import fr.ird.observe.ObserveTopiaPersistenceContext; +import fr.ird.observe.services.ObserveTopiaApplicationContextFactory; +import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationTopiaH2; +import fr.ird.observe.services.service.AbstractServiceTopiaTest; +import fr.ird.observe.services.service.DataSourceService; +import fr.ird.observe.test.DatabaseName; +import fr.ird.observe.test.spi.DatabaseNameConfiguration; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.topia.persistence.support.TopiaSqlQuery; + +import java.io.File; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +/** + * Created on 28/12/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ObserveReplicationEngineTest extends AbstractServiceTopiaTest { + + protected DataSourceService service; + + private static final ImmutableMap<String, Long> TRIP_SEINE_1_RESULT = ImmutableMap + .<String, Long>builder() + .put("observe_seine.trip", 1L) + .put("observe_seine.gearusefeatures", 2L) + .put("observe_seine.gearusefeaturesmeasurement", 4L) + .put("observe_seine.route", 34L) + .put("observe_seine.set", 35L) + .put("observe_seine.activity", 862L) + .put("observe_seine.observedsystem", 21L) + .put("observe_seine.floatingobject", 54L) + .put("observe_seine.schoolestimate", 27L) + .put("observe_seine.nontargetcatch", 109L) + .put("observe_seine.nontargetsample", 20L) + .put("observe_seine.nontargetlength", 767L) + .put("observe_seine.targetcatch", 73L) + .put("observe_seine.targetsample", 10L) + .put("observe_seine.targetlength", 85L) + .put("observe_seine.objectobservedspecies", 3L) + .put("observe_seine.objectschoolestimate", 1L) + .put("observe_seine.transmittingbuoy", 62L) + .put("observe_seine.activity_observedsystem", 1182L) + .build(); + + private static final ImmutableMap<String, Long> TRIP_SEINE_2_RESULT = ImmutableMap + .<String, Long>builder() + .put("observe_seine.trip", 1L) + .put("observe_seine.gearusefeatures", 0L) + .put("observe_seine.gearusefeaturesmeasurement", 0L) + .put("observe_seine.route", 46L) + .put("observe_seine.set", 42L) + .put("observe_seine.activity", 657L) + .put("observe_seine.observedsystem", 21L) + .put("observe_seine.activity_observedsystem", 652L) + .put("observe_seine.floatingobject", 50L) + .put("observe_seine.schoolestimate", 10L) + .put("observe_seine.nontargetcatch", 112L) + .put("observe_seine.nontargetsample", 19L) + .put("observe_seine.nontargetlength", 242L) + .put("observe_seine.targetcatch", 73L) + .put("observe_seine.targetsample", 0L) + .put("observe_seine.targetlength", 0L) + .put("observe_seine.objectobservedspecies", 2L) + .put("observe_seine.objectschoolestimate", 3L) + .put("observe_seine.transmittingbuoy", 68L) + .build(); + + private static final ImmutableMap<String, Long> TRIP_SEINE_3_RESULT = ImmutableMap + .<String, Long>builder() + .put("observe_seine.trip", 1L) + .put("observe_seine.gearusefeatures", 0L) + .put("observe_seine.gearusefeaturesmeasurement", 0L) + .put("observe_seine.route", 45L) + .put("observe_seine.set", 35L) + .put("observe_seine.activity", 816L) + .put("observe_seine.observedsystem", 21L) + .put("observe_seine.floatingobject", 41L) + .put("observe_seine.schoolestimate", 35L) + .put("observe_seine.nontargetcatch", 104L) + .put("observe_seine.nontargetsample", 16L) + .put("observe_seine.nontargetlength", 1193L) + .put("observe_seine.targetcatch", 118L) + .put("observe_seine.targetsample", 14L) + .put("observe_seine.targetlength", 53L) + .put("observe_seine.objectobservedspecies", 0L) + .put("observe_seine.objectschoolestimate", 24L) + .put("observe_seine.transmittingbuoy", 39L) + .put("observe_seine.activity_observedsystem", 321L) + .build(); + + private static final ImmutableMap<String, Long> TRIP_LONGLINE_1_RESULT = ImmutableMap + .<String, Long>builder() + .put("observe_longline.trip", 1L) + .put("observe_longline.set", 13L) + .put("observe_longline.activity", 13L) + .put("observe_longline.gearusefeatures", 0L) + .put("observe_longline.gearusefeaturesmeasurement", 0L) + .put("observe_longline.encounter", 27L) + .put("observe_longline.sensorused", 0L) + .put("observe_longline.tdr", 0L) + .put("observe_longline.baitscomposition", 13L) + .put("observe_longline.floatlinescomposition", 65L) + .put("observe_longline.hookscomposition", 39L) + .put("observe_longline.branchlinescomposition", 13L) + .put("observe_longline.section", 234L) + .put("observe_longline.basket", 3042L) + .put("observe_longline.branchline", 18252L) + .put("observe_longline.catch", 435L) + .put("observe_longline.catch_predator", 49L) + .put("observe_longline.mitigationtype", 15L) + .put("observe_longline.sizemeasure", 340L) + .put("observe_longline.weightmeasure", 0L) + .put("observe_longline.tdrrecord", 0L) + .put("observe_longline.species_tdr", 0L) + .put("observe_longline.mitigationtype_set", 0L) + .build(); + + private static final ImmutableMap<String, Long> TRIP_LONGLINE_2_RESULT = ImmutableMap + .<String, Long>builder() + .put("observe_longline.trip", 1L) + .put("observe_longline.set", 5L) + .put("observe_longline.activity", 5L) + .put("observe_longline.gearusefeatures", 0L) + .put("observe_longline.gearusefeaturesmeasurement", 0L) + .put("observe_longline.encounter", 3L) + .put("observe_longline.sensorused", 0L) + .put("observe_longline.tdr", 0L) + .put("observe_longline.baitscomposition", 10L) + .put("observe_longline.floatlinescomposition", 0L) + .put("observe_longline.hookscomposition", 10L) + .put("observe_longline.branchlinescomposition", 2L) + .put("observe_longline.section", 95L) + .put("observe_longline.basket", 1140L) + .put("observe_longline.branchline", 6840L) + .put("observe_longline.catch", 152L) + .put("observe_longline.mitigationtype", 15L) + .put("observe_longline.sizemeasure", 116L) + .put("observe_longline.weightmeasure", 0L) + .put("observe_longline.tdrrecord", 0L) + .put("observe_longline.species_tdr", 0L) + .put("observe_longline.mitigationtype_set", 0L) + .put("observe_longline.catch_predator", 9L) + .build(); + + private static final ImmutableMap<String, Long> TRIP_LONGLINE_3_RESULT = ImmutableMap + .<String, Long>builder() + .put("observe_longline.trip", 1L) + .put("observe_longline.set", 14L) + .put("observe_longline.activity", 14L) + .put("observe_longline.gearusefeatures", 0L) + .put("observe_longline.gearusefeaturesmeasurement", 0L) + .put("observe_longline.encounter", 12L) + .put("observe_longline.sensorused", 0L) + .put("observe_longline.tdr", 0L) + .put("observe_longline.baitscomposition", 14L) + .put("observe_longline.floatlinescomposition", 36L) + .put("observe_longline.hookscomposition", 42L) + .put("observe_longline.branchlinescomposition", 14L) + .put("observe_longline.section", 243L) + .put("observe_longline.basket", 3229L) + .put("observe_longline.branchline", 19374L) + .put("observe_longline.catch", 677L) + .put("observe_longline.MitigationType_set", 0L) + .put("observe_longline.sizemeasure", 647L) + .put("observe_longline.weightmeasure", 0L) + .put("observe_longline.catch_Predator", 25L) + .put("observe_longline.tdrrecord", 0L) + .put("observe_longline.Species_tdr", 0L) + .build(); + + protected ObserveReplicationEngine engine; + + protected ObserveReplicationContext replicationContext; + + protected ObserveTopiaApplicationContext targetTopiaApplicationContext; + + protected ObserveTopiaApplicationContext sourceTopiaApplicationContext; + + @Before + public void setUp() throws Exception { + service = dataSourceResource.newService(DataSourceService.class); + + File targetDatabaseDirectory = new File(dataSourceResource.getTestDirectory(), "targetReplication"); + ObserveDataSourceConfigurationTopiaH2 targetTopiaConfiguration = applicationContextResource.createDataSource(dataSourceResource.getDbVersion(), DatabaseName.referentiel.name(), targetDatabaseDirectory); + + targetTopiaApplicationContext = ObserveTopiaApplicationContextFactory.getOrCreateTopiaApplicationContext(targetTopiaConfiguration); + + sourceTopiaApplicationContext = dataSourceResource.getTopiaApplicationContext(); + + replicationContext = new ObserveReplicationContext(sourceTopiaApplicationContext, targetTopiaApplicationContext, 1000); + engine = new ObserveReplicationEngine(replicationContext); + + } + + @DatabaseNameConfiguration(DatabaseName.dataForTestSeine) + @Test + public void testReplicateTripSeine1() throws Exception { + testTripSeine(engine, TRIP_SEINE_ID_1, TRIP_SEINE_1_RESULT); + } + + @DatabaseNameConfiguration(DatabaseName.dataForTestSeine) + @Test + public void testReplicateTripSeine2() throws Exception { + testTripSeine(engine, TRIP_SEINE_ID_2, TRIP_SEINE_2_RESULT); + } + + @DatabaseNameConfiguration(DatabaseName.dataForTestSeine) + @Test + public void testReplicateTripSeine3() throws Exception { + testTripSeine(engine, TRIP_SEINE_ID_3, TRIP_SEINE_3_RESULT); + } + + @DatabaseNameConfiguration(DatabaseName.dataForTestLongline) + @Test + public void testReplicateTripLonline1() throws Exception { + testTripLongline(engine, TRIP_LONGLINE_ID_1, TRIP_LONGLINE_1_RESULT); + } + + @DatabaseNameConfiguration(DatabaseName.dataForTestLongline) + @Test + public void testReplicateTripLonline2() throws Exception { + testTripLongline(engine, TRIP_LONGLINE_ID_2, TRIP_LONGLINE_2_RESULT); + } + + @DatabaseNameConfiguration(DatabaseName.dataForTestLongline) + @Test + public void testReplicateTripLonline3() throws Exception { + testTripLongline(engine, TRIP_LONGLINE_ID_3, TRIP_LONGLINE_3_RESULT); + } + + protected void testTripSeine(ObserveReplicationEngine engine, String tripId, ImmutableMap<String, Long> expectedResults) { + + ImmutableSet<ObserveReplicationActionContext> actionContexts = engine.replicateTripSeine(tripId); + + assertReplicateTripResults(expectedResults, actionContexts); + + } + + protected void testTripLongline(ObserveReplicationEngine engine, String tripId, ImmutableMap<String, Long> expectedResults) { + + ImmutableSet<ObserveReplicationActionContext> actionContexts = engine.replicateTripLongline(tripId); + + assertReplicateTripResults(expectedResults, actionContexts); + + } + + private void assertReplicateTripResults(ImmutableMap<String, Long> expectedResults, ImmutableSet<ObserveReplicationActionContext> actionContexts) { + + try (ObserveTopiaPersistenceContext persistenceContext = targetTopiaApplicationContext.newPersistenceContext()) { + + for (ObserveReplicationActionContext actionContext : actionContexts) { + + String fullyTableName = actionContext.getFullyTableName(); + + if (!expectedResults.containsKey(fullyTableName.toLowerCase())) { + + CountTableSqlWork countQuery = new CountTableSqlWork(fullyTableName); + Long count = persistenceContext.getSqlSupport().findSingleResult(countQuery); + + System.out.println(fullyTableName + " , " + count); + } + + } + + for (Map.Entry<String, Long> entry : expectedResults.entrySet()) { + String fullyTableName = entry.getKey(); + Long expectedCount = entry.getValue(); + CountTableSqlWork countQuery = new CountTableSqlWork(fullyTableName); + Long count = persistenceContext.getSqlSupport().findSingleResult(countQuery); + + System.out.println("Found: " + fullyTableName + " , " + count); + Assert.assertEquals("Should have found " + expectedCount + " on " + fullyTableName + ", but was " + count, expectedCount, count); + + } + + } + } + + + protected class CountTableSqlWork extends TopiaSqlQuery<Long> { + + private final String fullyTableName; + + public CountTableSqlWork(String fullyTableName) { + this.fullyTableName = fullyTableName; + } + + @Override + public PreparedStatement prepareQuery(Connection connection) throws SQLException { + String sql = "SELECT count(*) FROM " + fullyTableName; + PreparedStatement statement = connection.prepareStatement(sql); + + return statement; + } + + @Override + public Long prepareResult(ResultSet set) throws SQLException { + return set.getLong(1); + } + } + +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.