Skip to content

Commit

Permalink
fix(suggester): add url and version properties (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave authored Mar 7, 2024
1 parent e98ea50 commit f9dc401
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
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>
<packaging>jar</packaging>
<version>1.3.1</version>
<version>1.3.2</version>
<name>Pogues Model</name>
<description>Classes and converters for the Pogues model</description>
<url>https://inseefr.github.io/Pogues-Model/</url>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/xsd/Questionnaire.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@
<xs:element name="stopWords" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="order" type="SuggesterOrder" minOccurs="0"/>
<xs:element name="queryParser" type="SuggesterQueryParser"/>
<xs:element name="url" type="xs:string" minOccurs="0"/>
<xs:element name="version" type="xs:integer" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package fr.insee.pogues.test.suggester;

import fr.insee.pogues.conversion.JSONDeserializer;
import fr.insee.pogues.conversion.JSONSerializer;
import fr.insee.pogues.conversion.JSONToXMLTranslator;
import fr.insee.pogues.model.CodeList;
import fr.insee.pogues.model.CodeLists;
import fr.insee.pogues.model.Questionnaire;
import fr.insee.pogues.model.SuggesterParametersType;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.xmlunit.assertj3.XmlAssert;

import javax.xml.bind.JAXBException;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

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

class SuggesterParametersTypeTest {

private final String jsonSuggesters = """
{
"id": "questionnaire-id",
"CodeLists": {
"CodeList": [
{
"SuggesterParameters": {
"url": "some url",
"version": 1
}
}
]
}
}""";

@Test
void serializeJsonSuggesters() throws JAXBException, UnsupportedEncodingException, JSONException {
//
CodeList codeList = new CodeList();
SuggesterParametersType suggesterParametersType = new SuggesterParametersType();
suggesterParametersType.setUrl("some url");
suggesterParametersType.setVersion(BigInteger.ONE);
codeList.setSuggesterParameters(suggesterParametersType);
//
Questionnaire questionnaire = new Questionnaire();
questionnaire.setId("questionnaire-id");
questionnaire.setCodeLists(new CodeLists());
questionnaire.getCodeLists().getCodeList().add(codeList);

//
JSONSerializer jsonSerializer = new JSONSerializer();
String result = jsonSerializer.serialize(questionnaire);

// Current json serializer implementation wraps content in a "Questionnaire" node
String expectedJson = "{\"Questionnaire\":" + jsonSuggesters + "}";
JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT);
}

@Test
void deserializeJsonSuggesters() throws JAXBException {
//
JSONDeserializer jsonDeserializer = new JSONDeserializer();
Questionnaire questionnaire = jsonDeserializer.deserialize(
new ByteArrayInputStream(jsonSuggesters.getBytes()));
//
SuggesterParametersType suggesterParameters = questionnaire.getCodeLists().getCodeList().getFirst().getSuggesterParameters();
assertEquals("some url", suggesterParameters.getUrl());
assertEquals(BigInteger.ONE, suggesterParameters.getVersion());
}

@Test
void suggestersJsonToXml() throws JAXBException {
//
String xmlResult = new JSONToXMLTranslator().translate(jsonSuggesters);
//
String expectedXml = """
<?xml version="1.0" encoding="UTF-8"?>
<Questionnaire xmlns="http://xml.insee.fr/schema/applis/pogues" id="questionnaire-id">
<CodeLists>
<CodeList>
<SuggesterParameters>
<url>some url</url>
<version>1</version>
</SuggesterParameters>
</CodeList>
</CodeLists>
</Questionnaire>
""";
XmlAssert.assertThat(xmlResult).and(expectedXml).ignoreWhitespace().areIdentical();
}

}

0 comments on commit f9dc401

Please sign in to comment.