From 282a34db5c652b5b1b93d6dd0278c8451cf560b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9nave?= <59770457+nsenave@users.noreply.github.com> Date: Fri, 23 Feb 2024 09:56:23 +0100 Subject: [PATCH] refactor: remove lunatic patches (#913) * test(dynamic table): clean up expected lunatic * test: unit test for lunatic scalar variable * build: bump lunatic model to 3.3.4 * refactor: remove patches for lunatic cleaning * refactor: remove patches for lunatic resizing * refactor: remove patches for lunatic missing block * chore: remove unused mockito lib Note: using it shows a warning since java 21. * test(label): remove usage of deprecated lunatic methods * refactor: remove patches in lunatic serializer * chore: maven local at first for dependency resolution So that maven local is checked first to resolve dependencies. * chore: version 3.17.2 * test(variables): remove redundant test This test exists in Lunatic-Model. --- build.gradle | 2 +- eno-core/build.gradle | 2 - .../lunatic/CleaningConcernedVariable.java | 11 - .../eno/core/model/lunatic/CleaningEntry.java | 22 -- .../model/lunatic/LunaticResizingEntry.java | 18 -- .../lunatic/LunaticResizingInterface.java | 8 - .../lunatic/LunaticResizingPairwiseEntry.java | 19 -- .../eno/core/model/lunatic/MissingBlock.java | 13 - .../lunatic/LunaticAddCleaningVariables.java | 67 ++--- .../lunatic/LunaticAddMissingVariables.java | 26 +- .../lunatic/resizing/LunaticAddResizing.java | 81 +----- .../resizing/LunaticLoopResizingLogic.java | 60 +++-- .../LunaticPairwiseResizingLogic.java | 82 ++++-- .../eno/core/serialize/LunaticSerializer.java | 149 ----------- .../core/mapping/out/lunatic/LabelTests.java | 9 +- .../LunaticAddCleaningVariablesTest.java | 244 ++++++++---------- ...unaticAddMissingVariablesPairwiseTest.java | 27 +- .../LunaticAddMissingVariablesTest.java | 115 ++++----- .../resizing/LunaticAddResizingTest.java | 55 ++-- .../LunaticLoopResizingLogicTest.java | 86 +++--- .../LunaticPairwiseResizingLogicTest.java | 17 +- .../insee/eno/core/sandbox/MockitoTests.java | 20 -- .../LunaticSerializerCleaningTest.java | 64 ----- .../LunaticSerializerMissingBlockTest.java | 66 ----- .../LunaticSerializerResizingTest.java | 57 ---- .../lunatic/lunatic-dynamic-table-2.json | 66 +---- settings.gradle | 4 +- 27 files changed, 401 insertions(+), 989 deletions(-) delete mode 100644 eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningConcernedVariable.java delete mode 100644 eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningEntry.java delete mode 100644 eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingEntry.java delete mode 100644 eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingInterface.java delete mode 100644 eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingPairwiseEntry.java delete mode 100644 eno-core/src/main/java/fr/insee/eno/core/model/lunatic/MissingBlock.java delete mode 100644 eno-core/src/test/java/fr/insee/eno/core/sandbox/MockitoTests.java delete mode 100644 eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerCleaningTest.java delete mode 100644 eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerMissingBlockTest.java delete mode 100644 eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerResizingTest.java diff --git a/build.gradle b/build.gradle index d3ec62613..bda3f5eb6 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ tasks.withType(JavaCompile).configureEach { allprojects { group = 'fr.insee.eno' - version = '3.17.1' + version = '3.17.2' } subprojects { diff --git a/eno-core/build.gradle b/eno-core/build.gradle index aaa3f4369..57e25ac90 100644 --- a/eno-core/build.gradle +++ b/eno-core/build.gradle @@ -46,8 +46,6 @@ dependencies { testImplementation 'org.assertj:assertj-core' // JSONAssert testImplementation 'org.skyscreamer:jsonassert' - // Mockito - testImplementation 'org.mockito:mockito-core' // Lombok compileOnly 'org.projectlombok:lombok' diff --git a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningConcernedVariable.java b/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningConcernedVariable.java deleted file mode 100644 index 40f94cfaf..000000000 --- a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningConcernedVariable.java +++ /dev/null @@ -1,11 +0,0 @@ -package fr.insee.eno.core.model.lunatic; - -import lombok.AllArgsConstructor; -import lombok.Data; - -@Data -@AllArgsConstructor -public class CleaningConcernedVariable { - private String name; - private String filter; -} \ No newline at end of file diff --git a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningEntry.java b/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningEntry.java deleted file mode 100644 index 7e30ad759..000000000 --- a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/CleaningEntry.java +++ /dev/null @@ -1,22 +0,0 @@ -package fr.insee.eno.core.model.lunatic; - -import lombok.AllArgsConstructor; -import lombok.Data; - -import java.util.List; - -/** - * Cleaning entry for the 'cleaning' block of a Lunatic questionnaire. - * Note: to be deported in Lunatic-Model later on. - */ -@Data -@AllArgsConstructor -public class CleaningEntry { - - /** Variable name (key of the resizing entry). */ - private String variableName; - - /** List of variable names (value of the resizing entry). */ - private List concernedVariables; - -} diff --git a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingEntry.java b/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingEntry.java deleted file mode 100644 index eb80e2666..000000000 --- a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingEntry.java +++ /dev/null @@ -1,18 +0,0 @@ -package fr.insee.eno.core.model.lunatic; - -import lombok.AllArgsConstructor; -import lombok.Data; - -import java.util.Collection; - -@Data -@AllArgsConstructor -public class LunaticResizingEntry implements LunaticResizingInterface { - /** Resizing variable. */ - private String name; - /** Resizing expression. */ - private String size; - /** Resized variables. */ - private Collection variables; - -} \ No newline at end of file diff --git a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingInterface.java b/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingInterface.java deleted file mode 100644 index a6e2bc1c8..000000000 --- a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingInterface.java +++ /dev/null @@ -1,8 +0,0 @@ -package fr.insee.eno.core.model.lunatic; - -/** Define an interface for Lunatic resizing objects, - * so that we can use something a bit less vague than Object, - * and we can make some polymorphism. */ -public interface LunaticResizingInterface { - String getName(); -} diff --git a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingPairwiseEntry.java b/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingPairwiseEntry.java deleted file mode 100644 index b869e4304..000000000 --- a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/LunaticResizingPairwiseEntry.java +++ /dev/null @@ -1,19 +0,0 @@ -package fr.insee.eno.core.model.lunatic; - -import lombok.AllArgsConstructor; -import lombok.Data; - -import java.util.Collection; -import java.util.List; - -@Data -@AllArgsConstructor -public class LunaticResizingPairwiseEntry implements LunaticResizingInterface { - /** Collected variable he pairwise is based on. */ - private String name; - /** List of size 2 containing size expressions for x and y-axis of the pairwise. */ - private List sizeForLinksVariables; - /** Resized variables. */ - private Collection linksVariables; - -} diff --git a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/MissingBlock.java b/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/MissingBlock.java deleted file mode 100644 index 4d7eb4259..000000000 --- a/eno-core/src/main/java/fr/insee/eno/core/model/lunatic/MissingBlock.java +++ /dev/null @@ -1,13 +0,0 @@ -package fr.insee.eno.core.model.lunatic; - -import lombok.AllArgsConstructor; -import lombok.Data; - -import java.util.List; - -@Data -@AllArgsConstructor -public class MissingBlock { - private String missingName; - private List names; -} diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariables.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariables.java index 8f41a9a91..dc8b34b67 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariables.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariables.java @@ -1,7 +1,5 @@ package fr.insee.eno.core.processing.out.steps.lunatic; -import fr.insee.eno.core.model.lunatic.CleaningConcernedVariable; -import fr.insee.eno.core.model.lunatic.CleaningEntry; import fr.insee.eno.core.processing.ProcessingStep; import fr.insee.lunatic.model.flat.*; import lombok.extern.slf4j.Slf4j; @@ -28,16 +26,17 @@ public class LunaticAddCleaningVariables implements ProcessingStep components = lunaticQuestionnaire.getComponents(); - List cleaningVariables = createCleaningVariables(components, lunaticQuestionnaire); + List cleaningVariables = createCleaningVariables(components, lunaticQuestionnaire); if(cleaningVariables.isEmpty()) { return; } CleaningType cleaningType = new CleaningType(); - lunaticQuestionnaire.setCleaning(cleaningType); - cleaningType.getAny().addAll(groupCleaningVariables(cleaningVariables)); + cleaningVariables.forEach(cleaningType::addCleaningEntry); + + lunaticQuestionnaire.setCleaning(cleaningType); } /** @@ -47,8 +46,8 @@ public void apply(Questionnaire lunaticQuestionnaire) { * @param lunaticQuestionnaire Lunatic questionnaire to process. * @return All cleaning entries for the questionnaire's components. */ - private List createCleaningVariables(List components, Questionnaire lunaticQuestionnaire) { - List cleaningEntries = new ArrayList<>(); + private List createCleaningVariables(List components, Questionnaire lunaticQuestionnaire) { + List cleaningEntries = new ArrayList<>(); components.stream() .filter(component -> component.getConditionFilter() != null) @@ -69,7 +68,7 @@ private List createCleaningVariables(List componen .map(ComponentNestingType.class::cast) .forEach(nestingComponent -> cleaningEntries.addAll(createCleaningVariables(nestingComponent.getComponents(), lunaticQuestionnaire))); - return cleaningEntries; + return groupCleaningVariables(cleaningEntries); } /** @@ -77,7 +76,7 @@ private List createCleaningVariables(List componen * @param componentType component to process (must be a simple response type) * @return cleaning variables for this component */ - private List createCleaningVariablesFromSimpleResponseComponent( + private List createCleaningVariablesFromSimpleResponseComponent( ComponentType componentType, Questionnaire lunaticQuestionnaire) { if(!(componentType instanceof ComponentSimpleResponseType simpleResponseType)) { throw new IllegalArgumentException(String.format( @@ -92,13 +91,17 @@ private List createCleaningVariablesFromSimpleResponseComponent( } // Cleaning value (here one value per key, objects with same key are grouped together afterward) - CleaningConcernedVariable concernedVariable = new CleaningConcernedVariable( + CleanedVariableEntry cleanedVariableEntry = new CleanedVariableEntry( simpleResponseType.getResponse().getName(), componentType.getConditionFilter().getValue()); // Cleaning entries return bindingDependencies.stream() - .map(bindingDependency -> new CleaningEntry(bindingDependency, List.of(concernedVariable))) + .map(bindingDependency -> { + CleaningVariableEntry cleaningVariableEntry = new CleaningVariableEntry(bindingDependency); + cleaningVariableEntry.addCleanedVariable(cleanedVariableEntry); + return cleaningVariableEntry; + }) .toList(); } @@ -121,7 +124,7 @@ private static List filterNonCollectedVariables(List variableNam * @param componentType component to process (must be a multiple response type) * @return cleaning variables for this component */ - private List createCleaningVariablesFromMultipleResponseComponent(ComponentType componentType) { + private List createCleaningVariablesFromMultipleResponseComponent(ComponentType componentType) { if(!(componentType instanceof ComponentMultipleResponseType)) { throw new IllegalArgumentException(String.format("Cannot create cleaning variable from this multiple response component %s", componentType.getId())); } @@ -131,36 +134,40 @@ private List createCleaningVariablesFromMultipleResponseComponent } String conditionFilter = componentType.getConditionFilter().getValue(); - List concernedVariables; + List cleanedVariableEntries; switch(componentType.getComponentType()) { - case TABLE -> concernedVariables = ((Table) componentType).getBodyLines().stream() + case TABLE -> cleanedVariableEntries = ((Table) componentType).getBodyLines().stream() .map(BodyLine::getBodyCells) .flatMap(Collection::stream) .map(BodyCell::getResponse) .filter(Objects::nonNull) .map(ResponseType::getName) - .map(name -> new CleaningConcernedVariable(name, conditionFilter)) + .map(name -> new CleanedVariableEntry(name, conditionFilter)) .toList(); - case ROSTER_FOR_LOOP -> concernedVariables = ((RosterForLoop) componentType).getComponents().stream() + case ROSTER_FOR_LOOP -> cleanedVariableEntries = ((RosterForLoop) componentType).getComponents().stream() .map(BodyCell::getResponse) .filter(Objects::nonNull) .map(ResponseType::getName) - .map(name -> new CleaningConcernedVariable(name, conditionFilter)) + .map(name -> new CleanedVariableEntry(name, conditionFilter)) .toList(); - case CHECKBOX_GROUP -> concernedVariables = ((CheckboxGroup) componentType).getResponses().stream() + case CHECKBOX_GROUP -> cleanedVariableEntries = ((CheckboxGroup) componentType).getResponses().stream() .map(ResponsesCheckboxGroup::getResponse) .map(ResponseType::getName) - .map(name -> new CleaningConcernedVariable(name, conditionFilter)) + .map(name -> new CleanedVariableEntry(name, conditionFilter)) .toList(); default -> throw new IllegalArgumentException(String.format("Cannot create cleaning variable from this multiple response component %s, componentType not defined", componentType.getId())); } return bindingDependencies.stream() - .map(bindingDependency -> new CleaningEntry(bindingDependency, concernedVariables)) + .map(bindingDependency -> { + CleaningVariableEntry cleaningVariableEntry = new CleaningVariableEntry(bindingDependency); + cleanedVariableEntries.forEach(cleaningVariableEntry::addCleanedVariable); + return cleaningVariableEntry; + }) .toList(); } @@ -169,19 +176,21 @@ private List createCleaningVariablesFromMultipleResponseComponent * @param cleaningEntries cleaning variables to regroup * @return grouped variables */ - private List groupCleaningVariables(List cleaningEntries) { - Map groupCleaningVariables = new LinkedHashMap<>(); - for (CleaningEntry cleaningEntry : cleaningEntries) { - if(!groupCleaningVariables.containsKey(cleaningEntry.getVariableName())) { - groupCleaningVariables.put(cleaningEntry.getVariableName(), cleaningEntry); + private List groupCleaningVariables(List cleaningEntries) { + + Map groupCleaningVariables = new LinkedHashMap<>(); + + for (CleaningVariableEntry cleaningEntry : cleaningEntries) { + if(!groupCleaningVariables.containsKey(cleaningEntry.getCleaningVariableName())) { + groupCleaningVariables.put(cleaningEntry.getCleaningVariableName(), cleaningEntry); continue; } - CleaningEntry groupVariable = groupCleaningVariables.get(cleaningEntry.getVariableName()); - List concernedVariables = new ArrayList<>(groupVariable.getConcernedVariables()); - concernedVariables.addAll(cleaningEntry.getConcernedVariables()); - groupVariable.setConcernedVariables(concernedVariables); + CleaningVariableEntry groupVariable = groupCleaningVariables.get(cleaningEntry.getCleaningVariableName()); + cleaningEntry.getCleanedVariableNames().forEach(cleanedVariableName -> + groupVariable.addCleanedVariable(cleaningEntry.getCleanedVariable(cleanedVariableName))); } + return groupCleaningVariables.values().stream() .toList(); } diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariables.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariables.java index 7827a0dcb..12ec0c51c 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariables.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariables.java @@ -1,7 +1,5 @@ package fr.insee.eno.core.processing.out.steps.lunatic; -import fr.insee.eno.core.exceptions.business.LunaticLoopException; -import fr.insee.eno.core.model.lunatic.MissingBlock; import fr.insee.eno.core.model.question.Question; import fr.insee.eno.core.processing.ProcessingStep; import fr.insee.eno.core.reference.EnoCatalog; @@ -58,9 +56,9 @@ public void apply(Questionnaire lunaticQuestionnaire) { components.forEach(component -> processComponentsMissingResponse(component, lunaticQuestionnaire)); - List missingBlocks = createMissingBlocks(components); + List missingBlocks = createMissingBlocks(components); // New list so that we put missing blocks next to the corresponding reversed missing block - List allMissingBlocks = new ArrayList<>(); + List allMissingBlocks = new ArrayList<>(); missingBlocks.forEach(missingBlock -> { allMissingBlocks.add(missingBlock); allMissingBlocks.addAll(createReversedMissingBlocks(missingBlock)); @@ -68,7 +66,7 @@ public void apply(Questionnaire lunaticQuestionnaire) { if (!allMissingBlocks.isEmpty()) { MissingType missingType = new MissingType(); - missingType.getAny().addAll(allMissingBlocks); + allMissingBlocks.forEach(missingType::addMissingEntry); lunaticQuestionnaire.setMissingBlock(missingType); } } @@ -139,15 +137,17 @@ private void addMissingVariable(IVariableType variable, String missingResponseNa * @param components List of components used to generate missing block entries. * @return list of missing blocks */ - private List createMissingBlocks(List components) { - List missingBlocks = new ArrayList<>(); + private List createMissingBlocks(List components) { + List missingBlocks = new ArrayList<>(); // generate blocks on components with missing response attribute (included main loop) missingBlocks.addAll(components.stream() .filter(componentType -> componentType.getMissingResponse() != null) .map(component -> { String missingResponseName = component.getMissingResponse().getName(); List names = LunaticUtils.getResponseNames(component); - return new MissingBlock(missingResponseName, names); + MissingEntry missingEntry = new MissingEntry(missingResponseName); + names.forEach(name -> missingEntry.getCorrespondingVariables().add(name)); + return missingEntry; }) .toList()); @@ -176,9 +176,13 @@ private List createMissingBlocks(List components) { * @param missingBlock Missing block from which we need to create reversed missing blocks. * @return List of reversed missing blocks. */ - private List createReversedMissingBlocks(MissingBlock missingBlock) { - return missingBlock.getNames().stream() - .map(name -> new MissingBlock(name, List.of(missingBlock.getMissingName()))) + private List createReversedMissingBlocks(MissingEntry missingBlock) { + return missingBlock.getCorrespondingVariables().stream() + .map(name -> { + MissingEntry missingEntry = new MissingEntry(name); + missingEntry.getCorrespondingVariables().add(missingBlock.getVariableName()); + return missingEntry; + }) .toList(); } diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizing.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizing.java index a8812a706..54799a11b 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizing.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizing.java @@ -1,9 +1,6 @@ package fr.insee.eno.core.processing.out.steps.lunatic.resizing; -import fr.insee.eno.core.exceptions.business.LunaticLogicException; import fr.insee.eno.core.model.EnoQuestionnaire; -import fr.insee.eno.core.model.lunatic.LunaticResizingEntry; -import fr.insee.eno.core.model.lunatic.LunaticResizingPairwiseEntry; import fr.insee.eno.core.processing.ProcessingStep; import fr.insee.eno.core.reference.EnoIndex; import fr.insee.lunatic.model.flat.*; @@ -25,7 +22,7 @@ public LunaticAddResizing(EnoQuestionnaire enoQuestionnaire) { @Override public void apply(Questionnaire lunaticQuestionnaire) { // - List resizingList = new ArrayList<>(); + ResizingType resizingType = new ResizingType(); // LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); @@ -37,83 +34,13 @@ public void apply(Questionnaire lunaticQuestionnaire) { lunaticQuestionnaire.getComponents().forEach(component -> { ComponentTypeEnum componentType = component.getComponentType(); if (Objects.requireNonNull(componentType) == ComponentTypeEnum.LOOP) { - resizingList.addAll(loopResizingLogic.buildResizingEntries((Loop) component)); + loopResizingLogic.buildResizingEntries((Loop) component, resizingType); } if (componentType == ComponentTypeEnum.PAIRWISE_LINKS) { - resizingList.addAll(pairwiseResizingLogic.buildPairwiseResizingEntries((PairwiseLinks) component)); + pairwiseResizingLogic.buildPairwiseResizingEntries((PairwiseLinks) component, resizingType); } }); - // Manage eventual resizing duplicates - List resizingListWithoutDuplicates = noDuplicatesControl(resizingList); - // Set the resizing list only if it is not empty - if(!resizingListWithoutDuplicates.isEmpty()) { - ResizingType resizingType = new ResizingType(); - resizingType.setAny(resizingListWithoutDuplicates); - lunaticQuestionnaire.setResizing(resizingType); - } - } - - /** Throws an exception if there are duplicates in the resizing list. - * If there is duplicate keys, resizing entries are merged is the resizing expression is the same, - * an exception is thrown otherwise. - * @param resizingList Resizing list with eventual duplicates. - * @return The resizing list with duplicate entries merged. */ - private List noDuplicatesControl(List resizingList) { - // Duplicates between regular entries - Map resizingMap = new HashMap<>(); - List resizingEntries = resizingList.stream() - .filter(LunaticResizingEntry.class::isInstance) - .map(LunaticResizingEntry.class::cast) - .toList(); - resizingEntries.forEach(resizingEntry -> { - String variableName = resizingEntry.getName(); - if (! resizingMap.containsKey(variableName)) - resizingMap.put(variableName, resizingEntry); - else - resizingMap.put(variableName, mergeResizingEntries(resizingMap.get(variableName), resizingEntry)); - }); - List result = new ArrayList<>(resizingMap.values()); - // Duplicate between pairwise entry and regular entries - Optional pairwiseResizingEntry = resizingList - .stream() - .filter(LunaticResizingPairwiseEntry.class::isInstance) - .map(LunaticResizingPairwiseEntry.class::cast) - .findAny(); - if (pairwiseResizingEntry.isPresent()) { - if (resizingMap.containsKey(pairwiseResizingEntry.get().getName())) { - // Note: Lunatic resizing modeling has to be changed so that there wouldn't be any issue here - log.warn(String.format( - "Variable '%s' is used to define the size of the pairwise links question and other " + - "components (loops or dynamic tables), this may cause problems.", - pairwiseResizingEntry.get().getName())); - } - result.add(pairwiseResizingEntry.get()); - } - // - return result; - } - - /** - * Merge resizing entries given into a single one. - * @param entry1 Resizing entry. - * @param entry2 Resizing entry, with same name. - * @return A merged resizing entry with variables from both entries. - */ - public LunaticResizingEntry mergeResizingEntries(LunaticResizingEntry entry1, LunaticResizingEntry entry2) { - // (should not happen, but you never know) - if (! entry1.getName().equals(entry2.getName())) - throw new IllegalArgumentException("Resizing entries with different names cannot be merged."); - // - if (! entry1.getSize().equals(entry2.getSize())) - throw new LunaticLogicException(String.format( - "Variable '%s' is used to define the size of different components in the questionnaire. " + - "Check loop 'max' iteration expressions, dynamic table max size expressions.", - entry1.getName())); - // - LunaticResizingEntry mergedEntry = new LunaticResizingEntry( - entry1.getName(), entry1.getSize(), entry1.getVariables()); - mergedEntry.getVariables().addAll(entry2.getVariables()); - return mergedEntry; + lunaticQuestionnaire.setResizing(resizingType); } } diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogic.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogic.java index 4c6bb74b6..9720ac01d 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogic.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogic.java @@ -1,22 +1,19 @@ package fr.insee.eno.core.processing.out.steps.lunatic.resizing; +import fr.insee.eno.core.exceptions.business.LunaticLogicException; import fr.insee.eno.core.exceptions.technical.MappingException; import fr.insee.eno.core.model.EnoQuestionnaire; import fr.insee.eno.core.model.calculated.BindingReference; -import fr.insee.eno.core.model.lunatic.LunaticResizingEntry; import fr.insee.eno.core.model.navigation.LinkedLoop; import fr.insee.eno.core.model.navigation.StandaloneLoop; import fr.insee.eno.core.model.question.DynamicTableQuestion; import fr.insee.eno.core.processing.out.steps.lunatic.LunaticLoopResolution; import fr.insee.eno.core.reference.EnoIndex; import fr.insee.eno.core.utils.LunaticUtils; -import fr.insee.lunatic.model.flat.IVariableType; -import fr.insee.lunatic.model.flat.Loop; -import fr.insee.lunatic.model.flat.Questionnaire; -import fr.insee.lunatic.model.flat.VariableTypeEnum; +import fr.insee.lunatic.model.flat.*; import lombok.extern.slf4j.Slf4j; -import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; import java.util.Set; @@ -35,11 +32,11 @@ public LunaticLoopResizingLogic(Questionnaire lunaticQuestionnaire, EnoQuestionn } /** - * Build resizing entries for a loop. + * Insert resizing entries for the given loop. * @param lunaticLoop Lunatic loop object. - * @return list of resizing entries of the loop. + * @param lunaticResizing Lunatic resizing block object. */ - public List buildResizingEntries(Loop lunaticLoop) { + public void buildResizingEntries(Loop lunaticLoop, ResizingType lunaticResizing) { // Corresponding Eno loop object fr.insee.eno.core.model.navigation.Loop enoLoop = (fr.insee.eno.core.model.navigation.Loop) @@ -48,8 +45,8 @@ public List buildResizingEntries(Loop lunaticLoop) { throw new MappingException(String.format( "Eno loop object corresponding to Lunatic loop '%s' cannot be found.", lunaticLoop.getId())); - // Variable names that are the keys of the resizing - List resizingVariableNames = new ArrayList<>(); + // Variable names that are the keys of the resizing (using a set to make sure there is no duplicates) + Set resizingVariableNames = new LinkedHashSet<>(); // Expression that resize the concerned variables String sizeExpression = null; @@ -57,25 +54,21 @@ public List buildResizingEntries(Loop lunaticLoop) { resizingVariableNames.addAll(findResizingVariablesForLoop(enoStandaloneLoop)); sizeExpression = lunaticLoop.getLines().getMax().getValue(); } - if (enoLoop instanceof LinkedLoop enoLinkedLoop){ + if (enoLoop instanceof LinkedLoop enoLinkedLoop) { resizingVariableNames.add(findResizingVariableForLinkedLoop(enoLinkedLoop)); sizeExpression = lunaticLoop.getIterations().getValue(); } if (resizingVariableNames.isEmpty()) - return new ArrayList<>(); + return; // Concerned variables to be resized // Note: external variables are not concerned since their values are not designed to be changed dynamically - Set resizedVariableNames = LunaticUtils.getCollectedVariablesInLoop(lunaticLoop); + List resizedVariableNames = LunaticUtils.getCollectedVariablesInLoop(lunaticLoop).stream().toList(); - List resizingLoopEntries = new ArrayList<>(); String finalSizeExpression = sizeExpression; // (due to usage in lambda) - resizingVariableNames.forEach(variableName -> - resizingLoopEntries.add( - new LunaticResizingEntry(variableName, finalSizeExpression, resizedVariableNames))); - - return resizingLoopEntries; + resizingVariableNames.forEach(variableName -> insertIterationEntry( + lunaticResizing, variableName, finalSizeExpression, resizedVariableNames)); } private List findResizingVariablesForLoop(StandaloneLoop enoLoop) { @@ -112,7 +105,7 @@ private String findResizingVariableForLinkedLoop(LinkedLoop enoLinkedLoop) { .findAny(); if (referenceTable.isPresent()) // Return the first column variable - return referenceTable.get().getVariableNames().get(0); + return referenceTable.get().getVariableNames().getFirst(); // If neither main loop nor dynamic table reference is found: exception throw new MappingException(String.format( @@ -120,4 +113,29 @@ private String findResizingVariableForLinkedLoop(LinkedLoop enoLinkedLoop) { enoLinkedLoop.getReference(), enoLinkedLoop.getId())); } + private static void insertIterationEntry(ResizingType lunaticResizing, + String resizingVariableName, String sizeExpression, List resizedVariableNames) { + // If no entry for the resizing variable name given, create it + if (lunaticResizing.getResizingEntry(resizingVariableName) == null) { + ResizingIterationEntry resizingIterationEntry = new ResizingIterationEntry(); + resizingIterationEntry.setSize(sizeExpression); + resizingIterationEntry.getVariables().addAll(resizedVariableNames); + lunaticResizing.putResizingEntry(resizingVariableName, resizingIterationEntry); + return; + } + // Otherwise update existing one, only if the size expression is the same + ResizingEntry resizingEntry = lunaticResizing.getResizingEntry(resizingVariableName); + if (resizingEntry.getSize() == null) // Entry can be a pairwise entry, in this case the 'iteration' size is not set yet + resizingEntry.setSize(sizeExpression); + if (! sizeExpression.equals(resizingEntry.getSize())) + throw new LunaticLogicException(String.format( + "Variable '%s' is used to define the size of different iterations in the questionnaire. " + + "Check loop 'max' iteration expressions, dynamic table max size expressions.", + resizingVariableName)); + resizedVariableNames.forEach(resizedVariableName -> { + if (! resizingEntry.getVariables().contains(resizedVariableName)) + resizingEntry.getVariables().add(resizedVariableName); + }); + } + } diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogic.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogic.java index e0f0ad6da..564571486 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogic.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogic.java @@ -2,14 +2,12 @@ import fr.insee.eno.core.exceptions.technical.LunaticPairwiseException; import fr.insee.eno.core.exceptions.technical.MappingException; -import fr.insee.eno.core.model.lunatic.LunaticResizingPairwiseEntry; import fr.insee.eno.core.model.question.PairwiseQuestion; import fr.insee.eno.core.reference.EnoIndex; import fr.insee.eno.core.utils.LunaticUtils; import fr.insee.lunatic.model.flat.*; -import java.util.ArrayList; -import java.util.List; +import java.util.LinkedHashSet; import java.util.Optional; import java.util.Set; @@ -24,11 +22,11 @@ public LunaticPairwiseResizingLogic(Questionnaire lunaticQuestionnaire, EnoIndex } /** - * Build resizing entries for a pairwise component. + * Insert resizing entries for the given pairwise component. * @param pairwiseLinks Lunatic pairwise object. - * @return list of resizing entries of the loop. + * @param lunaticResizing Lunatic resizing block object. */ - public List buildPairwiseResizingEntries(PairwiseLinks pairwiseLinks) { + public void buildPairwiseResizingEntries(PairwiseLinks pairwiseLinks, ResizingType lunaticResizing) { // Corresponding Eno object PairwiseQuestion enoPairwiseQuestion = (PairwiseQuestion) enoIndex.get(pairwiseLinks.getId()); @@ -37,28 +35,23 @@ public List buildPairwiseResizingEntries(PairwiseL "Eno pairwise question corresponding to Lunatic pairwise object '%s' cannot be found.", pairwiseLinks.getId())); - // Variable names that are the keys of the resizing - List resizingVariableNames = findResizingVariablesForPairwise(pairwiseLinks, enoPairwiseQuestion); + // Variable names that are the keys of the resizing (using a set to make sure there is no duplicates) + Set resizingVariableNames = findResizingVariablesForPairwise(pairwiseLinks, enoPairwiseQuestion); + // Expressions that resize the concerned (pairwise) variable + String xSizeExpression = pairwiseLinks.getXAxisIterations().getValue(); + String ySizeExpression = pairwiseLinks.getYAxisIterations().getValue(); if (resizingVariableNames.isEmpty()) - return new ArrayList<>(); + return; - // Expressions that resize the concerned variables - // (Note: pairwise variables are 'two dimensions', that's why there are two expressions) - List sizeExpressions = List.of( - pairwiseLinks.getXAxisIterations().getValue(), pairwiseLinks.getYAxisIterations().getValue()); - // Concerned variables to be resized - Set resizedVariableNames = Set.of(LunaticUtils.getPairwiseResponseVariable(pairwiseLinks)); + // Concerned (pairwise) variable to be resized + String pairwiseVariableName = LunaticUtils.getPairwiseResponseVariable(pairwiseLinks); - List resizingPairwiseEntries = new ArrayList<>(); - resizingVariableNames.forEach(variableName -> - resizingPairwiseEntries.add( - new LunaticResizingPairwiseEntry(variableName, sizeExpressions, resizedVariableNames))); - - return resizingPairwiseEntries; + resizingVariableNames.forEach(variableName -> insertPairwiseEntry( + lunaticResizing, variableName, xSizeExpression, ySizeExpression, pairwiseVariableName)); } - private List findResizingVariablesForPairwise(PairwiseLinks pairwiseLinks, PairwiseQuestion enoPairwiseQuestion) { + private Set findResizingVariablesForPairwise(PairwiseLinks pairwiseLinks, PairwiseQuestion enoPairwiseQuestion) { // Source variable of the pairwise String pairwiseSourceVariableName = enoPairwiseQuestion.getLoopVariableName(); // Find corresponding variable object @@ -71,12 +64,51 @@ private List findResizingVariablesForPairwise(PairwiseLinks pairwiseLink pairwiseSourceVariableName, pairwiseLinks)); // If it not a calculated, simply return the variable name if (! VariableTypeEnum.CALCULATED.equals(correspondingVariable.get().getVariableType())) - return List.of(pairwiseSourceVariableName); + return Set.of(pairwiseSourceVariableName); // Otherwise return its binding dependencies (without the calculated variable) VariableType pairwiseSourceVariable = (VariableType) correspondingVariable.get(); - return pairwiseSourceVariable.getBindingDependencies().stream() + return new LinkedHashSet<>(pairwiseSourceVariable.getBindingDependencies().stream() .filter(variableName -> !pairwiseSourceVariableName.equals(variableName)) - .toList(); + .toList()); + } + + /** + * Insert or update a pairwise resizing entry for the given resizing variable name. + * @param lunaticResizing Lunatic resizing block object. + * @param resizingVariableName Name of a resizing variable. + * @param xSizeExpression Expression of the pairwise 'xAxis' size. + * @param ySizeExpression Expression of the pairwise 'yAxis' size. + * @param linksVariableName Pairwise links variable name. + */ + private static void insertPairwiseEntry(ResizingType lunaticResizing, + String resizingVariableName, String xSizeExpression, String ySizeExpression, + String linksVariableName) { + // If no entry for the resizing variable name given, create it + if (lunaticResizing.getResizingEntry(resizingVariableName) == null) { + ResizingPairwiseEntry resizingPairwiseEntry = new ResizingPairwiseEntry(); + resizingPairwiseEntry.getSizeForLinksVariables().add(xSizeExpression); + resizingPairwiseEntry.getSizeForLinksVariables().add(ySizeExpression); + resizingPairwiseEntry.getLinksVariables().add(linksVariableName); + lunaticResizing.putResizingEntry(resizingVariableName, resizingPairwiseEntry); + return; + } + // If there is an iteration entry, convert it to a pairwise resizing entry and update it + ResizingEntry resizingEntry = lunaticResizing.getResizingEntry(resizingVariableName); + if (resizingEntry instanceof ResizingIterationEntry) { + ResizingEntry previousEntry = lunaticResizing.removeResizingEntry(resizingVariableName); + ResizingPairwiseEntry resizingPairwiseEntry = new ResizingPairwiseEntry(); + resizingPairwiseEntry.setSize(previousEntry.getSize()); + resizingPairwiseEntry.getVariables().addAll(previousEntry.getVariables()); + resizingPairwiseEntry.getSizeForLinksVariables().add(xSizeExpression); + resizingPairwiseEntry.getSizeForLinksVariables().add(ySizeExpression); + resizingPairwiseEntry.getLinksVariables().add(linksVariableName); + lunaticResizing.putResizingEntry(resizingVariableName, resizingPairwiseEntry); + } + // If there is already a pairwise resizing entry for this resizing variable name, + // it means that several pairwise questions were encountered + // (should not happen, but you never know) + if (resizingEntry instanceof ResizingPairwiseEntry) + throw new LunaticPairwiseException("Having several pairwise links question is not authorized."); } } diff --git a/eno-core/src/main/java/fr/insee/eno/core/serialize/LunaticSerializer.java b/eno-core/src/main/java/fr/insee/eno/core/serialize/LunaticSerializer.java index 146b64b1e..164f593d4 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/serialize/LunaticSerializer.java +++ b/eno-core/src/main/java/fr/insee/eno/core/serialize/LunaticSerializer.java @@ -1,22 +1,9 @@ package fr.insee.eno.core.serialize; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.JsonNodeFactory; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fasterxml.jackson.databind.node.ValueNode; import fr.insee.eno.core.exceptions.business.LunaticSerializationException; -import fr.insee.eno.core.model.lunatic.*; import fr.insee.lunatic.conversion.JsonSerializer; import fr.insee.lunatic.exception.SerializationException; -import fr.insee.lunatic.model.flat.CleaningType; -import fr.insee.lunatic.model.flat.MissingType; import fr.insee.lunatic.model.flat.Questionnaire; -import fr.insee.lunatic.model.flat.ResizingType; - -import java.util.List; public class LunaticSerializer { private LunaticSerializer() { @@ -30,12 +17,6 @@ private LunaticSerializer() { * @throws LunaticSerializationException if serialization fails. */ public static String serializeToJson(Questionnaire lunaticQuestionnaire) throws LunaticSerializationException { - String lunaticJson = rawSerialization(lunaticQuestionnaire); - return extendedSerialization(lunaticJson, lunaticQuestionnaire); - } - - /** Return the json string produced by Lunatic-Model json serializer. */ - private static String rawSerialization(Questionnaire lunaticQuestionnaire) { JsonSerializer jsonSerializer = new JsonSerializer(); try { return jsonSerializer.serialize(lunaticQuestionnaire); @@ -44,134 +25,4 @@ private static String rawSerialization(Questionnaire lunaticQuestionnaire) { } } - /* The following part should be supported by Lunatic-Model: */ - - /** - * convert a questionnaire to json string with resize variables included - * @param lunaticQuestionnaire lunatic questionnaire - * @return json string of the questionnaire with resize included - * @throws LunaticSerializationException serialization exception - */ - private static String extendedSerialization(String lunaticJson, Questionnaire lunaticQuestionnaire) - throws LunaticSerializationException { - ObjectMapper mapper = new ObjectMapper(); - try { - ObjectNode questionnaireNode = (ObjectNode) mapper.readTree(lunaticJson); - - CleaningType cleaningType = lunaticQuestionnaire.getCleaning(); - if(cleaningType != null && !cleaningType.getAny().isEmpty()) { - questionnaireNode.set("cleaning", createCleaningObject(lunaticQuestionnaire, mapper)); - } - - MissingType missingType = lunaticQuestionnaire.getMissingBlock(); - if(missingType != null && !missingType.getAny().isEmpty()) { - questionnaireNode.set("missingBlock", createMissingBlocks(lunaticQuestionnaire, mapper)); - } - - ResizingType resizingType = lunaticQuestionnaire.getResizing(); - if(resizingType != null && !resizingType.getAny().isEmpty()) { - questionnaireNode.set("resizing", createResizingObject(lunaticQuestionnaire, mapper)); - } - return mapper.writeValueAsString(questionnaireNode); - } catch(JsonProcessingException ex) { - throw new LunaticSerializationException(ex.getMessage()); - } - } - - /** - * Create missing blocks json node - * @param lunaticQuestionnaire lunatic questionnaire - * @return the json node containing all missing blocks in correct format - */ - private static ObjectNode createMissingBlocks(Questionnaire lunaticQuestionnaire, ObjectMapper mapper) { - ObjectNode missingBlocksJson = mapper.createObjectNode(); - lunaticQuestionnaire.getMissingBlock().getAny().stream() - .map(MissingBlock.class::cast) - .forEach(missingBlock -> { - ArrayNode namesArray = JsonNodeFactory.instance.arrayNode(); - missingBlock.getNames().forEach(namesArray::add); - missingBlocksJson.set(missingBlock.getMissingName(), namesArray); - }); - return missingBlocksJson; - } - - /** - * Create cleaning json node - * @param lunaticQuestionnaire lunatic questionnaire - * @return the json node containing the cleaning object in correct format - */ - private static ObjectNode createCleaningObject(Questionnaire lunaticQuestionnaire, ObjectMapper mapper) { - ObjectNode cleaningBlocksNode = mapper.createObjectNode(); - lunaticQuestionnaire.getCleaning().getAny().stream() - .map(CleaningEntry.class::cast) - .forEach(cleaningVariable -> { - ObjectNode concernedVariablesNode = JsonNodeFactory.instance.objectNode(); - for(CleaningConcernedVariable concernedVariable : cleaningVariable.getConcernedVariables()) { - ValueNode filterNode = JsonNodeFactory.instance.textNode(concernedVariable.getFilter()); - concernedVariablesNode.set(concernedVariable.getName(), filterNode); - } - cleaningBlocksNode.set(cleaningVariable.getVariableName(), concernedVariablesNode); - }); - return cleaningBlocksNode; - } - - /** - * Create resizing node for lunatic questionnaire - * @param lunaticQuestionnaire questionnaire lunatic - * @param mapper object mapper - * @return the resizing node - */ - private static ObjectNode createResizingObject(Questionnaire lunaticQuestionnaire, ObjectMapper mapper) { - List resizingList = lunaticQuestionnaire.getResizing().getAny(); - - ObjectNode jsonResizingNode = mapper.createObjectNode(); - for(Object resizingObject: resizingList) { - if(resizingObject instanceof LunaticResizingPairwiseEntry variable) { - jsonResizingNode.set(variable.getName(), resizingPairwiseEntryToJsonNode(variable)); - } - if(resizingObject instanceof LunaticResizingEntry variable) { - jsonResizingNode.set(variable.getName(), resizingEntryToJsonNode(variable)); - } - } - return jsonResizingNode; - } - - /** - * Build entry for resizing entry (concerns loops and dynamic tables). - * @param resizingEntry Resizing entry object. - * @return Resizing entry as a json node. - */ - private static JsonNode resizingEntryToJsonNode(LunaticResizingEntry resizingEntry) { - ArrayNode variablesArray = JsonNodeFactory.instance.arrayNode(); - resizingEntry.getVariables().forEach(variablesArray::add); - - ObjectNode loopVariableNode = JsonNodeFactory.instance.objectNode(); - - ValueNode sizeNodeValue = JsonNodeFactory.instance.textNode(resizingEntry.getSize()); - loopVariableNode.set("size", sizeNodeValue); - loopVariableNode.set("variables", variablesArray); - - return loopVariableNode; - } - - /** - * Build variable for pairwise resizing entry. - * @param resizingPairwiseEntry Pairwise-case resizing entry object. - * @return Resizing entry as a json node. - */ - private static JsonNode resizingPairwiseEntryToJsonNode(LunaticResizingPairwiseEntry resizingPairwiseEntry) { - ArrayNode linksVariablesBuilder = JsonNodeFactory.instance.arrayNode(); - resizingPairwiseEntry.getLinksVariables().forEach(linksVariablesBuilder::add); - - ArrayNode sizeForLinksVariablesBuilder = JsonNodeFactory.instance.arrayNode(); - resizingPairwiseEntry.getSizeForLinksVariables().forEach(sizeForLinksVariablesBuilder::add); - - ObjectNode pairwiseVariableNode = JsonNodeFactory.instance.objectNode(); - - pairwiseVariableNode.set("linksVariables", linksVariablesBuilder); - pairwiseVariableNode.set("sizeForLinksVariables", sizeForLinksVariablesBuilder); - - return pairwiseVariableNode; - } - } diff --git a/eno-core/src/test/java/fr/insee/eno/core/mapping/out/lunatic/LabelTests.java b/eno-core/src/test/java/fr/insee/eno/core/mapping/out/lunatic/LabelTests.java index 37f4a1407..1f67db244 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/mapping/out/lunatic/LabelTests.java +++ b/eno-core/src/test/java/fr/insee/eno/core/mapping/out/lunatic/LabelTests.java @@ -6,6 +6,7 @@ import fr.insee.eno.core.model.label.Label; import fr.insee.eno.core.model.label.QuestionnaireLabel; import fr.insee.lunatic.model.flat.LabelType; +import fr.insee.lunatic.model.flat.LabelTypeEnum; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -21,7 +22,7 @@ void basicLabel_type() { LunaticMapper lunaticMapper = new LunaticMapper(); lunaticMapper.mapEnoObject(enoLabel, lunaticLabel); // - assertEquals("VTL|MD", lunaticLabel.getType()); + assertEquals(LabelTypeEnum.VTL_MD, lunaticLabel.getTypeEnum()); } @Test @@ -33,7 +34,7 @@ void dynamicLabel_type() { LunaticMapper lunaticMapper = new LunaticMapper(); lunaticMapper.mapEnoObject(enoLabel, lunaticLabel); // - assertEquals("VTL|MD", lunaticLabel.getType()); + assertEquals(LabelTypeEnum.VTL_MD, lunaticLabel.getTypeEnum()); } @Test @@ -45,7 +46,7 @@ void questionnaireLabel_type() { LunaticMapper lunaticMapper = new LunaticMapper(); lunaticMapper.mapEnoObject(enoLabel, lunaticLabel); // - assertEquals("VTL|MD", lunaticLabel.getType()); + assertEquals(LabelTypeEnum.VTL_MD, lunaticLabel.getTypeEnum()); } @Test @@ -57,7 +58,7 @@ void calculatedExpression_type() { LunaticMapper lunaticMapper = new LunaticMapper(); lunaticMapper.mapEnoObject(enoCalculatedExpression, lunaticLabel); // - assertEquals("VTL", lunaticLabel.getType()); + assertEquals(LabelTypeEnum.VTL, lunaticLabel.getTypeEnum()); } } diff --git a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariablesTest.java b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariablesTest.java index 2dd89faf5..19f3493d3 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariablesTest.java +++ b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddCleaningVariablesTest.java @@ -1,13 +1,13 @@ package fr.insee.eno.core.processing.out.steps.lunatic; -import fr.insee.eno.core.model.lunatic.CleaningConcernedVariable; -import fr.insee.eno.core.model.lunatic.CleaningEntry; import fr.insee.lunatic.model.flat.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; @@ -23,8 +23,6 @@ class LunaticAddCleaningVariablesTest { CheckboxGroup checkboxGroup; Loop loop; - Questionnaire lunaticQuestionnaire; - @BeforeEach void init() { // @@ -47,7 +45,7 @@ void whenNoComponentWithFilter_shouldNotHaveCleaning() { for (ComponentType component : components) { component.setConditionFilter(null); } - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().addAll(components); processing.apply(lunaticQuestionnaire); @@ -62,7 +60,7 @@ void whenNoDependenciesInFilters_shouldNotHaveCleaning() { for (ComponentType component : components) { component.setConditionFilter(buildConditionFilter("true", new ArrayList<>())); } - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().addAll(components); processing.apply(lunaticQuestionnaire); @@ -72,8 +70,8 @@ void whenNoDependenciesInFilters_shouldNotHaveCleaning() { @Test void simpleResponseComponentWithBindingDependencies_shouldHaveCleaningEntries() { - - lunaticQuestionnaire = new Questionnaire(); + // + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().add(input); input.setConditionFilter(buildConditionFilter("(SUM1 < 10)", List.of("Q11", "Q12"))); @@ -82,67 +80,69 @@ void simpleResponseComponentWithBindingDependencies_shouldHaveCleaningEntries() lunaticQuestionnaire.getVariables().add(buildCollectedVariable("Q11")); lunaticQuestionnaire.getVariables().add(buildCollectedVariable("Q12")); + // processing.apply(lunaticQuestionnaire); - List variables = lunaticQuestionnaire.getCleaning().getAny().stream() - .map(CleaningEntry.class::cast) - .toList(); - - assertEquals(2, variables.size()); - - CleaningEntry variable = variables.get(0); - assertEquals("Q11", variable.getVariableName()); - assertEquals(1, variable.getConcernedVariables().size()); - assertEquals("SHORT_TEXT", variable.getConcernedVariables().get(0).getName()); - assertEquals("(SUM1 < 10)", variable.getConcernedVariables().get(0).getFilter()); - - variable = variables.get(1); - assertEquals("Q12", variable.getVariableName()); - assertEquals(1, variable.getConcernedVariables().size()); - assertEquals("SHORT_TEXT", variable.getConcernedVariables().get(0).getName()); - assertEquals("(SUM1 < 10)", variable.getConcernedVariables().get(0).getFilter()); + + // + CleaningType cleaningType = lunaticQuestionnaire.getCleaning(); + + assertEquals(2, cleaningType.countCleaningVariables()); + + CleaningVariableEntry q11Entry = cleaningType.getCleaningEntry("Q11"); + assertNotNull(q11Entry); + assertEquals(1, q11Entry.countCleanedVariables()); + assertNotNull(q11Entry.getCleanedVariable("SHORT_TEXT")); + assertEquals("(SUM1 < 10)", q11Entry.getCleanedVariable("SHORT_TEXT").filterExpression()); + + CleaningVariableEntry q12Entry = cleaningType.getCleaningEntry("Q12"); + assertNotNull(q12Entry); + assertEquals(1, q12Entry.countCleanedVariables()); + assertNotNull(q12Entry.getCleanedVariable("SHORT_TEXT")); + assertEquals("(SUM1 < 10)", q12Entry.getCleanedVariable("SHORT_TEXT").filterExpression()); } @Test void componentWithBindingDependenciesWithinLoop_shouldHaveCleaningEntries() { - + // dropdown.setConditionFilter(buildConditionFilter("(SUM1 < 10)", List.of("Q1", "Q2"))); - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().add(loop); lunaticQuestionnaire.getVariables().add(buildCalculatedVariable("SUM1")); lunaticQuestionnaire.getVariables().add(buildCollectedVariable("Q1")); lunaticQuestionnaire.getVariables().add(buildCollectedVariable("Q2")); + // processing.apply(lunaticQuestionnaire); - List variables = lunaticQuestionnaire.getCleaning().getAny().stream() - .map(CleaningEntry.class::cast) - .toList(); - - assertEquals(2, variables.size()); - - CleaningEntry variable = variables.get(0); - assertEquals("Q1", variable.getVariableName()); - assertEquals(1, variable.getConcernedVariables().size()); - assertEquals("DROPDOWN", variable.getConcernedVariables().get(0).getName()); - assertEquals("(SUM1 < 10)", variable.getConcernedVariables().get(0).getFilter()); - - variable = variables.get(1); - assertEquals("Q2", variable.getVariableName()); - assertEquals(1, variable.getConcernedVariables().size()); - assertEquals("DROPDOWN", variable.getConcernedVariables().get(0).getName()); - assertEquals("(SUM1 < 10)", variable.getConcernedVariables().get(0).getFilter()); + + // + CleaningType cleaningType = lunaticQuestionnaire.getCleaning(); + + assertEquals(2, cleaningType.countCleaningVariables()); + + CleaningVariableEntry q1Entry = cleaningType.getCleaningEntry("Q1"); + assertNotNull(q1Entry); + assertEquals(1, q1Entry.countCleanedVariables()); + assertNotNull(q1Entry.getCleanedVariable("DROPDOWN")); + assertEquals("(SUM1 < 10)", q1Entry.getCleanedVariable("DROPDOWN").filterExpression()); + + CleaningVariableEntry q2Entry = cleaningType.getCleaningEntry("Q2"); + assertNotNull(q2Entry); + assertEquals(1, q2Entry.countCleanedVariables()); + assertNotNull(q2Entry.getCleanedVariable("DROPDOWN")); + assertEquals("(SUM1 < 10)", q2Entry.getCleanedVariable("DROPDOWN").filterExpression()); } @Nested class ComponentsWithCommonDependencies { - private Map cleaningEntries; + private CleaningType cleaningType; @BeforeEach void setupComponentsHavingCommonDependencies() { - - lunaticQuestionnaire = new Questionnaire(); + // + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().addAll(List.of(input, loop)); input.setConditionFilter(buildConditionFilter("(TEST > 30)", List.of("Q1", "Q2"))); @@ -155,121 +155,100 @@ void setupComponentsHavingCommonDependencies() { lunaticQuestionnaire.getVariables().add(buildCollectedVariable("Q1")); lunaticQuestionnaire.getVariables().add(buildCollectedVariable("Q2")); + // processing.apply(lunaticQuestionnaire); - cleaningEntries = new HashMap<>(); - lunaticQuestionnaire.getCleaning().getAny().stream() - .map(CleaningEntry.class::cast) - .forEach(cleaningEntry -> cleaningEntries.put(cleaningEntry.getVariableName(), cleaningEntry)); + // + cleaningType = lunaticQuestionnaire.getCleaning(); } @Test void shouldHaveGroupedCleaningVariables_count(){ - assertEquals(2, cleaningEntries.size()); + assertEquals(2, cleaningType.countCleaningVariables()); } @Test void shouldHaveGroupedCleaningVariables_keys() { - assertThat(cleaningEntries.keySet()).containsExactlyInAnyOrderElementsOf( + assertThat(cleaningType.getCleaningVariableNames()).containsExactlyInAnyOrderElementsOf( Set.of("Q1", "Q2")); } @Test void shouldHaveGroupedCleaningVariables_values() { - CleaningEntry cleaningEntry1 = cleaningEntries.get("Q1"); - assertEquals(2, cleaningEntry1.getConcernedVariables().size()); - assertEquals("SHORT_TEXT", cleaningEntry1.getConcernedVariables().get(0).getName()); - assertEquals("(TEST > 30)", cleaningEntry1.getConcernedVariables().get(0).getFilter()); - assertEquals("DROPDOWN", cleaningEntry1.getConcernedVariables().get(1).getName()); - assertEquals("(SUM1 < 10)", cleaningEntry1.getConcernedVariables().get(1).getFilter()); - - CleaningEntry cleaningEntry2 = cleaningEntries.get("Q2"); - assertEquals(2, cleaningEntry2.getConcernedVariables().size()); - assertEquals("SHORT_TEXT", cleaningEntry2.getConcernedVariables().get(0).getName()); - assertEquals("(TEST > 30)", cleaningEntry2.getConcernedVariables().get(0).getFilter()); - assertEquals("INTEGER", cleaningEntry2.getConcernedVariables().get(1).getName()); - assertEquals("(SUM2 < 10)", cleaningEntry2.getConcernedVariables().get(1).getFilter()); + CleaningVariableEntry cleaningEntry1 = cleaningType.getCleaningEntry("Q1"); + assertEquals(2, cleaningEntry1.countCleanedVariables()); + assertEquals("(TEST > 30)", cleaningEntry1.getCleanedVariable("SHORT_TEXT").filterExpression()); + assertEquals("(SUM1 < 10)", cleaningEntry1.getCleanedVariable("DROPDOWN").filterExpression()); + + CleaningVariableEntry cleaningEntry2 = cleaningType.getCleaningEntry("Q2"); + assertEquals(2, cleaningEntry2.countCleanedVariables()); + assertEquals("(TEST > 30)", cleaningEntry2.getCleanedVariable("SHORT_TEXT").filterExpression()); + assertEquals("(SUM2 < 10)", cleaningEntry2.getCleanedVariable("INTEGER").filterExpression()); } } @Test void checkboxGroupWithBindingDependencies_shouldHaveCleaningEntries() { - - lunaticQuestionnaire = new Questionnaire(); + // + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().add(checkboxGroup); checkboxGroup.setConditionFilter(buildConditionFilter("(SUM1 < 10)", List.of("Q11", "Q12"))); + // processing.apply(lunaticQuestionnaire); - List variables = lunaticQuestionnaire.getCleaning().getAny().stream() - .map(CleaningEntry.class::cast) - .toList(); - - assertEquals(2, variables.size()); - - CleaningEntry variable = variables.get(0); - assertEquals("Q11", variable.getVariableName()); - List concernedVariables = variable.getConcernedVariables(); - assertEquals(2, concernedVariables.size()); - assertEquals("MODALITY1", concernedVariables.get(0).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(0).getFilter()); - assertEquals("MODALITY2", concernedVariables.get(1).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(1).getFilter()); - - variable = variables.get(1); - assertEquals("Q12", variable.getVariableName()); - concernedVariables = variable.getConcernedVariables(); - assertEquals(2, concernedVariables.size()); - assertEquals("MODALITY1", concernedVariables.get(0).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(0).getFilter()); - assertEquals("MODALITY2", concernedVariables.get(1).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(1).getFilter()); + + // + CleaningType cleaningType = lunaticQuestionnaire.getCleaning(); + + assertEquals(2, cleaningType.countCleaningVariables()); + + CleaningVariableEntry q11Entry = cleaningType.getCleaningEntry("Q11"); + assertEquals(2, q11Entry.countCleanedVariables()); + assertEquals("(SUM1 < 10)", q11Entry.getCleanedVariable("MODALITY1").filterExpression()); + assertEquals("(SUM1 < 10)", q11Entry.getCleanedVariable("MODALITY2").filterExpression()); + + CleaningVariableEntry q12Entry = cleaningType.getCleaningEntry("Q11"); + assertEquals(2, q12Entry.countCleanedVariables()); + assertEquals("(SUM1 < 10)", q12Entry.getCleanedVariable("MODALITY1").filterExpression()); + assertEquals("(SUM1 < 10)", q12Entry.getCleanedVariable("MODALITY2").filterExpression()); } @Test void tableWithBindingDependencies_shouldHaveCleaningEntries() { - - lunaticQuestionnaire = new Questionnaire(); + // + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().add(table); table.setConditionFilter(buildConditionFilter("(SUM1 < 10)", List.of("Q11", "Q12"))); + // processing.apply(lunaticQuestionnaire); - List variables = lunaticQuestionnaire.getCleaning().getAny().stream() - .map(CleaningEntry.class::cast) - .toList(); - - assertEquals(2, variables.size()); - - CleaningEntry variable = variables.get(0); - assertEquals("Q11", variable.getVariableName()); - List concernedVariables = variable.getConcernedVariables(); - assertEquals(3, concernedVariables.size()); - assertEquals("CELL1", concernedVariables.get(0).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(0).getFilter()); - assertEquals("CELL2", concernedVariables.get(1).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(1).getFilter()); - assertEquals("CELL3", concernedVariables.get(2).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(2).getFilter()); - - variable = variables.get(1); - assertEquals("Q12", variable.getVariableName()); - concernedVariables = variable.getConcernedVariables(); - assertEquals(3, concernedVariables.size()); - assertEquals("CELL1", concernedVariables.get(0).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(0).getFilter()); - assertEquals("CELL2", concernedVariables.get(1).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(1).getFilter()); - assertEquals("CELL3", concernedVariables.get(2).getName()); - assertEquals("(SUM1 < 10)", concernedVariables.get(2).getFilter()); + + // + CleaningType cleaningType = lunaticQuestionnaire.getCleaning(); + + assertEquals(2, cleaningType.countCleaningVariables()); + + CleaningVariableEntry q11Entry = cleaningType.getCleaningEntry("Q11"); + assertEquals(3, q11Entry.countCleanedVariables()); + assertEquals("(SUM1 < 10)", q11Entry.getCleanedVariable("CELL1").filterExpression()); + assertEquals("(SUM1 < 10)", q11Entry.getCleanedVariable("CELL2").filterExpression()); + assertEquals("(SUM1 < 10)", q11Entry.getCleanedVariable("CELL3").filterExpression()); + + CleaningVariableEntry q12Entry = cleaningType.getCleaningEntry("Q12"); + assertEquals(3, q12Entry.countCleanedVariables()); + assertEquals("(SUM1 < 10)", q12Entry.getCleanedVariable("CELL1").filterExpression()); + assertEquals("(SUM1 < 10)", q12Entry.getCleanedVariable("CELL2").filterExpression()); + assertEquals("(SUM1 < 10)", q12Entry.getCleanedVariable("CELL3").filterExpression()); } @Test void externalBindingDependency_shouldNotBeInCleaningEntries() { - - lunaticQuestionnaire = new Questionnaire(); + // + Questionnaire lunaticQuestionnaire = new Questionnaire(); lunaticQuestionnaire.getComponents().add(input); input.setConditionFilter(buildConditionFilter("(SUM1 < 10)", List.of("Q1", "EXTERNAL1"))); @@ -278,18 +257,17 @@ void externalBindingDependency_shouldNotBeInCleaningEntries() { lunaticQuestionnaire.getVariables().add(buildCollectedVariable("Q1")); lunaticQuestionnaire.getVariables().add(buildExternalVariable("EXTERNAL1")); + // processing.apply(lunaticQuestionnaire); - List cleaningEntries = lunaticQuestionnaire.getCleaning().getAny().stream() - .map(CleaningEntry.class::cast) - .toList(); - assertEquals(1, cleaningEntries.size()); + // + CleaningType cleaningType = lunaticQuestionnaire.getCleaning(); + + assertEquals(1, cleaningType.countCleaningVariables()); - CleaningEntry variable = cleaningEntries.get(0); - assertEquals("Q1", variable.getVariableName()); - assertEquals(1, variable.getConcernedVariables().size()); - assertEquals("SHORT_TEXT", variable.getConcernedVariables().get(0).getName()); - assertEquals("(SUM1 < 10)", variable.getConcernedVariables().get(0).getFilter()); + CleaningVariableEntry q1Entry = cleaningType.getCleaningEntry("Q1"); + assertEquals(1, q1Entry.countCleanedVariables()); + assertEquals("(SUM1 < 10)", q1Entry.getCleanedVariable("SHORT_TEXT").filterExpression()); } /* ----- Private utility methods below ----- */ @@ -392,7 +370,7 @@ private ResponseType buildResponse(String name) { private ConditionFilterType buildConditionFilter(String vtlExpression, List bindingDependencies) { ConditionFilterType conditionFilter = new ConditionFilterType(); - conditionFilter.setType("VTL"); + conditionFilter.setType(LabelTypeEnum.VTL); conditionFilter.setValue(vtlExpression); conditionFilter.getBindingDependencies().addAll(bindingDependencies); return conditionFilter; diff --git a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesPairwiseTest.java b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesPairwiseTest.java index 7bfc79df2..2b387a78c 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesPairwiseTest.java +++ b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesPairwiseTest.java @@ -1,10 +1,8 @@ package fr.insee.eno.core.processing.out.steps.lunatic; -import fr.insee.eno.core.model.lunatic.MissingBlock; import fr.insee.lunatic.model.flat.*; import org.junit.jupiter.api.Test; -import java.util.List; import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; @@ -41,23 +39,18 @@ void pairwiseMissingVariable_unitTest() { assertTrue(pairwiseMissingVariable.isPresent()); assertInstanceOf(VariableTypeTwoDimensionsArray.class, pairwiseMissingVariable.get()); // - List missingEntries = lunaticQuestionnaire.getMissingBlock().getAny().stream() - .map(MissingBlock.class::cast).toList(); - assertEquals(2, missingEntries.size()); + MissingType missingType = lunaticQuestionnaire.getMissingBlock(); + assertEquals(2, missingType.countMissingEntries()); // - Optional pairwiseMissingBlock = missingEntries.stream() - .filter(missingBlock -> "FOO_LINKS_MISSING".equals(missingBlock.getMissingName())) - .findAny(); - assertTrue(pairwiseMissingBlock.isPresent()); - assertEquals(1, pairwiseMissingBlock.get().getNames().size()); - assertEquals("FOO_LINKS", pairwiseMissingBlock.get().getNames().get(0)); + MissingEntry pairwiseMissingEntry = missingType.getMissingEntry("FOO_LINKS_MISSING"); + assertNotNull(pairwiseMissingEntry); + assertEquals(1, pairwiseMissingEntry.getCorrespondingVariables().size()); + assertEquals("FOO_LINKS", pairwiseMissingEntry.getCorrespondingVariables().getFirst()); // - Optional pairwiseReverseMissingBlock = missingEntries.stream() - .filter(missingBlock -> "FOO_LINKS".equals(missingBlock.getMissingName())) - .findAny(); - assertTrue(pairwiseReverseMissingBlock.isPresent()); - assertEquals(1, pairwiseReverseMissingBlock.get().getNames().size()); - assertEquals("FOO_LINKS_MISSING", pairwiseReverseMissingBlock.get().getNames().get(0)); + MissingEntry pairwiseReverseMissingEntry = missingType.getMissingEntry("FOO_LINKS"); + assertNotNull(pairwiseReverseMissingEntry); + assertEquals(1, pairwiseReverseMissingEntry.getCorrespondingVariables().size()); + assertEquals("FOO_LINKS_MISSING", pairwiseReverseMissingEntry.getCorrespondingVariables().getFirst()); } } diff --git a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesTest.java b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesTest.java index ffa1d4e65..e10c67436 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesTest.java +++ b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddMissingVariablesTest.java @@ -1,7 +1,6 @@ package fr.insee.eno.core.processing.out.steps.lunatic; import fr.insee.eno.core.model.EnoQuestionnaire; -import fr.insee.eno.core.model.lunatic.MissingBlock; import fr.insee.eno.core.model.question.SingleResponseQuestion; import fr.insee.eno.core.model.question.TextQuestion; import fr.insee.eno.core.reference.EnoCatalog; @@ -32,8 +31,6 @@ class LunaticAddMissingVariablesTest { CheckboxGroup checkboxGroup; RosterForLoop rosterForLoop; - Questionnaire lunaticQuestionnaire; - EnoQuestionnaire enoQuestionnaire; EnoCatalog enoCatalog; @@ -41,7 +38,6 @@ class LunaticAddMissingVariablesTest { @BeforeEach void init() { enoQuestionnaire = new EnoQuestionnaire(); - lunaticQuestionnaire = new Questionnaire(); input = buildInput("jfazww20", "TEXTECOURT"); textarea = buildTextarea("jfazwjyv", "TEXTELONG"); inputNumber = buildNumber("jfjh1ndk", "INTEGER"); @@ -59,13 +55,14 @@ void init() { @Test void whenApplyingMissingProcessingQuestionnaireMissingAttributeIsTrue() { + Questionnaire lunaticQuestionnaire = new Questionnaire(); processing.apply(lunaticQuestionnaire); assertTrue(lunaticQuestionnaire.getMissing()); } @Test void whenSimpleQuestionsGenerateCorrectMissingResponse() { - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List components = lunaticQuestionnaire.getComponents(); components.addAll(List.of(input, textarea, inputNumber, datepicker, checkboxBoolean, radio, checkboxOne, dropdown)); @@ -82,7 +79,7 @@ void whenComplexQuestionGenerateCorrectMissingResponse() { enoCatalog = new EnoCatalog(enoQuestionnaire); processing = new LunaticAddMissingVariables(enoCatalog, true); - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List components = lunaticQuestionnaire.getComponents(); components.add(table); @@ -94,7 +91,7 @@ void whenComplexQuestionGenerateCorrectMissingResponse() { @ParameterizedTest @ValueSource(booleans = {true, false}) void whenLoopGenerateMissingResponseOnSubComponents(boolean paginatedLoop) { - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List questionnaireComponents = lunaticQuestionnaire.getComponents(); @@ -111,7 +108,7 @@ void whenLoopGenerateMissingResponseOnSubComponents(boolean paginatedLoop) { @Test void whenSimpleQuestionsGenerateCorrectVariables() { - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List components = lunaticQuestionnaire.getComponents(); components.addAll(List.of(input, textarea, inputNumber, datepicker, checkboxBoolean, radio, checkboxOne, dropdown)); processing.apply(lunaticQuestionnaire); @@ -126,7 +123,7 @@ void whenSimpleQuestionsGenerateCorrectVariables() { @Test void whenMultipleQuestionsGenerateCorrectVariables() { - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List components = lunaticQuestionnaire.getComponents(); components.addAll(List.of(checkboxGroup, table, rosterForLoop)); processing.apply(lunaticQuestionnaire); @@ -148,7 +145,7 @@ void whenMultipleQuestionsGenerateCorrectVariables() { @ParameterizedTest @ValueSource(booleans = {true, false}) void whenLoopGenerateCorrectVariables(boolean paginatedLoop) { - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List components = lunaticQuestionnaire.getComponents(); Loop loop = buildLoop("jdfhjis5", List.of(input, textarea, inputNumber, datepicker, checkboxBoolean, radio, checkboxOne, dropdown), @@ -165,26 +162,24 @@ void whenLoopGenerateCorrectVariables(boolean paginatedLoop) { @Test void whenSimpleQuestionsGenerateCorrectMissingBlocks() { - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List components = lunaticQuestionnaire.getComponents(); components.addAll(List.of(input, textarea, inputNumber, datepicker, checkboxBoolean, radio, checkboxOne, dropdown)); processing.apply(lunaticQuestionnaire); - List responseTypes = components.stream().map(ComponentSimpleResponseType.class::cast).toList(); - List missingBlocks = lunaticQuestionnaire.getMissingBlock().getAny().stream() - .map(MissingBlock.class::cast).toList(); + MissingType missingType = lunaticQuestionnaire.getMissingBlock(); for(ComponentType component : components) { String responseName = ((ComponentSimpleResponseType) component).getResponse().getName(); String missingResponseName = component.getMissingResponse().getName(); - assertTrue(missingBlocks.stream() - .anyMatch(missingBlock -> missingBlock.getMissingName().equals(missingResponseName) - && missingBlock.getNames().size() == 1 - && missingBlock.getNames().contains(responseName))); - assertTrue(missingBlocks.stream() - .anyMatch(missingBlock -> missingBlock.getMissingName().equals(responseName) - && missingBlock.getNames().size() == 1 - && missingBlock.getNames().contains(missingResponseName))); + // Missing block + assertNotNull(missingType.getMissingEntry(missingResponseName)); + assertEquals(1, missingType.getMissingEntry(missingResponseName).getCorrespondingVariables().size()); + assertEquals(responseName, missingType.getMissingEntry(missingResponseName).getCorrespondingVariables().getFirst()); + // Reverse missing block + assertNotNull(missingType.getMissingEntry(responseName)); + assertEquals(1, missingType.getMissingEntry(responseName).getCorrespondingVariables().size()); + assertEquals(missingResponseName, missingType.getMissingEntry(responseName).getCorrespondingVariables().getFirst()); } } @@ -192,7 +187,7 @@ void whenSimpleQuestionsGenerateCorrectMissingBlocks() { @ValueSource(booleans = {true, false}) void whenLoopGenerateMissingBlocksFromSubComponents(boolean paginatedLoop) { // Given - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List questionnaireComponents = lunaticQuestionnaire.getComponents(); // List loopComponents = new ArrayList<>(List.of(input, textarea, inputNumber, datepicker, checkboxBoolean, radio, checkboxOne, dropdown)); @@ -201,26 +196,25 @@ void whenLoopGenerateMissingBlocksFromSubComponents(boolean paginatedLoop) { // When processing.apply(lunaticQuestionnaire); // Then - List missingBlocks = lunaticQuestionnaire.getMissingBlock().getAny().stream() - .map(MissingBlock.class::cast).toList(); + MissingType missingType = lunaticQuestionnaire.getMissingBlock(); // for(ComponentType loopComponent : loopComponents) { String responseName = ((ComponentSimpleResponseType) loopComponent).getResponse().getName(); String missingResponseName = loopComponent.getMissingResponse().getName(); - assertTrue(missingBlocks.stream() - .anyMatch(missingBlock -> missingBlock.getMissingName().equals(missingResponseName) - && missingBlock.getNames().size() == 1 - && missingBlock.getNames().contains(responseName))); - assertTrue(missingBlocks.stream() - .anyMatch(missingBlock -> missingBlock.getMissingName().equals(responseName) - && missingBlock.getNames().size() == 1 - && missingBlock.getNames().contains(missingResponseName))); + // Missing block + assertNotNull(missingType.getMissingEntry(missingResponseName)); + assertEquals(1, missingType.getMissingEntry(missingResponseName).getCorrespondingVariables().size()); + assertEquals(responseName, missingType.getMissingEntry(missingResponseName).getCorrespondingVariables().getFirst()); + // Reverse missing block + assertNotNull(missingType.getMissingEntry(responseName)); + assertEquals(1, missingType.getMissingEntry(responseName).getCorrespondingVariables().size()); + assertEquals(missingResponseName, missingType.getMissingEntry(responseName).getCorrespondingVariables().getFirst()); } } @Test void whenPairwiseGenerateMissingBlocksFromSubComponents() { - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List questionnaireComponents = lunaticQuestionnaire.getComponents(); List pairwiseComponents = new ArrayList<>(List.of(dropdown)); @@ -228,46 +222,49 @@ void whenPairwiseGenerateMissingBlocksFromSubComponents() { questionnaireComponents.add(pairwiseLinks); processing.apply(lunaticQuestionnaire); - ComponentType pairwiseInnerComponent = pairwiseComponents.get(0); - List missingBlocks = lunaticQuestionnaire.getMissingBlock().getAny().stream() - .map(MissingBlock.class::cast).toList(); - - assertTrue(missingBlocks.stream().anyMatch(missingBlock -> - missingBlock.getMissingName().equals(pairwiseInnerComponent.getMissingResponse().getName()) - && missingBlock.getNames().size() == 1 - && missingBlock.getNames().contains(((ComponentSimpleResponseType) pairwiseInnerComponent).getResponse().getName()))); - assertTrue(missingBlocks.stream().anyMatch(missingBlock -> - missingBlock.getMissingName().equals(((ComponentSimpleResponseType) pairwiseInnerComponent).getResponse().getName()) - && missingBlock.getNames().size() == 1 - && missingBlock.getNames().contains(pairwiseInnerComponent.getMissingResponse().getName()))); + ComponentType pairwiseInnerComponent = pairwiseComponents.getFirst(); + MissingType missingType = lunaticQuestionnaire.getMissingBlock(); + + String pairwiseMissingResponseName = pairwiseInnerComponent.getMissingResponse().getName(); + String pairwiseResponseName = ((ComponentSimpleResponseType) pairwiseInnerComponent).getResponse().getName(); + // Missing block + assertNotNull(missingType.getMissingEntry(pairwiseMissingResponseName)); + assertEquals(1, missingType.getMissingEntry(pairwiseMissingResponseName).getCorrespondingVariables().size()); + assertEquals(pairwiseResponseName, missingType.getMissingEntry(pairwiseMissingResponseName).getCorrespondingVariables().getFirst()); + // Reverse missing block + assertNotNull(missingType.getMissingEntry(pairwiseResponseName)); + assertEquals(1, missingType.getMissingEntry(pairwiseResponseName).getCorrespondingVariables().size()); + assertEquals(pairwiseMissingResponseName, missingType.getMissingEntry(pairwiseResponseName).getCorrespondingVariables().getFirst()); } @Test void whenCheckboxGroupQuestionsGenerateCorrectMissingBlocks() { // Given: questionnaire with simple response components - lunaticQuestionnaire = new Questionnaire(); + Questionnaire lunaticQuestionnaire = new Questionnaire(); List components = lunaticQuestionnaire.getComponents(); components.add(checkboxGroup); + // When processing.apply(lunaticQuestionnaire); + // Then - List missingBlocks = lunaticQuestionnaire.getMissingBlock().getAny().stream() - .map(MissingBlock.class::cast).toList(); + MissingType missingType = lunaticQuestionnaire.getMissingBlock(); + // List responseNames = checkboxGroup.getResponses().stream() .map(responsesCheckboxGroup -> responsesCheckboxGroup.getResponse().getName()) .toList(); String missingResponseName = checkboxGroup.getMissingResponse().getName(); - // - assertTrue(missingBlocks.stream() - .anyMatch(missingBlock -> missingBlock.getMissingName().equals(missingResponseName) - && missingBlock.getNames().size() == responseNames.size() - && missingBlock.getNames().containsAll(responseNames))); - responseNames.forEach(responseName -> - assertTrue(missingBlocks.stream() - .anyMatch(missingBlock -> missingBlock.getMissingName().equals(responseName) - && missingBlock.getNames().size() == 1 - && missingBlock.getNames().contains(missingResponseName)))); + // Missing block + assertNotNull(missingType.getMissingEntry(missingResponseName)); + assertEquals(responseNames.size(), missingType.getMissingEntry(missingResponseName).getCorrespondingVariables().size()); + assertTrue(missingType.getMissingEntry(missingResponseName).getCorrespondingVariables().containsAll(responseNames)); + // Reverse missing block + responseNames.forEach(responseName -> { + assertNotNull(missingType.getMissingEntry(responseName)); + assertEquals(1, missingType.getMissingEntry(responseName).getCorrespondingVariables().size()); + assertEquals(missingResponseName, missingType.getMissingEntry(responseName).getCorrespondingVariables().getFirst()); + }); } private CheckboxGroup buildCheckboxGroup(String id, List names) { diff --git a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizingTest.java b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizingTest.java index fdf998252..78046c491 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizingTest.java +++ b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticAddResizingTest.java @@ -4,26 +4,25 @@ import fr.insee.eno.core.exceptions.business.DDIParsingException; import fr.insee.eno.core.mappers.LunaticMapper; import fr.insee.eno.core.model.EnoQuestionnaire; -import fr.insee.eno.core.model.lunatic.LunaticResizingEntry; -import fr.insee.eno.core.model.lunatic.LunaticResizingPairwiseEntry; import fr.insee.eno.core.parameter.EnoParameters; import fr.insee.eno.core.processing.out.steps.lunatic.LunaticLoopResolution; import fr.insee.eno.core.processing.out.steps.lunatic.LunaticSortComponents; import fr.insee.eno.core.processing.out.steps.lunatic.table.LunaticTableProcessing; import fr.insee.lunatic.model.flat.Questionnaire; +import fr.insee.lunatic.model.flat.ResizingPairwiseEntry; +import fr.insee.lunatic.model.flat.ResizingType; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.List; -import java.util.Optional; -import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; class LunaticAddResizingTest { - private static List resizingList; + private static ResizingType resizingType; @BeforeAll static void init() throws DDIParsingException { @@ -44,54 +43,34 @@ static void init() throws DDIParsingException { new LunaticAddResizing(enoQuestionnaire).apply(lunaticQuestionnaire); // Then - resizingList = lunaticQuestionnaire.getResizing().getAny(); + resizingType = lunaticQuestionnaire.getResizing(); // -> tests } @Test - void resizingListIsNotEmpty() { - assertFalse(resizingList.isEmpty()); + void resizingEntriesCount() { + assertEquals(3, resizingType.countResizingEntries()); } @Test void loopResizing() { - List resizingEntries = resizingList.stream() - .filter(LunaticResizingEntry.class::isInstance) - .map(LunaticResizingEntry.class::cast) - .toList(); // - assertEquals(2, resizingEntries.size()); + assertEquals("nvl(NUMBER, 1)", resizingType.getResizingEntry("NUMBER").getSize()); + assertThat(resizingType.getResizingEntry("NUMBER").getVariables()) + .containsExactlyInAnyOrderElementsOf(List.of("Q2", "PAIRWISE_SOURCE")); // - Optional loopResizingEntry = resizingEntries.stream() - .filter(resizingEntry -> "NUMBER".equals(resizingEntry.getName())) - .findAny(); - assertTrue(loopResizingEntry.isPresent()); - assertEquals("nvl(NUMBER, 1)", loopResizingEntry.get().getSize()); - assertThat(loopResizingEntry.get().getVariables()).containsExactlyInAnyOrderElementsOf( - List.of("Q2", "PAIRWISE_SOURCE")); - // - Optional linkedLoopResizingEntry = resizingEntries.stream() - .filter(resizingEntry -> "Q2".equals(resizingEntry.getName())) - .findAny(); - assertTrue(linkedLoopResizingEntry.isPresent()); - assertEquals("count(Q2)", linkedLoopResizingEntry.get().getSize()); - assertEquals(Set.of("Q3"), linkedLoopResizingEntry.get().getVariables()); + assertEquals("count(Q2)", resizingType.getResizingEntry("Q2").getSize()); + assertEquals(List.of("Q3"), resizingType.getResizingEntry("Q2").getVariables()); } @Test void pairwiseResizing() { - List resizingPairwiseEntries = resizingList.stream() - .filter(LunaticResizingPairwiseEntry.class::isInstance) - .map(LunaticResizingPairwiseEntry.class::cast) - .toList(); - // - assertEquals(1, resizingPairwiseEntries.size()); // - LunaticResizingPairwiseEntry pairwiseResizingEntry = resizingPairwiseEntries.get(0); - assertEquals("PAIRWISE_SOURCE", pairwiseResizingEntry.getName()); + ResizingPairwiseEntry resizingPairwiseEntry = assertInstanceOf(ResizingPairwiseEntry.class, + resizingType.getResizingEntry("PAIRWISE_SOURCE")); assertEquals(List.of("count(PAIRWISE_SOURCE)", "count(PAIRWISE_SOURCE)"), - pairwiseResizingEntry.getSizeForLinksVariables()); - assertEquals(Set.of("LINKS"), pairwiseResizingEntry.getLinksVariables()); + resizingPairwiseEntry.getSizeForLinksVariables()); + assertEquals(List.of("LINKS"), resizingPairwiseEntry.getLinksVariables()); } } diff --git a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogicTest.java b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogicTest.java index b65c12f3b..59157e877 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogicTest.java +++ b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticLoopResizingLogicTest.java @@ -3,7 +3,6 @@ import fr.insee.eno.core.model.EnoQuestionnaire; import fr.insee.eno.core.model.calculated.BindingReference; import fr.insee.eno.core.model.calculated.CalculatedExpression; -import fr.insee.eno.core.model.lunatic.LunaticResizingEntry; import fr.insee.eno.core.model.navigation.LinkedLoop; import fr.insee.eno.core.model.navigation.StandaloneLoop; import fr.insee.eno.core.model.question.BooleanQuestion; @@ -16,7 +15,6 @@ import org.junit.jupiter.api.Test; import java.util.List; -import java.util.Optional; import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; @@ -26,6 +24,7 @@ class LunaticLoopResizingLogicTest { private Questionnaire lunaticQuestionnaire; + private ResizingType lunaticResizing; private EnoQuestionnaire enoQuestionnaire; private EnoIndex enoIndex; private Loop lunaticLoop; @@ -35,6 +34,7 @@ class LunaticLoopResizingLogicTest { void resizingUnitTestsCanvas() { // lunaticQuestionnaire = new Questionnaire(); + lunaticResizing = new ResizingType(); // lunaticLoop = new Loop(); lunaticLoop.setId("loop-id"); @@ -95,13 +95,12 @@ void oneComponentInLoop_testResizingEntryNameAndSize() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertEquals(1, resizingEntries.size()); - assertEquals("LOOP_SIZE_VAR", resizingEntries.get(0).getName()); - assertEquals("nvl(LOOP_SIZE_VAR, 1)", resizingEntries.get(0).getSize()); - assertEquals(Set.of("RESPONSE_VAR"), resizingEntries.get(0).getVariables()); + assertEquals(1, lunaticResizing.countResizingEntries()); + assertEquals("nvl(LOOP_SIZE_VAR, 1)", lunaticResizing.getResizingEntry("LOOP_SIZE_VAR").getSize()); + assertEquals(List.of("RESPONSE_VAR"), lunaticResizing.getResizingEntry("LOOP_SIZE_VAR").getVariables()); } @Test @@ -153,11 +152,12 @@ void simpleResponseComponents_resizedVariables() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertThat(resizingEntries.get(0).getVariables()).containsExactlyInAnyOrderElementsOf( - Set.of("BOOLEAN_VAR", "INPUT_VAR", "TEXT_VAR", "NUMBER_VAR", "DATE_VAR", + assertThat(lunaticResizing.getResizingEntry("LOOP_SIZE_VAR").getVariables()) + .containsExactlyInAnyOrderElementsOf(Set.of( + "BOOLEAN_VAR", "INPUT_VAR", "TEXT_VAR", "NUMBER_VAR", "DATE_VAR", "DROPDOWN_VAR", "RADIO_VAR", "CHECKBOX_VAR")); } @@ -178,11 +178,11 @@ void checkboxGroupComponent_resizedVariables() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertThat(resizingEntries.get(0).getVariables()).containsExactlyInAnyOrderElementsOf( - List.of("RESPONSE_VAR1", "RESPONSE_VAR2")); + assertThat(lunaticResizing.getResizingEntry("LOOP_SIZE_VAR").getVariables()) + .containsExactlyInAnyOrderElementsOf(List.of("RESPONSE_VAR1", "RESPONSE_VAR2")); } @Test @@ -209,11 +209,11 @@ void tableComponent_resizedVariables() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertThat(resizingEntries.get(0).getVariables()).containsExactlyInAnyOrderElementsOf( - Set.of("CELL11", "CELL12", "CELL21", "CELL22")); + assertThat(lunaticResizing.getResizingEntry("LOOP_SIZE_VAR").getVariables()) + .containsExactlyInAnyOrderElementsOf(Set.of("CELL11", "CELL12", "CELL21", "CELL22")); } @Test @@ -233,17 +233,15 @@ void twoVariablesInLoopExpression_twoResizingEntries() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertEquals(2, resizingEntries.size()); + assertEquals(2, lunaticResizing.countResizingEntries()); // - assertThat(resizingEntries.stream().map(LunaticResizingEntry::getName).toList()) - .containsExactlyInAnyOrderElementsOf(List.of("LOOP_SIZE_VAR", "LOOP_SIZE_VAR2")); - // - resizingEntries.forEach(resizingEntry -> { + List.of("LOOP_SIZE_VAR", "LOOP_SIZE_VAR2").forEach(resizingVariableName -> { + ResizingEntry resizingEntry = lunaticResizing.getResizingEntry(resizingVariableName); assertEquals("LOOP_SIZE_VAR + LOOP_SIZE_VAR2", resizingEntry.getSize()); - assertEquals(Set.of("RESPONSE_VAR"), resizingEntry.getVariables()); + assertEquals(List.of("RESPONSE_VAR"), resizingEntry.getVariables()); }); } @@ -255,9 +253,9 @@ void loopFixedMaxSize_noResizingEntries() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertTrue(resizingEntries.isEmpty()); + assertEquals(0, lunaticResizing.countResizingEntries()); } @Test @@ -276,9 +274,9 @@ void loopFixedMaxSize_withExternal_noResizingEntries() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertTrue(resizingEntries.isEmpty()); + assertEquals(0, lunaticResizing.countResizingEntries()); } @Test @@ -310,13 +308,12 @@ void externalVariableInLoopExpression_shouldHaveResizingEntriesForCollectedDepen // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertEquals(1, resizingEntries.size()); - assertEquals("COLLECTED_VAR", resizingEntries.get(0).getName()); - assertEquals("CALCULATED_VAR", resizingEntries.get(0).getSize()); - assertEquals(Set.of("RESPONSE_VAR"), resizingEntries.get(0).getVariables()); + assertEquals(1, lunaticResizing.countResizingEntries()); + assertEquals("CALCULATED_VAR", lunaticResizing.getResizingEntry("COLLECTED_VAR").getSize()); + assertEquals(List.of("RESPONSE_VAR"), lunaticResizing.getResizingEntry("COLLECTED_VAR").getVariables()); } @Test @@ -336,13 +333,12 @@ void calculatedVariableInLoopExpression_shouldNotAddResizingEntries() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLoop); + loopResizingLogic.buildResizingEntries(lunaticLoop, lunaticResizing); // Then - assertEquals(1, resizingEntries.size()); - assertEquals("LOOP_SIZE_VAR", resizingEntries.get(0).getName()); - assertEquals("LOOP_SIZE_VAR + EXTERNAL_VAR", resizingEntries.get(0).getSize()); - assertEquals(Set.of("RESPONSE_VAR"), resizingEntries.get(0).getVariables()); + assertEquals(1, lunaticResizing.countResizingEntries()); + assertEquals("LOOP_SIZE_VAR + EXTERNAL_VAR", lunaticResizing.getResizingEntry("LOOP_SIZE_VAR").getSize()); + assertEquals(List.of("RESPONSE_VAR"), lunaticResizing.getResizingEntry("LOOP_SIZE_VAR").getVariables()); } @Test @@ -371,14 +367,10 @@ void resizing_linkedLoop() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLinkedLoop); + loopResizingLogic.buildResizingEntries(lunaticLinkedLoop, lunaticResizing); // Then - Optional searchedResizingEntry = resizingEntries.stream() - .filter(resizingEntry -> "RESPONSE_VAR".equals(resizingEntry.getName())) - .findAny(); - assertTrue(searchedResizingEntry.isPresent()); - LunaticResizingEntry linkedLoopResizingEntry = searchedResizingEntry.get(); + ResizingEntry linkedLoopResizingEntry = lunaticResizing.getResizingEntry("RESPONSE_VAR"); assertEquals("count(RESPONSE_VAR)", linkedLoopResizingEntry.getSize()); assertEquals(1, linkedLoopResizingEntry.getVariables().size()); assertTrue(linkedLoopResizingEntry.getVariables().contains("LINKED_LOOP_RESPONSE")); @@ -418,14 +410,10 @@ void resizing_linkedLoop_basedOnDynamicTable() { // When LunaticLoopResizingLogic loopResizingLogic = new LunaticLoopResizingLogic( lunaticQuestionnaire, enoQuestionnaire, enoIndex); - List resizingEntries = loopResizingLogic.buildResizingEntries(lunaticLinkedLoop); + loopResizingLogic.buildResizingEntries(lunaticLinkedLoop, lunaticResizing); // Then - Optional searchedResizingEntry = resizingEntries.stream() - .filter(resizingEntry -> "TABLE_RESPONSE1".equals(resizingEntry.getName())) - .findAny(); - assertTrue(searchedResizingEntry.isPresent()); - LunaticResizingEntry linkedLoopResizingEntry = searchedResizingEntry.get(); + ResizingEntry linkedLoopResizingEntry = lunaticResizing.getResizingEntry("TABLE_RESPONSE1"); assertEquals("count(TABLE_RESPONSE1)", linkedLoopResizingEntry.getSize()); assertEquals(1, linkedLoopResizingEntry.getVariables().size()); assertTrue(linkedLoopResizingEntry.getVariables().contains("LINKED_LOOP_RESPONSE")); diff --git a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogicTest.java b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogicTest.java index 50d9a157f..2facaf86f 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogicTest.java +++ b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/resizing/LunaticPairwiseResizingLogicTest.java @@ -1,6 +1,5 @@ package fr.insee.eno.core.processing.out.steps.lunatic.resizing; -import fr.insee.eno.core.model.lunatic.LunaticResizingPairwiseEntry; import fr.insee.eno.core.model.question.PairwiseQuestion; import fr.insee.eno.core.reference.EnoIndex; import fr.insee.lunatic.model.flat.*; @@ -8,8 +7,8 @@ import java.util.List; -import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; class LunaticPairwiseResizingLogicTest { @@ -17,6 +16,7 @@ class LunaticPairwiseResizingLogicTest { void pairwiseResizingTest() { // Questionnaire lunaticQuestionnaire = new Questionnaire(); + ResizingType resizingType = new ResizingType(); // PairwiseLinks lunaticPairwise = new PairwiseLinks(); lunaticPairwise.setId("pairwise-id"); @@ -48,15 +48,14 @@ void pairwiseResizingTest() { // When LunaticPairwiseResizingLogic pairwiseResizingLogic = new LunaticPairwiseResizingLogic( lunaticQuestionnaire, enoIndex); - List pairwiseResizingEntries = pairwiseResizingLogic - .buildPairwiseResizingEntries(lunaticPairwise); + pairwiseResizingLogic.buildPairwiseResizingEntries(lunaticPairwise, resizingType); // Test - assertEquals(1, pairwiseResizingEntries.size()); - assertEquals("LOOP_VAR", pairwiseResizingEntries.get(0).getName()); - assertEquals(List.of("count(LOOP_VAR)", "count(LOOP_VAR)"), pairwiseResizingEntries.get(0).getSizeForLinksVariables()); - assertThat(pairwiseResizingEntries.get(0).getLinksVariables()) - .containsExactlyInAnyOrderElementsOf(List.of("LINKS_VAR")); + assertEquals(1, resizingType.countResizingEntries()); + ResizingPairwiseEntry resizingPairwiseEntry = assertInstanceOf(ResizingPairwiseEntry.class, + resizingType.getResizingEntry("LOOP_VAR")); + assertEquals(List.of("count(LOOP_VAR)", "count(LOOP_VAR)"), resizingPairwiseEntry.getSizeForLinksVariables()); + assertEquals(List.of("LINKS_VAR"), resizingPairwiseEntry.getLinksVariables()); } } diff --git a/eno-core/src/test/java/fr/insee/eno/core/sandbox/MockitoTests.java b/eno-core/src/test/java/fr/insee/eno/core/sandbox/MockitoTests.java deleted file mode 100644 index 4b191992a..000000000 --- a/eno-core/src/test/java/fr/insee/eno/core/sandbox/MockitoTests.java +++ /dev/null @@ -1,20 +0,0 @@ -package fr.insee.eno.core.sandbox; - -import fr.insee.eno.core.model.EnoQuestionnaire; -import fr.insee.eno.core.reference.EnoIndex; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -class MockitoTests { - - @Test - void helloMockito() { - String someId = "foo-id"; - EnoIndex enoIndex = Mockito.mock(EnoIndex.class); - Mockito.when(enoIndex.get(someId)).thenReturn(new EnoQuestionnaire()); - assertTrue(enoIndex.get(someId) instanceof EnoQuestionnaire); - } - -} diff --git a/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerCleaningTest.java b/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerCleaningTest.java deleted file mode 100644 index 052abdcbe..000000000 --- a/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerCleaningTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package fr.insee.eno.core.serialize; - -import fr.insee.eno.core.exceptions.business.LunaticSerializationException; -import fr.insee.eno.core.model.lunatic.CleaningConcernedVariable; -import fr.insee.eno.core.model.lunatic.CleaningEntry; -import fr.insee.lunatic.model.flat.CleaningType; -import fr.insee.lunatic.model.flat.Questionnaire; -import org.json.JSONException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.skyscreamer.jsonassert.JSONAssert; -import org.skyscreamer.jsonassert.JSONCompareMode; - -import java.util.List; - -class LunaticSerializerCleaningTest { - private Questionnaire lunaticQuestionnaire; - - - @BeforeEach - void init() { - lunaticQuestionnaire = new Questionnaire(); - CleaningType cleaningType = new CleaningType(); - lunaticQuestionnaire.setCleaning(cleaningType); - - CleaningConcernedVariable concernedVariable1 = new CleaningConcernedVariable("CONCERNED_VARIABLE1", "filter1"); - CleaningConcernedVariable concernedVariable2 = new CleaningConcernedVariable("CONCERNED_VARIABLE2", "filter2"); - CleaningConcernedVariable concernedVariable3 = new CleaningConcernedVariable("CONCERNED_VARIABLE3", "filter3"); - CleaningConcernedVariable concernedVariable4 = new CleaningConcernedVariable("CONCERNED_VARIABLE4", "filter4"); - CleaningConcernedVariable concernedVariable5 = new CleaningConcernedVariable("CONCERNED_VARIABLE5", "filter5"); - CleaningConcernedVariable concernedVariable6 = new CleaningConcernedVariable("CONCERNED_VARIABLE6", "filter6"); - - List data = cleaningType.getAny(); - data.add(new CleaningEntry("SOME_VARIABLE1", List.of(concernedVariable1, concernedVariable2))); - data.add(new CleaningEntry("SOME_VARIABLE2", List.of(concernedVariable3, concernedVariable4))); - data.add(new CleaningEntry("SOME_VARIABLE3", List.of(concernedVariable5, concernedVariable6))); - - cleaningType.getAny().addAll(data); - } - - @Test - void whenTransformingToJsonCleaningBlockIsCorrect() throws LunaticSerializationException, JSONException { - String questionnaireJson = LunaticSerializer.serializeToJson(lunaticQuestionnaire); - String expectedJson = """ - { - "cleaning": { - "SOME_VARIABLE1": { - "CONCERNED_VARIABLE1": "filter1", - "CONCERNED_VARIABLE2": "filter2" - }, - "SOME_VARIABLE2": { - "CONCERNED_VARIABLE3": "filter3", - "CONCERNED_VARIABLE4": "filter4" - }, - "SOME_VARIABLE3": { - "CONCERNED_VARIABLE5": "filter5", - "CONCERNED_VARIABLE6": "filter6" - } - } - }"""; - - JSONAssert.assertEquals(expectedJson,questionnaireJson, JSONCompareMode.STRICT); - } -} \ No newline at end of file diff --git a/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerMissingBlockTest.java b/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerMissingBlockTest.java deleted file mode 100644 index 00f25ffa0..000000000 --- a/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerMissingBlockTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package fr.insee.eno.core.serialize; - -import fr.insee.eno.core.exceptions.business.LunaticSerializationException; -import fr.insee.eno.core.model.lunatic.MissingBlock; -import fr.insee.lunatic.model.flat.MissingType; -import fr.insee.lunatic.model.flat.Questionnaire; -import org.json.JSONException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.skyscreamer.jsonassert.JSONAssert; -import org.skyscreamer.jsonassert.JSONCompareMode; - -import java.util.List; - -class LunaticSerializerMissingBlockTest { - private Questionnaire lunaticQuestionnaire; - - - @BeforeEach - void init() { - lunaticQuestionnaire = new Questionnaire(); - MissingType missingType = new MissingType(); - lunaticQuestionnaire.setMissingBlock(missingType); - - List data = missingType.getAny(); - data.add(new MissingBlock("SOME_QUESTION_MISSING", List.of("SOME_QUESTION"))); - data.add(new MissingBlock("SOME_QUESTION", List.of("SOME_QUESTION_MISSING"))); - data.add(new MissingBlock("SOME_TABLE_MISSING", List.of("TABLE_VAR1", "TABLE_VAR2", "TABLE_VAR3"))); - data.add(new MissingBlock("TABLE_VAR1", List.of("SOME_TABLE_MISSING"))); - data.add(new MissingBlock("TABLE_VAR2", List.of("SOME_TABLE_MISSING"))); - data.add(new MissingBlock("TABLE_VAR3", List.of("SOME_TABLE_MISSING"))); - } - - @Test - void whenTransformingToJsonMissingBlockIsCorrect() throws LunaticSerializationException, JSONException { - String questionnaireJson = LunaticSerializer.serializeToJson(lunaticQuestionnaire); - - String expectedJson = """ - { - "missingBlock":{ - "SOME_QUESTION_MISSING":[ - "SOME_QUESTION" - ], - "SOME_QUESTION":[ - "SOME_QUESTION_MISSING" - ], - "SOME_TABLE_MISSING":[ - "TABLE_VAR1", - "TABLE_VAR2", - "TABLE_VAR3" - ], - "TABLE_VAR1":[ - "SOME_TABLE_MISSING" - ], - "TABLE_VAR2":[ - "SOME_TABLE_MISSING" - ], - "TABLE_VAR3":[ - "SOME_TABLE_MISSING" - ] - } - }"""; - - JSONAssert.assertEquals(expectedJson,questionnaireJson, JSONCompareMode.STRICT); - } -} diff --git a/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerResizingTest.java b/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerResizingTest.java deleted file mode 100644 index 9c1190659..000000000 --- a/eno-core/src/test/java/fr/insee/eno/core/serialize/LunaticSerializerResizingTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package fr.insee.eno.core.serialize; - -import fr.insee.eno.core.exceptions.business.LunaticSerializationException; -import fr.insee.eno.core.model.lunatic.LunaticResizingEntry; -import fr.insee.eno.core.model.lunatic.LunaticResizingPairwiseEntry; -import fr.insee.lunatic.model.flat.Questionnaire; -import fr.insee.lunatic.model.flat.ResizingType; -import org.json.JSONException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.skyscreamer.jsonassert.JSONAssert; -import org.skyscreamer.jsonassert.JSONCompareMode; - -import java.util.List; - -class LunaticSerializerResizingTest { - private Questionnaire lunaticQuestionnaire; - - @BeforeEach - void init() { - lunaticQuestionnaire = new Questionnaire(); - ResizingType resizingType = new ResizingType(); - lunaticQuestionnaire.setResizing(resizingType); - - List data = resizingType.getAny(); - data.add(new LunaticResizingEntry("loopVariable", "count(NB)", List.of("NB"))); - data.add(new LunaticResizingEntry("loopVariable1", "count(NB1,PRENOM1)", List.of("NB1", "PRENOM1"))); - data.add(new LunaticResizingPairwiseEntry("pairwise", List.of("count(NBP)", "count(PRENOMP)"), List.of("NBP", "PRENOMP"))); - data.add(new LunaticResizingPairwiseEntry("pairwise1", List.of("count(NBP1)", "count(PRENOMP1)"), List.of("NBP1", "PRENOMP1"))); - } - - @Test - void whenTransformingToJsonResizingIsCorrect() throws LunaticSerializationException, JSONException { - String questionnaireJson = LunaticSerializer.serializeToJson(lunaticQuestionnaire); - - String expectedJson = """ - { - "resizing": { - "loopVariable": { "size": "count(NB)", "variables": ["NB"] }, - "loopVariable1": { - "size": "count(NB1,PRENOM1)", - "variables": ["NB1", "PRENOM1"] - }, - "pairwise": { - "linksVariables": ["NBP", "PRENOMP"], - "sizeForLinksVariables": ["count(NBP)", "count(PRENOMP)"] - }, - "pairwise1": { - "linksVariables": ["NBP1", "PRENOMP1"], - "sizeForLinksVariables": ["count(NBP1)", "count(PRENOMP1)"] - } - } - } - """; - JSONAssert.assertEquals(expectedJson, questionnaireJson, JSONCompareMode.STRICT); - } -} diff --git a/eno-core/src/test/resources/integration/lunatic/lunatic-dynamic-table-2.json b/eno-core/src/test/resources/integration/lunatic/lunatic-dynamic-table-2.json index a42c397ed..7e94a91ec 100644 --- a/eno-core/src/test/resources/integration/lunatic/lunatic-dynamic-table-2.json +++ b/eno-core/src/test/resources/integration/lunatic/lunatic-dynamic-table-2.json @@ -137,9 +137,6 @@ "id": "kfxmjupm-CI-0" } ], - "bindingDependencies": [ - "TABLEAUBASIQUE1" - ], "response": { "name": "TABLEAUBASIQUE1" }, @@ -184,9 +181,6 @@ }, { "componentType": "Radio", - "bindingDependencies": [ - "TABLEAUBASIQUE2" - ], "response": { "name": "TABLEAUBASIQUE2" }, @@ -209,10 +203,6 @@ "id": "lrf3d3rk-QOP-lrf3paxv" } ], - "bindingDependencies": [ - "TABLEAUBASIQUE1", - "TABLEAUBASIQUE2" - ], "positioning": "HORIZONTAL", "hierarchy": { "sequence": { @@ -259,60 +249,6 @@ } }, "mandatory": false - }, - { - "componentType": "Sequence", - "hierarchy": { - "sequence": { - "id": "COMMENT-SEQ", - "page": "3", - "label": { - "type": "VTL|MD", - "value": "\"Commentaire\"" - } - } - }, - "conditionFilter": { - "type": "VTL", - "value": "true" - }, - "id": "COMMENT-SEQ", - "page": "3", - "label": { - "type": "VTL|MD", - "value": "\"Commentaire\"" - } - }, - { - "componentType": "Textarea", - "bindingDependencies": [ - "COMMENT_QE" - ], - "response": { - "name": "COMMENT_QE" - }, - "hierarchy": { - "sequence": { - "id": "COMMENT-SEQ", - "page": "3", - "label": { - "type": "VTL|MD", - "value": "\"Commentaire\"" - } - } - }, - "conditionFilter": { - "type": "VTL", - "value": "true" - }, - "id": "COMMENT-QUESTION", - "page": "4", - "label": { - "type": "VTL|MD", - "value": "\"Avez-vous des remarques concernant l'enquĂȘte ou des commentaires ?\"" - }, - "mandatory": false, - "maxLength": 2000 } ], "pagination": "question", @@ -327,5 +263,5 @@ "generatingDate": "15-01-2024 15:38:29", "missing": false, "id": "lrf3chy8", - "maxPage": "4" + "maxPage": "2" } \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index e1ea8c708..c3b7c924d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,6 +8,7 @@ pluginManagement { dependencyResolutionManagement { repositories { + mavenLocal() mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots" @@ -15,12 +16,11 @@ dependencyResolutionManagement { snapshotsOnly() } } - mavenLocal() } versionCatalogs { libs { - version('lunatic-model', '3.2.7') + version('lunatic-model', '3.3.4') version('pogues-model', '1.0.5') library('lunatic-model', 'fr.insee.lunatic', 'lunatic-model').versionRef('lunatic-model') library('pogues-model', 'fr.insee.pogues', 'pogues-model').versionRef('pogues-model')