-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: enum for questionnaire pagination (#265)
- Loading branch information
Showing
3 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/java/fr/insee/lunatic/conversion/PaginationSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |