From 47cb9aa69fb4262a2aa366edf33952167fb2f721 Mon Sep 17 00:00:00 2001 From: Nicolas Senave Date: Tue, 17 Dec 2024 16:33:32 +0100 Subject: [PATCH] test: unit test for iso 8601 formatter --- .../core/i18n/date/Iso8601FormatterTest.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 eno-core/src/test/java/fr/insee/eno/core/i18n/date/Iso8601FormatterTest.java diff --git a/eno-core/src/test/java/fr/insee/eno/core/i18n/date/Iso8601FormatterTest.java b/eno-core/src/test/java/fr/insee/eno/core/i18n/date/Iso8601FormatterTest.java new file mode 100644 index 000000000..8ee746f6a --- /dev/null +++ b/eno-core/src/test/java/fr/insee/eno/core/i18n/date/Iso8601FormatterTest.java @@ -0,0 +1,89 @@ +package fr.insee.eno.core.i18n.date; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class Iso8601FormatterTest { + + private final Iso8601Formatter formatter = new Iso8601Formatter(); + + @Test + void convertYearMontDate_validDate_shouldReturnFormattedDate() { + // Arrange + String inputDate = "2024-12"; + + // Act + DateFormatter.Result result = formatter.convertYearMontDate(inputDate); + + // Assert + assertTrue(result.isValid()); + assertEquals("2024-12", result.value()); + } + + @Test + void convertYearMontDate_invalidDate_shouldReturnFailure() { + // Arrange + String inputDate = "2024/12"; + + // Act + DateFormatter.Result result = formatter.convertYearMontDate(inputDate); + + // Assert + assertFalse(result.isValid()); + assertTrue(result.errorMessage().contains("YYYY-MM")); + } + + @Test + void convertYearMontDate_emptyString_shouldReturnFailure() { + // Arrange + String inputDate = ""; + + // Act + DateFormatter.Result result = formatter.convertYearMontDate(inputDate); + + // Assert + assertFalse(result.isValid()); + assertTrue(result.errorMessage().contains("YYYY-MM")); + } + + @Test + void convertYearMontDayDate_validDate_shouldReturnFormattedDate() { + // Arrange + String inputDate = "2024-12-17"; + + // Act + DateFormatter.Result result = formatter.convertYearMontDayDate(inputDate); + + // Assert + assertTrue(result.isValid()); + assertEquals("2024-12-17", result.value()); + } + + @Test + void convertYearMontDayDate_invalidDate_shouldReturnFailure() { + // Arrange + String inputDate = "2024/12/17"; + + // Act + DateFormatter.Result result = formatter.convertYearMontDayDate(inputDate); + + // Assert + assertFalse(result.isValid()); + assertTrue(result.errorMessage().contains("YYYY-MM-DD")); + } + + @Test + void convertYearMontDayDate_emptyString_shouldReturnFailure() { + // Arrange + String inputDate = ""; + + // Act + DateFormatter.Result result = formatter.convertYearMontDayDate(inputDate); + + // Assert + assertFalse(result.isValid()); + assertTrue(result.errorMessage().contains("YYYY-MM-DD")); + } + +}