Skip to content

Commit

Permalink
refactor: enum for questionnaire pagination (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave committed Aug 12, 2024
1 parent 91bfa5b commit 4159e29
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/main/java/fr/insee/lunatic/model/flat/Pagination.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
36 changes: 34 additions & 2 deletions src/main/java/fr/insee/lunatic/model/flat/Questionnaire.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 4159e29

Please sign in to comment.