|
| 1 | +package com.baeldung.jpa.localdatetimequery; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.time.LocalDate; |
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 13 | +import org.springframework.context.annotation.Import; |
| 14 | + |
| 15 | +@DataJpaTest |
| 16 | +@Import(EventCriteriaRepository.class) |
| 17 | +public class EventRepositoryUnitTest { |
| 18 | + @Autowired |
| 19 | + private EventRepository eventRepository; |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private EventCriteriaRepository eventCriteriaRepository; |
| 23 | + |
| 24 | + private LocalDate testDate; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + public void setUp() { |
| 28 | + testDate = LocalDate.of(2025, 10, 12); |
| 29 | + |
| 30 | + eventRepository.deleteAll(); |
| 31 | + |
| 32 | + eventRepository.save(new Event("Morning Meeting", LocalDateTime.of(2025, 10, 12, 9, 0, 0))); |
| 33 | + eventRepository.save(new Event("Lunch Discussion", LocalDateTime.of(2025, 10, 12, 12, 30, 0))); |
| 34 | + eventRepository.save(new Event("Evening Review", LocalDateTime.of(2025, 10, 12, 18, 45, 0))); |
| 35 | + eventRepository.save(new Event("Next Day Planning", LocalDateTime.of(2025, 10, 13, 10, 0, 0))); |
| 36 | + } |
| 37 | + @Test |
| 38 | + public void givenLocalDateAndEventsWithTimestamps_whenQueryUsingBetween_thenReturnAllEventsForThatDay() { |
| 39 | + LocalDateTime startOfDay = testDate.atStartOfDay(); |
| 40 | + LocalDateTime endOfDay = testDate.plusDays(1).atStartOfDay(); |
| 41 | + |
| 42 | + List<Event> results = eventRepository.findByCreatedAtBetween(startOfDay, endOfDay); |
| 43 | + |
| 44 | + assertEquals(3, results.size()); |
| 45 | + assertEquals("Morning Meeting", results.get(0).getName()); |
| 46 | + assertEquals("Lunch Discussion", results.get(1).getName()); |
| 47 | + assertEquals("Evening Review", results.get(2).getName()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenLocalDateAndEventsWithTimestamps_whenQueryUsingExplicitBoundaries_thenReturnAllEventsForThatDay() { |
| 52 | + LocalDateTime startOfDay = testDate.atStartOfDay(); |
| 53 | + LocalDateTime endOfDay = testDate.plusDays(1).atStartOfDay(); |
| 54 | + |
| 55 | + List<Event> results = eventRepository.findByCreatedAtGreaterThanEqualAndCreatedAtLessThan(startOfDay, endOfDay); |
| 56 | + |
| 57 | + assertEquals(3, results.size()); |
| 58 | + assertEquals("Morning Meeting", results.get(0).getName()); |
| 59 | + assertEquals("Lunch Discussion", results.get(1).getName()); |
| 60 | + assertEquals("Evening Review", results.get(2).getName()); |
| 61 | + } |
| 62 | + |
| 63 | +// @Test |
| 64 | +// public void givenLocalDateAndEventsWithTimestamps_whenQueryUsingJpqlDateFunction_thenReturnAllEventsForThatDay() { |
| 65 | +// LocalDate queryDate = LocalDate.of(2025, 10, 12); |
| 66 | +// |
| 67 | +// List<Event> results = eventRepository.findByDate(queryDate); |
| 68 | +// |
| 69 | +// assertEquals(3, results.size()); |
| 70 | +// assertEquals("Morning Meeting", results.get(0).getName()); |
| 71 | +// assertEquals("Lunch Discussion", results.get(1).getName()); |
| 72 | +// assertEquals("Evening Review", results.get(2).getName()); |
| 73 | +// } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void givenLocalDateAndEventsWithTimestamps_whenQueryUsingCriteriaApi_thenReturnAllEventsForThatDay() { |
| 77 | + LocalDate queryDate = LocalDate.of(2025, 10, 12); |
| 78 | + |
| 79 | + List<Event> results = eventCriteriaRepository.findByCreatedDate(queryDate); |
| 80 | + |
| 81 | + assertEquals(3, results.size()); |
| 82 | + assertEquals("Morning Meeting", results.get(0).getName()); |
| 83 | + assertEquals("Lunch Discussion", results.get(1).getName()); |
| 84 | + assertEquals("Evening Review", results.get(2).getName()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void givenLocalDateAndEventsWithTimestamps_whenQueryUsingNativeSql_thenReturnAllEventsForThatDay() { |
| 89 | + LocalDateTime startOfDay = testDate.atStartOfDay(); |
| 90 | + LocalDateTime endOfDay = testDate.plusDays(1).atStartOfDay(); |
| 91 | + |
| 92 | + List<Event> results = eventRepository.findByDateRangeNative(startOfDay, endOfDay); |
| 93 | + |
| 94 | + assertEquals(3, results.size()); |
| 95 | + assertEquals("Morning Meeting", results.get(0).getName()); |
| 96 | + assertEquals("Lunch Discussion", results.get(1).getName()); |
| 97 | + assertEquals("Evening Review", results.get(2).getName()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void givenLocalDateForDifferentDay_whenQueryUsingBetween_thenReturnOnlyEventForThatDay() { |
| 102 | + LocalDate differentDate = LocalDate.of(2025, 10, 13); |
| 103 | + LocalDateTime startOfDay = differentDate.atStartOfDay(); |
| 104 | + LocalDateTime endOfDay = differentDate.plusDays(1).atStartOfDay(); |
| 105 | + |
| 106 | + List<Event> results = eventRepository.findByCreatedAtBetween(startOfDay, endOfDay); |
| 107 | + |
| 108 | + assertEquals(1, results.size()); |
| 109 | + assertEquals("Next Day Planning", results.get(0).getName()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void givenNoEventsForDate_whenQueryUsingBetween_thenReturnEmptyList() { |
| 114 | + LocalDate emptyDate = LocalDate.of(2025, 10, 14); |
| 115 | + LocalDateTime startOfDay = emptyDate.atStartOfDay(); |
| 116 | + LocalDateTime endOfDay = emptyDate.plusDays(1).atStartOfDay(); |
| 117 | + |
| 118 | + List<Event> results = eventRepository.findByCreatedAtBetween(startOfDay, endOfDay); |
| 119 | + |
| 120 | + assertEquals(0, results.size()); |
| 121 | + } |
| 122 | +} |
0 commit comments