branch feature/7458 updated (4086edc -> 8683d26)
This is an automated email from the git hooks/post-receive script. New change to branch feature/7458 in repository observe. See http://git.codelutin.com/observe.git from 4086edc add activitySeine Services (refs #7458). new a7d0bd5 typage du labelset new ad485d1 Ajout dun premier binding pour les RTP new 8683d26 Premiers tests pour ReferentialServiceTopia The 3 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 8683d266283c84244fde46a57b5693f6f2cd7534 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Aug 18 10:57:13 2015 +0200 Premiers tests pour ReferentialServiceTopia commit ad485d156bb9d1342776560332a118203a667593 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Aug 18 10:56:36 2015 +0200 Ajout dun premier binding pour les RTP commit a7d0bd51fbe8e816aa62025cf3687fc2ba774b29 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Aug 18 09:59:18 2015 +0200 typage du labelset Summary of changes: .../dto/referential/ReferentialLabelDtos.java | 3 +- .../dto/referential/ReferentialLabelSetDto.java | 17 +++ .../dto/referential/ReferentialLabelSetDtos.java | 14 +++ .../services/service/ReferentialServiceRest.java | 2 +- observe-services-topia/pom.xml | 7 ++ .../services/ObserveServiceFactoryTopia.java | 3 +- .../services/service/ReferentialServiceTopia.java | 132 +++++++++++++++------ .../service/ApplicationContextResource.java | 75 ++++++++++++ .../services/service/DataSourceResource.java | 126 ++++++++++++++++++++ .../service/ReferentialServiceTopiaTest.java | 47 +++++++- 10 files changed, 381 insertions(+), 45 deletions(-) create mode 100644 observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDto.java create mode 100644 observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDtos.java create mode 100644 observe-services-topia/src/test/java/fr/ird/observe/services/service/ApplicationContextResource.java create mode 100644 observe-services-topia/src/test/java/fr/ird/observe/services/service/DataSourceResource.java -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/7458 in repository observe. See http://git.codelutin.com/observe.git commit a7d0bd51fbe8e816aa62025cf3687fc2ba774b29 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Aug 18 09:59:18 2015 +0200 typage du labelset --- .../dto/referential/ReferentialLabelSetDto.java | 17 +++++++++++++++++ .../dto/referential/ReferentialLabelSetDtos.java | 14 ++++++++++++++ .../services/service/ReferentialServiceRest.java | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDto.java b/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDto.java new file mode 100644 index 0000000..2b8a45d --- /dev/null +++ b/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDto.java @@ -0,0 +1,17 @@ +package fr.ird.observe.services.dto.referential; + +public class ReferentialLabelSetDto<R extends ReferentialDto> extends AbstractReferentialLabelSetDto { + + private static final long serialVersionUID = 1L; + + protected final Class<R> type; + + public ReferentialLabelSetDto(Class<R> type) { + this.type = type; + } + + public Class<R> getType() { + return type; + } + +} diff --git a/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDtos.java b/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDtos.java new file mode 100644 index 0000000..0baa167 --- /dev/null +++ b/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelSetDtos.java @@ -0,0 +1,14 @@ +package fr.ird.observe.services.dto.referential; + +import java.util.LinkedHashSet; + +public class ReferentialLabelSetDtos extends AbstractReferentialLabelSetDtos { + + public static <R extends ReferentialDto> ReferentialLabelSetDto<R> newReferentialLabelSetDto(Class<R> type, + LinkedHashSet<ReferentialLabelDto<R>> labels) { + ReferentialLabelSetDto<R> dto = new ReferentialLabelSetDto<>(type); + dto.setReferentialLabel((LinkedHashSet)labels); + return dto; + } + +} diff --git a/observe-services-rest/src/main/java/fr/ird/observe/services/service/ReferentialServiceRest.java b/observe-services-rest/src/main/java/fr/ird/observe/services/service/ReferentialServiceRest.java index d3d88e6..fe60d68 100644 --- a/observe-services-rest/src/main/java/fr/ird/observe/services/service/ReferentialServiceRest.java +++ b/observe-services-rest/src/main/java/fr/ird/observe/services/service/ReferentialServiceRest.java @@ -18,7 +18,7 @@ public class ReferentialServiceRest extends ObserveServiceRest implements Refere @Override public <R extends ReferentialDto> ReferentialLabelSetDto getReferentialLabelSet(Class<R> type) { - return new ReferentialLabelSetDto(); + return new ReferentialLabelSetDto(type); } @Override -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/7458 in repository observe. See http://git.codelutin.com/observe.git commit ad485d156bb9d1342776560332a118203a667593 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Aug 18 10:56:36 2015 +0200 Ajout dun premier binding pour les RTP --- .../fr/ird/observe/services/dto/referential/ReferentialLabelDtos.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelDtos.java b/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelDtos.java index 176d9c0..b914192 100644 --- a/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelDtos.java +++ b/observe-services-model/src/main/java/fr/ird/observe/services/dto/referential/ReferentialLabelDtos.java @@ -67,7 +67,8 @@ public class ReferentialLabelDtos extends AbstractReferentialLabelDtos { .put(GearCaracteristicTypeDto.class, DEFAULT_PROPERTY_NAMES) .put(GearDto.class, DEFAULT_PROPERTY_NAMES) .put(HarbourDto.class, new String[]{ReferentialDto.PROPERTY_CODE, HarbourDto.PROPERTY_NAME, HarbourDto.PROPERTY_LOCODE}) - .put(LengthWeightParameterDto.class, new String[]{}) + // FIXME A revoir car on veut en fait ocean/libelle et species/scientificLabel + .put(LengthWeightParameterDto.class, new String[]{LengthWeightParameterDto.PROPERTY_SEX, LengthWeightParameterDto.PROPERTY_OCEAN, LengthWeightParameterDto.PROPERTY_SPECIES, LengthWeightParameterDto.PROPERTY_LENGTH_WEIGHT_FORMULA, LengthWeightParameterDto.PROPERTY_WEIGHT_LENGTH_FORMULA}) .put(OceanDto.class, DEFAULT_PROPERTY_NAMES) .put(OrganismDto.class, DEFAULT_PROPERTY_NAMES) .put(PersonDto.class, new String[]{PersonDto.PROPERTY_FIRST_NAME, PersonDto.PROPERTY_LAST_NAME}) -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/7458 in repository observe. See http://git.codelutin.com/observe.git commit 8683d266283c84244fde46a57b5693f6f2cd7534 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Aug 18 10:57:13 2015 +0200 Premiers tests pour ReferentialServiceTopia --- observe-services-topia/pom.xml | 7 ++ .../services/ObserveServiceFactoryTopia.java | 3 +- .../services/service/ReferentialServiceTopia.java | 132 +++++++++++++++------ .../service/ApplicationContextResource.java | 75 ++++++++++++ .../services/service/DataSourceResource.java | 126 ++++++++++++++++++++ .../service/ReferentialServiceTopiaTest.java | 47 +++++++- 6 files changed, 347 insertions(+), 43 deletions(-) diff --git a/observe-services-topia/pom.xml b/observe-services-topia/pom.xml index 67df2d2..cdab983 100644 --- a/observe-services-topia/pom.xml +++ b/observe-services-topia/pom.xml @@ -39,6 +39,13 @@ <version>${project.version}</version> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>observe-business</artifactId> + <version>${project.version}</version> + <classifier>tests</classifier> + </dependency> + <!-- commons dependencies --> diff --git a/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceFactoryTopia.java b/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceFactoryTopia.java index b3aad6f..eedb4e4 100644 --- a/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceFactoryTopia.java +++ b/observe-services-topia/src/main/java/fr/ird/observe/services/ObserveServiceFactoryTopia.java @@ -83,8 +83,7 @@ public class ObserveServiceFactoryTopia extends ObserveServiceFactorySupport { Object result; - if (serviceContext.getDataSource() == null /* FIXME Supprimer ce test dès qu'on aura une ds dans le context */ - || methodNamesToByPass.contains(method.getName()) + if (methodNamesToByPass.contains(method.getName()) || method.isAnnotationPresent(NoDataAccess.class)) { result = invokeMethod(method, args); diff --git a/observe-services-topia/src/main/java/fr/ird/observe/services/service/ReferentialServiceTopia.java b/observe-services-topia/src/main/java/fr/ird/observe/services/service/ReferentialServiceTopia.java index 4916b1c..4214bf6 100644 --- a/observe-services-topia/src/main/java/fr/ird/observe/services/service/ReferentialServiceTopia.java +++ b/observe-services-topia/src/main/java/fr/ird/observe/services/service/ReferentialServiceTopia.java @@ -2,6 +2,7 @@ package fr.ird.observe.services.service; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import fr.ird.observe.ObserveDAOHelper; import fr.ird.observe.entities.referentiel.Country; import fr.ird.observe.entities.referentiel.FpaZone; import fr.ird.observe.entities.referentiel.Gear; @@ -76,7 +77,10 @@ import fr.ird.observe.services.dto.referential.OrganismDto; import fr.ird.observe.services.dto.referential.PersonDto; import fr.ird.observe.services.dto.referential.ProgramDto; import fr.ird.observe.services.dto.referential.ReferentialDto; +import fr.ird.observe.services.dto.referential.ReferentialLabelDto; +import fr.ird.observe.services.dto.referential.ReferentialLabelDtos; import fr.ird.observe.services.dto.referential.ReferentialLabelSetDto; +import fr.ird.observe.services.dto.referential.ReferentialLabelSetDtos; import fr.ird.observe.services.dto.referential.SexDto; import fr.ird.observe.services.dto.referential.SpeciesDto; import fr.ird.observe.services.dto.referential.SpeciesGroupDto; @@ -125,9 +129,14 @@ import fr.ird.observe.services.dto.referential.seine.TransmittingBuoyTypeDto; import fr.ird.observe.services.dto.referential.seine.VesselActivitySeineDto; import fr.ird.observe.services.dto.referential.seine.WeightCategoryDto; import fr.ird.observe.services.dto.referential.seine.WindDto; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaEntity; import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -139,40 +148,41 @@ import java.util.Set; public class ReferentialServiceTopia extends ObserveServiceTopia implements ReferentialService { protected static final ImmutableMap<Class<?>, Class<?>> entityToModelTypeCache = new ImmutableMap.Builder<Class<?>, Class<?>>() - .put(VesselSizeCategory.class, VesselSizeCategoryDto.class) + .put(Country.class, CountryDto.class) - .put(Harbour.class, HarbourDto.class) - .put(VesselType.class, VesselTypeDto.class) - .put(Vessel.class, VesselDto.class) - .put(SpeciesGroup.class, SpeciesGroupDto.class) - .put(Species.class, SpeciesDto.class) - .put(Sex.class, SexDto.class) .put(FpaZone.class, FpaZoneDto.class) - .put(SpeciesList.class, SpeciesListDto.class) - .put(Person.class, PersonDto.class) + .put(GearCaracteristic.class, GearCaracteristicDto.class) + .put(GearCaracteristicType.class, GearCaracteristicTypeDto.class) + .put(Gear.class, GearDto.class) + .put(Harbour.class, HarbourDto.class) + .put(LengthWeightParameter.class, LengthWeightParameterDto.class) .put(Ocean.class, OceanDto.class) .put(Organism.class, OrganismDto.class) - .put(LengthWeightParameter.class, LengthWeightParameterDto.class) + .put(Person.class, PersonDto.class) .put(Program.class, ProgramDto.class) - .put(GearCaracteristicType.class, GearCaracteristicTypeDto.class) - .put(GearCaracteristic.class, GearCaracteristicDto.class) - .put(Gear.class, GearDto.class) + .put(Sex.class, SexDto.class) + .put(Species.class, SpeciesDto.class) + .put(SpeciesGroup.class, SpeciesGroupDto.class) + .put(SpeciesList.class, SpeciesListDto.class) + .put(Vessel.class, VesselDto.class) + .put(VesselSizeCategory.class, VesselSizeCategoryDto.class) + .put(VesselType.class, VesselTypeDto.class) - .put(VesselActivitySeine.class, VesselActivitySeineDto.class) - .put(SurroundingActivity.class, SurroundingActivityDto.class) - .put(ReasonForNullSet.class, ReasonForNullSetDto.class) - .put(ReasonForNoFishing.class, ReasonForNoFishingDto.class) - .put(SpeciesFate.class, SpeciesFateDto.class) - .put(ObjectFate.class, ObjectFateDto.class) - .put(WeightCategory.class, WeightCategoryDto.class) .put(DetectionMode.class, DetectionModeDto.class) - .put(TransmittingBuoyOperation.class, TransmittingBuoyOperationDto.class) + .put(ObjectFate.class, ObjectFateDto.class) .put(ObjectOperation.class, ObjectOperationDto.class) + .put(ObjectType.class, ObjectTypeDto.class) + .put(ObservedSystem.class, ObservedSystemDto.class) .put(ReasonForDiscard.class, ReasonForDiscardDto.class) + .put(ReasonForNoFishing.class, ReasonForNoFishingDto.class) + .put(ReasonForNullSet.class, ReasonForNullSetDto.class) + .put(SpeciesFate.class, SpeciesFateDto.class) .put(SpeciesStatus.class, SpeciesStatusDto.class) - .put(ObservedSystem.class, ObservedSystemDto.class) + .put(SurroundingActivity.class, SurroundingActivityDto.class) + .put(TransmittingBuoyOperation.class, TransmittingBuoyOperationDto.class) .put(TransmittingBuoyType.class, TransmittingBuoyTypeDto.class) - .put(ObjectType.class, ObjectTypeDto.class) + .put(VesselActivitySeine.class, VesselActivitySeineDto.class) + .put(WeightCategory.class, WeightCategoryDto.class) .put(Wind.class, WindDto.class) .put(BaitHaulingStatus.class, BaitHaulingStatusDto.class) @@ -184,8 +194,8 @@ public class ReferentialServiceTopia extends ObserveServiceTopia implements Refe .put(HookPosition.class, HookPositionDto.class) .put(HookSize.class, HookSizeDto.class) .put(HookType.class, HookTypeDto.class) - .put(ItemVerticalPosition.class, ItemVerticalPositionDto.class) .put(ItemHorizontalPosition.class, ItemHorizontalPositionDto.class) + .put(ItemVerticalPosition.class, ItemVerticalPositionDto.class) .put(LightsticksColor.class, LightsticksColorDto.class) .put(LightsticksType.class, LightsticksTypeDto.class) .put(LineType.class, LineTypeDto.class) @@ -218,14 +228,21 @@ public class ReferentialServiceTopia extends ObserveServiceTopia implements Refe @Override public <D extends ReferentialDto> ReferentialLabelSetDto getReferentialLabelSet(Class<D> dtoType) { - Class<? extends TopiaEntity> entityType = getEntityType(dtoType); - return new ReferentialLabelSetDto(); + Class<TopiaEntity> entityType = getEntityType(dtoType); + List<TopiaEntity> entities = loadEntities(entityType); + LinkedHashSet<ReferentialLabelDto<D>> labels = new LinkedHashSet<>(); + for (TopiaEntity entity : entities) { + ReferentialLabelDto<D> dto = ReferentialLabelDtos.newReferentialLabelDto(dtoType, entity); + labels.add(dto); + } + ReferentialLabelSetDto labelSetDto = ReferentialLabelSetDtos.newReferentialLabelSetDto(dtoType, labels); + return labelSetDto; } @Override public <D extends ReferentialDto> FormDto<D> loadToRead(Class<D> dtoType, String id) { Class<? extends TopiaEntity> entityType = getEntityType(dtoType); - TopiaEntity entity = null; + TopiaEntity entity = loadEntity(entityType, id); FormDto<D> form = entityToReferentialFormDto(dtoType, entity); return form; } @@ -233,7 +250,7 @@ public class ReferentialServiceTopia extends ObserveServiceTopia implements Refe @Override public <D extends ReferentialDto> FormDto<D> loadToEdit(Class<D> dtoType, String id) { Class<? extends TopiaEntity> entityType = getEntityType(dtoType); - TopiaEntity entity = null; + TopiaEntity entity = loadEntity(entityType, id); FormDto<D> form = entityToReferentialFormDto(dtoType, entity); return form; } @@ -241,16 +258,18 @@ public class ReferentialServiceTopia extends ObserveServiceTopia implements Refe @Override public <D extends ReferentialDto> FormDto<D> preCreate(Class<D> dtoType) { Class<? extends TopiaEntity> entityType = getEntityType(dtoType); - TopiaEntity entity = null; + TopiaEntity entity = newEntity(entityType); + //TODO Voir selon le type d'entité si il y a des valeurs par défaut à utiliser FormDto<D> form = entityToReferentialFormDto(dtoType, entity); return form; } public <D extends ReferentialDto> String save(FormDto<D> form) { - Class<D> dtoType= form.getType(); + Class<D> dtoType = form.getType(); TopiaEntity entity = referentialDtoToEntity(form.getForm()); - // TODO Save + Class<TopiaEntity> entityType = getEntityType(dtoType); + entity = saveEntity(entityType, entity); FormDto<D> model = entityToReferentialFormDto(dtoType, entity); return model.getForm().getId(); } @@ -258,30 +277,69 @@ public class ReferentialServiceTopia extends ObserveServiceTopia implements Refe @Override public <D extends ReferentialDto> void delete(Class<D> dtoType, String id) { Class<? extends TopiaEntity> entityType = getEntityType(dtoType); - + deleteEntity(entityType, Collections.singleton(id)); } @Override public <D extends ReferentialDto> void delete(Class<D> dtoType, Collection<String> ids) { Class<? extends TopiaEntity> entityType = getEntityType(dtoType); + deleteEntity(entityType, ids); + } + + protected <E extends TopiaEntity> List<E> loadEntities(Class<E> entityType) { + TopiaContext transaction = serviceContext.getTransaction(); + TopiaDAO<E> dao = ObserveDAOHelper.getDAO(transaction, entityType); + List<E> entities = dao.findAll(); + return entities; + } + + protected <E extends TopiaEntity> E loadEntity(Class<E> entityType, String id) { + TopiaContext transaction = serviceContext.getTransaction(); + TopiaDAO<E> dao = ObserveDAOHelper.getDAO(transaction, entityType); + E entity = dao.findByTopiaId(id); + return entity; + } + + protected <E extends TopiaEntity> E newEntity(Class<E> entityType) { + TopiaContext transaction = serviceContext.getTransaction(); + TopiaDAO<E> dao = ObserveDAOHelper.getDAO(transaction, entityType); + E entity = dao.newInstance(); + return entity; + } + protected <E extends TopiaEntity> E saveEntity(Class<E> entityType, E entity) { + TopiaContext transaction = serviceContext.getTransaction(); + TopiaDAO<E> dao = ObserveDAOHelper.getDAO(transaction, entityType); + entity = dao.update(entity); + return entity; + } + + protected <E extends TopiaEntity> void deleteEntity(Class<E> entityType, Iterable<String> ids) { + TopiaContext transaction = serviceContext.getTransaction(); + TopiaDAO<E> dao = ObserveDAOHelper.getDAO(transaction, entityType); + for (String id : ids) { + E entity = dao.findByTopiaId(id); + dao.delete(entity); + } } protected <D extends ReferentialDto> FormDto<D> entityToReferentialFormDto(Class<D> dtoType, TopiaEntity entity) { try (ReferentialDtoBuilder<D> dtoBuilder = ReferentialDtoBuilder.create(dtoType)) { + + // copy entity dtoBuilder.copyEntity(entity); D dto = dtoBuilder.build(); - Set<Class<? extends ReferentialDto>> referentialDtoTypes = dtoBuilder.getReferentialDtoTypes(); + // build label sets detected while copy + Set<Class<? extends ReferentialDto>> referentialDtoTypes = dtoBuilder.getReferentialDtoTypes(); ImmutableSet.Builder<ReferentialLabelSetDto> labels = new ImmutableSet.Builder<>(); for (Class<? extends ReferentialDto> referentialDtoType : referentialDtoTypes) { ReferentialLabelSetDto referentialLabelSet = getReferentialLabelSet(referentialDtoType); labels.add(referentialLabelSet); } FormDto<D> form = FormDtos.newFormDto(dtoType, dto, labels.build()); - form.setForm(dto); - return form; + } } @@ -293,12 +351,12 @@ public class ReferentialServiceTopia extends ObserveServiceTopia implements Refe } protected <D extends ReferentialDto, E extends TopiaEntity> Class<E> getEntityType(Class<D> dtoType) { - Class<?> entityType = entityToModelTypeCache.get(dtoType); + Class<?> entityType = modelToEntityTypeCache.get(dtoType); return (Class<E>) entityType; } protected <E extends TopiaEntity, M extends ReferentialDto> Class<M> getDtoType(Class<E> entityType) { - Class<?> modelType = modelToEntityTypeCache.get(entityType); + Class<?> modelType = entityToModelTypeCache.get(entityType); return (Class<M>) modelType; } diff --git a/observe-services-topia/src/test/java/fr/ird/observe/services/service/ApplicationContextResource.java b/observe-services-topia/src/test/java/fr/ird/observe/services/service/ApplicationContextResource.java new file mode 100644 index 0000000..8b4cbcb --- /dev/null +++ b/observe-services-topia/src/test/java/fr/ird/observe/services/service/ApplicationContextResource.java @@ -0,0 +1,75 @@ +package fr.ird.observe.services.service; + +import fr.ird.observe.IObserveConfig; +import fr.ird.observe.ObserveApplicationContext; +import fr.ird.observe.ObserveServiceHelper; +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.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.nuiton.util.version.Version; + +/** + * Created on 18/08/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ApplicationContextResource implements TestRule { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ApplicationContextResource.class); + + protected final Version databaseVersion; + + public ApplicationContextResource(Version databaseVersion) { + this.databaseVersion = databaseVersion; + } + + public String getScriptPath(String classifier) { + return Scripts.getBackupScript(databaseVersion, classifier); + } + + public ObserveApplicationContext getApplicationContext() { + return ObserveServiceHelper.get(); + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + + @Override + public void evaluate() throws Throwable { + before(description); + try { + base.evaluate(); + } finally { + after(description); + } + } + }; + } + + protected void before(Description description) { + + Class<?> testClass = description.getTestClass(); + if (log.isDebugEnabled()) { + log.debug("Starts " + testClass.getName()); + } + TestHelper.initTest(testClass); + TestHelper.createApplicationContext(); + TestHelper.setConfig(IObserveConfig.DB_VERSION, databaseVersion.toString()); + + } + + protected void after(Description description) { + + Class<?> testClass = description.getTestClass(); + if (log.isDebugEnabled()) { + log.debug("Ends " + testClass.getName()); + } + + } +} diff --git a/observe-services-topia/src/test/java/fr/ird/observe/services/service/DataSourceResource.java b/observe-services-topia/src/test/java/fr/ird/observe/services/service/DataSourceResource.java new file mode 100644 index 0000000..e699a79 --- /dev/null +++ b/observe-services-topia/src/test/java/fr/ird/observe/services/service/DataSourceResource.java @@ -0,0 +1,126 @@ +package fr.ird.observe.services.service; + +import fr.ird.observe.ObserveTechnicalException; +import fr.ird.observe.db.DBTestHelper; +import fr.ird.observe.db.DataSource; +import fr.ird.observe.db.DataSourceException; +import fr.ird.observe.test.TestHelper; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.nuiton.topia.TopiaContext; + +import java.io.File; +import java.net.URL; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Created on 18/08/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class DataSourceResource implements TestRule { + + /** Logger. */ + private static final Log log = LogFactory.getLog(DataSourceResource.class); + + protected final String dbPath; + + protected DataSource dataSource; + + protected Map<String, TopiaContext> transactions = new LinkedHashMap<>(); + + public DataSourceResource(String dbPath) { + this.dbPath = dbPath; + } + + public DataSource getDataSource() { + return dataSource; + } + + public TopiaContext newTransaction(String name) throws DataSourceException { + TopiaContext topiaContext = dataSource.beginTransaction(name); + transactions.put(name, topiaContext); + return topiaContext; + } + + public Statement apply(final Statement base, final Description description) { + return new Statement() { + + @Override + public void evaluate() throws Throwable { + before(description); + try { + base.evaluate(); + } finally { + after(description); + } + } + }; + } + + protected void before(Description description) throws Throwable { + + Class<?> testClass = description.getTestClass(); + String methodName = description.getMethodName(); + if (log.isDebugEnabled()) { + log.debug("Starts " + testClass.getName() + "::" + methodName); + } + + + URL dbUrl = getClass().getResource(dbPath); + + Assert.assertNotNull("could not find resource : " + dbPath, dbUrl); + + File localDbFile = TestHelper.newLocalDB(methodName); + + dataSource = DBTestHelper.createAndOpenFromDump( + localDbFile, + dbUrl, + false, + false, + true, + true, + true); + + + } + + protected void after(Description description) { + + Class<?> testClass = description.getTestClass(); + String methodName = description.getMethodName(); + if (log.isDebugEnabled()) { + log.debug("Ends " + testClass.getName() + "::" + methodName); + } + + if (dataSource != null && dataSource.isOpen()) { + + for (Map.Entry<String, TopiaContext> entry : transactions.entrySet()) { + TopiaContext topiaContext = entry.getValue(); + if (!topiaContext.isClosed()) { + String transactionKey = entry.getKey(); + try { + dataSource.closeTransaction(topiaContext, transactionKey); + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("Could not close transaction: " + transactionKey, e); + } + } + } + } + + try { + dataSource.doClose(false); + } catch (DataSourceException e) { + throw new ObserveTechnicalException("Could not close datasource: " + dataSource, e); + } + } + + } + +} diff --git a/observe-services-topia/src/test/java/fr/ird/observe/services/service/ReferentialServiceTopiaTest.java b/observe-services-topia/src/test/java/fr/ird/observe/services/service/ReferentialServiceTopiaTest.java index b702093..4353ea7 100644 --- a/observe-services-topia/src/test/java/fr/ird/observe/services/service/ReferentialServiceTopiaTest.java +++ b/observe-services-topia/src/test/java/fr/ird/observe/services/service/ReferentialServiceTopiaTest.java @@ -1,12 +1,21 @@ package fr.ird.observe.services.service; +import fr.ird.observe.db.DataSource; +import fr.ird.observe.entities.migration.H2DataSourceMigration; import fr.ird.observe.services.ObserveServiceContextTopia; import fr.ird.observe.services.ObserveServiceMainFactory; import fr.ird.observe.services.dto.referential.ReferentialLabelSetDto; -import fr.ird.observe.services.dto.referential.SexDto; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.persistence.TopiaDAO; + +import java.util.Map; /** * Created on 16/08/15. @@ -15,21 +24,51 @@ import org.junit.Test; */ public class ReferentialServiceTopiaTest { + /** Logger. */ + private static final Log log = LogFactory.getLog(ReferentialServiceTopiaTest.class); + + @ClassRule + public static final ApplicationContextResource applicationContextResource = new ApplicationContextResource(H2DataSourceMigration.V_4_0); + + @Rule + public final DataSourceResource dataSourceResource = new DataSourceResource(applicationContextResource.getScriptPath("referentiel")); + protected ReferentialService service; @Before public void setUp() throws Exception { + DataSource localDataSource = dataSourceResource.getDataSource(); + ObserveServiceContextTopia context = new ObserveServiceContextTopia(); - service = ObserveServiceMainFactory.get().newService(context, ReferentialService.class); + context.setDataSource(localDataSource); + + ObserveServiceMainFactory serviceFactory = ObserveServiceMainFactory.get(); + service = serviceFactory.newService(context, ReferentialService.class); } @Test public void testGetReferentialLabelSet() throws Exception { - ReferentialLabelSetDto referentialLabelSet = service.getReferentialLabelSet(SexDto.class); - Assert.assertNotNull(referentialLabelSet); + TopiaContext tx = dataSourceResource.newTransaction("testGetReferentialLabelSet"); + + for (Map.Entry<Class<?>, Class<?>> entry : ReferentialServiceTopia.modelToEntityTypeCache.entrySet()) { + Class dtoType = entry.getKey(); + Class entityType = entry.getValue(); + + TopiaDAO dao = dataSourceResource.getDataSource().getDAO(tx, entityType); + long expectedCount = dao.count(); + + if (log.isInfoEnabled()) { + log.info("Test referential of type: " + dtoType.getSimpleName() + " expected count: " + expectedCount); + } + + ReferentialLabelSetDto referentialLabelSet = service.getReferentialLabelSet(dtoType); + Assert.assertNotNull(referentialLabelSet); + Assert.assertEquals(expectedCount, referentialLabelSet.sizeReferentialLabel()); + + } } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.
participants (1)
-
codelutin.com scm