-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature #10] Implement and use custom deserializers for date values.
Simplify literal value presence validation.
- Loading branch information
Showing
5 changed files
with
79 additions
and
5 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
28 changes: 28 additions & 0 deletions
28
src/main/java/cz/cvut/kbss/jsonld/deserialization/datetime/LocalDateDeserializer.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,28 @@ | ||
package cz.cvut.kbss.jsonld.deserialization.datetime; | ||
|
||
import cz.cvut.kbss.jopa.datatype.xsd.XsdDateMapper; | ||
import cz.cvut.kbss.jsonld.JsonLd; | ||
import cz.cvut.kbss.jsonld.deserialization.DeserializationContext; | ||
import cz.cvut.kbss.jsonld.deserialization.ValueDeserializer; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Map; | ||
|
||
import static cz.cvut.kbss.jsonld.deserialization.datetime.OffsetDateTimeDeserializer.missingValueException; | ||
|
||
/** | ||
* Deserializes values to {@link LocalDate}. | ||
* <p> | ||
* The values are expected to be String in the ISO date format. | ||
*/ | ||
public class LocalDateDeserializer implements ValueDeserializer<LocalDate> { | ||
|
||
@Override | ||
public LocalDate deserialize(Map<?, ?> jsonNode, DeserializationContext<LocalDate> ctx) { | ||
final Object value = jsonNode.get(JsonLd.VALUE); | ||
if (value == null) { | ||
throw missingValueException(jsonNode); | ||
} | ||
return XsdDateMapper.map(value.toString()); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/test/java/cz/cvut/kbss/jsonld/deserialization/datetime/LocalDateDeserializerTest.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,40 @@ | ||
package cz.cvut.kbss.jsonld.deserialization.datetime; | ||
|
||
import cz.cvut.kbss.jsonld.JsonLd; | ||
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.OffsetTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static cz.cvut.kbss.jsonld.deserialization.datetime.OffsetDateTimeDeserializerTest.deserializationContext; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class LocalDateDeserializerTest { | ||
|
||
private final LocalDateDeserializer sut = new LocalDateDeserializer(); | ||
|
||
@Test | ||
void deserializeDeserializesSpecifiedIsoFormattedDateString() { | ||
final LocalDate value = LocalDate.now(); | ||
final Map<String, Object> input = Collections.singletonMap(JsonLd.VALUE, value.format(DateTimeFormatter.ISO_DATE)); | ||
|
||
final LocalDate result = sut.deserialize(input, deserializationContext(LocalDate.class)); | ||
assertEquals(value, result); | ||
} | ||
|
||
@Test | ||
void deserializeThrowsJsonLdDeserializationExceptionWhenInputIsMissingValueAttribute() { | ||
final Map<String, Object> input = Collections.singletonMap("notValue", LocalDate.now().format(DateTimeFormatter.ISO_DATE)); | ||
|
||
final JsonLdDeserializationException ex = assertThrows(JsonLdDeserializationException.class, | ||
() -> sut.deserialize(input, deserializationContext(LocalDate.class))); | ||
assertThat(ex.getMessage(), containsString(JsonLd.VALUE)); | ||
assertThat(ex.getMessage(), containsString("missing")); | ||
} | ||
} |