Skip to content

Commit 417e739

Browse files
authored
feat: compute scope label in get variables (#380)
1 parent 2b7b4b4 commit 417e739

File tree

3 files changed

+562
-1
lines changed

3 files changed

+562
-1
lines changed

src/main/java/fr/insee/pogues/service/VariableService.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,28 @@ public List<VariableType> getVersionVariables(UUID versionId) throws Exception {
6464
return getQuestionnaireVariables(questionnaire);
6565
}
6666

67+
/**
68+
* Get the questionnaire's variables and, if they have a scope, compute the scope name instead of the id.
69+
* @param questionnaire Questionnaire from which we want the variables
70+
* @return Questionnaire's variables with a readable scope.
71+
*/
6772
private List<VariableType> getQuestionnaireVariables(Questionnaire questionnaire) {
68-
return questionnaire.getVariables().getVariable().stream().toList();
73+
List<VariableType> variables = questionnaire.getVariables().getVariable().stream().toList();
74+
variables.forEach(v -> computeScopeNameFromScopeId(v, questionnaire.getIterations()));
75+
return variables;
76+
}
77+
78+
/**
79+
* Compute the scope name instead of the id. If no related scope is found, do nothing.
80+
* @param variable Variable to update
81+
* @param iterations Iterations in which we will find the scope name
82+
*/
83+
private void computeScopeNameFromScopeId(VariableType variable, Questionnaire.Iterations iterations) {
84+
String scopeId = variable.getScope();
85+
if (scopeId != null) {
86+
IterationType iteration = iterations.getIteration().stream().filter(v -> scopeId.equals(v.getId())).toList().getFirst();
87+
if (iteration != null) variable.setScope(iteration.getName());
88+
}
6989
}
7090

7191
/**

src/test/java/fr/insee/pogues/service/VariableServiceTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
import org.mockito.Mock;
1616
import org.mockito.junit.jupiter.MockitoExtension;
1717

18+
import java.math.BigInteger;
1819
import java.util.List;
1920

2021
import static fr.insee.pogues.utils.Utils.loadQuestionnaireFromResources;
2122
import static fr.insee.pogues.utils.json.JSONFunctions.jsonStringtoJsonNode;
23+
import static org.assertj.core.api.Assertions.assertThat;
2224
import static org.junit.jupiter.api.Assertions.*;
2325

2426
@ExtendWith(MockitoExtension.class)
@@ -57,6 +59,36 @@ void getQuestionnaireVariables_success() throws Exception {
5759
assertEquals(expected.getId(), res.getFirst().getId());
5860
}
5961

62+
@Test
63+
@DisplayName("Should fetch questionnaire variables and compute the scope name")
64+
void getQuestionnaireVariables_success_scopeName() throws Exception {
65+
// Given a questionnaire with 1 variable
66+
Questionnaire mockQuestionnaire = loadQuestionnaireFromResources("service/withScope.json");
67+
String mockQuestionnaireString = PoguesSerializer.questionnaireJavaToString(mockQuestionnaire);
68+
JsonNode mockQuestionnaireJSON = jsonStringtoJsonNode(mockQuestionnaireString);
69+
questionnaireService.createQuestionnaire(mockQuestionnaireJSON);
70+
71+
String questionnaireId = "ma0nzzmj";
72+
String variableId = "mdy51s3x";
73+
74+
VariableType expected = new CollectedVariableType();
75+
expected.setId(variableId);
76+
expected.setName("NOMDUPOKEM");
77+
expected.setLabel("NOMDUPOKEM label");
78+
expected.setScope("LOOP_TEST");
79+
TextDatatypeType expectedDatatype = new TextDatatypeType();
80+
expectedDatatype.setTypeName(DatatypeTypeEnum.TEXT);
81+
expectedDatatype.setMaxLength(BigInteger.valueOf(249));
82+
expected.setDatatype(expectedDatatype);
83+
84+
// When we get the questionnaire's variables
85+
List<VariableType> res = variableService.getQuestionnaireVariables(questionnaireId);
86+
87+
// Then the variable is fetched
88+
VariableType variable = res.stream().filter(v -> variableId.equals(v.getId())).toList().getFirst();
89+
assertThat(variable).usingRecursiveComparison().isEqualTo(expected);
90+
}
91+
6092
@Test
6193
@DisplayName("Should trigger an error when we try to fetch variables from a questionnaire that does not exist")
6294
void getQuestionnaireVariables_error_questionnaireNotFound() {

0 commit comments

Comments
 (0)