Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unit objects in body cells #275

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.15.1</version>
<version>3.15.2</version>
<name>Lunatic Model</name>
<description>Classes and converters for the Lunatic model</description>
<url>https://inseefr.github.io/Lunatic-Model/</url>
Expand Down
49 changes: 48 additions & 1 deletion src/main/java/fr/insee/lunatic/model/flat/BodyCell.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fr.insee.lunatic.model.flat;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -41,7 +43,10 @@ public class BodyCell {
protected LabelType label;
protected String format;
protected String dateFormat;
protected String unit;

/** For input number cells. */
protected InputNumber.Unit unit;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
protected List<Option> options;
/** For suggester cells: Name of the code list used for auto-completion. */
Expand Down Expand Up @@ -74,4 +79,46 @@ public BodyCell() {
this.optionResponses = new ArrayList<>();
}

@JsonProperty("unit")
public InputNumber.Unit getUnitWrapper() {
return unit;
}

@JsonProperty("unit")
public void setUnit(InputNumber.Unit unit) {
this.unit = unit;
}

/** Legacy unit string property.
* @deprecated Use label unit. */
@JsonIgnore
@Deprecated(since = "3.15.2")
public String getUnit() {
if (unit == null)
return null;
return unit.getValue();
}

@JsonIgnore
public LabelType getUnitLabel() {
if (unit == null)
return null;
return unit.getLabel();
}

/** Legacy unit string property.
* @deprecated Use label unit. */
@JsonIgnore
@Deprecated(since = "3.15.2")
public void setUnit(String value) {
unit = new InputNumber.Unit();
unit.setValue(value);
}

@JsonIgnore
public void setUnit(LabelType labelType) {
unit = new InputNumber.Unit();
unit.setLabel(labelType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.insee.lunatic.model.flat.BodyCell;
import fr.insee.lunatic.model.flat.ComponentTypeEnum;
import fr.insee.lunatic.model.flat.ResponseType;
import fr.insee.lunatic.model.flat.*;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand All @@ -25,6 +23,21 @@ class BodyCellSerializationTest {
}
}""";

private final String jsonInputNumberCell = """
{
"id": "foo-id",
"componentType": "InputNumber",
"min": 0,
"max": 100,
"unit": {
"value": "%",
"type": "VTL|MD"
},
"response": {
"name": "FOO"
}
}""";

@Test
void serializeSuggesterCell() throws JsonProcessingException, JSONException {
//
Expand Down Expand Up @@ -53,4 +66,39 @@ void deserializeSuggesterCell() throws JsonProcessingException {
assertEquals("FOO", bodyCell.getResponse().getName());
}

@Test
void serializeInputNumberCell() throws JsonProcessingException, JSONException {
//
BodyCell bodyCell = new BodyCell();
bodyCell.setId("foo-id");
bodyCell.setComponentType(ComponentTypeEnum.INPUT_NUMBER);
bodyCell.setMin(0d);
bodyCell.setMax(100d);
bodyCell.setUnit(new LabelType());
bodyCell.getUnitLabel().setValue("%");
bodyCell.getUnitLabel().setType(LabelTypeEnum.VTL_MD);
bodyCell.setResponse(new ResponseType());
bodyCell.getResponse().setName("FOO");
//
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String result = objectMapper.writeValueAsString(bodyCell);
//
JSONAssert.assertEquals(jsonInputNumberCell, result, JSONCompareMode.STRICT);
}

@Test
void deserializeInputNumberCell() throws JsonProcessingException {
//
BodyCell bodyCell = new ObjectMapper().readValue(jsonInputNumberCell, BodyCell.class);
//
assertEquals("foo-id", bodyCell.getId());
assertEquals(ComponentTypeEnum.INPUT_NUMBER, bodyCell.getComponentType());
assertEquals(0d, bodyCell.getMin());
assertEquals(100d, bodyCell.getMax());
assertEquals("%", bodyCell.getUnitLabel().getValue());
assertEquals(LabelTypeEnum.VTL_MD, bodyCell.getUnitLabel().getType());
assertEquals("FOO", bodyCell.getResponse().getName());
}

}
Loading