Skip to content
Closed
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
32 changes: 25 additions & 7 deletions src/main/java/fr/insee/genesis/domain/utils/DataVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -78,7 +80,7 @@ private static SurveyUnitModel createForcedSurveyUnitModel(
.interrogationId(interrogationId)
.state(DataState.FORCED)
.mode(sampleSurveyUnitModel.getMode())
.recordDate(LocalDateTime.now())
.recordDate(LocalDateTime.now().plusSeconds(1)) // Add 1 second to avoid same recordDate as COLLECTED
.fileDate(sampleSurveyUnitModel.getFileDate())
.collectedVariables(new ArrayList<>())
.externalVariables(new ArrayList<>())
Expand Down Expand Up @@ -132,7 +134,8 @@ private static Set<String> getInterrogationIds(List<SurveyUnitModel> surveyUnitM
* @param correctedCollectedVariables FORCED document variables
*/
private static void collectedVariablesManagement(List<SurveyUnitModel> srcSurveyUnitModelsOfInterrogationId, VariablesMap variablesMap, List<VariableModel> correctedCollectedVariables){
Set<String> variableNames = new HashSet<>();
Map<String,List<Integer>> variableIterations = new LinkedHashMap<>();

List<VariableModel> variablesToVerify = new ArrayList<>();

//Sort from more priority to less
Expand All @@ -141,10 +144,7 @@ private static void collectedVariablesManagement(List<SurveyUnitModel> srcSurvey
//Get more priority variables to verify
for(SurveyUnitModel srcSurveyUnitModel : sortedSurveyUnitModels){
for(VariableModel collectedVariable : srcSurveyUnitModel.getCollectedVariables()){
if(!variableNames.contains(collectedVariable.varId())){
variableNames.add(collectedVariable.varId());
variablesToVerify.add(collectedVariable);
}
addIteration(collectedVariable, variableIterations, variablesToVerify);
}
}

Expand All @@ -164,6 +164,24 @@ private static void collectedVariablesManagement(List<SurveyUnitModel> srcSurvey
}
}

private static void addIteration(VariableModel variableToCheck, Map<String, List<Integer>> variableIterations, List<VariableModel> variablesToVerify) {
if(!variableIterations.containsKey(variableToCheck.varId())
|| !variableIterations.get(variableToCheck.varId()).contains(variableToCheck.iteration())){
List<Integer> iterations = variableIterations.containsKey(variableToCheck.varId()) ?
variableIterations.get(variableToCheck.varId())
: new ArrayList<>();
if(!iterations.contains(variableToCheck.iteration())){
iterations.add(variableToCheck.iteration());
}
variableIterations.put(
variableToCheck.varId(),
iterations
);

variablesToVerify.add(variableToCheck);
}
}

private static VariableModel verifyVariable(VariableModel variableModel, fr.insee.bpm.metadata.model.Variable variableDefinition) {
if(isParseError(variableModel.value(), variableDefinition.getType())){
return VariableModel.builder()
Expand All @@ -178,7 +196,7 @@ private static VariableModel verifyVariable(VariableModel variableModel, fr.inse
}

private static void externalVariablesManagement(List<SurveyUnitModel> srcSuModels, VariablesMap variablesMap, List<VariableModel> correctedExternalVariables) {
//COLLECTED only
//External variables are in COLLECTED documents only
Optional<SurveyUnitModel> surveyUnitModelOptional = srcSuModels.stream().filter(
surveyUnitModel -> surveyUnitModel.getState().equals(DataState.COLLECTED)
).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,65 @@ void shouldCorrectInvalidValuesInForcedSurveyUnit() {
// WHEN
DataVerifier.verifySurveyUnits(surveyUnits, variablesMap);

// FORCED values added
// THEN FORCED values added
SurveyUnitModel forcedUnit = surveyUnits.get(1);
Assertions.assertEquals(DataState.FORCED, forcedUnit.getState());
Assertions.assertEquals(1, forcedUnit.getCollectedVariables().size());
Assertions.assertEquals("", forcedUnit.getCollectedVariables().getFirst().value()); // Corrected values
}

@Test
void shouldCorrectInvalidIterationOnForcedSurveyUnit() {
// GIVEN
// ADD invalid values
surveyUnits.clear();
VariableModel collectedVariable1 = VariableModel.builder()
.varId("var1")
.value("123")
.scope("loop1")
.iteration(1)
.parentId("parent1")
.build();
VariableModel collectedVariable2 = VariableModel.builder()
.varId("var1")
.value("invalid")
.scope("loop1")
.iteration(2)
.parentId("parent1")
.build();
VariableModel collectedVariable3 = VariableModel.builder()
.varId("var2")
.value("false")
.scope("loop2")
.iteration(1)
.parentId("parent2")
.build();
VariableModel collectedVariable4 = VariableModel.builder()
.varId("var2")
.value("Not a boolean")
.scope("loop2")
.iteration(2)
.parentId("parent2")
.build();
SurveyUnitModel surveyUnit = SurveyUnitModel.builder()
.interrogationId("UE1100000001")
.questionnaireId("Quest1")
.campaignId("Camp1")
.state(DataState.COLLECTED)
.collectedVariables(List.of(collectedVariable1, collectedVariable2, collectedVariable3, collectedVariable4))
.externalVariables(List.of())
.build();
surveyUnits.add(surveyUnit);

// WHEN
DataVerifier.verifySurveyUnits(surveyUnits, variablesMap);

// THEN FORCED values added
Assertions.assertTrue(surveyUnits.size() > 1);
SurveyUnitModel forcedUnit = surveyUnits.get(1);
Assertions.assertEquals(DataState.FORCED, forcedUnit.getState());
Assertions.assertEquals(2, forcedUnit.getCollectedVariables().size());
Assertions.assertEquals("", forcedUnit.getCollectedVariables().getFirst().value()); // Corrected values
Assertions.assertEquals("", forcedUnit.getCollectedVariables().get(1).value());
}
}
Loading