diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index ae4a389d..e5ddf0e8 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -21,13 +21,27 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Set up JDK 21 uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' cache: maven - - name: Build with Maven + + - name: Clone BPM + uses: actions/checkout@master + with: + repository: InseeFr/BPM + path: bpm + + - name: Build BPM + run: | + cd bpm + mvn clean install + cd .. + + - name: Build Genesis with Maven run: mvn -B package --file pom.xml # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive diff --git a/pom.xml b/pom.xml index 0030c15a..5926584b 100644 --- a/pom.xml +++ b/pom.xml @@ -131,6 +131,13 @@ junit-vintage-engine test + + + + fr.insee.bpm + bpm + 1.0.0 + diff --git a/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java b/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java index 296ce529..e56c7793 100644 --- a/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java +++ b/src/main/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapter.java @@ -1,11 +1,16 @@ package fr.insee.genesis.controller.adapter; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; import fr.insee.genesis.controller.sources.xml.LunaticXmlCollectedData; import fr.insee.genesis.controller.sources.xml.LunaticXmlSurveyUnit; import fr.insee.genesis.controller.sources.xml.ValueType; import fr.insee.genesis.controller.utils.LoopIdentifier; -import fr.insee.genesis.domain.dtos.*; + +import fr.insee.genesis.domain.dtos.CollectedVariableDto; +import fr.insee.genesis.domain.dtos.DataState; +import fr.insee.genesis.domain.dtos.Mode; +import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto; +import fr.insee.genesis.domain.dtos.VariableDto; +import fr.insee.bpm.metadata.model.VariablesMap; import lombok.experimental.UtilityClass; import java.time.LocalDateTime; diff --git a/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java b/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java index 992d45e5..4c941e96 100644 --- a/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java +++ b/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java @@ -5,8 +5,6 @@ import fr.insee.genesis.controller.responses.SurveyUnitUpdateSimplified; import fr.insee.genesis.controller.service.SurveyUnitQualityService; import fr.insee.genesis.controller.service.VolumetryLogService; -import fr.insee.genesis.controller.sources.ddi.DDIReader; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; import fr.insee.genesis.controller.sources.xml.LunaticXmlCampaign; import fr.insee.genesis.controller.sources.xml.LunaticXmlDataParser; import fr.insee.genesis.controller.sources.xml.LunaticXmlDataSequentialParser; @@ -25,11 +23,16 @@ import fr.insee.genesis.exceptions.GenesisException; import fr.insee.genesis.exceptions.NoDataError; import fr.insee.genesis.infrastructure.utils.FileUtils; + +import fr.insee.bpm.exceptions.MetadataParserException; +import fr.insee.bpm.metadata.model.VariablesMap; +import fr.insee.bpm.metadata.reader.ddi.DDIReader; + import io.swagger.v3.oas.annotations.Operation; import lombok.extern.slf4j.Slf4j; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.DeleteMapping; @@ -87,9 +90,11 @@ public ResponseEntity saveResponsesFromXmlFile(@RequestParam("pathLunati Path ddiFilePath = Paths.get(ddiFile); VariablesMap variablesMap; try { - variablesMap = DDIReader.getVariablesFromDDI(ddiFilePath.toFile().toURI().toURL()); - } catch (GenesisException e) { - return ResponseEntity.status(e.getStatus()).body(e.getMessage()); + variablesMap = + DDIReader.getMetadataFromDDI(ddiFilePath.toFile().toURI().toURL().toString(), + new FileInputStream(ddiFilePath.toFile())).getVariables(); + } catch (MetadataParserException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); } log.info(String.format("Try to read Xml file : %s", xmlFile)); @@ -340,7 +345,8 @@ private void treatCampaignWithMode(String campaignName, Mode mode, List modalities = new ArrayList<>(); - - - /** Common name of the different modalities of the UCQ variable, if this variable is itself a modality of a Mcq variable. */ - @Getter - @Setter - String mcqName; - - public UcqVariable(String name, Group group, VariableType type) { - super(name, group, type); - if (type != VariableType.STRING) { - log.warn(String.format("%s type given when creating UCQ \"%s\"", type, name)); - log.warn("Type of a UCQ variable should be STRING."); - } - } - - public UcqVariable(String name, Group group, VariableType type, String variableLength) { - super(name, group, type, variableLength); - if (type != VariableType.STRING) { - log.warn(String.format("%s type given when creating UCQ \"%s\"", type, name)); - log.warn("Type of a UCQ variable should be STRING."); - } - } - - /** Add a modality using the object. */ - public void addModality(UcqModality modality) { - modalities.add(modality); - } - - /** - * Method to register a modality of the UCQ. - * - * @param value A possible value of the variable. - * @param text The text associated with the value. - */ - public void addModality(String value, String text) { - modalities.add(new UcqModality(value, text)); - } - - /** - * Method to register a modality of the UCQ. - * - * @param value A possible value of the UCQ variable. - * @param text The text associated with the value. - * @param variableName The name of the variable associated with the modality - * (when carrying out paper data). - */ - public void addModality(String value, String text, String variableName) { - modalities.add(new UcqModality(value, text, variableName)); - } - - /** Return the possible values of the variable. */ - public Set getValues() { - return modalities.stream().map(ucqModality -> ucqModality.value).collect(Collectors.toSet()); - } - - /** Return the names of eventual variables associated with modalities. */ - public Set getModalityNames() { - return modalities.stream().map(ucqModality -> ucqModality.variableName).collect(Collectors.toSet()); - } - - /** - * Get a modality from its associated value. - * - * @param value One of the possible values of the variable. - * @return The modality object that corresponds to this value. If there is no - * modality with this value, a null is returned. If several modalities - * correspond to the value, the first modality that corresponds is - * returned. - */ - public UcqModality getModalityFromValue(String value) { - List res = modalities.stream().filter(ucqModality -> ucqModality.getValue().equals(value)) - .toList(); - if (res.size() == 1) { - return res.get(0); - } else if (res.isEmpty()) { - log.debug(String.format("Value \"%s\" not registered in UCQ named \"%s\".", value, name)); - return null; - } else { - log.debug(String.format("Several modalities with value \"%s\" are registered in UCQ named \"%s\".", value, - name)); - return res.get(0); - } - } - - /** - * Get a modality from its name (that may be filled when carrying out paper - * data). - * - * @param modalityName The variable name of a modality of the UCQ. - * @return The modality object that corresponds to this name. If there is no - * modality with this name, a null is returned. If several modalities - * correspond to the name, the first modality that corresponds is - * returned. - */ - public UcqModality getModalityFromName(String modalityName) { - List res = modalities.stream() - .filter(ucqModality -> ucqModality.getVariableName().equals(modalityName)).toList(); - if (res.size() == 1) { - return res.get(0); - } else if (res.isEmpty()) { - log.debug(String.format("Modality name \"%s\" not registered in UCQ named \"%s\".", modalityName, name)); - return null; - } else { - log.debug(String.format("Several modality variables named \"%s\" are registered in UCQ named \"%s\".", - modalityName, name)); - return res.get(0); - } - } -} diff --git a/src/main/java/fr/insee/genesis/controller/sources/ddi/Variable.java b/src/main/java/fr/insee/genesis/controller/sources/ddi/Variable.java deleted file mode 100644 index 5fab6aca..00000000 --- a/src/main/java/fr/insee/genesis/controller/sources/ddi/Variable.java +++ /dev/null @@ -1,72 +0,0 @@ -package fr.insee.genesis.controller.sources.ddi; - -import lombok.Getter; -import lombok.Setter; - -/** - * Object class to represent a variable. - * - */ -public class Variable { - - /** Variable name. */ - @Getter - protected String name; - - /** Group reference */ - @Getter - protected Group group; - - /** Variable type from the enum class (STRING, INTEGER, DATE, ...) */ - @Getter - protected VariableType type; - - /** Format for SAS script import */ - @Getter - protected String sasFormat; - - /** Maximum length received in input for the variable. */ - @Getter - @Setter - protected int maxLengthData; - - /** Name of the item used to collect the answer. */ - @Getter - @Setter - protected String questionItemName; - - /** Identifies if the variable is part a question grid */ - @Getter - @Setter - protected boolean isInQuestionGrid; - - public Variable(String name, Group group, VariableType type) { - this.name = name; - this.group = group; - this.type = type; - } - - public Variable(String name, Group group, VariableType type, String sasFormat) { - this.name = name; - this.group = group; - this.type = type; - this.sasFormat = sasFormat; - } - - public String getGroupName() { - return group.getName(); - } - - public int getExpectedLength(){ - if (this.sasFormat != null && this.sasFormat.contains(".")){ - String[] sasFormatPart = this.sasFormat.split("\\."); - return Integer.parseInt(sasFormatPart[0]); - } - if (this.sasFormat != null){ - return Integer.parseInt(this.sasFormat); - } - // Not sure about that return - return 1; - } - -} diff --git a/src/main/java/fr/insee/genesis/controller/sources/ddi/VariableType.java b/src/main/java/fr/insee/genesis/controller/sources/ddi/VariableType.java deleted file mode 100644 index 7a5584bc..00000000 --- a/src/main/java/fr/insee/genesis/controller/sources/ddi/VariableType.java +++ /dev/null @@ -1,53 +0,0 @@ -package fr.insee.genesis.controller.sources.ddi; - -import lombok.Getter; -import lombok.extern.log4j.Log4j2; - -import java.util.Date; - -/** - * Enum class for variable types. - * - * */ -@Log4j2 -@Getter -public enum VariableType { - STRING ("character","character", "STRING"), - INTEGER ("integer","integer", "INTEGER"), - NUMBER ("number","numeric", "NUMBER"), - BOOLEAN ("logical","logical", "BOOLEAN"), - DATE ("Date","Date", "STRING"); - - private String dataTableType; - private String formatR; - private String vtlType; - - VariableType(String dataTableType, String formatR, String vtlType) { - this.dataTableType =dataTableType; - this.formatR=formatR; - this.vtlType=vtlType; - } - - public static VariableType getTypeFromJavaClass(Class clazz){ - // memo: https://www.w3schools.com/java/java_data_types.asp - if(clazz.isAssignableFrom(Float.class) - || clazz.isAssignableFrom(Double.class)){ - return NUMBER; - } else if (clazz.isAssignableFrom(Integer.class) - || clazz.isAssignableFrom(Long.class) - || clazz.isAssignableFrom(Short.class)){ - return INTEGER; - } else if (clazz.isAssignableFrom(String.class) - || clazz.isAssignableFrom(Character.class)){ - return STRING; - } else if (clazz.isAssignableFrom(Boolean.class)){ - return BOOLEAN; - } else if (clazz.isAssignableFrom(Date.class)){ - return DATE; - } else { - log.warn(String.format("Unrecognized type for class %s ", clazz)); - return null; - } - } - -} diff --git a/src/main/java/fr/insee/genesis/controller/sources/ddi/VariablesMap.java b/src/main/java/fr/insee/genesis/controller/sources/ddi/VariablesMap.java deleted file mode 100644 index 623ca8b2..00000000 --- a/src/main/java/fr/insee/genesis/controller/sources/ddi/VariablesMap.java +++ /dev/null @@ -1,253 +0,0 @@ -package fr.insee.genesis.controller.sources.ddi; - - -import fr.insee.genesis.Constants; -import lombok.Getter; -import lombok.extern.log4j.Log4j2; - -import java.util.*; -import java.util.stream.Collectors; - -/** - * Object class to represent a set of variables. - * Contains a flat map and its structured equivalent. - */ -@Log4j2 -public class VariablesMap { - - /** Map containing the variables. - * Keys: a variable name. - * Values: Variable. */ - @Getter - protected final LinkedHashMap variables = new LinkedHashMap<>(); - - /** Map containing the groups. - * Keys: group name. - * Values: Group. */ - protected final LinkedHashMap groups = new LinkedHashMap<>(); - - /** The root group is created when creating a VariablesMap instance. */ - public VariablesMap() { - groups.put(Constants.ROOT_GROUP_NAME, new Group(Constants.ROOT_GROUP_NAME)); - } - - /** Register a variable in the map. */ - public void putVariable(Variable variable) { - variables.put(variable.getName(), variable); - } - /** Remove the variable with given name from the map. */ - public void removeVariable(String name){ - if (variables.get(name) != null) { - variables.remove(name); - } else { - log.debug(String.format("Variable named \"%s\" is not in the variables map", name)); - } - } - - /** Return the variable with given name. */ - public Variable getVariable(String variableName){ - Variable variable = this.variables.get(variableName); - if(variable == null) { - log.debug(String.format("Variable named \"%s\" is unknown", variableName)); - } - return variable; - } - - /** Return the names of all variables in the map. */ - public Set getVariableNames() { - return variables.keySet(); - } - - public Set getDistinctVariableNamesAndFullyQualifiedNames(){ - Set set = new HashSet<>(); - set.addAll(getFullyQualifiedNames()); - set.addAll(getVariableNames()); - return set; - } - - /** Register a group in the map. */ - public void putGroup(Group group) { - groups.put(group.getName(), group); - } - - /** Return the group with given name. */ - public Group getGroup(String groupName) { - return groups.get(groupName); - } - - /** Return the root group. */ - public Group getRootGroup() { - if (! groups.containsKey(Constants.ROOT_GROUP_NAME)) { - log.debug("Root group not in the variables map."); - } - return groups.get(Constants.ROOT_GROUP_NAME); - } - - /** Return the name of all groups registered in the map, including the root group. */ - public List getGroupNames() { - return new ArrayList<>(groups.keySet()); - } - - /** Return the names of all groups registered in the map, except the root group. */ - public List getSubGroupNames() { - return groups.keySet() - .stream().filter(name -> ! groups.get(name).isRoot()) - .toList(); - } - - /** Return the number of groups in the map (including the root group). */ - public int getGroupsCount() { - return groups.size(); - } - - /** Identifiers are not represented by Variable objects, they are: - * - the root identifier (fixed value), - * - each subgroup name is also an identifier name. - * @return The list of all identifiers associated to the variables map. */ - public List getIdentifierNames() { - List res = new ArrayList<>(List.of(Constants.ROOT_IDENTIFIER_NAME)); - res.addAll(getSubGroupNames()); - return res; - } - - /** Return true if there is a variable under the given name. */ - public boolean hasVariable(String variableName) { - return variables.containsKey(variableName); - } - - /** Return true if there is a group under the given name. */ - public boolean hasGroup(String groupName) { - return groups.containsKey(groupName); - } - - /** Return false is there is only the root group. */ - public boolean hasSubGroups() { - int size = groups.size(); - if (size < 1) { - log.debug("No groups in this variables map. Should have at least the root group."); - } - return size > 1; - } - - /** Return the fully qualified name of a variable, that is - * - the name of the variable if it is in the root group. - * - the variable name prefixed with its group and parent group names, otherwise. - * - * In the second case, the separator use is defined by Constants.METADATA_SEPARATOR. */ - public String getFullyQualifiedName(String variableName) { - if (this.hasVariable(variableName)) { - - /* done using StringBuilder, maybe concatenate a list of strings is better - https://stackoverflow.com/a/523913/13425151 */ - - StringBuilder res = new StringBuilder(variableName); - Variable variable = variables.get(variableName); - Group group = variable.getGroup(); - while(! group.isRoot()) { - res.insert(0, group.getName() + Constants.METADATA_SEPARATOR); - group = groups.get(group.getParentName()); - } - return res.toString(); - } - else { - log.debug(String.format( "Trying to get fully qualified name for unknown variable \"%s\". null returned.", - variableName)); - return null; - } - } - - /** Return the fully qualified names of all variables in the map. */ - public Set getFullyQualifiedNames() { - return variables.keySet().stream().map(this::getFullyQualifiedName).collect(Collectors.toSet()); - } - - /** Return the variables names that belongs to the group. */ - public Set getGroupVariableNames(String groupName) { - return variables.keySet() - .stream().filter(name -> variables.get(name).getGroupName().equals(groupName)) - .collect(Collectors.toSet()); - } - - public List getGroupVariableNamesAsList(String groupName) { - return variables.keySet() - .stream().filter(name -> variables.get(name).getGroupName().equals(groupName)) - .toList(); - } - - /** Return true if there is a McqVariable that has the given question name in its mcqName attribute. */ - public boolean hasMcq(String questionName) { - return variables.values().stream() - .filter(McqVariable.class::isInstance) - .anyMatch(mcqVariable -> // (FILTER_RESULT variables are upper case) - ((McqVariable) mcqVariable).getQuestionItemName().equals(questionName) - || ((McqVariable) mcqVariable).getQuestionItemName().equalsIgnoreCase(questionName)); - } - - public Group getMcqGroup(String questionName) { - return variables.values().stream() - .filter(McqVariable.class::isInstance) - .filter(mcqVariable -> ((McqVariable) mcqVariable).getQuestionItemName().equals(questionName)) - .map(Variable::getGroup) - .findFirst().orElse(null); - } - - /** Return true if there is a variable from a question grid that has the given question name in its questionName attribute. */ - public boolean isInQuestionGrid(String questionName){ - return variables.values().stream() - .filter(Variable::isInQuestionGrid) - .anyMatch(variable -> // (FILTER_RESULT variables are upper case) - (variable.getQuestionItemName().equals(questionName) - || variable.getQuestionItemName().equalsIgnoreCase(questionName))); - } - - public Group getQuestionGridGroup(String questionName) { - return variables.values().stream() - .filter(Variable::isInQuestionGrid) - .filter(variable -> variable.getQuestionItemName().equals(questionName)) - .map(Variable::getGroup) - .findFirst().orElse(null); - } - - /** Return true if there is a UCQ variable with the name given. */ - public boolean hasUcq(String variableName) { - Variable variable = variables.get(variableName); - if (variable != null) { - return variable instanceof UcqVariable; - } - return false; - } - - /** Return true if there is a UCQ variable depending on a MCQ variable with the name given. */ - public boolean hasUcqMcq(String variableName) { - Variable variable = variables.get(variableName); - if (variable != null) { - return variable instanceof UcqVariable ucqVariable && !ucqVariable.getQuestionItemName().isEmpty(); - } - return false; - } - /** Return the list of all UCQ variables registered in the map. */ - public List getUcqVariables() { - return variables.values().stream() - .filter(UcqVariable.class::isInstance) - .map(UcqVariable.class::cast) - .toList(); - } - /** Return the list of all names of UCQ variables registered in the map. */ - public List getUcqVariablesNames() { - return variables.values().stream() - .filter(UcqVariable.class::isInstance) - .map(ucqVariable -> ((UcqVariable) ucqVariable).getQuestionItemName()) - .filter(Objects::nonNull) - .distinct() - .toList(); - } - /** Return the list of all names of MCQ variables registered in the map. */ - public List getMcqVariablesNames() { - return variables.values().stream() - .filter(McqVariable.class::isInstance) - .map(mcqVariable -> ((McqVariable) mcqVariable).getQuestionItemName()) - .filter(Objects::nonNull) - .distinct() - .toList(); - } -} diff --git a/src/main/java/fr/insee/genesis/controller/utils/DataVerifier.java b/src/main/java/fr/insee/genesis/controller/utils/DataVerifier.java index 11cee5b6..f7828a9f 100644 --- a/src/main/java/fr/insee/genesis/controller/utils/DataVerifier.java +++ b/src/main/java/fr/insee/genesis/controller/utils/DataVerifier.java @@ -1,13 +1,13 @@ package fr.insee.genesis.controller.utils; import fr.insee.genesis.Constants; -import fr.insee.genesis.controller.sources.ddi.Variable; -import fr.insee.genesis.controller.sources.ddi.VariableType; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; import fr.insee.genesis.domain.dtos.CollectedVariableDto; import fr.insee.genesis.domain.dtos.DataState; import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto; import fr.insee.genesis.domain.dtos.VariableDto; +import fr.insee.bpm.metadata.model.Variable; +import fr.insee.bpm.metadata.model.VariableType; +import fr.insee.bpm.metadata.model.VariablesMap; import lombok.extern.slf4j.Slf4j; import java.time.LocalDateTime; diff --git a/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java b/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java index c6ddff51..4f35fcea 100644 --- a/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java +++ b/src/main/java/fr/insee/genesis/controller/utils/LoopIdentifier.java @@ -1,8 +1,8 @@ package fr.insee.genesis.controller.utils; import fr.insee.genesis.Constants; -import fr.insee.genesis.controller.sources.ddi.Variable; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; +import fr.insee.bpm.metadata.model.Variable; +import fr.insee.bpm.metadata.model.VariablesMap; import lombok.extern.slf4j.Slf4j; import java.util.Arrays; diff --git a/src/test/java/cucumber/functional_tests/MainDefinitions.java b/src/test/java/cucumber/functional_tests/MainDefinitions.java index 03a11bc9..3464aeaa 100644 --- a/src/test/java/cucumber/functional_tests/MainDefinitions.java +++ b/src/test/java/cucumber/functional_tests/MainDefinitions.java @@ -3,13 +3,14 @@ import cucumber.TestConstants; import fr.insee.genesis.controller.adapter.LunaticXmlAdapter; import fr.insee.genesis.controller.service.SurveyUnitQualityService; -import fr.insee.genesis.controller.sources.ddi.DDIReader; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; import fr.insee.genesis.controller.sources.xml.LunaticXmlCampaign; import fr.insee.genesis.controller.sources.xml.LunaticXmlDataParser; import fr.insee.genesis.controller.sources.xml.LunaticXmlSurveyUnit; import fr.insee.genesis.domain.dtos.*; import fr.insee.genesis.exceptions.GenesisException; +import fr.insee.bpm.exceptions.MetadataParserException; +import fr.insee.bpm.metadata.model.VariablesMap; +import fr.insee.bpm.metadata.reader.ddi.DDIReader; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; @@ -17,6 +18,7 @@ import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; +import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; @@ -40,7 +42,7 @@ public void init(String directory){ } @When("We create DTOs from file {string} with DDI {string}") - public void get_dtos(String fileName, String DDIName) throws IOException, ParserConfigurationException, SAXException, GenesisException { + public void get_dtos(String fileName, String DDIName) throws IOException, ParserConfigurationException, SAXException, GenesisException, MetadataParserException { Path filePath = inDirectory.resolve(fileName); Path ddiFilePath = ddiDirectory.resolve(directory).resolve(DDIName); @@ -48,7 +50,10 @@ public void get_dtos(String fileName, String DDIName) throws IOException, Parser LunaticXmlDataParser parser = new LunaticXmlDataParser(); LunaticXmlCampaign campaign; campaign = parser.parseDataFile(filePath); - VariablesMap variablesMap = DDIReader.getVariablesFromDDI(ddiFilePath.toFile().toURI().toURL()); + VariablesMap variablesMap = DDIReader.getMetadataFromDDI( + ddiFilePath.toFile().toURI().toURL().toString(), + new FileInputStream(ddiFilePath.toFile()) + ).getVariables(); List suDtos = new ArrayList<>(); for (LunaticXmlSurveyUnit su : campaign.getSurveyUnits()) { suDtos.addAll(LunaticXmlAdapter.convert(su, variablesMap, campaign.getIdCampaign(), Mode.WEB)); diff --git a/src/test/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapterTest.java b/src/test/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapterTest.java index c893cb67..d30e2b9b 100644 --- a/src/test/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapterTest.java +++ b/src/test/java/fr/insee/genesis/controller/adapter/LunaticXmlAdapterTest.java @@ -1,15 +1,18 @@ package fr.insee.genesis.controller.adapter; + import fr.insee.genesis.Constants; -import fr.insee.genesis.controller.sources.ddi.Group; -import fr.insee.genesis.controller.sources.ddi.Variable; -import fr.insee.genesis.controller.sources.ddi.VariableType; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; import fr.insee.genesis.controller.sources.xml.*; import fr.insee.genesis.domain.dtos.CollectedVariableDto; import fr.insee.genesis.domain.dtos.DataState; import fr.insee.genesis.domain.dtos.Mode; import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto; + +import fr.insee.bpm.metadata.model.Group; +import fr.insee.bpm.metadata.model.MetadataModel; +import fr.insee.bpm.metadata.model.Variable; +import fr.insee.bpm.metadata.model.VariableType; + import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -29,8 +32,8 @@ class LunaticXmlAdapterTest { LunaticXmlSurveyUnit lunaticXmlSurveyUnit5 = new LunaticXmlSurveyUnit(); LunaticXmlSurveyUnit lunaticXmlSurveyUnit6 = new LunaticXmlSurveyUnit(); LunaticXmlSurveyUnit lunaticXmlSurveyUnit7 = new LunaticXmlSurveyUnit(); + MetadataModel metadataModel = new MetadataModel(); LunaticXmlSurveyUnit lunaticXmlSurveyUnit8 = new LunaticXmlSurveyUnit(); - VariablesMap variablesMap = new VariablesMap(); @BeforeEach void setUp() { @@ -189,16 +192,16 @@ void setUp() { //VariablesMap Group group = new Group(LOOP_NAME, Constants.ROOT_GROUP_NAME); Variable var1 = new Variable("var1", group, VariableType.STRING, "1"); - Variable var2 = new Variable("var2", variablesMap.getRootGroup(), VariableType.STRING, "1"); - variablesMap.putVariable(var1); - variablesMap.putVariable(var2); + Variable var2 = new Variable("var2", metadataModel.getRootGroup(), VariableType.STRING, "1"); + metadataModel.getVariables().putVariable(var1); + metadataModel.getVariables().putVariable(var2); } @Test @DisplayName("SurveyUnitUpdateDto should not be null") void test01() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).isNotNull().isNotEmpty(); } @@ -207,7 +210,7 @@ void test01() { @DisplayName("SurveyUnitUpdateDto should have the right idQuest") void test02() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos.get(0).getIdQuest()).isEqualTo("idQuest1"); } @@ -216,7 +219,7 @@ void test02() { @DisplayName("SurveyUnitUpdateDto should have the right id") void test03() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos.get(0).getIdUE()).isEqualTo("idUE1"); } @@ -225,7 +228,7 @@ void test03() { @DisplayName("SurveyUnitUpdateDto should contains 4 variable state updates") void test04() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit1, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos.get(0).getCollectedVariables()).hasSize(4); } @@ -234,7 +237,7 @@ void test04() { @DisplayName("There should be a EDITED DTO with EDITED data") void test05() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit2, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit2, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).hasSize(2); Assertions.assertThat(suDtos).filteredOn(surveyUnitUpdateDto -> @@ -256,7 +259,7 @@ void test05() { @DisplayName("There should be both EDITED DTO and FORCED DTO if there is EDITED and FORCED data") void test06() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit3, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit3, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).hasSize(3); Assertions.assertThat(suDtos).filteredOn(surveyUnitUpdateDto -> @@ -271,7 +274,7 @@ void test06() { @DisplayName("There should be a EDITED DTO and PREVIOUS DTO if there is EDITED and PREVIOUS data") void test07() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit4, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit4, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).hasSize(3); Assertions.assertThat(suDtos).filteredOn(surveyUnitUpdateDto -> @@ -286,7 +289,7 @@ void test07() { @DisplayName("There should be multiple DTOs if there is different data states (all 4)") void test08() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit5, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit5, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).hasSize(4); Assertions.assertThat(suDtos).filteredOn(surveyUnitUpdateDto -> @@ -304,7 +307,7 @@ void test08() { @DisplayName("If a variable not present in DDI then he is in the root group") void test09() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit6, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit6, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).hasSize(1); @@ -321,7 +324,7 @@ void test09() { @DisplayName("If a variable A not present in DDI and is the extension of a known variable B, then the variable A has B as related and is in the same group") void test10() { // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit7, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit7, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).hasSize(1); @@ -338,7 +341,7 @@ void test10() { @DisplayName("Value should be affected in the good loop iteration") void test11(){ // When - List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit8, variablesMap, ID_CAMPAIGN, Mode.WEB); + List suDtos = LunaticXmlAdapter.convert(lunaticXmlSurveyUnit8, metadataModel.getVariables(), ID_CAMPAIGN, Mode.WEB); // Then Assertions.assertThat(suDtos).hasSize(1); Assertions.assertThat(suDtos.get(0).getCollectedVariables()).hasSize(3); diff --git a/src/test/java/fr/insee/genesis/controller/sources/ddi/DDIReaderTest.java b/src/test/java/fr/insee/genesis/controller/sources/ddi/DDIReaderTest.java deleted file mode 100644 index 8451c72d..00000000 --- a/src/test/java/fr/insee/genesis/controller/sources/ddi/DDIReaderTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package fr.insee.genesis.controller.sources.ddi; - -import fr.insee.genesis.Constants; -import fr.insee.genesis.exceptions.GenesisException; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.net.MalformedURLException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Set; - -class DDIReaderTest { - - static VariablesMap simpsonsVariables; - - @BeforeAll - static void setUp() throws MalformedURLException, GenesisException { - String DDI_FILE= "src/test/resources/ddi/simpson2305.xml"; - Path ddiFilePath = Paths.get(DDI_FILE); - simpsonsVariables = DDIReader.getVariablesFromDDI(ddiFilePath.toFile().toURI().toURL()); - } - @Test - void variablesShouldNotBeNull(){ - Assertions.assertThat(simpsonsVariables).isNotNull(); - } - - @Test - void checkGroups(){ - Assertions.assertThat(simpsonsVariables.hasGroup("FAVOURITE_CHAR")).isTrue(); - Assertions.assertThat(simpsonsVariables.hasGroup(Constants.ROOT_GROUP_NAME)).isTrue(); - Assertions.assertThat(simpsonsVariables.hasGroup("m1")).isFalse(); - } - - @Test - void expectedVariablesShouldBePresent(){ - Set expectedVariables = Set.of( - "FAVOURITE_CHAR1", "FAVOURITE_CHAR2","FAVOURITE_CHAR3","FAVOURITE_CHAR33CL", - "SUM_EXPENSES", "LAST_BROADCAST", "COMMENT", "READY", "PRODUCER", "SEASON_NUMBER", "DATEFIRST", - "CITY", "MAYOR", "STATE", "PET1", "PET4", "PETOCL", "ICE_FLAVOUR1", "ICE_FLAVOUR4", "ICE_FLAVOUROTCL", - "NUCLEAR_CHARACTER1", "NUCLEAR_CHARACTER4", "BIRTH_CHARACTER1", "BIRTH_CHARACTER5", - "PERCENTAGE_EXPENSES11", "PERCENTAGE_EXPENSES71", - "LAST_FOOD_SHOPPING11", "LAST_FOOD_SHOPPING813CL","CLOWNING11", "CLOWNING43", "TRAVEL11", "TRAVEL46", - "FEELCHAREV1", "FEELCHAREV4","LEAVDURATION11", "LEAVDURATION52", "NB_CHAR", - "SURVEY_COMMENT"); - - for (String variableName : expectedVariables) { - Assertions.assertThat(simpsonsVariables.hasVariable(variableName)).isTrue(); - } - } - - @Test - void variablesShouldBeInExpectedGroup() { - Assertions.assertThat(simpsonsVariables.getVariable("SUM_EXPENSES").getGroup().getName()).isEqualTo("FAVOURITE_CHAR"); - Assertions.assertThat(simpsonsVariables.getVariable("SURVEY_COMMENT").getGroup().getName()).isEqualTo(Constants.ROOT_GROUP_NAME); - Assertions.assertThat(simpsonsVariables.getVariable("FAVOURITE_CHAR1").getGroup().getName()).isEqualTo("FAVOURITE_CHAR"); - Assertions.assertThat(simpsonsVariables.getVariable("FAVOURITE_CHAR33CL").getGroup().getName()).isEqualTo("FAVOURITE_CHAR"); - } - -} diff --git a/src/test/java/fr/insee/genesis/controller/sources/ddi/VariablesMapTest.java b/src/test/java/fr/insee/genesis/controller/sources/ddi/VariablesMapTest.java deleted file mode 100644 index 981f3ee2..00000000 --- a/src/test/java/fr/insee/genesis/controller/sources/ddi/VariablesMapTest.java +++ /dev/null @@ -1,314 +0,0 @@ -package fr.insee.genesis.controller.sources.ddi; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.List; -import java.util.Set; - -import fr.insee.genesis.Constants; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import lombok.extern.log4j.Log4j2; - -@Log4j2 -public class VariablesMapTest { - - private final static String LOOP1_NAME = "INDIVIDUALS_LOOP"; - private final static String LOOP2_NAME = "CARS_LOOP"; - private final static String ADDRESS_VARIABLE_NAME = "ADDRESS"; - private final static String HOUSEHOLD_INCOME_VARIABLE_NAME = "HOUSEHOLD_INCOME"; - private final static String FIRST_NAME_VARIABLE_NAME = "FIRST_NAME"; - private final static String LAST_NAME_VARIABLE_NAME = "LAST_NAME"; - private final static String GENDER_VARIABLE_NAME = "GENDER"; - private final static String CAR_COLOR_VARIABLE_NAME = "CAR_COLOR"; - private final static String VEHICLE_OWNER_VARIABLE_NAME = "VEHICLE_OWNER"; - private final static String CAR_OWNER_VARIABLE_NAME = "CAR_OWNER"; - private final static String MOTO_OWNER_VARIABLE_NAME = "MOTO_OWNER"; - private final static String SEX_VARIABLE_NAME = "SEXE"; - - private final static String DUMMY_VARIABLE_NAME = "DUMMY"; - private final static String UNKNOWN_QUESTION_VARIABLE_NAME = "UNKNOWN_QUESTION"; - - private final static String RELATIONSHIP_QUESTION_ITEM = "RELATIONSHIP"; - - - private VariablesMap variablesMap; - - @BeforeEach - public void createTestVariablesMap() { - variablesMap = new VariablesMap(); - - - Group rootGroup = variablesMap.getRootGroup(); - Group individualsGroup = new Group(LOOP1_NAME, Constants.ROOT_GROUP_NAME); - Group carsGroup = new Group(LOOP2_NAME, LOOP1_NAME); - - variablesMap.putGroup(individualsGroup); - variablesMap.putGroup(carsGroup); - - variablesMap.putVariable( - new Variable(ADDRESS_VARIABLE_NAME, rootGroup, VariableType.STRING)); - variablesMap.putVariable( - new Variable(HOUSEHOLD_INCOME_VARIABLE_NAME, rootGroup, VariableType.NUMBER)); - variablesMap.putVariable( - new Variable(FIRST_NAME_VARIABLE_NAME, individualsGroup, VariableType.STRING)); - variablesMap.putVariable( - new Variable(LAST_NAME_VARIABLE_NAME, individualsGroup, VariableType.STRING)); - variablesMap.putVariable( - new Variable(GENDER_VARIABLE_NAME, individualsGroup, VariableType.STRING)); - variablesMap.putVariable( - new Variable(CAR_COLOR_VARIABLE_NAME, carsGroup, VariableType.STRING)); - } - - @Test - void testRootGroup() { - assertTrue(variablesMap.hasGroup(Constants.ROOT_GROUP_NAME)); - assertEquals(Constants.ROOT_GROUP_NAME, variablesMap.getRootGroup().getName()); - assertNull(variablesMap.getRootGroup().getParentName()); - } - - @Test - void testGetVariableByName() { - - // Get - Variable rootVariable = variablesMap.getVariable(HOUSEHOLD_INCOME_VARIABLE_NAME); - Variable group1Variable = variablesMap.getVariable(FIRST_NAME_VARIABLE_NAME); - Variable group2Variable = variablesMap.getVariable(CAR_COLOR_VARIABLE_NAME); - - // Get a variable that does not exist - log.debug("Trying to get a variable that does not exist in a test function, " + - "a second message should pop in the log."); - Variable dummyVariable = variablesMap.getVariable(DUMMY_VARIABLE_NAME); - - // - assertEquals(HOUSEHOLD_INCOME_VARIABLE_NAME, rootVariable.getName()); - assertEquals(FIRST_NAME_VARIABLE_NAME, group1Variable.getName()); - assertEquals(CAR_COLOR_VARIABLE_NAME, group2Variable.getName()); - assertNull(dummyVariable); - } - - @Test - void testRemoveAndHasVariable() { - - // Remove - variablesMap.removeVariable(HOUSEHOLD_INCOME_VARIABLE_NAME); - variablesMap.removeVariable(CAR_COLOR_VARIABLE_NAME); - - // Remove a variable that does not exist - log.debug("Trying to remove a variable that does not exist in a test function, " + - "a second message should pop in the log."); - variablesMap.removeVariable("FOO"); - - // - assertFalse(variablesMap.hasVariable(HOUSEHOLD_INCOME_VARIABLE_NAME)); - assertTrue(variablesMap.hasVariable(FIRST_NAME_VARIABLE_NAME)); - assertFalse(variablesMap.hasVariable(CAR_COLOR_VARIABLE_NAME)); - } - - @Test - void getIdentifierNamesTest() { - assertEquals( - List.of(Constants.ROOT_IDENTIFIER_NAME, LOOP1_NAME, LOOP2_NAME), - variablesMap.getIdentifierNames() - ); - } - - @Test - void getFullyQualifiedNameTest() { - assertEquals(HOUSEHOLD_INCOME_VARIABLE_NAME, - variablesMap.getFullyQualifiedName(HOUSEHOLD_INCOME_VARIABLE_NAME)); - assertEquals("INDIVIDUALS_LOOP.FIRST_NAME", - variablesMap.getFullyQualifiedName(FIRST_NAME_VARIABLE_NAME)); - assertEquals("INDIVIDUALS_LOOP.CARS_LOOP.CAR_COLOR", - variablesMap.getFullyQualifiedName(CAR_COLOR_VARIABLE_NAME)); - } - - @Test - void testGetGroupVariableNames() { - assertTrue(variablesMap.getGroupVariableNames(Constants.ROOT_GROUP_NAME) - .containsAll(Set.of(ADDRESS_VARIABLE_NAME, HOUSEHOLD_INCOME_VARIABLE_NAME))); - assertTrue(variablesMap.getGroupVariableNames(LOOP1_NAME) - .containsAll(Set.of(FIRST_NAME_VARIABLE_NAME, LAST_NAME_VARIABLE_NAME, GENDER_VARIABLE_NAME))); - assertTrue(variablesMap.getGroupVariableNames(LOOP2_NAME) - .contains(CAR_COLOR_VARIABLE_NAME)); - } - - @Test - void testMcqMethods() { - // - Group group = variablesMap.getGroup(LOOP1_NAME); - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_A").group(group).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Spouse").build()); - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_B").group(group).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Child").build()); - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_C").group(group).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Parent").build()); - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_D").group(group).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Other").build()); - // - assertTrue(variablesMap.hasMcq(RELATIONSHIP_QUESTION_ITEM)); - assertSame(RELATIONSHIP_QUESTION_ITEM, variablesMap.getVariable(RELATIONSHIP_QUESTION_ITEM + "_A").getQuestionItemName()); - assertFalse(variablesMap.hasMcq(ADDRESS_VARIABLE_NAME)); - assertFalse(variablesMap.hasMcq(FIRST_NAME_VARIABLE_NAME)); - assertFalse(variablesMap.hasMcq(CAR_COLOR_VARIABLE_NAME)); - assertFalse(variablesMap.hasMcq(UNKNOWN_QUESTION_VARIABLE_NAME)); - // - assertSame(group, variablesMap.getMcqGroup(RELATIONSHIP_QUESTION_ITEM)); - assertNull(variablesMap.getMcqGroup(ADDRESS_VARIABLE_NAME)); - assertNull(variablesMap.getMcqGroup(FIRST_NAME_VARIABLE_NAME)); - assertNull(variablesMap.getMcqGroup(CAR_COLOR_VARIABLE_NAME)); - assertNull(variablesMap.getMcqGroup(UNKNOWN_QUESTION_VARIABLE_NAME)); - } - - @Test - void testGetVariablesNames() { - variablesMap = createCompleteFakeVariablesMap(); - // KSE et KGA à trouver, une par liste - List ucqMcqVariablesNames = variablesMap.getUcqVariablesNames(); - List mcqVariablesNames = variablesMap.getMcqVariablesNames(); - Set variablesNames = variablesMap.getVariableNames(); - // Check ucq - assertTrue(ucqMcqVariablesNames.contains(VEHICLE_OWNER_VARIABLE_NAME)); - assertFalse(ucqMcqVariablesNames.contains(CAR_OWNER_VARIABLE_NAME)); - // Check mcq - assertFalse(mcqVariablesNames.contains(VEHICLE_OWNER_VARIABLE_NAME)); - assertTrue(mcqVariablesNames.contains(RELATIONSHIP_QUESTION_ITEM)); - assertFalse(mcqVariablesNames.contains(RELATIONSHIP_QUESTION_ITEM + "_A")); - // Check mcq - assertFalse(variablesNames.contains(VEHICLE_OWNER_VARIABLE_NAME)); - assertTrue(variablesNames.contains(CAR_OWNER_VARIABLE_NAME)); - assertTrue(variablesNames.contains(MOTO_OWNER_VARIABLE_NAME)); - - assertTrue(variablesMap.hasMcq(RELATIONSHIP_QUESTION_ITEM)); - assertTrue(variablesMap.hasUcq(CAR_OWNER_VARIABLE_NAME)); - assertTrue(variablesMap.hasUcqMcq(CAR_OWNER_VARIABLE_NAME)); - assertFalse(variablesMap.hasUcqMcq(VEHICLE_OWNER_VARIABLE_NAME)); - assertFalse(variablesMap.hasMcq(ADDRESS_VARIABLE_NAME)); - assertFalse(variablesMap.hasMcq(FIRST_NAME_VARIABLE_NAME)); - assertFalse(variablesMap.hasMcq(CAR_COLOR_VARIABLE_NAME)); - assertFalse(variablesMap.hasMcq(UNKNOWN_QUESTION_VARIABLE_NAME)); - } - - /* Variables map objects to test multimode management */ - - /** - * Return a VariablesMap object containing variables named as follows: - * - FIRST_NAME, LAST_NAME, AGE at the root - * - CAR_COLOR in a group named CARS_LOOP - */ - public static VariablesMap createCompleteFakeVariablesMap(){ - - VariablesMap variablesMap = new VariablesMap(); - - // Groups - Group rootGroup = variablesMap.getRootGroup(); - Group carsGroup = new Group(LOOP2_NAME, Constants.ROOT_GROUP_NAME); - variablesMap.putGroup(carsGroup); - - // Variables - variablesMap.putVariable(new Variable(LAST_NAME_VARIABLE_NAME, rootGroup, VariableType.STRING, "20")); - variablesMap.putVariable(new Variable(FIRST_NAME_VARIABLE_NAME, rootGroup, VariableType.STRING, "50")); - variablesMap.putVariable(new Variable("AGE", rootGroup, VariableType.INTEGER, "50")); - variablesMap.putVariable(new Variable(CAR_COLOR_VARIABLE_NAME, carsGroup, VariableType.STRING, "50")); - - // unique choice question variable - UcqVariable ucq = new UcqVariable(SEX_VARIABLE_NAME, rootGroup, VariableType.STRING, "50"); - ucq.addModality("1", "Male"); - ucq.addModality("2", "Female"); - variablesMap.putVariable(ucq); - - // unique choice question variable related to multiple choices question - UcqVariable ucqMcq1 = new UcqVariable(CAR_OWNER_VARIABLE_NAME, rootGroup, VariableType.STRING, "50"); - ucqMcq1.setQuestionItemName(VEHICLE_OWNER_VARIABLE_NAME); - ucqMcq1.addModality("1", "Yes"); - ucqMcq1.addModality("2", "No"); - UcqVariable ucqMcq2 = new UcqVariable(MOTO_OWNER_VARIABLE_NAME, rootGroup, VariableType.STRING, "50"); - ucqMcq2.setQuestionItemName(VEHICLE_OWNER_VARIABLE_NAME); - ucqMcq2.addModality("1", "Yes"); - ucqMcq2.addModality("2", "No"); - variablesMap.putVariable(ucqMcq1); - variablesMap.putVariable(ucqMcq2); - - // multiple choices question variable - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_A").group(rootGroup).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Spouse").build()); - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_B").group(rootGroup).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Child").build()); - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_C").group(rootGroup).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Parent").build()); - variablesMap.putVariable(McqVariable.builder() - .name(RELATIONSHIP_QUESTION_ITEM + "_D").group(rootGroup).questionItemName(RELATIONSHIP_QUESTION_ITEM).text("Other").build()); - - return variablesMap; - } - - public static VariablesMap createAnotherFakeVariablesMap(){ - - VariablesMap variablesMap = new VariablesMap(); - - // Groups - Group rootGroup = variablesMap.getRootGroup(); - Group carsGroup = new Group(LOOP2_NAME, Constants.ROOT_GROUP_NAME); - variablesMap.putGroup(carsGroup); - - // Variables - variablesMap.putVariable(new Variable(LAST_NAME_VARIABLE_NAME, rootGroup, VariableType.STRING, "50")); - variablesMap.putVariable(new Variable(FIRST_NAME_VARIABLE_NAME, rootGroup, VariableType.STRING, "20")); - variablesMap.putVariable(new Variable(ADDRESS_VARIABLE_NAME, rootGroup, VariableType.STRING, "50")); - variablesMap.putVariable(new Variable(CAR_COLOR_VARIABLE_NAME, carsGroup, VariableType.STRING, "500")); - - return variablesMap; - } - - /* Variables map objects to test information levels management */ - - public static VariablesMap createVariablesMap_rootOnly() { - VariablesMap variablesMap = new VariablesMap(); - - Group rootGroup = variablesMap.getRootGroup(); - - variablesMap.putGroup(rootGroup); - - variablesMap.putVariable( - new Variable(ADDRESS_VARIABLE_NAME, rootGroup, VariableType.STRING)); - variablesMap.putVariable( - new Variable(HOUSEHOLD_INCOME_VARIABLE_NAME, rootGroup, VariableType.NUMBER)); - - return variablesMap; - } - - public static VariablesMap createVariablesMap_oneLevel() { - VariablesMap variablesMap = createVariablesMap_rootOnly(); - - Group individualsGroup = new Group(LOOP1_NAME, Constants.ROOT_GROUP_NAME); - - variablesMap.putGroup(individualsGroup); - - variablesMap.putVariable( - new Variable(FIRST_NAME_VARIABLE_NAME, individualsGroup, VariableType.STRING)); - variablesMap.putVariable( - new Variable(LAST_NAME_VARIABLE_NAME, individualsGroup, VariableType.STRING)); - variablesMap.putVariable( - new Variable(GENDER_VARIABLE_NAME, individualsGroup, VariableType.STRING)); - - return variablesMap; - } - - public static VariablesMap createVariablesMap_twoLevels() { - VariablesMap variablesMap = createVariablesMap_oneLevel(); - - Group carsGroup = new Group(LOOP2_NAME, LOOP1_NAME); - - variablesMap.putGroup(carsGroup); - - variablesMap.putVariable( - new Variable(CAR_COLOR_VARIABLE_NAME, carsGroup, VariableType.STRING)); - - return variablesMap; - } - -} diff --git a/src/test/java/fr/insee/genesis/controller/utils/DataVerifierTest.java b/src/test/java/fr/insee/genesis/controller/utils/DataVerifierTest.java index 327d8de2..a1a6cfd8 100644 --- a/src/test/java/fr/insee/genesis/controller/utils/DataVerifierTest.java +++ b/src/test/java/fr/insee/genesis/controller/utils/DataVerifierTest.java @@ -1,11 +1,12 @@ package fr.insee.genesis.controller.utils; -import fr.insee.genesis.controller.sources.ddi.Variable; -import fr.insee.genesis.controller.sources.ddi.VariableType; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; import fr.insee.genesis.domain.dtos.*; import fr.insee.genesis.domain.dtos.VariableDto; +import fr.insee.bpm.metadata.model.MetadataModel; +import fr.insee.bpm.metadata.model.Variable; +import fr.insee.bpm.metadata.model.VariableType; +import fr.insee.bpm.metadata.model.VariablesMap; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -19,68 +20,68 @@ class DataVerifierTest { static List testSurveyUnitUpdateDtos = new ArrayList<>(); - static VariablesMap variablesMap; + static MetadataModel metadataModel; // Given @BeforeAll static void setUp() { //Variable definitions - variablesMap = new VariablesMap(); + metadataModel = new MetadataModel(); //Invalid Collected Variables only //1 Variable 1 State 1 Value - createCase(1,1,1,true,false,"TestUE1",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,1,1,true,false,"TestUE1",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //1 Variable 1 State 2 Values - createCase(1,1,2,true,false,"TestUE2",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,1,2,true,false,"TestUE2",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //1 Variable 2 States 1 Value - createCase(1,2,1,true,false,"TestUE3",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,2,1,true,false,"TestUE3",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //1 Variable 2 States 2 Values - createCase(1,2,2,true,false,"TestUE4",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,2,2,true,false,"TestUE4",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 1 State 1 Value - createCase(2,1,1,true,false,"TestUE5",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,1,1,true,false,"TestUE5",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 1 State 2 Values - createCase(2,1,2,true,false,"TestUE6",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,1,2,true,false,"TestUE6",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 2 States 1 Value - createCase(2,2,1,true,false,"TestUE7",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,2,1,true,false,"TestUE7",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 2 State 2 Value - createCase(2,2,2,true,false,"TestUE8",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,2,2,true,false,"TestUE8",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //With invalid ExternalVariables - createCase(1,1,1,true,true,"TestUE9",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,1,1,true,true,"TestUE9",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //1 Variable 1 State 2 Values - createCase(1,1,2,true,true,"TestUE10",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,1,2,true,true,"TestUE10",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //1 Variable 2 States 1 Value - createCase(1,2,1,true,true,"TestUE11",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,2,1,true,true,"TestUE11",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //1 Variable 2 States 2 Values - createCase(1,2,2,true,true,"TestUE12",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,2,2,true,true,"TestUE12",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 1 State 1 Value - createCase(2,1,1,true,true,"TestUE13",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,1,1,true,true,"TestUE13",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 1 State 2 Values - createCase(2,1,2,true,true,"TestUE14",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,1,2,true,true,"TestUE14",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 2 States 1 Value - createCase(2,2,1,true,true,"TestUE15",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,2,1,true,true,"TestUE15",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //2 Variables 2 State 2 Value - createCase(2,2,2,true,true,"TestUE16",testSurveyUnitUpdateDtos,variablesMap); + createCase(2,2,2,true,true,"TestUE16",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //Valid variables only - createCase(1,1,1,false,true,"TestUE17",testSurveyUnitUpdateDtos,variablesMap); - createCase(2,2,2,false,true,"TestUE18",testSurveyUnitUpdateDtos,variablesMap); + createCase(1,1,1,false,true,"TestUE17",testSurveyUnitUpdateDtos,metadataModel.getVariables()); + createCase(2,2,2,false,true,"TestUE18",testSurveyUnitUpdateDtos,metadataModel.getVariables()); //Manual modifications //Valid 2nd variable on 5th and 13th case @@ -123,7 +124,7 @@ static void setUp() { suDtoEdited.getCollectedVariables().remove(1); //When - DataVerifier.verifySurveyUnits(testSurveyUnitUpdateDtos,variablesMap); + DataVerifier.verifySurveyUnits(testSurveyUnitUpdateDtos,metadataModel.getVariables()); } private static void createCase(int variableNumber, int stateNumber, int valueNumber, boolean hasIncorrectValues, boolean hasExternalVariables, String idUE, List testSurveyUnitUpdateDtos, VariablesMap variablesMap) { @@ -135,7 +136,7 @@ private static void createCase(int variableNumber, int stateNumber, int valueNum List values = new ArrayList<>(); if(!variablesMap.hasVariable("testInteger" + variableIndex)) { - Variable var = new Variable("testInteger" + variableIndex, variablesMap.getRootGroup(), VariableType.INTEGER, "10"); + Variable var = new Variable("testInteger" + variableIndex, metadataModel.getRootGroup(), VariableType.INTEGER, "10"); variablesMap.putVariable(var); } diff --git a/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java b/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java index 861db1ad..dff57d53 100644 --- a/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java +++ b/src/test/java/fr/insee/genesis/controller/utils/LoopIdentifierTest.java @@ -1,10 +1,10 @@ package fr.insee.genesis.controller.utils; import fr.insee.genesis.Constants; -import fr.insee.genesis.controller.sources.ddi.Group; -import fr.insee.genesis.controller.sources.ddi.Variable; -import fr.insee.genesis.controller.sources.ddi.VariableType; -import fr.insee.genesis.controller.sources.ddi.VariablesMap; +import fr.insee.bpm.metadata.model.Group; +import fr.insee.bpm.metadata.model.MetadataModel; +import fr.insee.bpm.metadata.model.Variable; +import fr.insee.bpm.metadata.model.VariableType; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -12,89 +12,89 @@ class LoopIdentifierTest { - VariablesMap variablesMap; + MetadataModel metadataModel; private static final String LOOP_NAME = "BOUCLE1"; @BeforeEach void setUp() { // Given - this.variablesMap = new VariablesMap(); + this.metadataModel = new MetadataModel(); Group group = new Group(LOOP_NAME, Constants.ROOT_GROUP_NAME); Variable var1 = new Variable("var1", group, VariableType.STRING, "1"); - Variable var2 = new Variable("var2", variablesMap.getRootGroup(), VariableType.STRING, "1"); - variablesMap.putVariable(var1); - variablesMap.putVariable(var2); + Variable var2 = new Variable("var2", metadataModel.getRootGroup(), VariableType.STRING, "1"); + metadataModel.getVariables().putVariable(var1); + metadataModel.getVariables().putVariable(var2); } @Test @DisplayName("Should return _2") void test01() { //When + Then - Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var1", variablesMap, 2)).isEqualTo(String.format("%s_2",LOOP_NAME)); + Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var1", metadataModel.getVariables(), 2)).isEqualTo(String.format("%s_2",LOOP_NAME)); } @Test @DisplayName("Should return the root group name") void test02() { //When + Then - Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var2", variablesMap, 1)).isEqualTo(Constants.ROOT_GROUP_NAME); + Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var2", metadataModel.getVariables(), 1)).isEqualTo(Constants.ROOT_GROUP_NAME); } @Test @DisplayName("Should return the root group name if the variable is not present in the variables map") void test03(){ //When + Then - Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var3", variablesMap, 2)).isEqualTo(Constants.ROOT_GROUP_NAME); + Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var3", metadataModel.getVariables(), 2)).isEqualTo(Constants.ROOT_GROUP_NAME); } @Test @DisplayName("Should return the root group as related") void test04(){ //When + Then - Assertions.assertThat(LoopIdentifier.getRelatedVariableName("var1", variablesMap)).isEqualTo(Constants.ROOT_GROUP_NAME); + Assertions.assertThat(LoopIdentifier.getRelatedVariableName("var1", metadataModel.getVariables())).isEqualTo(Constants.ROOT_GROUP_NAME); } @Test @DisplayName("Should return null as related") void test05(){ //When + Then - Assertions.assertThat(LoopIdentifier.getRelatedVariableName("var2", variablesMap)).isNull(); + Assertions.assertThat(LoopIdentifier.getRelatedVariableName("var2", metadataModel.getVariables())).isNull(); } @Test @DisplayName("Should return var1 group if missing suffix") void test06(){ //When + Then - Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var1_MISSING", variablesMap,1)).isEqualTo(LOOP_NAME); + Assertions.assertThat(LoopIdentifier.getLoopIdentifier("var1_MISSING", metadataModel.getVariables(),1)).isEqualTo(LOOP_NAME); } @Test @DisplayName("Should return var1 as related if missing suffix") void test07(){ //When + Then - Assertions.assertThat(LoopIdentifier.getRelatedVariableName("var1_MISSING", variablesMap)).isEqualTo("var1"); + Assertions.assertThat(LoopIdentifier.getRelatedVariableName("var1_MISSING", metadataModel.getVariables())).isEqualTo("var1"); } @Test @DisplayName("Should return var1 group if filter result prefix") void test08(){ //When + Then - Assertions.assertThat(LoopIdentifier.getLoopIdentifier("FILTER_RESULT_var1", variablesMap,1)).isEqualTo(LOOP_NAME); + Assertions.assertThat(LoopIdentifier.getLoopIdentifier("FILTER_RESULT_var1", metadataModel.getVariables(),1)).isEqualTo(LOOP_NAME); } @Test @DisplayName("Should return var1 as related if filter result prefix") void test09(){ //When + Then - Assertions.assertThat(LoopIdentifier.getRelatedVariableName("FILTER_RESULT_var1", variablesMap)).isEqualTo("var1"); + Assertions.assertThat(LoopIdentifier.getRelatedVariableName("FILTER_RESULT_var1", metadataModel.getVariables())).isEqualTo("var1"); } @Test @DisplayName("Should return root as related if eno variable") void test10(){ //When + Then - Assertions.assertThat(LoopIdentifier.getRelatedVariableName(Constants.getEnoVariables()[0], variablesMap)).isEqualTo(Constants.ROOT_GROUP_NAME); + Assertions.assertThat(LoopIdentifier.getRelatedVariableName(Constants.getEnoVariables()[0], metadataModel.getVariables())).isEqualTo(Constants.ROOT_GROUP_NAME); } }