Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,42 +233,25 @@ public long countSchedules() {
public DataProcessingContextModel getContext(String interrogationId) throws GenesisException {
List<SurveyUnitModel> 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<String> partitionIds = new HashSet<>();
Set<String> campaignIds = new HashSet<>();
Set<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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<SurveyUnitModel> sus = new ArrayList<>();
Expand All @@ -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
Expand All @@ -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<SurveyUnitModel> sus = new ArrayList<>();
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<SurveyUnitModel> 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);
}
}
Loading