Delete CLOSED posts only a week after closing; track closedAt
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package com.krylov.refound.service;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.krylov.refound.entity.Post;
|
||||
import com.krylov.refound.enums.PostStatus;
|
||||
import com.krylov.refound.repository.PostRepository;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
class SchedulerServiceDeleteExpiredPostsTest {
|
||||
|
||||
private PostRepository postRepository;
|
||||
private PostDeletionService postDeletionService;
|
||||
private SchedulerService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
postRepository = mock(PostRepository.class);
|
||||
postDeletionService = mock(PostDeletionService.class);
|
||||
service = new SchedulerService(postRepository, mock(PostModerationExecutorService.class), postDeletionService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletesOnlyPostsClosedMoreThanAWeekAgo() {
|
||||
when(postRepository.findByStatusAndClosedAtBefore(any(), any())).thenReturn(List.of());
|
||||
when(postRepository.findByCreatedAtBefore(any())).thenReturn(List.of());
|
||||
|
||||
service.deleteExpiredPosts();
|
||||
|
||||
ArgumentCaptor<LocalDateTime> closedBefore = ArgumentCaptor.forClass(LocalDateTime.class);
|
||||
verify(postRepository).findByStatusAndClosedAtBefore(eq(PostStatus.CLOSED), closedBefore.capture());
|
||||
assertThat(closedBefore.getValue()).isCloseTo(LocalDateTime.now().minusDays(7), within(1, ChronoUnit.MINUTES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stillDeletesPostsOlderThanSixMonths() {
|
||||
when(postRepository.findByStatusAndClosedAtBefore(any(), any())).thenReturn(List.of());
|
||||
when(postRepository.findByCreatedAtBefore(any())).thenReturn(List.of());
|
||||
|
||||
service.deleteExpiredPosts();
|
||||
|
||||
ArgumentCaptor<LocalDateTime> createdBefore = ArgumentCaptor.forClass(LocalDateTime.class);
|
||||
verify(postRepository).findByCreatedAtBefore(createdBefore.capture());
|
||||
assertThat(createdBefore.getValue()).isCloseTo(LocalDateTime.now().minusMonths(6), within(1, ChronoUnit.MINUTES));
|
||||
}
|
||||
|
||||
@Test
|
||||
void postThatIsBothClosedAndOldIsDeletedOnce() {
|
||||
Post closedAndOld = post(1L);
|
||||
Post onlyOld = post(2L);
|
||||
when(postRepository.findByStatusAndClosedAtBefore(any(), any())).thenReturn(List.of(closedAndOld));
|
||||
when(postRepository.findByCreatedAtBefore(any())).thenReturn(List.of(closedAndOld, onlyOld));
|
||||
|
||||
service.deleteExpiredPosts();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<Post>> deleted = ArgumentCaptor.forClass(List.class);
|
||||
verify(postDeletionService).delete(deleted.capture());
|
||||
assertThat(deleted.getValue()).containsExactly(closedAndOld, onlyOld);
|
||||
}
|
||||
|
||||
private static Post post(Long id) {
|
||||
Post post = new Post();
|
||||
post.setId(id);
|
||||
return post;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user