diff --git a/src/main/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParser.java b/src/main/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParser.java index ed6c61a9..ee39e086 100644 --- a/src/main/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParser.java +++ b/src/main/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParser.java @@ -203,13 +203,21 @@ private static Attribute getType(StartElement element) { return element.getAttributeByName(new QName("type")); } - private List readValues(XMLEventReader reader, String stateName) throws XMLStreamException { + /** + * Read multiple repeated values in XML (notably when reading loop data) + * @param reader the XML reader used to read XML + * @param elementName the repeated element name + * + * @return a list of ValueType for a specific element + * @throws XMLStreamException if XML reading error + */ + private List readValues(XMLEventReader reader, String elementName) throws XMLStreamException { List values = new ArrayList<>(); while(reader.hasNext()){ final XMLEvent event = reader.nextEvent(); - if (event.isEndElement() && event.asEndElement().getName().getLocalPart().equals(stateName)){ + if (event.isEndElement() && event.asEndElement().getName().getLocalPart().equals(elementName)){ // End of variable return values; } @@ -223,7 +231,6 @@ private List readValues(XMLEventReader reader, String stateName) thro values.add(new ValueType(value, type)); } } - return values; } @@ -246,13 +253,21 @@ private List readCalculatedOrExternal(XMLEventReader reader LunaticXmlOtherData variable = new LunaticXmlOtherData(); variable.setVariableName(variableName); - String type = getType(element).getValue(); - String value = reader.getElementText(); - + //Read values List values = new ArrayList<>(); - values.add(new ValueType(value, type)); - variable.setValues(values); + if(element.isStartElement() && getType(element) != null && !getType(element).getValue().equals("null")) { + //If only 1 value (determined by presence of type) + String type = getType(element).getValue(); + String value = reader.getElementText(); + values.add(new ValueType(value, type)); + variable.setValues(values); + lunaticXmlOtherDataList.add(variable); + continue; //Go to next XML event + } + // Multiple values (loop) + values = readValues(reader, variableName); + variable.setValues(values); lunaticXmlOtherDataList.add(variable); } } diff --git a/src/test/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParserTest.java b/src/test/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParserTest.java index d23518e0..74d6b37d 100644 --- a/src/test/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParserTest.java +++ b/src/test/java/fr/insee/genesis/controller/sources/xml/LunaticXmlDataSequentialParserTest.java @@ -1,10 +1,11 @@ package fr.insee.genesis.controller.sources.xml; import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import javax.xml.stream.XMLStreamException; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -20,8 +21,8 @@ class LunaticXmlDataSequentialParserTest { static LunaticXmlSurveyUnit surveyUnit; // Given + When - @BeforeAll - static void setUp() throws Exception { + @BeforeEach + void setUp() throws Exception { Path path = Path.of("src/test/resources/data_test_parser_xml.xml"); stream = new FileInputStream(path.toFile()); parser = new LunaticXmlDataSequentialParser(path, stream); @@ -81,8 +82,37 @@ void checkExternalVariableValue(){ Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues().getFirst().getValue()).isEqualTo("BOB"); } - @AfterAll - static void closeStream() throws IOException { + @Test + void checkExternalVariableValue_loops() throws IOException, XMLStreamException { + //Given + Path path = Path.of("src/test/resources/data_test_parser_xml_external_loops.xml"); + stream = new FileInputStream(path.toFile()); + parser = new LunaticXmlDataSequentialParser(path, stream); + + //When + campaign = parser.getCampaign(); + surveyUnit = parser.readNextSurveyUnit(); + + //Then + //Not null & volumetry + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst()).isNotNull(); + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues()).isNotEmpty(); + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues()).hasSize(3); + + //Content + //1 + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues().getFirst().getType()).isEqualTo("string"); + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues().getFirst().getValue()).isEqualTo("BOB1"); + //2 + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues().get(1).getType()).isEqualTo("string"); + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues().get(1).getValue()).isEqualTo("BOB2"); + //3 + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues().get(2).getType()).isEqualTo("string"); + Assertions.assertThat(surveyUnit.getData().getExternal().getFirst().getValues().get(2).getValue()).isEqualTo("BOB3"); + } + + @AfterEach + void closeStream() throws IOException { stream.close(); } diff --git a/src/test/resources/data_test_parser_xml_external_loops.xml b/src/test/resources/data_test_parser_xml_external_loops.xml new file mode 100644 index 00000000..6018d4dd --- /dev/null +++ b/src/test/resources/data_test_parser_xml_external_loops.xml @@ -0,0 +1,361 @@ + + + TEST2023X01 + + + + UE0000000001 + TEST2023X01 + + + + + + + + + + + + 012 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + 3 + + + + + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 2 + + + + + + + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + + + + + + + 1 + + + + + + + + 2 + 1 + + + + + + + + + + + + + + + + + LOIC BOB + ALICE + + + + + + + + + + + + + + + + true + + + + + + + + 1 + 2 + + + + + + + + + + + + + + + + + + 1 + + + + + + + + + + + + + + + + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1983-03-30 + 1990-08-18 + + + + + + + + + + + + + + + + + + + + + BOB1 + BOB2 + BOB3 + + JENNY + + ETAGE 2 + PARIS + MADAME + 75000 + MONSIEUR + DUPOND + DURAND + 22 RUE DES TULIPES + + + + + whoAnswers1 + Un adulte du ménage + + + whoAnswers2 + + + + whoAnswers3 + + + + + + \ No newline at end of file