-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from InseeFr/hotFixMissing
Hot fix missing and filter_result
- Loading branch information
Showing
7 changed files
with
188 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/fr/insee/genesis/domain/utils/GroupUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
src/main/java/fr/insee/genesis/domain/utils/LoopIdentifier.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/test/java/fr/insee/genesis/controller/utils/GroupUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.