From 4159e29618cbfae6c81cf96f2251ad7ae6258a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9nave?= <59770457+nsenave@users.noreply.github.com> Date: Mon, 12 Aug 2024 10:47:10 +0200 Subject: [PATCH] refactor: enum for questionnaire pagination (#265) --- .../insee/lunatic/model/flat/Pagination.java | 41 ++++++++++++++ .../lunatic/model/flat/Questionnaire.java | 36 +++++++++++- .../PaginationSerializationTest.java | 56 +++++++++++++++++++ 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 src/main/java/fr/insee/lunatic/model/flat/Pagination.java create mode 100644 src/test/java/fr/insee/lunatic/conversion/PaginationSerializationTest.java diff --git a/src/main/java/fr/insee/lunatic/model/flat/Pagination.java b/src/main/java/fr/insee/lunatic/model/flat/Pagination.java new file mode 100644 index 00000000..c7b71259 --- /dev/null +++ b/src/main/java/fr/insee/lunatic/model/flat/Pagination.java @@ -0,0 +1,41 @@ +package fr.insee.lunatic.model.flat; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Pagination mode of a questionnaire. + * Defines how the components should be paginated (e.g. one per page, or grouped by sequence). + */ +public enum Pagination { + + /** Each question is on a single page. */ + QUESTION("question"), + + /** Questions of a same sequence are on the same page. */ + SEQUENCE("sequence"), + + /** No page number on components. */ + NONE("none"); + + @JsonValue + private final String value; + + @JsonCreator + Pagination(String value) { + this.value = value; + } + + public String value() { + return value; + } + + public static Pagination fromValue(String value) { + for (Pagination pagination : Pagination.values()) { + if (pagination.value.equals(value)) + return pagination; + } + throw new IllegalArgumentException(value); + } + +} diff --git a/src/main/java/fr/insee/lunatic/model/flat/Questionnaire.java b/src/main/java/fr/insee/lunatic/model/flat/Questionnaire.java index 3c9fc670..2a3802bd 100644 --- a/src/main/java/fr/insee/lunatic/model/flat/Questionnaire.java +++ b/src/main/java/fr/insee/lunatic/model/flat/Questionnaire.java @@ -1,8 +1,8 @@ package fr.insee.lunatic.model.flat; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; import fr.insee.lunatic.model.flat.variable.VariableType; import lombok.Getter; import lombok.Setter; @@ -21,7 +21,7 @@ public class Questionnaire extends ComponentType { protected String lunaticModelVersion; protected String generatingDate; protected Boolean missing; - protected String pagination; + protected Pagination pagination; protected String maxPage; @JsonInclude(JsonInclude.Include.NON_EMPTY) @@ -42,4 +42,36 @@ public Questionnaire () { this.variables = new ArrayList<>(); } + @JsonProperty("pagination") + public Pagination getPaginationEnum() { + return pagination; + } + + @JsonProperty("pagination") + public void setPagination(Pagination pagination) { + this.pagination = pagination; + } + + + /** + * Method for the legacy string pagination attribute. + * @return String value of the questionnaire pagination mode. + * @deprecated Use the Pagination enum. + */ + @JsonIgnore + @Deprecated(since = "3.14.0") + public String getPagination() { + return pagination.value(); + } + + /** + * Method for the legacy string pagination attribute. + * @deprecated Use the Pagination enum. + */ + @JsonIgnore + @Deprecated(since = "3.14.0") + public void setPagination(String pagination) { + this.pagination = Pagination.fromValue(pagination); + } + } diff --git a/src/test/java/fr/insee/lunatic/conversion/PaginationSerializationTest.java b/src/test/java/fr/insee/lunatic/conversion/PaginationSerializationTest.java new file mode 100644 index 00000000..1580252f --- /dev/null +++ b/src/test/java/fr/insee/lunatic/conversion/PaginationSerializationTest.java @@ -0,0 +1,56 @@ +package fr.insee.lunatic.conversion; + +import fr.insee.lunatic.exception.SerializationException; +import fr.insee.lunatic.model.flat.Pagination; +import fr.insee.lunatic.model.flat.Questionnaire; +import org.json.JSONException; +import org.junit.jupiter.api.Test; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; + +import java.io.ByteArrayInputStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PaginationSerializationTest { + + @Test + void serializePagination() throws SerializationException, JSONException { + // + Questionnaire questionnaire = new Questionnaire(); + questionnaire.setPagination(Pagination.QUESTION); + // + String result = new JsonSerializer().serialize(questionnaire); + // + String expected = """ + {"componentType": "Questionnaire", "pagination": "question"}"""; + JSONAssert.assertEquals(expected, result, JSONCompareMode.STRICT); + } + + @Test + void deserializePagination() throws SerializationException { + // + String jsonInput = """ + {"componentType": "Questionnaire", "pagination": "question"}"""; + // + Questionnaire questionnaire = new JsonDeserializer().deserialize(new ByteArrayInputStream(jsonInput.getBytes())); + // + assertEquals(Pagination.QUESTION, questionnaire.getPaginationEnum()); + assertEquals("question", questionnaire.getPagination()); // backward compatibility test + } + + // backward compatibility test + @Test + void serializeStringPagination() throws SerializationException, JSONException { + // + Questionnaire questionnaire = new Questionnaire(); + questionnaire.setPagination("question"); + // + String result = new JsonSerializer().serialize(questionnaire); + // + String expected = """ + {"componentType": "Questionnaire", "pagination": "question"}"""; + JSONAssert.assertEquals(expected, result, JSONCompareMode.STRICT); + } + +}