diff --git a/CHANGELOG.md b/CHANGELOG.md index a00e5d50..72655aa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +## 2.1.0 [TODO] +### Fixed +- Fixed withReview still based on campaignId + ## 2.0.4 [2026-01-13] ### Added - Debug endpoints (Get raw data by collectionInstrumentId, delete last execution) diff --git a/src/main/java/fr/insee/genesis/domain/service/context/DataProcessingContextService.java b/src/main/java/fr/insee/genesis/domain/service/context/DataProcessingContextService.java index 4f19be63..71a5aa06 100644 --- a/src/main/java/fr/insee/genesis/domain/service/context/DataProcessingContextService.java +++ b/src/main/java/fr/insee/genesis/domain/service/context/DataProcessingContextService.java @@ -233,42 +233,25 @@ public long countSchedules() { public DataProcessingContextModel getContext(String interrogationId) throws GenesisException { List surveyUnitModels = surveyUnitPersistencePort.findByInterrogationId(interrogationId); if(surveyUnitModels.isEmpty()){ - throw new GenesisException(404,"No interrogation in database with id %s".formatted(interrogationId)); + throw new GenesisException(404, "No interrogation in database with id %s".formatted(interrogationId)); } - Set partitionIds = new HashSet<>(); - Set campaignIds = new HashSet<>(); Set collectionInstrumentIds = new HashSet<>(); for (SurveyUnitModel su : surveyUnitModels){ - if (su.getCampaignId()!=null){ - campaignIds.add(su.getCampaignId()); - } - if (su.getCollectionInstrumentId()!=null){ + if (su.getCollectionInstrumentId() != null){ collectionInstrumentIds.add(su.getCollectionInstrumentId()); } } - if(campaignIds.size() > 1 || collectionInstrumentIds.size()>1){ - throw new GenesisException(500,"Multiple partitions for interrogation %s".formatted(interrogationId)); - } - - if(campaignIds.isEmpty() && collectionInstrumentIds.isEmpty()){ - return null; - } - - DataProcessingContextModel contextModel = new DataProcessingContextModel(); - if (!campaignIds.isEmpty()){ - contextModel = DataProcessingContextMapper.INSTANCE.documentToModel( - dataProcessingContextPersistancePort.findByPartitionId(campaignIds.stream().toList().getFirst()) - ); + if(collectionInstrumentIds.size() > 1){ + throw new GenesisException(500,"Multiple collection instruments for interrogation %s".formatted(interrogationId)); } - if (contextModel.getPartitionId()==null && !collectionInstrumentIds.isEmpty()) { - contextModel = DataProcessingContextMapper.INSTANCE.documentToModel( - dataProcessingContextPersistancePort.findByPartitionId(collectionInstrumentIds.stream().toList().getFirst())); + if(collectionInstrumentIds.isEmpty()){ + throw new GenesisException(404,"No collection instrument found for interrogation %s".formatted(interrogationId)); } - return contextModel; + return dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentIds.stream().toList().getFirst()); } @Override diff --git a/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java b/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java index d8f3ac38..30bfafe8 100644 --- a/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java +++ b/src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java @@ -229,6 +229,7 @@ void getLatestByStatesSurveyDataTest() throws GenesisException { DataProcessingContextDocument doc = new DataProcessingContextDocument(); doc.setPartitionId("TEST-TABLEAUX"); + doc.setCollectionInstrumentId(DEFAULT_COLLECTION_INSTRUMENT_ID); doc.setKraftwerkExecutionScheduleList(new ArrayList<>()); doc.setWithReview(true); dataProcessingContextPersistancePortStub.getMongoStub().add(doc); @@ -324,6 +325,7 @@ void getLatestByStatesSurveyDataTest_invalid_collected() throws GenesisException DataProcessingContextDocument doc = new DataProcessingContextDocument(); doc.setPartitionId("TEST-TABLEAUX"); + doc.setCollectionInstrumentId(DEFAULT_COLLECTION_INSTRUMENT_ID); doc.setKraftwerkExecutionScheduleList(new ArrayList<>()); doc.setWithReview(true); dataProcessingContextPersistancePortStub.getMongoStub().add(doc); diff --git a/src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceTest.java b/src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceTest.java index c7e93a97..1e911815 100644 --- a/src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceTest.java +++ b/src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceTest.java @@ -51,6 +51,7 @@ void reset(){ )); DataProcessingContextDocument doc = new DataProcessingContextDocument(); doc.setPartitionId("TEST"); + doc.setCollectionInstrumentId("TEST"); doc.setKraftwerkExecutionScheduleList(kraftwerkExecutionScheduleList); doc.setWithReview(false); dataProcessingContextPersistencePortStub.getMongoStub().add(doc); @@ -129,7 +130,7 @@ void updateLastExecutionName_test() throws GenesisException { LocalDateTime localDateTime = LocalDateTime.now(); //When - dataProcessingContextService.updateLastExecutionDate("TEST", localDateTime); + dataProcessingContextService.updateLastExecutionDateByCollectionInstrumentId("TEST", localDateTime); //Then Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub()).hasSize(1); @@ -171,7 +172,7 @@ void removeExpiredSchedules_test_delete_document() throws GenesisException { null )); DataProcessingContextDocument doc = new DataProcessingContextDocument(); - doc.setPartitionId("TEST2"); + doc.setCollectionInstrumentId("TEST2"); doc.setKraftwerkExecutionScheduleList(kraftwerkExecutionScheduleList); doc.setWithReview(false); dataProcessingContextPersistencePortStub.getMongoStub().add(doc); @@ -183,23 +184,23 @@ void removeExpiredSchedules_test_delete_document() throws GenesisException { //Survey schedule document deleted Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub()).hasSize(2); Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub().stream().filter( - scheduleDocument -> scheduleDocument.getPartitionId().equals("TEST2") + scheduleDocument -> scheduleDocument.getCollectionInstrumentId().equals("TEST2") ).toList()).hasSize(1); Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub().stream().filter( - scheduleDocument -> scheduleDocument.getPartitionId().equals("TEST2") + scheduleDocument -> scheduleDocument.getCollectionInstrumentId().equals("TEST2") ).toList().getFirst().getKraftwerkExecutionScheduleList()).isEmpty(); } @Test - void getContext_shouldThrow500IfMultiplePartitions() { + void getContext_shouldThrow500IfMultipleCollectionInstruments() { // Given SurveyUnitModel su1 = SurveyUnitModel.builder() - .campaignId("CAMPAIGN1") + .collectionInstrumentId("CAMPAIGN1") .interrogationId("00001") .build(); SurveyUnitModel su2 = SurveyUnitModel.builder() - .campaignId("CAMPAIGN2") + .collectionInstrumentId("CAMPAIGN2") .interrogationId("00001") .build(); List sus = new ArrayList<>(); @@ -212,7 +213,7 @@ void getContext_shouldThrow500IfMultiplePartitions() { //To ensure test is portable on Unix/Linux/macOS and windows systems String normalizedMessage = ex.getMessage().replaceAll("\\r?\\n", ""); Assertions.assertThat(ex.getStatus()).isEqualTo(500); - Assertions.assertThat(normalizedMessage).isEqualTo("Multiple partitions for interrogation 00001"); + Assertions.assertThat(normalizedMessage).isEqualTo("Multiple collection instruments for interrogation 00001"); } @Test @@ -224,10 +225,10 @@ void getContext_shouldThrow404IfNoInterrogations() { } @Test - void getContext_shouldReturnContextIfOnePartition() throws GenesisException { + void getContext_shouldReturnContextIfOneCollectionInstrument() throws GenesisException { // Given SurveyUnitModel su1 = SurveyUnitModel.builder() - .campaignId("TEST") + .collectionInstrumentId("TEST") .interrogationId("00001") .build(); List sus = new ArrayList<>(); @@ -237,7 +238,7 @@ void getContext_shouldReturnContextIfOnePartition() throws GenesisException { // When & Then Assertions.assertThat(result).isNotNull(); - Assertions.assertThat(result.getPartitionId()).isEqualTo("TEST"); + Assertions.assertThat(result.getCollectionInstrumentId()).isEqualTo("TEST"); Assertions.assertThat(result.isWithReview()).isFalse(); } diff --git a/src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceUnitTest.java b/src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceUnitTest.java new file mode 100644 index 00000000..2be7cc2d --- /dev/null +++ b/src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceUnitTest.java @@ -0,0 +1,136 @@ +package fr.insee.genesis.domain.service.context; + +import fr.insee.genesis.TestConstants; +import fr.insee.genesis.domain.model.context.DataProcessingContextModel; +import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel; +import fr.insee.genesis.domain.ports.spi.DataProcessingContextPersistancePort; +import fr.insee.genesis.domain.ports.spi.SurveyUnitPersistencePort; +import fr.insee.genesis.exceptions.GenesisException; +import lombok.SneakyThrows; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +class DataProcessingContextServiceUnitTest { + + static DataProcessingContextService dataProcessingContextService; + static DataProcessingContextPersistancePort dataProcessingContextPersistancePort; + static SurveyUnitPersistencePort surveyUnitPersistencePort; + + @BeforeEach + void setUp() { + dataProcessingContextPersistancePort = mock(DataProcessingContextPersistancePort.class); + surveyUnitPersistencePort = mock(SurveyUnitPersistencePort.class); + dataProcessingContextService = new DataProcessingContextService( + dataProcessingContextPersistancePort, + surveyUnitPersistencePort + ); + } + + @Test + @SneakyThrows + void getContext_test() { + //GIVEN + String collectionInstrumentId = TestConstants.DEFAULT_COLLECTION_INSTRUMENT_ID; + String interrogationId = TestConstants.DEFAULT_INTERROGATION_ID; + SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder() + .interrogationId(interrogationId) + .collectionInstrumentId(collectionInstrumentId) + .build(); + doReturn(Collections.singletonList(surveyUnitModel)) + .when(surveyUnitPersistencePort) + .findByInterrogationId(any()); + DataProcessingContextModel dataProcessingContextModel = DataProcessingContextModel.builder() + .collectionInstrumentId(collectionInstrumentId) + .withReview(true) + .build(); + doReturn(dataProcessingContextModel).when(dataProcessingContextPersistancePort).findByCollectionInstrumentId(any()); + + //WHEN + DataProcessingContextModel returnedContext = + dataProcessingContextService.getContext(interrogationId); + + //THEN + Assertions.assertThat(returnedContext.getCollectionInstrumentId()).isEqualTo(collectionInstrumentId); + Assertions.assertThat(returnedContext.isWithReview()).isTrue(); + } + + @Test + @SneakyThrows + void getContext_no_surveyUnit_exception_test() { + //GIVEN + doReturn(new ArrayList<>()) + .when(surveyUnitPersistencePort) + .findByInterrogationId(any()); + //WHEN + THEN + Assertions.assertThatThrownBy(() -> + dataProcessingContextService.getContext(TestConstants.DEFAULT_INTERROGATION_ID)) + .isInstanceOf(GenesisException.class); + } + + @Test + @SneakyThrows + void getContext_multiple_CollectionInstruementIds_test() { + //GIVEN + String collectionInstrumentId = TestConstants.DEFAULT_COLLECTION_INSTRUMENT_ID; + String interrogationId = TestConstants.DEFAULT_INTERROGATION_ID; + List surveyUnitModelList = new ArrayList<>(); + SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder() + .interrogationId(interrogationId) + .collectionInstrumentId(collectionInstrumentId) + .build(); + surveyUnitModelList.add(surveyUnitModel); + SurveyUnitModel surveyUnitModel2 = SurveyUnitModel.builder() + .interrogationId(interrogationId) + .collectionInstrumentId(collectionInstrumentId + "2") + .build(); + surveyUnitModelList.add(surveyUnitModel2); + doReturn(surveyUnitModelList) + .when(surveyUnitPersistencePort) + .findByInterrogationId(any()); + DataProcessingContextModel dataProcessingContextModel = DataProcessingContextModel.builder() + .collectionInstrumentId(collectionInstrumentId) + .withReview(true) + .build(); + doReturn(dataProcessingContextModel).when(dataProcessingContextPersistancePort).findByCollectionInstrumentId(any()); + + //WHEN + THEN + Assertions.assertThatThrownBy(() -> + dataProcessingContextService.getContext(TestConstants.DEFAULT_INTERROGATION_ID)) + .isInstanceOf(GenesisException.class); + } + + @Test + @SneakyThrows + void getContext_no_CollectionInstruementIds_test() { + //GIVEN + //GIVEN + String collectionInstrumentId = TestConstants.DEFAULT_COLLECTION_INSTRUMENT_ID; + String interrogationId = TestConstants.DEFAULT_INTERROGATION_ID; + SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder() + .interrogationId(interrogationId) + .collectionInstrumentId(null) + .build(); + doReturn(Collections.singletonList(surveyUnitModel)) + .when(surveyUnitPersistencePort) + .findByInterrogationId(any()); + DataProcessingContextModel dataProcessingContextModel = DataProcessingContextModel.builder() + .collectionInstrumentId(collectionInstrumentId) + .withReview(true) + .build(); + doReturn(dataProcessingContextModel).when(dataProcessingContextPersistancePort).findByCollectionInstrumentId(any()); + + //WHEN + THEN + Assertions.assertThatThrownBy(() -> + dataProcessingContextService.getContext(TestConstants.DEFAULT_INTERROGATION_ID)) + .isInstanceOf(GenesisException.class); + } +} \ No newline at end of file