Add relative time and date #6

Merged
stne3960 merged 5 commits from relative_dates into main 2025-11-03 23:38:56 +01:00
Showing only changes of commit c64cce2e71 - Show all commits

View File

@ -8,6 +8,10 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -83,4 +87,171 @@ class MockServiceTest {
Optional<MockEndpoint> result = mockService.getMatchedEndpoint("POST", "/greet/Alice"); Optional<MockEndpoint> result = mockService.getMatchedEndpoint("POST", "/greet/Alice");
assertTrue(result.isEmpty()); assertTrue(result.isEmpty());
} }
@Test
void shouldHandleTypedPlaceholders() {
Map<String, String> pathVars = Map.of(
"id", "123",
"price", "99.99",
"active", "true"
);
// Test int type
Object intResult = mockService.replacePlaceholders("{id:int}", pathVars);
assertInstanceOf(Integer.class, intResult);
assertEquals(123, intResult);
// Test float type
Object floatResult = mockService.replacePlaceholders("{price:float}", pathVars);
assertInstanceOf(Double.class, floatResult);
assertEquals(99.99, floatResult);
// Test bool type
Object boolResult = mockService.replacePlaceholders("{active:bool}", pathVars);
assertInstanceOf(Boolean.class, boolResult);
assertEquals(true, boolResult);
}
@Test
void shouldHandleDatetimeOffsetPattern() {
// Test datetime with positive offset
Object result = mockService.replacePlaceholders("{datetime+2d}", Map.of());
assertInstanceOf(String.class, result);
LocalDateTime expected = LocalDateTime.now().plusDays(2);
String expectedStr = expected.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
// Compare just the date part since time might differ by milliseconds
assertTrue(((String) result).startsWith(expected.format(DateTimeFormatter.ISO_LOCAL_DATE)));
}
@Test
void shouldHandleDatetimeOffsetWithHoursAndMinutes() {
// Test datetime with days, hours, and minutes
Object result = mockService.replacePlaceholders("{datetime+1d2h30m}", Map.of());
assertInstanceOf(String.class, result);
LocalDateTime expected = LocalDateTime.now().plusDays(1).plusHours(2).plusMinutes(30);
String expectedStr = expected.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
// Compare just the date and hour part
assertTrue(((String) result).startsWith(expected.format(DateTimeFormatter.ISO_LOCAL_DATE) + "T"));
}
@Test
void shouldHandleDatetimeOffsetWithNegativeOffset() {
// Test datetime with negative offset
Object result = mockService.replacePlaceholders("{datetime-3d}", Map.of());
assertInstanceOf(String.class, result);
LocalDateTime expected = LocalDateTime.now().minusDays(3);
// Compare just the date part
assertTrue(((String) result).startsWith(expected.format(DateTimeFormatter.ISO_LOCAL_DATE)));
}
@Test
void shouldHandleDatetimeAtSpecificTime() {
// Test datetime with specific time
Object result = mockService.replacePlaceholders("{datetime+2d@14:30}", Map.of());
assertInstanceOf(String.class, result);
LocalDate expectedDate = LocalDate.now().plusDays(2);
// Check that the result contains the expected date and time
assertTrue(((String) result).contains(expectedDate.format(DateTimeFormatter.ISO_LOCAL_DATE)));
assertTrue(((String) result).contains("14:30"));
}
@Test
void shouldHandleDateOffsetPattern() {
// Test date with positive offset
Object result = mockService.replacePlaceholders("{date+5d}", Map.of());
assertInstanceOf(String.class, result);
LocalDate expected = LocalDate.now().plusDays(5);
String expectedStr = expected.format(DateTimeFormatter.ISO_LOCAL_DATE);
assertEquals(expectedStr, result);
}
@Test
void shouldHandleDateOffsetWithNegativeOffset() {
// Test date with negative offset
Object result = mockService.replacePlaceholders("{date-7d}", Map.of());
assertInstanceOf(String.class, result);
LocalDate expected = LocalDate.now().minusDays(7);
String expectedStr = expected.format(DateTimeFormatter.ISO_LOCAL_DATE);
assertEquals(expectedStr, result);
}
@Test
void shouldHandleDatetimeWithRfc3339Format() {
// Test datetime with RFC3339 format
Object result = mockService.replacePlaceholders("{datetime+1d:rfc3339}", Map.of());
assertInstanceOf(String.class, result);
// RFC3339 format should end with 'Z' for UTC
assertTrue(((String) result).endsWith("Z"));
}
@Test
void shouldHandleDateWithCustomFormat() {
// Test date with custom format
Object result = mockService.replacePlaceholders("{date+1d:MMM dd, yyyy}", Map.of());
assertInstanceOf(String.class, result);
LocalDate expected = LocalDate.now().plusDays(1);
String expectedStr = expected.format(DateTimeFormatter.ofPattern("MMM dd, yyyy"));
assertEquals(expectedStr, result);
}
@Test
void shouldHandleDatetimeAtTimeWithFormat() {
// Test datetime at specific time with format
Object result = mockService.replacePlaceholders("{datetime+1d@09:00:rfc3339}", Map.of());
assertInstanceOf(String.class, result);
// Should be a valid RFC3339 formatted string
assertTrue(((String) result).endsWith("Z"));
assertTrue(((String) result).contains("09:00"));
}
@Test
void shouldHandleInlinePlaceholderReplacement() {
Map<String, String> pathVars = Map.of("name", "Alice", "age", "25");
Object result = mockService.replacePlaceholders(
"User {name} is {age:int} years old",
pathVars
);
assertInstanceOf(String.class, result);
assertEquals("User Alice is 25 years old", result);
}
@Test
void shouldHandleMixedPlaceholders() {
Map<String, String> pathVars = Map.of("userId", "42");
LocalDate expectedDate = LocalDate.now().plusDays(1);
String expectedDateStr = expectedDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
Object result = mockService.replacePlaceholders(
Map.of(
"userId", "{userId:int}",
"expiryDate", "{date+1d}"
),
pathVars
);
assertInstanceOf(Map.class, result);
Map<?, ?> resultMap = (Map<?, ?>) result;
assertEquals(42, resultMap.get("userId"));
assertEquals(expectedDateStr, resultMap.get("expiryDate"));
}
} }