-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
069c7fc
commit 79220e3
Showing
1 changed file
with
51 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,65 @@ | ||
package fr.insee.rmes.utils; | ||
|
||
import fr.insee.rmes.exceptions.RmesException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import java.time.LocalDateTime; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DateUtilsTest { | ||
|
||
@Test | ||
void should_return_date_if_good_format() { | ||
String date = DateUtils.getDate("2018-12-17"); | ||
assertEquals("2018-12-17", date); | ||
void getCurrentDate_ShouldReturnNonNullValue() { | ||
String currentDate = DateUtils.getCurrentDate(); | ||
assertNotNull(currentDate); | ||
} | ||
|
||
@Test | ||
void parseDateTime_ShouldParseValidZonedDateTime() { | ||
String dateStr = "2024-12-09T12:30:45+01:00"; | ||
LocalDateTime result = DateUtils.parseDateTime(dateStr); | ||
assertEquals(LocalDateTime.of(2024, 12, 9, 12, 30, 45), result); | ||
} | ||
|
||
@Test | ||
void parseDateTime_ShouldParseValidDateWithoutTime() { | ||
String dateStr = "2024-12-09"; | ||
LocalDateTime result = DateUtils.parseDateTime(dateStr); | ||
assertEquals(LocalDateTime.of(2024, 12, 9, 0, 0), result); | ||
} | ||
|
||
@Test | ||
void parseDateTime_ShouldReturnNullForInvalidDate() { | ||
String invalidDate = "invalid-date"; | ||
assertThrows(Exception.class, () -> DateUtils.parseDateTime(invalidDate)); | ||
} | ||
|
||
@Test | ||
void getDate_ShouldConvertToISODateFormat() { | ||
String isoDateTime = "2024-12-09T15:45:30"; | ||
String result = DateUtils.getDate(isoDateTime); | ||
assertEquals("2024-12-09", result); | ||
} | ||
|
||
@Test | ||
void getDate_ShouldReturnOriginalForInvalidDate() { | ||
String invalidDate = "invalid-date"; | ||
String result = DateUtils.getDate(invalidDate); | ||
assertEquals(invalidDate, result); | ||
} | ||
|
||
@Test | ||
void toDate_ShouldFormatToDDMMYYYY() { | ||
String dateTime = "2024-12-09T15:45:30"; | ||
String result = DateUtils.toDate(dateTime); | ||
assertEquals("09/12/2024", result); | ||
} | ||
|
||
@Test | ||
void should_return_date_if_bad_format() { | ||
String date = DateUtils.getDate("12-17"); | ||
assertEquals("12-17", date); | ||
void toDate_ShouldReturnOriginalStringForShortString() { | ||
String shortString = "2024"; | ||
String result = DateUtils.toDate(shortString); | ||
assertEquals(shortString, result); | ||
} | ||
|
||
} |