diff --git a/pom.xml b/pom.xml index 5750c73c..bb01dfe9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ lunatic-model jar - 3.2.5 + 3.2.6-SNAPSHOT Lunatic Model Classes and converters for the Lunatic model https://inseefr.github.io/Lunatic-Model/ diff --git a/src/main/java/fr/insee/lunatic/model/flat/LabelType.java b/src/main/java/fr/insee/lunatic/model/flat/LabelType.java index 3b499f8e..87d11e27 100644 --- a/src/main/java/fr/insee/lunatic/model/flat/LabelType.java +++ b/src/main/java/fr/insee/lunatic/model/flat/LabelType.java @@ -1,5 +1,6 @@ package fr.insee.lunatic.model.flat; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.Getter; import lombok.Setter; @@ -8,10 +9,58 @@ "value", "type" }) -@Getter -@Setter public class LabelType { + @Getter @Setter protected String value; + protected String type; + @JsonIgnore + private LabelTypeEnum typeEnum; + + /** + * Get label type. + * @return String value of label type. + * @deprecated The string property is being replaced with an enum. Use the getTypeEnum method. + */ + @Deprecated(since = "3.2.6") + public String getType() { + if (typeEnum != null) + return typeEnum.toString(); + return type; + } + + /** + * Set string label type. + * @param type String value of label type, can be either "VTL_MD" or "VTL". + * @deprecated The string property is being replaced with an enum. + */ + @Deprecated(since = "3.2.6") + public void setType(String type) { + if (! ("VTL_MD".equals(type) || "VTL".equals(type))) + throw new IllegalArgumentException("Label type can be either \"VTL_MD\" or \"VTL\"."); + this.type = type; + this.typeEnum = null; + } + + /** + * Temporary getter for the type property being changed to an enum. In a future version, the getType + * will return the proper label type. + * @return Label type. + */ + public LabelTypeEnum getTypeEnum() { + if (type != null) + return LabelTypeEnum.valueOf(type); + return typeEnum; + } + + /** + * Set label type. + * @param labelTypeEnum Label type. + */ + public void setType(LabelTypeEnum labelTypeEnum) { + this.type = null; + this.typeEnum = labelTypeEnum; + } + } diff --git a/src/main/java/fr/insee/lunatic/model/flat/LabelTypeEnum.java b/src/main/java/fr/insee/lunatic/model/flat/LabelTypeEnum.java new file mode 100644 index 00000000..dc54ce66 --- /dev/null +++ b/src/main/java/fr/insee/lunatic/model/flat/LabelTypeEnum.java @@ -0,0 +1,10 @@ +package fr.insee.lunatic.model.flat; + +public enum LabelTypeEnum { + + /** Label that is a VTL expression and contains Markdown formatting. */ + VTL_MD, + /** Label that is a VTL expression. */ + VTL + +} diff --git a/src/test/java/fr/insee/lunatic/conversion/LabelSerializationTest.java b/src/test/java/fr/insee/lunatic/conversion/LabelSerializationTest.java new file mode 100644 index 00000000..494fb506 --- /dev/null +++ b/src/test/java/fr/insee/lunatic/conversion/LabelSerializationTest.java @@ -0,0 +1,69 @@ +package fr.insee.lunatic.conversion; + +import fr.insee.lunatic.exception.SerializationException; +import fr.insee.lunatic.model.flat.LabelType; +import fr.insee.lunatic.model.flat.LabelTypeEnum; +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 LabelSerializationTest { + + @Test + void serializeFromQuestionnaire_usingEnumType() throws SerializationException, JSONException { + // + Questionnaire questionnaire = new Questionnaire(); + LabelType label = new LabelType(); + label.setValue("Foo label"); + label.setType(LabelTypeEnum.VTL_MD); + questionnaire.setLabel(label); + // + JsonSerializer jsonSerializer = new JsonSerializer(); + String result = jsonSerializer.serialize(questionnaire); + // + String expected = """ + {"label": {"value": "Foo label", "type": "VTL_MD"}} + """; + JSONAssert.assertEquals(expected, result, JSONCompareMode.STRICT); + } + + @Test + void serializeFromQuestionnaire_usingStringType() throws SerializationException, JSONException { + // + Questionnaire questionnaire = new Questionnaire(); + LabelType label = new LabelType(); + label.setValue("Foo label"); + label.setType("VTL_MD"); + questionnaire.setLabel(label); + // + JsonSerializer jsonSerializer = new JsonSerializer(); + String result = jsonSerializer.serialize(questionnaire); + // + String expected = """ + {"label": {"value": "Foo label", "type": "VTL_MD"}} + """; + JSONAssert.assertEquals(expected, result, JSONCompareMode.STRICT); + } + + @Test + void deserializeFromQuestionnaire() throws SerializationException { + // + String jsonInput = """ + {"label": {"value": "Foo label", "type": "VTL_MD"}} + """; + // + JsonDeserializer jsonDeserializer = new JsonDeserializer(); + Questionnaire questionnaire = jsonDeserializer.deserialize(new ByteArrayInputStream(jsonInput.getBytes())); + // + LabelType label = questionnaire.getLabel(); + assertEquals(LabelTypeEnum.VTL_MD, label.getTypeEnum()); + assertEquals("VTL_MD", label.getType()); + } + +}