-
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: create enum class for label type (#124)
* refactor: create enum class for label type * fix(label type): right value for 'vtl md' * chore: bump version after fix * refactor(label type): proper type in condition filter too * chore: version 3.2.7
- Loading branch information
Showing
6 changed files
with
243 additions
and
6 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
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
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
32 changes: 32 additions & 0 deletions
32
src/main/java/fr/insee/lunatic/model/flat/LabelTypeEnum.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,32 @@ | ||
package fr.insee.lunatic.model.flat; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
public enum LabelTypeEnum { | ||
|
||
/** Label that is a VTL expression and contains Markdown formatting. */ | ||
VTL_MD("VTL|MD"), | ||
/** Label that is a VTL expression. */ | ||
VTL("VTL"); | ||
|
||
private final String value; | ||
|
||
LabelTypeEnum(String v) { | ||
value = v; | ||
} | ||
|
||
@JsonValue | ||
public String value() { | ||
return value; | ||
} | ||
|
||
public static LabelTypeEnum fromValue(String v) { | ||
for (LabelTypeEnum c : LabelTypeEnum.values()) { | ||
if (c.value.equals(v)) { | ||
return c; | ||
} | ||
} | ||
throw new IllegalArgumentException(v); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/fr/insee/lunatic/conversion/ConditionFilterSerializationTest.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,68 @@ | ||
package fr.insee.lunatic.conversion; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import fr.insee.lunatic.model.flat.ConditionFilterType; | ||
import fr.insee.lunatic.model.flat.LabelTypeEnum; | ||
import org.json.JSONException; | ||
import org.junit.jupiter.api.Test; | ||
import org.skyscreamer.jsonassert.JSONAssert; | ||
import org.skyscreamer.jsonassert.JSONCompareMode; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ConditionFilterSerializationTest { | ||
|
||
@Test | ||
void serializeConditionFilter() throws JsonProcessingException, JSONException { | ||
// | ||
ConditionFilterType conditionFilterType = new ConditionFilterType(); | ||
conditionFilterType.setValue("if FOO then BAR else BAZ"); | ||
conditionFilterType.setType(LabelTypeEnum.VTL); | ||
conditionFilterType.setBindingDependencies(List.of("FOO", "BAR", "BAZ")); | ||
// | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String result = objectMapper.writerFor(ConditionFilterType.class).writeValueAsString(conditionFilterType); | ||
// | ||
String expected = """ | ||
{"value": "if FOO then BAR else BAZ", "type": "VTL"} | ||
"""; // (binding dependencies are ignored) | ||
JSONAssert.assertEquals(expected, result, JSONCompareMode.STRICT); | ||
} | ||
|
||
@Test // to be removed when string type is removed | ||
void serializeConditionFilter_stringType() throws JsonProcessingException, JSONException { | ||
// | ||
ConditionFilterType conditionFilterType = new ConditionFilterType(); | ||
conditionFilterType.setValue("if FOO then BAR else BAZ"); | ||
conditionFilterType.setType("VTL"); | ||
conditionFilterType.setBindingDependencies(List.of("FOO", "BAR", "BAZ")); | ||
// | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String result = objectMapper.writerFor(ConditionFilterType.class).writeValueAsString(conditionFilterType); | ||
// | ||
String expected = """ | ||
{"value": "if FOO then BAR else BAZ", "type": "VTL"} | ||
"""; // (binding dependencies are ignored) | ||
JSONAssert.assertEquals(expected, result, JSONCompareMode.STRICT); | ||
} | ||
|
||
@Test | ||
void deserializeConditionFilter() throws JsonProcessingException { | ||
// | ||
String jsonInput = """ | ||
{"value": "if FOO then BAR else BAZ", "type": "VTL"} | ||
"""; | ||
// | ||
ConditionFilterType conditionFilterType = new ObjectMapper().readValue(jsonInput, ConditionFilterType.class); | ||
// | ||
assertEquals("if FOO then BAR else BAZ", conditionFilterType.getValue()); | ||
assertEquals(LabelTypeEnum.VTL, conditionFilterType.getTypeEnum()); | ||
assertEquals("VTL", conditionFilterType.getType()); // to be removed when string type is removed | ||
assertTrue(conditionFilterType.getBindingDependencies().isEmpty()); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/test/java/fr/insee/lunatic/conversion/LabelSerializationTest.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,88 @@ | ||
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; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
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 labelObject_usingStringType_illegalValue() { | ||
LabelType label = new LabelType(); | ||
assertThrows(IllegalArgumentException.class, () -> label.setType("Foo type")); | ||
} | ||
|
||
@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()); | ||
} | ||
|
||
@Test | ||
void deserializeFromQuestionnaire_illegalTypeValue() { | ||
// | ||
String jsonInput = """ | ||
{"label": {"value": "Foo label", "type": "Foo value"}} | ||
"""; | ||
// | ||
JsonDeserializer jsonDeserializer = new JsonDeserializer(); | ||
assertThrows(SerializationException.class, () -> | ||
jsonDeserializer.deserialize(new ByteArrayInputStream(jsonInput.getBytes()))); | ||
} | ||
|
||
} |