Skip to content

Commit

Permalink
refactor(label type): proper type in condition filter too
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave committed Nov 30, 2023
1 parent 8a26707 commit 4de1ebe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
})
@Getter
@Setter
public class ConditionFilterType {
public class ConditionFilterType extends LabelType {

protected String value;
protected String type;
@JsonIgnore
protected List<String> bindingDependencies;

Expand Down
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());
}

}

0 comments on commit 4de1ebe

Please sign in to comment.