Skip to content

Commit

Permalink
refactor: create enum class for label type
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave committed Nov 28, 2023
1 parent 3f1605f commit e0b7d28
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 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.2.5</version>
<version>3.2.6-SNAPSHOT</version>
<name>Lunatic Model</name>
<description>Classes and converters for the Lunatic model</description>
<url>https://inseefr.github.io/Lunatic-Model/</url>
Expand Down
53 changes: 51 additions & 2 deletions src/main/java/fr/insee/lunatic/model/flat/LabelType.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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -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 <code>getTypeEnum</code> 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 <code>getType</code>
* 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;
}

}
10 changes: 10 additions & 0 deletions src/main/java/fr/insee/lunatic/model/flat/LabelTypeEnum.java
Original file line number Diff line number Diff line change
@@ -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

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

}

0 comments on commit e0b7d28

Please sign in to comment.