Skip to content

Commit

Permalink
feat: text component (#227)
Browse files Browse the repository at this point in the history
* feat: add text component

* chore: bump version
  • Loading branch information
nsenave authored Jun 5, 2024
1 parent 29aceee commit c76ab2e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>lunatic-model</artifactId>
<packaging>jar</packaging>

<version>3.8.1</version>
<version>3.9.0</version>
<name>Lunatic Model</name>
<description>Classes and converters for the Lunatic model</description>
<url>https://inseefr.github.io/Lunatic-Model/</url>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/fr/insee/lunatic/model/flat/BodyLine.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.insee.lunatic.model.flat;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
Expand All @@ -19,6 +20,16 @@ public BodyLine() {
this.bodyCells = new ArrayList<>();
}

/**
* Private constructor only meant to be used by jackson (through reflection) for deserialization.
* @param bodyCells List mapped by jackson.
*/
@JsonCreator @SuppressWarnings("unused")
private BodyLine(final List<BodyCell> bodyCells) {
this.bodyCells = bodyCells;
}

@JsonValue
protected List<BodyCell> bodyCells;

}
3 changes: 2 additions & 1 deletion src/main/java/fr/insee/lunatic/model/flat/ComponentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
@JsonSubTypes.Type(value = CheckboxBoolean.class, name = "CheckboxBoolean"),
@JsonSubTypes.Type(value = Dropdown.class, name = "Dropdown"),
@JsonSubTypes.Type(value = Textarea.class, name = "Textarea"),
@JsonSubTypes.Type(value = Suggester.class, name = "Suggester")
@JsonSubTypes.Type(value = Suggester.class, name = "Suggester"),
@JsonSubTypes.Type(value = Text.class, name = "Text"),
})
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum ComponentTypeEnum {
RADIO("Radio"),
DROPDOWN("Dropdown"),
TEXTAREA("Textarea"),
SUGGESTER("Suggester");
SUGGESTER("Suggester"),
TEXT("Text");

private final String value;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/insee/lunatic/model/flat/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static boolean isQuestionComponent(ComponentType component) {
return false;
return switch (component.getComponentType()) {
case CHECKBOX_BOOLEAN, INPUT, TEXTAREA, INPUT_NUMBER, DATEPICKER, DURATION,
CHECKBOX_ONE, RADIO, DROPDOWN, SUGGESTER,
CHECKBOX_ONE, RADIO, DROPDOWN, SUGGESTER, TEXT,
CHECKBOX_GROUP, TABLE, ROSTER_FOR_LOOP, PAIRWISE_LINKS -> true;
case QUESTIONNAIRE, SEQUENCE, SUBSEQUENCE, QUESTION, LOOP -> false;
};
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/fr/insee/lunatic/model/flat/Text.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.insee.lunatic.model.flat;

/**
* Non-response component designed to display a label.
*/
public class Text extends ComponentType {

public Text() {
this.componentType = ComponentTypeEnum.TEXT;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package fr.insee.lunatic.conversion;

import fr.insee.lunatic.exception.SerializationException;
import fr.insee.lunatic.model.flat.*;
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;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

class TableSerializationTest {

private final String tableWithText = """
{
"componentType": "Questionnaire",
"components": [
{
"componentType": "Table",
"body": [
[
{
"componentType": "Text",
"id": "text-cell-id",
"label": {
"value": "\\"Label value.\\"",
"type": "VTL|MD"
}
}
]
]
}
]
}""";

@Test
void serializeTableWithTextCells() throws SerializationException, JSONException {
//
Questionnaire questionnaire = new Questionnaire();
Table table = new Table();
table.setComponentType(ComponentTypeEnum.TABLE);
table.getBodyLines().add(new BodyLine());
BodyCell textCell = new BodyCell();
textCell.setId("text-cell-id");
textCell.setComponentType(ComponentTypeEnum.TEXT);
textCell.setLabel(new LabelType());
textCell.getLabel().setType(LabelTypeEnum.VTL_MD);
textCell.getLabel().setValue("\"Label value.\"");
table.getBodyLines().getFirst().getBodyCells().add(textCell);
questionnaire.getComponents().add(table);
//
String result = new JsonSerializer().serialize(questionnaire);
//
JSONAssert.assertEquals(tableWithText, result, JSONCompareMode.STRICT);
}

@Test
void deserializeTableWithTextCells() throws SerializationException {
//
Questionnaire questionnaire = new JsonDeserializer().deserialize(
new ByteArrayInputStream(tableWithText.getBytes()));
//
Table table = assertInstanceOf(Table.class, questionnaire.getComponents().getFirst());
assertEquals(1, table.getBodyLines().size());
assertEquals(1, table.getBodyLines().getFirst().getBodyCells().size());
BodyCell textCell = table.getBodyLines().getFirst().getBodyCells().getFirst();
assertEquals(ComponentTypeEnum.TEXT, textCell.getComponentType());
assertEquals("\"Label value.\"", textCell.getLabel().getValue());
assertEquals(LabelTypeEnum.VTL_MD, textCell.getLabel().getType());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.insee.lunatic.conversion;

import fr.insee.lunatic.exception.SerializationException;
import fr.insee.lunatic.model.flat.*;
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;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

class TextSerializationTest {

private final String textComponentJson = """
{
"componentType": "Questionnaire",
"components": [
{
"id": "text-component-id",
"componentType": "Text",
"label": {
"value": "\\"Non-editable text.\\"",
"type": "VTL|MD"
}
}
]
}""";

@Test
void serializeTextComponent() throws SerializationException, JSONException {
//
Questionnaire questionnaire = new Questionnaire();
Text text = new Text();
text.setId("text-component-id");
text.setLabel(new LabelType());
text.getLabel().setValue("\"Non-editable text.\"");
text.getLabel().setType(LabelTypeEnum.VTL_MD);
questionnaire.getComponents().add(text);
//
String result = new JsonSerializer().serialize(questionnaire);
//
JSONAssert.assertEquals(textComponentJson, result, JSONCompareMode.STRICT);
}

@Test
void deserializeTextComponent() throws SerializationException {
//
Questionnaire questionnaire = new JsonDeserializer().deserialize(
new ByteArrayInputStream(textComponentJson.getBytes()));
//
Text text = assertInstanceOf(Text.class, questionnaire.getComponents().getFirst());
assertEquals(ComponentTypeEnum.TEXT, text.getComponentType());
assertEquals("\"Non-editable text.\"", text.getLabel().getValue());
assertEquals(LabelTypeEnum.VTL_MD, text.getLabel().getType());
}

}

0 comments on commit c76ab2e

Please sign in to comment.