Skip to content

Commit

Permalink
feat: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Dec 9, 2024
1 parent 069c7fc commit 79220e3
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions src/test/java/fr/insee/rmes/utils/DateUtilsTest.java
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);
}

}

0 comments on commit 79220e3

Please sign in to comment.