From 4de1ebefeb4b1030d21c81db88d822792656bd8a Mon Sep 17 00:00:00 2001 From: Nicolas Senave Date: Thu, 30 Nov 2023 16:38:17 +0100 Subject: [PATCH] refactor(label type): proper type in condition filter too --- .../model/flat/ConditionFilterType.java | 4 +- .../ConditionFilterSerializationTest.java | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/test/java/fr/insee/lunatic/conversion/ConditionFilterSerializationTest.java diff --git a/src/main/java/fr/insee/lunatic/model/flat/ConditionFilterType.java b/src/main/java/fr/insee/lunatic/model/flat/ConditionFilterType.java index 1cf05172..5bd6918f 100644 --- a/src/main/java/fr/insee/lunatic/model/flat/ConditionFilterType.java +++ b/src/main/java/fr/insee/lunatic/model/flat/ConditionFilterType.java @@ -15,10 +15,8 @@ }) @Getter @Setter -public class ConditionFilterType { +public class ConditionFilterType extends LabelType { - protected String value; - protected String type; @JsonIgnore protected List bindingDependencies; diff --git a/src/test/java/fr/insee/lunatic/conversion/ConditionFilterSerializationTest.java b/src/test/java/fr/insee/lunatic/conversion/ConditionFilterSerializationTest.java new file mode 100644 index 00000000..f501c88c --- /dev/null +++ b/src/test/java/fr/insee/lunatic/conversion/ConditionFilterSerializationTest.java @@ -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()); + } + +}