From e7c3ea72ede001d0ceed467834425232def32da0 Mon Sep 17 00:00:00 2001 From: QDIBYS Date: Fri, 23 Feb 2024 10:02:15 +0100 Subject: [PATCH] Renamed getParentGroupName to getParentName + extract method --- .../controller/adapter/LunaticXmlAdapter.java | 2 +- .../genesis/controller/utils/LoopIdentifier.java | 13 ++++++++----- .../controller/utils/LoopIdentifierTest.java | 10 +++++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java b/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java index 3fefda7d..ad297702 100644 --- a/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java +++ b/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java @@ -106,7 +106,7 @@ private static SurveyUnitUpdateDto getCollectedDataFromSurveyUnit(LunaticXmlSurv .idVar(lunaticXmlCollectedData.getVariableName()) .values(variableValues) .idLoop(LoopIdentifier.getLoopIdentifier(lunaticXmlCollectedData.getVariableName(), variablesMap, i)) - .idParent(LoopIdentifier.getParentGroupName(lunaticXmlCollectedData.getVariableName(), variablesMap)) + .idParent(LoopIdentifier.getParentName(lunaticXmlCollectedData.getVariableName(), variablesMap)) .build()); dataCount++; } diff --git a/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java b/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java index f56572a1..fb4c1a61 100644 --- a/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java +++ b/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java @@ -21,14 +21,12 @@ public static String getLoopIdentifier(String variableName, VariablesMap variabl if(variableName.startsWith(Constants.FILTER_RESULT_PREFIX) && variablesMap.hasVariable(variableName.replace(Constants.FILTER_RESULT_PREFIX,"")) ){ - Variable parentVariable = variablesMap.getVariable(variableName.replace(Constants.FILTER_RESULT_PREFIX,"")); - return parentVariable.getGroupName(); + return getParentGroupName(variablesMap, variableName, Constants.FILTER_RESULT_PREFIX); } if(variableName.endsWith(Constants.MISSING_SUFFIX) && variablesMap.hasVariable(variableName.replace(Constants.MISSING_SUFFIX,"")) ){ - Variable parentVariable = variablesMap.getVariable(variableName.replace(Constants.MISSING_SUFFIX,"")); - return parentVariable.getGroupName(); + return getParentGroupName(variablesMap, variableName, Constants.MISSING_SUFFIX); } log.debug("Variable {} not found in variablesMap and assigned in root group", variableName); return Constants.ROOT_GROUP_NAME; @@ -39,7 +37,12 @@ public static String getLoopIdentifier(String variableName, VariablesMap variabl return String.format("%s_%d", variable.getGroup().getName() ,index); } - public static String getParentGroupName(String variableName, VariablesMap variablesMap) { + private static String getParentGroupName(VariablesMap variablesMap, String variableName, String constantToReplace) { + Variable parentVariable = variablesMap.getVariable(variableName.replace(constantToReplace, "")); + return parentVariable.getGroupName(); + } + + public static String getParentName(String variableName, VariablesMap variablesMap) { Variable variable = variablesMap.getVariable(variableName); List varsEno = Arrays.asList(Constants.getEnoVariables()); if ( variable == null ) { diff --git a/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java b/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java index 07a53255..327a92ff 100644 --- a/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java +++ b/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java @@ -52,14 +52,14 @@ void test03(){ @DisplayName("Should return the root group as parent") void test04(){ //When + Then - Assertions.assertThat(LoopIdentifier.getParentGroupName("var1", variablesMap)).isEqualTo(Constants.ROOT_GROUP_NAME); + Assertions.assertThat(LoopIdentifier.getParentName("var1", variablesMap)).isEqualTo(Constants.ROOT_GROUP_NAME); } @Test @DisplayName("Should return null as parent") void test05(){ //When + Then - Assertions.assertThat(LoopIdentifier.getParentGroupName("var2", variablesMap)).isNull(); + Assertions.assertThat(LoopIdentifier.getParentName("var2", variablesMap)).isNull(); } @Test @@ -73,7 +73,7 @@ void test06(){ @DisplayName("Should return var1 as parent if missing suffix") void test07(){ //When + Then - Assertions.assertThat(LoopIdentifier.getParentGroupName("var1_MISSING", variablesMap)).isEqualTo("var1"); + Assertions.assertThat(LoopIdentifier.getParentName("var1_MISSING", variablesMap)).isEqualTo("var1"); } @Test @@ -87,14 +87,14 @@ void test08(){ @DisplayName("Should return var1 as parent if filter result prefix") void test09(){ //When + Then - Assertions.assertThat(LoopIdentifier.getParentGroupName("FILTER_RESULT_var1", variablesMap)).isEqualTo("var1"); + Assertions.assertThat(LoopIdentifier.getParentName("FILTER_RESULT_var1", variablesMap)).isEqualTo("var1"); } @Test @DisplayName("Should return root as parent if eno variable") void test10(){ //When + Then - Assertions.assertThat(LoopIdentifier.getParentGroupName(Constants.getEnoVariables()[0], variablesMap)).isEqualTo(Constants.ROOT_GROUP_NAME); + Assertions.assertThat(LoopIdentifier.getParentName(Constants.getEnoVariables()[0], variablesMap)).isEqualTo(Constants.ROOT_GROUP_NAME); } }