Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing attributes (childQuestionnaireRef & lastUpdatedDate) #141

Merged
merged 10 commits into from
Dec 11, 2024
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>fr.insee.pogues</groupId>
<artifactId>pogues-model</artifactId>
<version>1.4.1</version>
<version>1.4.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Pogues Model</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class JSONDeserializer {

private static final Logger logger = LoggerFactory.getLogger(JSONDeserializer.class);

public Questionnaire deserialize(String fileName) throws JAXBException, IOException {
public Questionnaire deserializeFile(String fileName) throws JAXBException, IOException {
laurentC35 marked this conversation as resolved.
Show resolved Hide resolved

if (fileName == null || fileName.isEmpty()) {
// TODO: throwing an exception instead of silent failing would be better here
Expand Down Expand Up @@ -52,6 +52,12 @@ public Questionnaire deserialize(InputStream jsonQuestionnaireInputStream) throw
return deserializeStreamSource(preProcessedStream);
}

public Questionnaire deserialize(String json) throws JAXBException {
logger.debug("Deserializing json questionnaire from input stream.");
StreamSource preProcessedStream = new StreamSource(new StringReader(json));
return deserializeStreamSource(preProcessedStream);
}

private static Questionnaire deserializeStreamSource(StreamSource json) throws JAXBException {

JAXBContext context = JAXBContext.newInstance(Questionnaire.class);
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/xsd/Questionnaire.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="childQuestionnaireRef" type="xs:string" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>childQuestionnaireRef currently contains the ids of references of questionnaires that the questionnaire depends.</xs:documentation>
laurentC35 marked this conversation as resolved.
Show resolved Hide resolved
</xs:annotation>
</xs:element>
</xs:sequence>
<xs:attribute name="agency" type="xs:token" default="com.example">
<xs:annotation>
Expand Down Expand Up @@ -122,6 +127,11 @@
entire questionnaire, the mix of languages is not supported. </xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="lastUpdatedDate" type="xs:string">
<xs:annotation>
<xs:documentation>lastUpdatedDate represents the date on which the questionnaire was last saved.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Expand Down
42 changes: 39 additions & 3 deletions src/test/java/fr/insee/pogues/test/JSONDeserializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,40 @@

import fr.insee.pogues.conversion.JSONDeserializer;
import fr.insee.pogues.model.Questionnaire;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

class JSONDeserializerTest {


private String jsonLastUpdatedDate;
private String jsonChildQuestionnaireRef;

@BeforeEach
void setup() {
jsonLastUpdatedDate = """
{
"id": "questionnaire-id",
"lastUpdatedDate": "Tue Dec 10 2024 10:33:38 GMT+0100 (heure normale d’Europe centrale)"
}
""";
jsonChildQuestionnaireRef = """
{
"id": "questionnaire-id",
"childQuestionnaireRef": [ "ref-id-1", "ref-id-2" ]
}
""";
}

@Test // Note: this test should be replaced by an exception test, see comment in deserializer class.
void deserializeFromNullInput_resultShouldBeNull() throws JAXBException, IOException {
assertNull(new JSONDeserializer().deserialize((String) null));
void deserializeFileFromNullInput_resultShouldBeNull() throws JAXBException, IOException {
assertNull(new JSONDeserializer().deserializeFile(null));
}

@Test
Expand All @@ -22,7 +44,7 @@ void testQuestionnaire() throws Exception {
long startTime = System.currentTimeMillis();

JSONDeserializer deserializer = new JSONDeserializer();
Questionnaire questionnaire = deserializer.deserialize("src/main/resources/examples/fr.insee-POPO-QPO-DOC.json");
Questionnaire questionnaire = deserializer.deserializeFile("src/main/resources/examples/fr.insee-POPO-QPO-DOC.json");

long elapsedTime = System.currentTimeMillis() - startTime;

Expand All @@ -31,4 +53,18 @@ void testQuestionnaire() throws Exception {
assertNotNull(questionnaire);
}

@Test
void testLastUpdatedDate() throws Exception {
JSONDeserializer deserializer = new JSONDeserializer();
Questionnaire questionnaire = deserializer.deserialize(jsonLastUpdatedDate);
assertEquals("Tue Dec 10 2024 10:33:38 GMT+0100 (heure normale d’Europe centrale)", questionnaire.getLastUpdatedDate());
}

@Test
void testChildQuestionnaireRef() throws Exception {
JSONDeserializer deserializer = new JSONDeserializer();
Questionnaire questionnaire = deserializer.deserialize(jsonChildQuestionnaireRef);
assertEquals(Arrays.asList("ref-id-1", "ref-id-2"), questionnaire.getChildQuestionnaireRef());
}

}
36 changes: 36 additions & 0 deletions src/test/java/fr/insee/pogues/test/XMLSerializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,40 @@ void serializeCodeList() throws JAXBException, UnsupportedEncodingException, JSO
XmlAssert.assertThat(result).and(expectedXml).ignoreWhitespace().areIdentical();
}


@Test
void serializeLastUpdated() throws JAXBException, UnsupportedEncodingException, JSONException {
// Given
Questionnaire questionnaire = new Questionnaire();
questionnaire.setLastUpdatedDate("fake-date");
// When
XMLSerializer xmlSerializer = new XMLSerializer();
String result = xmlSerializer.serialize(questionnaire);
// Then
String expectedXml = """
<?xml version="1.0" encoding="UTF-8"?>
<Questionnaire xmlns="http://xml.insee.fr/schema/applis/pogues" lastUpdatedDate="fake-date"/>
""";
XmlAssert.assertThat(result).and(expectedXml).ignoreWhitespace().areIdentical();
}

@Test
void serializeChildQuestionnaireRef() throws JAXBException, UnsupportedEncodingException, JSONException {
// Given
Questionnaire questionnaire = new Questionnaire();
questionnaire.getChildQuestionnaireRef().add("id-ref-1");
questionnaire.getChildQuestionnaireRef().add("id-ref-2");
// When
XMLSerializer xmlSerializer = new XMLSerializer();
String result = xmlSerializer.serialize(questionnaire);
// Then
String expectedXml = """
<?xml version="1.0" encoding="UTF-8"?>
<Questionnaire xmlns="http://xml.insee.fr/schema/applis/pogues">
<childQuestionnaireRef>id-ref-1</childQuestionnaireRef>
<childQuestionnaireRef>id-ref-2</childQuestionnaireRef>
</Questionnaire>
""";
XmlAssert.assertThat(result).and(expectedXml).ignoreWhitespace().areIdentical();
}
}
45 changes: 45 additions & 0 deletions src/test/java/fr/insee/pogues/test/XMLToJSONTranslatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class XMLToJSONTranslatorTest {

private String xmlCodeList;
private String xmlLastUpdatedDate;
private String xmlChildQuestionnaireRef;

@BeforeEach
void setup() {
Expand All @@ -28,6 +30,20 @@ void setup() {
<Label>CODE_2</Label>
</Code>
</CodeList>""";
xmlLastUpdatedDate = """
<Questionnaire
xmlns="http://xml.insee.fr/schema/applis/pogues"
id="questionnaire-id"
lastUpdatedDate="Tue Dec 10 2024 10:33:38 GMT+0100 (heure normale d’Europe centrale)"/>
""";
xmlChildQuestionnaireRef = """
<Questionnaire
xmlns="http://xml.insee.fr/schema/applis/pogues"
id="questionnaire-id">
<childQuestionnaireRef>ref-id-1</childQuestionnaireRef>
<childQuestionnaireRef>ref-id-2</childQuestionnaireRef>
</Questionnaire>
""";
}

@Test
Expand Down Expand Up @@ -55,4 +71,33 @@ void translateCodeList() throws JAXBException, JSONException {
JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT);
}

@Test
void translateLastUpdatedDate() throws JAXBException, JSONException {
//
XMLToJSONTranslator xmlToJsonTranslator = new XMLToJSONTranslator();
String result = xmlToJsonTranslator.translate(xmlLastUpdatedDate);
//
String expectedJson = """
{
"id": "questionnaire-id",
"lastUpdatedDate": "Tue Dec 10 2024 10:33:38 GMT+0100 (heure normale d’Europe centrale)"
}""";
JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT);
}

@Test
void translateChildQuestionnaireRef() throws JAXBException, JSONException {
//
XMLToJSONTranslator xmlToJsonTranslator = new XMLToJSONTranslator();
String result = xmlToJsonTranslator.translate(xmlChildQuestionnaireRef);
//
String expectedJson = """
{
"id": "questionnaire-id",
"childQuestionnaireRef": [ "ref-id-1", "ref-id-2" ]
}
""";
JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT);
}

}
Loading