Skip to content

Commit

Permalink
Merge pull request #173 from InseeFr/hotFixMissing
Browse files Browse the repository at this point in the history
Hot fix missing and filter_result
  • Loading branch information
loichenninger authored Feb 24, 2025
2 parents e6e34fb + b233ef2 commit 37c268c
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import fr.insee.genesis.controller.sources.xml.LunaticXmlSurveyUnit;
import fr.insee.genesis.controller.sources.xml.ValueType;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.utils.LoopIdentifier;
import fr.insee.genesis.domain.utils.GroupUtils;
import fr.insee.genesis.domain.model.surveyunit.DataState;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.VariableModel;
Expand Down Expand Up @@ -123,8 +123,8 @@ private static SurveyUnitModel getCollectedDataFromSurveyUnit(LunaticXmlSurveyUn
variableModels.add(VariableModel.builder()
.varId(lunaticXmlCollectedData.getVariableName())
.value(valueTypeList.get(i-1).getValue())
.scope(LoopIdentifier.getLoopIdentifier(lunaticXmlCollectedData.getVariableName(), variablesMap))
.parentId(LoopIdentifier.getRelatedVariableName(lunaticXmlCollectedData.getVariableName(), variablesMap))
.scope(GroupUtils.getGroupName(lunaticXmlCollectedData.getVariableName(), variablesMap))
.parentId(GroupUtils.getParentGroupName(lunaticXmlCollectedData.getVariableName(), variablesMap))
.iteration(i)
.build());
dataCount++;
Expand Down Expand Up @@ -160,9 +160,9 @@ private static void getExternalDataFromSurveyUnit(LunaticXmlSurveyUnit su, Surve
variableModels.add(VariableModel.builder()
.varId(lunaticXmlExternalData.getVariableName())
.value(valueTypeList.get(i-1).getValue())
.scope(LoopIdentifier.getLoopIdentifier(lunaticXmlExternalData.getVariableName(), variablesMap))
.scope(GroupUtils.getGroupName(lunaticXmlExternalData.getVariableName(), variablesMap))
.iteration(i)
.parentId(LoopIdentifier.getRelatedVariableName(lunaticXmlExternalData.getVariableName(), variablesMap))
.parentId(GroupUtils.getParentGroupName(lunaticXmlExternalData.getVariableName(), variablesMap))
.build());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import fr.insee.genesis.domain.model.surveyunit.VariableModel;
import fr.insee.genesis.domain.ports.api.SurveyUnitApiPort;
import fr.insee.genesis.domain.ports.spi.SurveyUnitPersistencePort;
import fr.insee.genesis.domain.utils.LoopIdentifier;
import fr.insee.genesis.domain.utils.GroupUtils;
import fr.insee.genesis.exceptions.GenesisException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand Down Expand Up @@ -280,7 +280,7 @@ public List<SurveyUnitModel> parseEditedVariables(
VariableModel collectedVariable = VariableModel.builder()
.varId(editedVariableDto.getVariableName())
.value(editedVariableDto.getVariableStateInputDto().getValue())
.parentId(LoopIdentifier.getRelatedVariableName(editedVariableDto.getVariableName(), variablesMap))
.parentId(GroupUtils.getParentGroupName(editedVariableDto.getVariableName(), variablesMap))
.scope(variablesMap.getVariable(editedVariableDto.getVariableName()).getGroupName())
.iteration(editedVariableDto.getIteration())
.build();
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/fr/insee/genesis/domain/utils/GroupUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package fr.insee.genesis.domain.utils;

import fr.insee.genesis.Constants;
import fr.insee.bpm.metadata.model.Variable;
import fr.insee.bpm.metadata.model.VariablesMap;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

@Slf4j
public class GroupUtils {

private GroupUtils() {
throw new IllegalStateException("Utility class");
}

public static String getGroupName(String variableName, VariablesMap variablesMap){
List<String> varsEno = Arrays.asList(Constants.getEnoVariables());
Variable variable = variablesMap.getVariable(variableName);
// If we don't find the variable, but it's A FILTER_RESULT or _MISSING variable
// Then we look for the variable from which it derives
if (variable == null) {
// Variables added by Eno and identified in the constants list have ROOT_GROUP_NAME scope
if(varsEno.contains(variableName))
{
return Constants.ROOT_GROUP_NAME;
}
String relatedVariable = getRelatedVariableName(variableName,variablesMap);
if (relatedVariable==null){
// If we don't find a related variable, we assign variable to ROOT_GROUP_NAME
log.debug("Variable {} not found in variablesMap and assigned in root group", variableName);
return Constants.ROOT_GROUP_NAME;
}
return variablesMap.getVariable(relatedVariable).getGroupName();
}
// If the variable was in we return directly the group name
return variable.getGroup().getName();
}

public static String getParentGroupName(String variableName, VariablesMap variablesMap){
List<String> varsEno = Arrays.asList(Constants.getEnoVariables());
Variable variable = variablesMap.getVariable(variableName);
// If we don't find the variable, but it's A FILTER_RESULT or _MISSING variable
// Then we look for the variable from which it derives
if (variable == null) {
// Variables added by Eno and identified in the constants list have ROOT_GROUP_NAME scope
if(varsEno.contains(variableName))
{
return null;
}
String relatedVariableName = getRelatedVariableName(variableName,variablesMap);
if (relatedVariableName==null){
// If we don't find a related variable, we assign variable to ROOT_GROUP_NAME
// so parent group is empty
log.debug("Variable {} not found in variablesMap and assigned in root group, parent group name is empty", variableName);
return null;
}
Variable relatedVariable = variablesMap.getVariable(relatedVariableName);
return relatedVariable.getGroup().isRoot() ? null : relatedVariable.getGroup().getParentName();
}
// If the variable was in metadata, we return directly the parent group name
return variable.getGroup().isRoot() ? null : variable.getGroup().getParentName();
}

private static String getRelatedVariableName(String variableName, VariablesMap variablesMap) {
Variable variable = variablesMap.getVariable(variableName);
if ( variable == null ) {
if(variablesMap.hasVariable(removePrefixOrSuffix(variableName, Constants.FILTER_RESULT_PREFIX)))
{
return removePrefixOrSuffix(variableName, Constants.FILTER_RESULT_PREFIX);
}
if(variablesMap.hasVariable(removePrefixOrSuffix(variableName, Constants.MISSING_SUFFIX))
){
return removePrefixOrSuffix(variableName, Constants.MISSING_SUFFIX);
}
return null;
}
if (variable.getGroup().isRoot()) {
return null;
}
return variable.getGroup().getParentName();
}

private static String removePrefixOrSuffix(String variableName, String pattern) {
if (variableName.startsWith(pattern)){
return variableName.replace(pattern, "");
}
if (variableName.endsWith(pattern)){
return variableName.replace(pattern, "");
}
return variableName;
}

}
67 changes: 0 additions & 67 deletions src/main/java/fr/insee/genesis/domain/utils/LoopIdentifier.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void setUp() {
lunaticXmlSurveyUnit6.setQuestionnaireModelId("questionnaireId1");
lunaticXmlSurveyUnit6.setData(lunaticXmlData);

//SurveyUnit 7 : COLLECTED only, has one unknown variable with known variable prefix
//SurveyUnit 7 : COLLECTED only, has two unknown variables with known variable prefix or suffix
lunaticXmlData = new LunaticXmlData();

lunaticXmlCollectedData = new LunaticXmlCollectedData();
Expand All @@ -168,7 +168,11 @@ void setUp() {
lunaticXmlCollectedData2.setVariableName("var1_MISSING");
lunaticXmlCollectedData2.setCollected(List.of(new ValueType("1", "string"), new ValueType("2", "string")));

collected = List.of(lunaticXmlCollectedData, lunaticXmlCollectedData2);
LunaticXmlCollectedData lunaticXmlCollectedData3 = new LunaticXmlCollectedData();
lunaticXmlCollectedData3.setVariableName("FILTER_RESULT_var1");
lunaticXmlCollectedData3.setCollected(List.of(new ValueType("1", "string"), new ValueType("1", "string")));

collected = List.of(lunaticXmlCollectedData, lunaticXmlCollectedData2, lunaticXmlCollectedData3);
lunaticXmlData.setCollected(collected);

lunaticXmlData.setExternal(external);
Expand Down Expand Up @@ -340,20 +344,23 @@ void test09() {
}

@Test
@DisplayName("If a variable A not present in DDI and is the extension of a known variable B, then the variable A has B as related and is in the same group")
@DisplayName("If a variable A not present in DDI and is the extension of a known variable B, then the variable A is in the same group")
void test10() {
// When
List<SurveyUnitModel> surveyUnitModels = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit7, metadataModel.getVariables(), CAMPAIGN_ID, Mode.WEB);

// Then
Assertions.assertThat(surveyUnitModels).hasSize(1);

Assertions.assertThat(surveyUnitModels.getFirst().getCollectedVariables()).filteredOn(collectedVariableModel ->
collectedVariableModel.varId().equals("var1_MISSING")).isNotEmpty();
collectedVariableModel.varId().equals("var1_MISSING")).hasSize(2);
Assertions.assertThat(surveyUnitModels.getFirst().getCollectedVariables().stream().filter(collectedVariableModel ->
collectedVariableModel.varId().equals("var1_MISSING")).toList().getFirst().parentId()).isNotNull().isEqualTo("var1");
collectedVariableModel.varId().equals("var1_MISSING")).toList().getFirst().parentId()).isNotNull().isEqualTo(Constants.ROOT_GROUP_NAME);
Assertions.assertThat(surveyUnitModels.getFirst().getCollectedVariables().stream().filter(collectedVariableModel ->
collectedVariableModel.varId().equals("var1_MISSING")).toList().getFirst().scope()).isNotEqualTo(Constants.ROOT_GROUP_NAME).isEqualTo(LOOP_NAME);
Assertions.assertThat(surveyUnitModels.getFirst().getCollectedVariables().stream().filter(collectedVariableModel ->
collectedVariableModel.varId().equals("FILTER_RESULT_var1")).toList().getFirst().parentId()).isNotNull().isEqualTo(Constants.ROOT_GROUP_NAME);
Assertions.assertThat(surveyUnitModels.getFirst().getCollectedVariables().stream().filter(collectedVariableModel ->
collectedVariableModel.varId().equals("FILTER_RESULT_var1")).toList().getFirst().scope()).isNotEqualTo(Constants.ROOT_GROUP_NAME).isEqualTo(LOOP_NAME);
}

@Test
Expand Down Expand Up @@ -395,4 +402,5 @@ void test12(){
Assertions.assertThat(surveyUnitModels.getFirst().getExternalVariables().getFirst().iteration()).isEqualTo(1);
Assertions.assertThat(surveyUnitModels.getFirst().getExternalVariables().getFirst().parentId()).isEqualTo(Constants.ROOT_GROUP_NAME);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package fr.insee.genesis.controller.utils;

import fr.insee.genesis.Constants;
import fr.insee.bpm.metadata.model.Group;
import fr.insee.bpm.metadata.model.MetadataModel;
import fr.insee.bpm.metadata.model.Variable;
import fr.insee.bpm.metadata.model.VariableType;
import fr.insee.genesis.domain.utils.GroupUtils;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class GroupUtilsTest {

MetadataModel metadataModel;

private static final String LOOP_NAME = "BOUCLE_TEST";

@BeforeEach
void setUp() {
// Given
this.metadataModel = new MetadataModel();
Group group = new Group(LOOP_NAME, Constants.ROOT_GROUP_NAME);
Variable var1 = new Variable("var1", group, VariableType.STRING, "1");
Variable var2 = new Variable("var2", metadataModel.getRootGroup(), VariableType.STRING, "1");
metadataModel.getVariables().putVariable(var1);
metadataModel.getVariables().putVariable(var2);
}

@Test
@DisplayName("Should return <LOOP_NAME>")
void test01() {
//When + Then
Assertions.assertThat(GroupUtils.getGroupName("var1", metadataModel.getVariables())).isEqualTo(LOOP_NAME);
}

@Test
@DisplayName("Should return the root group name")
void test02() {
//When + Then
Assertions.assertThat(GroupUtils.getGroupName("var2", metadataModel.getVariables())).isEqualTo(Constants.ROOT_GROUP_NAME);
}

@Test
@DisplayName("Should return the root group name if the variable is not present in the variables map")
void test03(){
//When + Then
Assertions.assertThat(GroupUtils.getGroupName("var3", metadataModel.getVariables())).isEqualTo(Constants.ROOT_GROUP_NAME);
}

@Test
@DisplayName("Should return var1 group if missing suffix")
void test04(){
//When + Then
Assertions.assertThat(GroupUtils.getGroupName("var1_MISSING", metadataModel.getVariables())).isEqualTo(LOOP_NAME);
}

@Test
@DisplayName("Should return var1 group if filter result prefix")
void test05(){
//When + Then
Assertions.assertThat(GroupUtils.getGroupName("FILTER_RESULT_var1", metadataModel.getVariables())).isEqualTo(LOOP_NAME);
}

@Test
@DisplayName("Parent group name of var1 should be root group name")
void test06(){
Assertions.assertThat(GroupUtils.getParentGroupName("var1",metadataModel.getVariables())).isEqualTo(Constants.ROOT_GROUP_NAME);
}

}
Loading

0 comments on commit 37c268c

Please sign in to comment.