-
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.
- Loading branch information
Showing
8 changed files
with
185 additions
and
2 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
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,42 @@ | ||
package fr.insee.lunatic.model.flat; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Getter | ||
@Slf4j | ||
public class Duration extends ComponentType implements ComponentSimpleResponseType { | ||
|
||
/** Value for a years/months duration format. */ | ||
public static final String YEARS_MONTHS_FORMAT = "PnYnM"; | ||
|
||
/** Value for a hours/minutes duration format. */ | ||
public static final String HOURS_MINUTES_FORMAT = "PTnHnM"; | ||
|
||
public Duration() { | ||
this.componentType = ComponentTypeEnum.DURATION; | ||
} | ||
|
||
/** Duration format in the XSD Duration Data Type style. | ||
* Must start with a 'P' (that stands for period). Then can be followed by 'nY' (years), 'nM' (months), 'nD' (days). | ||
* 'T' indicates the start of a time section that can be followed by 'nH' (hours), 'nM' (minutes), 'nS' (seconds). */ | ||
@JsonProperty(required = true) | ||
private String format; | ||
|
||
@JsonProperty(required = true) | ||
@Setter | ||
protected ResponseType response; | ||
|
||
/** | ||
* Sets the duration format. Warning: this method doesn't do any strict validation. | ||
* @param format Format in the XSD Duration Data Type style. | ||
*/ | ||
public void setFormat(String format) { | ||
if (! (YEARS_MONTHS_FORMAT.equals(format) || HOURS_MINUTES_FORMAT.equals(format))) | ||
log.warn("Format '{}' does not match Lunatic commonly-used formats.", format); | ||
this.format = format; | ||
} | ||
|
||
} |
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
95 changes: 95 additions & 0 deletions
95
src/test/java/fr/insee/lunatic/conversion/DurationSerializationTest.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,95 @@ | ||
package fr.insee.lunatic.conversion; | ||
|
||
import fr.insee.lunatic.exception.SerializationException; | ||
import fr.insee.lunatic.model.flat.*; | ||
import fr.insee.lunatic.utils.TestUtils; | ||
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 java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
|
||
class DurationSerializationTest { | ||
|
||
@Test | ||
void serializeDuration_yearsMonthsFormat() throws SerializationException, IOException, JSONException { | ||
// | ||
Questionnaire questionnaire = new Questionnaire(); | ||
questionnaire.setId("questionnaire-id"); | ||
// | ||
Duration duration = new Duration(); | ||
duration.setId("duration-id"); | ||
duration.setFormat(Duration.YEARS_MONTHS_FORMAT); | ||
duration.setLabel(new LabelType()); | ||
duration.getLabel().setValue("\"Duration (years/months)\""); | ||
duration.getLabel().setType(LabelTypeEnum.VTL_MD); | ||
duration.setDescription(new LabelType()); | ||
duration.getDescription().setValue("\"2 years and 5 months\""); | ||
duration.getDescription().setType(LabelTypeEnum.VTL_MD); | ||
duration.setResponse(new ResponseType()); | ||
duration.getResponse().setName("DURATION_VAR"); | ||
// | ||
questionnaire.getComponents().add(duration); | ||
|
||
// | ||
String result = new JsonSerializer().serialize(questionnaire); | ||
|
||
String expectedJson = TestUtils.readResourceFile("duration-years-months.json"); | ||
JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT); | ||
} | ||
|
||
@Test | ||
void serializeDuration_HoursMinutesFormat() throws SerializationException, IOException, JSONException { | ||
// | ||
Questionnaire questionnaire = new Questionnaire(); | ||
questionnaire.setId("questionnaire-id"); | ||
// | ||
Duration duration = new Duration(); | ||
duration.setId("duration-id"); | ||
duration.setFormat(Duration.HOURS_MINUTES_FORMAT); | ||
duration.setLabel(new LabelType()); | ||
duration.getLabel().setValue("\"Duration (hours/minutes)\""); | ||
duration.getLabel().setType(LabelTypeEnum.VTL_MD); | ||
duration.setDescription(new LabelType()); | ||
duration.getDescription().setValue("\"1 hour and 10 minutes\""); | ||
duration.getDescription().setType(LabelTypeEnum.VTL_MD); | ||
duration.setResponse(new ResponseType()); | ||
duration.getResponse().setName("DURATION_VAR"); | ||
// | ||
questionnaire.getComponents().add(duration); | ||
|
||
// | ||
String result = new JsonSerializer().serialize(questionnaire); | ||
|
||
String expectedJson = TestUtils.readResourceFile("duration-hours-minutes.json"); | ||
JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT); | ||
} | ||
|
||
@Test | ||
void deserializeDuration_yearsMonthsFormat() throws IOException, SerializationException { | ||
// | ||
String jsonInput = TestUtils.readResourceFile("duration-years-months.json"); | ||
// | ||
Questionnaire questionnaire = new JsonDeserializer().deserialize(new ByteArrayInputStream(jsonInput.getBytes())); | ||
// | ||
Duration duration = assertInstanceOf(Duration.class, questionnaire.getComponents().getFirst()); | ||
assertEquals("PnYnM", duration.getFormat()); | ||
} | ||
|
||
@Test | ||
void deserializeDuration_hoursMinutesFormat() throws IOException, SerializationException { | ||
// | ||
String jsonInput = TestUtils.readResourceFile("duration-hours-minutes.json"); | ||
// | ||
Questionnaire questionnaire = new JsonDeserializer().deserialize(new ByteArrayInputStream(jsonInput.getBytes())); | ||
// | ||
Duration duration = assertInstanceOf(Duration.class, questionnaire.getComponents().getFirst()); | ||
assertEquals("PTnHnM", duration.getFormat()); | ||
} | ||
|
||
} |
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,22 @@ | ||
{ | ||
"id": "questionnaire-id", | ||
"componentType": "Questionnaire", | ||
"components": [ | ||
{ | ||
"id": "duration-id", | ||
"componentType": "Duration", | ||
"format": "PTnHnM", | ||
"label": { | ||
"value": "\"Duration (hours/minutes)\"", | ||
"type": "VTL|MD" | ||
}, | ||
"description": { | ||
"value": "\"1 hour and 10 minutes\"", | ||
"type": "VTL|MD" | ||
}, | ||
"response": { | ||
"name": "DURATION_VAR" | ||
} | ||
} | ||
] | ||
} |
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,22 @@ | ||
{ | ||
"id": "questionnaire-id", | ||
"componentType": "Questionnaire", | ||
"components": [ | ||
{ | ||
"id": "duration-id", | ||
"componentType": "Duration", | ||
"format": "PnYnM", | ||
"label": { | ||
"value": "\"Duration (years/months)\"", | ||
"type": "VTL|MD" | ||
}, | ||
"description": { | ||
"value": "\"2 years and 5 months\"", | ||
"type": "VTL|MD" | ||
}, | ||
"response": { | ||
"name": "DURATION_VAR" | ||
} | ||
} | ||
] | ||
} |