mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-12 08:46:09 +00:00
fix: 后端代码格式化
This commit is contained in:
@@ -1,41 +1,51 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.repository.CommentRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.ReactionRepository;
|
||||
import com.openisle.repository.CommentSubscriptionRepository;
|
||||
import com.openisle.repository.NotificationRepository;
|
||||
import com.openisle.repository.PointHistoryRepository;
|
||||
import com.openisle.service.PointService;
|
||||
import com.openisle.exception.RateLimitException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.openisle.exception.RateLimitException;
|
||||
import com.openisle.repository.CommentRepository;
|
||||
import com.openisle.repository.CommentSubscriptionRepository;
|
||||
import com.openisle.repository.NotificationRepository;
|
||||
import com.openisle.repository.PointHistoryRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.ReactionRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.service.PointService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CommentServiceTest {
|
||||
@Test
|
||||
void addCommentRespectsRateLimit() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
CommentSubscriptionRepository subRepo = mock(CommentSubscriptionRepository.class);
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
PointHistoryRepository pointHistoryRepo = mock(PointHistoryRepository.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
|
||||
CommentService service = new CommentService(commentRepo, postRepo, userRepo,
|
||||
notifService, subService, reactionRepo, subRepo, nRepo, pointHistoryRepo, pointService, imageUploader);
|
||||
@Test
|
||||
void addCommentRespectsRateLimit() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
CommentSubscriptionRepository subRepo = mock(CommentSubscriptionRepository.class);
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
PointHistoryRepository pointHistoryRepo = mock(PointHistoryRepository.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
|
||||
when(commentRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(3L);
|
||||
CommentService service = new CommentService(
|
||||
commentRepo,
|
||||
postRepo,
|
||||
userRepo,
|
||||
notifService,
|
||||
subService,
|
||||
reactionRepo,
|
||||
subRepo,
|
||||
nRepo,
|
||||
pointHistoryRepo,
|
||||
pointService,
|
||||
imageUploader
|
||||
);
|
||||
|
||||
assertThrows(RateLimitException.class,
|
||||
() -> service.addComment("alice", 1L, "hi"));
|
||||
}
|
||||
when(commentRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(3L);
|
||||
|
||||
assertThrows(RateLimitException.class, () -> service.addComment("alice", 1L, "hi"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.openisle.repository.ImageRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.openisle.repository.ImageRepository;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CosImageUploaderTest {
|
||||
@Test
|
||||
void uploadReturnsUrl() {
|
||||
COSClient client = mock(COSClient.class);
|
||||
ImageRepository repo = mock(ImageRepository.class);
|
||||
CosImageUploader uploader = new CosImageUploader(client, repo, "bucket", "http://cos.example.com");
|
||||
|
||||
String url = uploader.upload("data".getBytes(), "img.png").join();
|
||||
@Test
|
||||
void uploadReturnsUrl() {
|
||||
COSClient client = mock(COSClient.class);
|
||||
ImageRepository repo = mock(ImageRepository.class);
|
||||
CosImageUploader uploader = new CosImageUploader(
|
||||
client,
|
||||
repo,
|
||||
"bucket",
|
||||
"http://cos.example.com"
|
||||
);
|
||||
|
||||
verify(client).putObject(any(PutObjectRequest.class));
|
||||
assertTrue(url.matches("http://cos.example.com/dynamic_assert/[a-f0-9]{32}\\.png"));
|
||||
}
|
||||
String url = uploader.upload("data".getBytes(), "img.png").join();
|
||||
|
||||
verify(client).putObject(any(PutObjectRequest.class));
|
||||
assertTrue(url.matches("http://cos.example.com/dynamic_assert/[a-f0-9]{32}\\.png"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,107 +1,165 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.openisle.dto.MedalDto;
|
||||
import com.openisle.model.MedalType;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.repository.CommentRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MedalServiceTest {
|
||||
@Test
|
||||
void getMedalsWithoutUser() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
@Test
|
||||
void getMedalsWithoutUser() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
|
||||
List<MedalDto> medals = service.getMedals(null);
|
||||
medals.forEach(m -> assertFalse(m.isCompleted()));
|
||||
assertEquals(6, medals.size());
|
||||
}
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
|
||||
@Test
|
||||
void getMedalsWithUser() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
List<MedalDto> medals = service.getMedals(null);
|
||||
medals.forEach(m -> assertFalse(m.isCompleted()));
|
||||
assertEquals(6, medals.size());
|
||||
}
|
||||
|
||||
when(commentRepo.countByAuthor_Id(1L)).thenReturn(120L);
|
||||
when(postRepo.countByAuthor_Id(1L)).thenReturn(80L);
|
||||
when(contributorService.getContributionLines(anyString())).thenReturn(0L);
|
||||
when(userRepo.countByCreatedAtBefore(any())).thenReturn(50L);
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setCreatedAt(LocalDateTime.of(2025, 9, 15, 0, 0));
|
||||
when(userRepo.findById(1L)).thenReturn(Optional.of(user));
|
||||
when(userRepo.findByUsername("user")).thenReturn(Optional.of(user));
|
||||
@Test
|
||||
void getMedalsWithUser() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
List<MedalDto> medals = service.getMedals(1L);
|
||||
when(commentRepo.countByAuthor_Id(1L)).thenReturn(120L);
|
||||
when(postRepo.countByAuthor_Id(1L)).thenReturn(80L);
|
||||
when(contributorService.getContributionLines(anyString())).thenReturn(0L);
|
||||
when(userRepo.countByCreatedAtBefore(any())).thenReturn(50L);
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setCreatedAt(LocalDateTime.of(2025, 9, 15, 0, 0));
|
||||
when(userRepo.findById(1L)).thenReturn(Optional.of(user));
|
||||
when(userRepo.findByUsername("user")).thenReturn(Optional.of(user));
|
||||
|
||||
assertEquals(MedalType.COMMENT, user.getDisplayMedal());
|
||||
assertTrue(medals.stream().filter(m -> m.getType() == MedalType.COMMENT).findFirst().orElseThrow().isCompleted());
|
||||
assertTrue(medals.stream().filter(m -> m.getType() == MedalType.COMMENT).findFirst().orElseThrow().isSelected());
|
||||
assertFalse(medals.stream().filter(m -> m.getType() == MedalType.POST).findFirst().orElseThrow().isCompleted());
|
||||
assertFalse(medals.stream().filter(m -> m.getType() == MedalType.POST).findFirst().orElseThrow().isSelected());
|
||||
assertTrue(medals.stream().filter(m -> m.getType() == MedalType.SEED).findFirst().orElseThrow().isCompleted());
|
||||
assertFalse(medals.stream().filter(m -> m.getType() == MedalType.SEED).findFirst().orElseThrow().isSelected());
|
||||
assertTrue(medals.stream().filter(m -> m.getType() == MedalType.PIONEER).findFirst().orElseThrow().isCompleted());
|
||||
assertFalse(medals.stream().filter(m -> m.getType() == MedalType.PIONEER).findFirst().orElseThrow().isSelected());
|
||||
verify(userRepo).save(user);
|
||||
}
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
List<MedalDto> medals = service.getMedals(1L);
|
||||
|
||||
@Test
|
||||
void selectMedal() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
assertEquals(MedalType.COMMENT, user.getDisplayMedal());
|
||||
assertTrue(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.COMMENT)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isCompleted()
|
||||
);
|
||||
assertTrue(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.COMMENT)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isSelected()
|
||||
);
|
||||
assertFalse(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.POST)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isCompleted()
|
||||
);
|
||||
assertFalse(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.POST)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isSelected()
|
||||
);
|
||||
assertTrue(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.SEED)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isCompleted()
|
||||
);
|
||||
assertFalse(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.SEED)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isSelected()
|
||||
);
|
||||
assertTrue(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.PIONEER)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isCompleted()
|
||||
);
|
||||
assertFalse(
|
||||
medals
|
||||
.stream()
|
||||
.filter(m -> m.getType() == MedalType.PIONEER)
|
||||
.findFirst()
|
||||
.orElseThrow()
|
||||
.isSelected()
|
||||
);
|
||||
verify(userRepo).save(user);
|
||||
}
|
||||
|
||||
when(commentRepo.countByAuthor_Id(1L)).thenReturn(120L);
|
||||
when(postRepo.countByAuthor_Id(1L)).thenReturn(0L);
|
||||
when(contributorService.getContributionLines(anyString())).thenReturn(0L);
|
||||
when(userRepo.countByCreatedAtBefore(any())).thenReturn(0L);
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setCreatedAt(LocalDateTime.of(2025, 9, 15, 0, 0));
|
||||
when(userRepo.findByUsername("user")).thenReturn(Optional.of(user));
|
||||
when(userRepo.findById(1L)).thenReturn(Optional.of(user));
|
||||
@Test
|
||||
void selectMedal() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
service.selectMedal("user", MedalType.COMMENT);
|
||||
assertEquals(MedalType.COMMENT, user.getDisplayMedal());
|
||||
}
|
||||
when(commentRepo.countByAuthor_Id(1L)).thenReturn(120L);
|
||||
when(postRepo.countByAuthor_Id(1L)).thenReturn(0L);
|
||||
when(contributorService.getContributionLines(anyString())).thenReturn(0L);
|
||||
when(userRepo.countByCreatedAtBefore(any())).thenReturn(0L);
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setCreatedAt(LocalDateTime.of(2025, 9, 15, 0, 0));
|
||||
when(userRepo.findByUsername("user")).thenReturn(Optional.of(user));
|
||||
when(userRepo.findById(1L)).thenReturn(Optional.of(user));
|
||||
|
||||
@Test
|
||||
void selectMedalNotCompleted() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
service.selectMedal("user", MedalType.COMMENT);
|
||||
assertEquals(MedalType.COMMENT, user.getDisplayMedal());
|
||||
}
|
||||
|
||||
when(commentRepo.countByAuthor_Id(1L)).thenReturn(10L);
|
||||
when(postRepo.countByAuthor_Id(1L)).thenReturn(0L);
|
||||
when(contributorService.getContributionLines(anyString())).thenReturn(0L);
|
||||
when(userRepo.countByCreatedAtBefore(any())).thenReturn(0L);
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setCreatedAt(LocalDateTime.of(2025, 9, 15, 0, 0));
|
||||
when(userRepo.findByUsername("user")).thenReturn(Optional.of(user));
|
||||
when(userRepo.findById(1L)).thenReturn(Optional.of(user));
|
||||
@Test
|
||||
void selectMedalNotCompleted() {
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
ContributorService contributorService = mock(ContributorService.class);
|
||||
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
assertThrows(IllegalArgumentException.class, () -> service.selectMedal("user", MedalType.COMMENT));
|
||||
}
|
||||
when(commentRepo.countByAuthor_Id(1L)).thenReturn(10L);
|
||||
when(postRepo.countByAuthor_Id(1L)).thenReturn(0L);
|
||||
when(contributorService.getContributionLines(anyString())).thenReturn(0L);
|
||||
when(userRepo.countByCreatedAtBefore(any())).thenReturn(0L);
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setCreatedAt(LocalDateTime.of(2025, 9, 15, 0, 0));
|
||||
when(userRepo.findByUsername("user")).thenReturn(Optional.of(user));
|
||||
when(userRepo.findById(1L)).thenReturn(Optional.of(user));
|
||||
|
||||
MedalService service = new MedalService(commentRepo, postRepo, userRepo, contributorService);
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
service.selectMedal("user", MedalType.COMMENT)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,274 +1,420 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.NotificationRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.ReactionRepository;
|
||||
import com.openisle.service.PushNotificationService;
|
||||
import java.util.concurrent.Executor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.HashSet;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.NotificationRepository;
|
||||
import com.openisle.repository.ReactionRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.service.PushNotificationService;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Executor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
class NotificationServiceTest {
|
||||
|
||||
@Test
|
||||
void markReadUpdatesOnlyOwnedNotifications() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void markReadUpdatesOnlyOwnedNotifications() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setUsername("alice");
|
||||
when(uRepo.findByUsername("alice")).thenReturn(Optional.of(user));
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setUsername("alice");
|
||||
when(uRepo.findByUsername("alice")).thenReturn(Optional.of(user));
|
||||
|
||||
Notification n1 = new Notification();
|
||||
n1.setId(10L);
|
||||
n1.setUser(user);
|
||||
Notification n2 = new Notification();
|
||||
n2.setId(11L);
|
||||
n2.setUser(user);
|
||||
when(nRepo.findAllById(List.of(10L, 11L))).thenReturn(List.of(n1, n2));
|
||||
Notification n1 = new Notification();
|
||||
n1.setId(10L);
|
||||
n1.setUser(user);
|
||||
Notification n2 = new Notification();
|
||||
n2.setId(11L);
|
||||
n2.setUser(user);
|
||||
when(nRepo.findAllById(List.of(10L, 11L))).thenReturn(List.of(n1, n2));
|
||||
|
||||
service.markRead("alice", List.of(10L, 11L));
|
||||
service.markRead("alice", List.of(10L, 11L));
|
||||
|
||||
assertTrue(n1.isRead());
|
||||
assertTrue(n2.isRead());
|
||||
verify(nRepo).saveAll(List.of(n1, n2));
|
||||
}
|
||||
assertTrue(n1.isRead());
|
||||
assertTrue(n2.isRead());
|
||||
verify(nRepo).saveAll(List.of(n1, n2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listNotificationsWithoutFilter() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void listNotificationsWithoutFilter() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User user = new User();
|
||||
user.setId(2L);
|
||||
user.setUsername("bob");
|
||||
user.setDisabledNotificationTypes(new HashSet<>());
|
||||
when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
||||
User user = new User();
|
||||
user.setId(2L);
|
||||
user.setUsername("bob");
|
||||
user.setDisabledNotificationTypes(new HashSet<>());
|
||||
when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
||||
|
||||
Notification n = new Notification();
|
||||
when(nRepo.findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class)))
|
||||
.thenReturn(new PageImpl<>(List.of(n)));
|
||||
Notification n = new Notification();
|
||||
when(nRepo.findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class))).thenReturn(
|
||||
new PageImpl<>(List.of(n))
|
||||
);
|
||||
|
||||
List<Notification> list = service.listNotifications("bob", null, 0, 10);
|
||||
List<Notification> list = service.listNotifications("bob", null, 0, 10);
|
||||
|
||||
assertEquals(1, list.size());
|
||||
verify(nRepo).findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class));
|
||||
}
|
||||
assertEquals(1, list.size());
|
||||
verify(nRepo).findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void countUnreadReturnsRepositoryValue() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void countUnreadReturnsRepositoryValue() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User user = new User();
|
||||
user.setId(3L);
|
||||
user.setUsername("carl");
|
||||
user.setDisabledNotificationTypes(new HashSet<>());
|
||||
when(uRepo.findByUsername("carl")).thenReturn(Optional.of(user));
|
||||
when(nRepo.countByUserAndRead(user, false)).thenReturn(5L);
|
||||
User user = new User();
|
||||
user.setId(3L);
|
||||
user.setUsername("carl");
|
||||
user.setDisabledNotificationTypes(new HashSet<>());
|
||||
when(uRepo.findByUsername("carl")).thenReturn(Optional.of(user));
|
||||
when(nRepo.countByUserAndRead(user, false)).thenReturn(5L);
|
||||
|
||||
long count = service.countUnread("carl");
|
||||
long count = service.countUnread("carl");
|
||||
|
||||
assertEquals(5L, count);
|
||||
verify(nRepo).countByUserAndRead(user, false);
|
||||
}
|
||||
assertEquals(5L, count);
|
||||
verify(nRepo).countByUserAndRead(user, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listNotificationsFiltersDisabledTypes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void listNotificationsFiltersDisabledTypes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User user = new User();
|
||||
user.setId(4L);
|
||||
user.setUsername("dana");
|
||||
when(uRepo.findByUsername("dana")).thenReturn(Optional.of(user));
|
||||
User user = new User();
|
||||
user.setId(4L);
|
||||
user.setUsername("dana");
|
||||
when(uRepo.findByUsername("dana")).thenReturn(Optional.of(user));
|
||||
|
||||
Notification n = new Notification();
|
||||
when(nRepo.findByUserAndTypeNotInOrderByCreatedAtDesc(eq(user), eq(user.getDisabledNotificationTypes()), any(Pageable.class)))
|
||||
.thenReturn(new PageImpl<>(List.of(n)));
|
||||
Notification n = new Notification();
|
||||
when(
|
||||
nRepo.findByUserAndTypeNotInOrderByCreatedAtDesc(
|
||||
eq(user),
|
||||
eq(user.getDisabledNotificationTypes()),
|
||||
any(Pageable.class)
|
||||
)
|
||||
).thenReturn(new PageImpl<>(List.of(n)));
|
||||
|
||||
List<Notification> list = service.listNotifications("dana", null, 0, 10);
|
||||
List<Notification> list = service.listNotifications("dana", null, 0, 10);
|
||||
|
||||
assertEquals(1, list.size());
|
||||
verify(nRepo).findByUserAndTypeNotInOrderByCreatedAtDesc(eq(user), eq(user.getDisabledNotificationTypes()), any(Pageable.class));
|
||||
}
|
||||
assertEquals(1, list.size());
|
||||
verify(nRepo).findByUserAndTypeNotInOrderByCreatedAtDesc(
|
||||
eq(user),
|
||||
eq(user.getDisabledNotificationTypes()),
|
||||
any(Pageable.class)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void countUnreadFiltersDisabledTypes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void countUnreadFiltersDisabledTypes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User user = new User();
|
||||
user.setId(5L);
|
||||
user.setUsername("erin");
|
||||
when(uRepo.findByUsername("erin")).thenReturn(Optional.of(user));
|
||||
when(nRepo.countByUserAndReadAndTypeNotIn(eq(user), eq(false), eq(user.getDisabledNotificationTypes())))
|
||||
.thenReturn(2L);
|
||||
User user = new User();
|
||||
user.setId(5L);
|
||||
user.setUsername("erin");
|
||||
when(uRepo.findByUsername("erin")).thenReturn(Optional.of(user));
|
||||
when(
|
||||
nRepo.countByUserAndReadAndTypeNotIn(
|
||||
eq(user),
|
||||
eq(false),
|
||||
eq(user.getDisabledNotificationTypes())
|
||||
)
|
||||
).thenReturn(2L);
|
||||
|
||||
long count = service.countUnread("erin");
|
||||
long count = service.countUnread("erin");
|
||||
|
||||
assertEquals(2L, count);
|
||||
verify(nRepo).countByUserAndReadAndTypeNotIn(eq(user), eq(false), eq(user.getDisabledNotificationTypes()));
|
||||
}
|
||||
assertEquals(2L, count);
|
||||
verify(nRepo).countByUserAndReadAndTypeNotIn(
|
||||
eq(user),
|
||||
eq(false),
|
||||
eq(user.getDisabledNotificationTypes())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createRegisterRequestNotificationsDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void createRegisterRequestNotificationsDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User admin = new User();
|
||||
admin.setId(10L);
|
||||
User applicant = new User();
|
||||
applicant.setId(20L);
|
||||
User admin = new User();
|
||||
admin.setId(10L);
|
||||
User applicant = new User();
|
||||
applicant.setId(20L);
|
||||
|
||||
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
|
||||
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
|
||||
|
||||
service.createRegisterRequestNotifications(applicant, "reason");
|
||||
service.createRegisterRequestNotifications(applicant, "reason");
|
||||
|
||||
verify(nRepo).deleteByTypeAndFromUser(NotificationType.REGISTER_REQUEST, applicant);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
verify(nRepo).deleteByTypeAndFromUser(NotificationType.REGISTER_REQUEST, applicant);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createActivityRedeemNotificationsDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void createActivityRedeemNotificationsDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User admin = new User();
|
||||
admin.setId(10L);
|
||||
User user = new User();
|
||||
user.setId(20L);
|
||||
User admin = new User();
|
||||
admin.setId(10L);
|
||||
User user = new User();
|
||||
user.setId(20L);
|
||||
|
||||
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
|
||||
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
|
||||
|
||||
service.createActivityRedeemNotifications(user, "contact");
|
||||
service.createActivityRedeemNotifications(user, "contact");
|
||||
|
||||
verify(nRepo).deleteByTypeAndFromUser(NotificationType.ACTIVITY_REDEEM, user);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
verify(nRepo).deleteByTypeAndFromUser(NotificationType.ACTIVITY_REDEEM, user);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createPointRedeemNotificationsDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void createPointRedeemNotificationsDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User admin = new User();
|
||||
admin.setId(10L);
|
||||
User user = new User();
|
||||
user.setId(20L);
|
||||
User admin = new User();
|
||||
admin.setId(10L);
|
||||
User user = new User();
|
||||
user.setId(20L);
|
||||
|
||||
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
|
||||
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
|
||||
|
||||
service.createPointRedeemNotifications(user, "contact");
|
||||
service.createPointRedeemNotifications(user, "contact");
|
||||
|
||||
verify(nRepo).deleteByTypeAndFromUser(NotificationType.POINT_REDEEM, user);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
verify(nRepo).deleteByTypeAndFromUser(NotificationType.POINT_REDEEM, user);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createNotificationSendsEmailForCommentReply() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void createNotificationSendsEmailForCommentReply() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User user = new User();
|
||||
user.setEmail("a@a.com");
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
Comment comment = new Comment();
|
||||
comment.setId(2L);
|
||||
when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
|
||||
User user = new User();
|
||||
user.setEmail("a@a.com");
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
Comment comment = new Comment();
|
||||
comment.setId(2L);
|
||||
when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
|
||||
|
||||
service.createNotification(user, NotificationType.COMMENT_REPLY, post, comment, null, null, null, null);
|
||||
service.createNotification(
|
||||
user,
|
||||
NotificationType.COMMENT_REPLY,
|
||||
post,
|
||||
comment,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
verify(email).sendEmail("a@a.com", "有人回复了你", "https://ex.com/posts/1#comment-2");
|
||||
verify(push).sendNotification(eq(user), contains("/posts/1#comment-2"));
|
||||
}
|
||||
verify(email).sendEmail("a@a.com", "有人回复了你", "https://ex.com/posts/1#comment-2");
|
||||
verify(push).sendNotification(eq(user), contains("/posts/1#comment-2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void postViewedNotificationDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
@Test
|
||||
void postViewedNotificationDeletesOldOnes() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
PushNotificationService push = mock(PushNotificationService.class);
|
||||
Executor executor = Runnable::run;
|
||||
NotificationService service = new NotificationService(
|
||||
nRepo,
|
||||
uRepo,
|
||||
email,
|
||||
push,
|
||||
rRepo,
|
||||
executor
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
User owner = new User();
|
||||
User viewer = new User();
|
||||
Post post = new Post();
|
||||
User owner = new User();
|
||||
User viewer = new User();
|
||||
Post post = new Post();
|
||||
|
||||
when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
|
||||
when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
|
||||
|
||||
service.createNotification(owner, NotificationType.POST_VIEWED, post, null, null, viewer, null, null);
|
||||
service.createNotification(
|
||||
owner,
|
||||
NotificationType.POST_VIEWED,
|
||||
post,
|
||||
null,
|
||||
null,
|
||||
viewer,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
verify(nRepo).deleteByTypeAndFromUserAndPost(NotificationType.POST_VIEWED, viewer, post);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
verify(nRepo).deleteByTypeAndFromUserAndPost(NotificationType.POST_VIEWED, viewer, post);
|
||||
verify(nRepo).save(any(Notification.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,40 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.PasswordStrength;
|
||||
import com.openisle.exception.FieldException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.openisle.exception.FieldException;
|
||||
import com.openisle.model.PasswordStrength;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PasswordValidatorTest {
|
||||
|
||||
@Test
|
||||
void lowStrengthRequiresSixChars() {
|
||||
PasswordValidator validator = new PasswordValidator(PasswordStrength.LOW);
|
||||
@Test
|
||||
void lowStrengthRequiresSixChars() {
|
||||
PasswordValidator validator = new PasswordValidator(PasswordStrength.LOW);
|
||||
|
||||
assertThrows(FieldException.class, () -> validator.validate("12345"));
|
||||
assertDoesNotThrow(() -> validator.validate("123456"));
|
||||
}
|
||||
assertThrows(FieldException.class, () -> validator.validate("12345"));
|
||||
assertDoesNotThrow(() -> validator.validate("123456"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mediumStrengthRules() {
|
||||
PasswordValidator validator = new PasswordValidator(PasswordStrength.MEDIUM);
|
||||
@Test
|
||||
void mediumStrengthRules() {
|
||||
PasswordValidator validator = new PasswordValidator(PasswordStrength.MEDIUM);
|
||||
|
||||
assertThrows(FieldException.class, () -> validator.validate("abc123"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("abcdefgh"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("12345678"));
|
||||
assertDoesNotThrow(() -> validator.validate("abcd1234"));
|
||||
}
|
||||
assertThrows(FieldException.class, () -> validator.validate("abc123"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("abcdefgh"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("12345678"));
|
||||
assertDoesNotThrow(() -> validator.validate("abcd1234"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void highStrengthRules() {
|
||||
PasswordValidator validator = new PasswordValidator(PasswordStrength.HIGH);
|
||||
@Test
|
||||
void highStrengthRules() {
|
||||
PasswordValidator validator = new PasswordValidator(PasswordStrength.HIGH);
|
||||
|
||||
assertThrows(FieldException.class, () -> validator.validate("Abc123$"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("abcd1234$xyz"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("ABCD1234$XYZ"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("AbcdABCDabcd"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("Abcd1234abcd"));
|
||||
assertDoesNotThrow(() -> validator.validate("Abcd1234$xyz"));
|
||||
}
|
||||
assertThrows(FieldException.class, () -> validator.validate("Abc123$"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("abcd1234$xyz"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("ABCD1234$XYZ"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("AbcdABCDabcd"));
|
||||
assertThrows(FieldException.class, () -> validator.validate("Abcd1234abcd"));
|
||||
assertDoesNotThrow(() -> validator.validate("Abcd1234$xyz"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,67 +1,66 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.openisle.model.PointHistory;
|
||||
import com.openisle.model.PointHistoryType;
|
||||
import com.openisle.model.Role;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.repository.PointHistoryRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import java.time.LocalDateTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@DataJpaTest
|
||||
@Import(PointService.class)
|
||||
class PointServiceRecalculateUserPointsTest {
|
||||
|
||||
@Autowired
|
||||
private PointService pointService;
|
||||
@Autowired
|
||||
private PointService pointService;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private PointHistoryRepository pointHistoryRepository;
|
||||
@Autowired
|
||||
private PointHistoryRepository pointHistoryRepository;
|
||||
|
||||
@Test
|
||||
void recalculatesBalanceAfterDeletion() {
|
||||
User user = new User();
|
||||
user.setUsername("u");
|
||||
user.setEmail("u@example.com");
|
||||
user.setPassword("p");
|
||||
user.setRole(Role.USER);
|
||||
userRepository.save(user);
|
||||
@Test
|
||||
void recalculatesBalanceAfterDeletion() {
|
||||
User user = new User();
|
||||
user.setUsername("u");
|
||||
user.setEmail("u@example.com");
|
||||
user.setPassword("p");
|
||||
user.setRole(Role.USER);
|
||||
userRepository.save(user);
|
||||
|
||||
PointHistory h1 = new PointHistory();
|
||||
h1.setUser(user);
|
||||
h1.setType(PointHistoryType.POST);
|
||||
h1.setAmount(30);
|
||||
h1.setBalance(30);
|
||||
h1.setCreatedAt(LocalDateTime.now().minusMinutes(2));
|
||||
pointHistoryRepository.save(h1);
|
||||
PointHistory h1 = new PointHistory();
|
||||
h1.setUser(user);
|
||||
h1.setType(PointHistoryType.POST);
|
||||
h1.setAmount(30);
|
||||
h1.setBalance(30);
|
||||
h1.setCreatedAt(LocalDateTime.now().minusMinutes(2));
|
||||
pointHistoryRepository.save(h1);
|
||||
|
||||
PointHistory h2 = new PointHistory();
|
||||
h2.setUser(user);
|
||||
h2.setType(PointHistoryType.COMMENT);
|
||||
h2.setAmount(10);
|
||||
h2.setBalance(40);
|
||||
h2.setCreatedAt(LocalDateTime.now().minusMinutes(1));
|
||||
pointHistoryRepository.save(h2);
|
||||
PointHistory h2 = new PointHistory();
|
||||
h2.setUser(user);
|
||||
h2.setType(PointHistoryType.COMMENT);
|
||||
h2.setAmount(10);
|
||||
h2.setBalance(40);
|
||||
h2.setCreatedAt(LocalDateTime.now().minusMinutes(1));
|
||||
pointHistoryRepository.save(h2);
|
||||
|
||||
user.setPoint(40);
|
||||
userRepository.save(user);
|
||||
user.setPoint(40);
|
||||
userRepository.save(user);
|
||||
|
||||
pointHistoryRepository.delete(h1);
|
||||
pointHistoryRepository.delete(h1);
|
||||
|
||||
int total = pointService.recalculateUserPoints(user);
|
||||
int total = pointService.recalculateUserPoints(user);
|
||||
|
||||
assertEquals(10, total);
|
||||
assertEquals(10, userRepository.findById(user.getId()).orElseThrow().getPoint());
|
||||
assertEquals(10, pointHistoryRepository.findById(h2.getId()).orElseThrow().getBalance());
|
||||
}
|
||||
assertEquals(10, total);
|
||||
assertEquals(10, userRepository.findById(user.getId()).orElseThrow().getPoint());
|
||||
assertEquals(10, pointHistoryRepository.findById(h2.getId()).orElseThrow().getBalance());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -8,83 +10,81 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@TestPropertySource(locations = "classpath:application.properties")
|
||||
@Transactional
|
||||
public class PostCommentStatsTest {
|
||||
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private CategoryRepository categoryRepository;
|
||||
@Autowired
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
@Autowired
|
||||
private TagRepository tagRepository;
|
||||
@Autowired
|
||||
private TagRepository tagRepository;
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
@Test
|
||||
public void testPostCommentStatsUpdate() {
|
||||
// Create test user
|
||||
User user = new User();
|
||||
user.setUsername("testuser");
|
||||
user.setEmail("test@example.com");
|
||||
user.setPassword("hash");
|
||||
user = userRepository.save(user);
|
||||
@Test
|
||||
public void testPostCommentStatsUpdate() {
|
||||
// Create test user
|
||||
User user = new User();
|
||||
user.setUsername("testuser");
|
||||
user.setEmail("test@example.com");
|
||||
user.setPassword("hash");
|
||||
user = userRepository.save(user);
|
||||
|
||||
// Create test category
|
||||
Category category = new Category();
|
||||
category.setName("Test Category");
|
||||
category.setDescription("Test Category Description");
|
||||
category.setIcon("test-icon");
|
||||
category = categoryRepository.save(category);
|
||||
// Create test category
|
||||
Category category = new Category();
|
||||
category.setName("Test Category");
|
||||
category.setDescription("Test Category Description");
|
||||
category.setIcon("test-icon");
|
||||
category = categoryRepository.save(category);
|
||||
|
||||
// Create test tag
|
||||
Tag tag = new Tag();
|
||||
tag.setName("Test Tag");
|
||||
tag.setDescription("Test Tag Description");
|
||||
tag.setIcon("test-tag-icon");
|
||||
tag = tagRepository.save(tag);
|
||||
// Create test tag
|
||||
Tag tag = new Tag();
|
||||
tag.setName("Test Tag");
|
||||
tag.setDescription("Test Tag Description");
|
||||
tag.setIcon("test-tag-icon");
|
||||
tag = tagRepository.save(tag);
|
||||
|
||||
// Create test post
|
||||
Post post = new Post();
|
||||
post.setTitle("Test Post");
|
||||
post.setContent("Test content");
|
||||
post.setAuthor(user);
|
||||
post.setCategory(category);
|
||||
post.getTags().add(tag);
|
||||
post.setStatus(PostStatus.PUBLISHED);
|
||||
post.setCommentCount(0L);
|
||||
post = postRepository.save(post);
|
||||
// Create test post
|
||||
Post post = new Post();
|
||||
post.setTitle("Test Post");
|
||||
post.setContent("Test content");
|
||||
post.setAuthor(user);
|
||||
post.setCategory(category);
|
||||
post.getTags().add(tag);
|
||||
post.setStatus(PostStatus.PUBLISHED);
|
||||
post.setCommentCount(0L);
|
||||
post = postRepository.save(post);
|
||||
|
||||
// Verify initial state
|
||||
assertEquals(0L, post.getCommentCount());
|
||||
assertNull(post.getLastReplyAt());
|
||||
// Verify initial state
|
||||
assertEquals(0L, post.getCommentCount());
|
||||
assertNull(post.getLastReplyAt());
|
||||
|
||||
// Add a comment
|
||||
commentService.addComment("testuser", post.getId(), "Test comment");
|
||||
// Add a comment
|
||||
commentService.addComment("testuser", post.getId(), "Test comment");
|
||||
|
||||
// Refresh post from database
|
||||
post = postRepository.findById(post.getId()).orElseThrow();
|
||||
// Refresh post from database
|
||||
post = postRepository.findById(post.getId()).orElseThrow();
|
||||
|
||||
// Verify comment count and last reply time are updated
|
||||
assertEquals(1L, post.getCommentCount());
|
||||
assertNotNull(post.getLastReplyAt());
|
||||
// Verify comment count and last reply time are updated
|
||||
assertEquals(1L, post.getCommentCount());
|
||||
assertNotNull(post.getLastReplyAt());
|
||||
|
||||
// Add another comment
|
||||
commentService.addComment("testuser", post.getId(), "Another comment");
|
||||
// Add another comment
|
||||
commentService.addComment("testuser", post.getId(), "Another comment");
|
||||
|
||||
// Refresh post again
|
||||
post = postRepository.findById(post.getId()).orElseThrow();
|
||||
// Refresh post again
|
||||
post = postRepository.findById(post.getId()).orElseThrow();
|
||||
|
||||
// Verify comment count is updated
|
||||
assertEquals(2L, post.getCommentCount());
|
||||
}
|
||||
// Verify comment count is updated
|
||||
assertEquals(2L, post.getCommentCount());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,300 +1,444 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.openisle.exception.RateLimitException;
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.*;
|
||||
import com.openisle.exception.RateLimitException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
|
||||
class PostServiceTest {
|
||||
@Test
|
||||
void deletePostRemovesReads() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
|
||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService,
|
||||
pointHistoryRepository, PublishMode.DIRECT, redisTemplate);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
@Test
|
||||
void deletePostRemovesReads() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
User author = new User();
|
||||
author.setId(1L);
|
||||
author.setRole(Role.USER);
|
||||
post.setAuthor(author);
|
||||
PostService service = new PostService(
|
||||
postRepo,
|
||||
userRepo,
|
||||
catRepo,
|
||||
tagRepo,
|
||||
lotteryRepo,
|
||||
pollPostRepo,
|
||||
pollVoteRepo,
|
||||
notifService,
|
||||
subService,
|
||||
commentService,
|
||||
commentRepo,
|
||||
reactionRepo,
|
||||
subRepo,
|
||||
notificationRepo,
|
||||
postReadService,
|
||||
imageUploader,
|
||||
taskScheduler,
|
||||
emailSender,
|
||||
context,
|
||||
pointService,
|
||||
postChangeLogService,
|
||||
pointHistoryRepository,
|
||||
PublishMode.DIRECT,
|
||||
redisTemplate
|
||||
);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
|
||||
when(postRepo.findById(1L)).thenReturn(Optional.of(post));
|
||||
when(userRepo.findByUsername("alice")).thenReturn(Optional.of(author));
|
||||
when(commentRepo.findByPostAndParentIsNullOrderByCreatedAtAsc(post)).thenReturn(List.of());
|
||||
when(reactionRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(subRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(notificationRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(pointHistoryRepository.findByPost(post)).thenReturn(List.of());
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
User author = new User();
|
||||
author.setId(1L);
|
||||
author.setRole(Role.USER);
|
||||
post.setAuthor(author);
|
||||
|
||||
service.deletePost(1L, "alice");
|
||||
when(postRepo.findById(1L)).thenReturn(Optional.of(post));
|
||||
when(userRepo.findByUsername("alice")).thenReturn(Optional.of(author));
|
||||
when(commentRepo.findByPostAndParentIsNullOrderByCreatedAtAsc(post)).thenReturn(List.of());
|
||||
when(reactionRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(subRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(notificationRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(pointHistoryRepository.findByPost(post)).thenReturn(List.of());
|
||||
|
||||
verify(postReadService).deleteByPost(post);
|
||||
verify(postRepo).delete(post);
|
||||
verify(postChangeLogService).deleteLogsForPost(post);
|
||||
}
|
||||
service.deletePost(1L, "alice");
|
||||
|
||||
@Test
|
||||
void deletePostByAdminNotifiesAuthor() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
verify(postReadService).deleteByPost(post);
|
||||
verify(postRepo).delete(post);
|
||||
verify(postChangeLogService).deleteLogsForPost(post);
|
||||
}
|
||||
|
||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService,
|
||||
pointHistoryRepository, PublishMode.DIRECT, redisTemplate);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
@Test
|
||||
void deletePostByAdminNotifiesAuthor() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
post.setTitle("T");
|
||||
post.setContent("");
|
||||
User author = new User();
|
||||
author.setId(2L);
|
||||
author.setRole(Role.USER);
|
||||
post.setAuthor(author);
|
||||
PostService service = new PostService(
|
||||
postRepo,
|
||||
userRepo,
|
||||
catRepo,
|
||||
tagRepo,
|
||||
lotteryRepo,
|
||||
pollPostRepo,
|
||||
pollVoteRepo,
|
||||
notifService,
|
||||
subService,
|
||||
commentService,
|
||||
commentRepo,
|
||||
reactionRepo,
|
||||
subRepo,
|
||||
notificationRepo,
|
||||
postReadService,
|
||||
imageUploader,
|
||||
taskScheduler,
|
||||
emailSender,
|
||||
context,
|
||||
pointService,
|
||||
postChangeLogService,
|
||||
pointHistoryRepository,
|
||||
PublishMode.DIRECT,
|
||||
redisTemplate
|
||||
);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
|
||||
User admin = new User();
|
||||
admin.setId(1L);
|
||||
admin.setRole(Role.ADMIN);
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
post.setTitle("T");
|
||||
post.setContent("");
|
||||
User author = new User();
|
||||
author.setId(2L);
|
||||
author.setRole(Role.USER);
|
||||
post.setAuthor(author);
|
||||
|
||||
when(postRepo.findById(1L)).thenReturn(Optional.of(post));
|
||||
when(userRepo.findByUsername("admin")).thenReturn(Optional.of(admin));
|
||||
when(commentRepo.findByPostAndParentIsNullOrderByCreatedAtAsc(post)).thenReturn(List.of());
|
||||
when(reactionRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(subRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(notificationRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(pointHistoryRepository.findByPost(post)).thenReturn(List.of());
|
||||
User admin = new User();
|
||||
admin.setId(1L);
|
||||
admin.setRole(Role.ADMIN);
|
||||
|
||||
service.deletePost(1L, "admin");
|
||||
when(postRepo.findById(1L)).thenReturn(Optional.of(post));
|
||||
when(userRepo.findByUsername("admin")).thenReturn(Optional.of(admin));
|
||||
when(commentRepo.findByPostAndParentIsNullOrderByCreatedAtAsc(post)).thenReturn(List.of());
|
||||
when(reactionRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(subRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(notificationRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(pointHistoryRepository.findByPost(post)).thenReturn(List.of());
|
||||
|
||||
verify(notifService).createNotification(eq(author), eq(NotificationType.POST_DELETED), isNull(),
|
||||
isNull(), isNull(), eq(admin), isNull(), eq("T"));
|
||||
}
|
||||
service.deletePost(1L, "admin");
|
||||
|
||||
@Test
|
||||
void createPostRespectsRateLimit() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
verify(notifService).createNotification(
|
||||
eq(author),
|
||||
eq(NotificationType.POST_DELETED),
|
||||
isNull(),
|
||||
isNull(),
|
||||
isNull(),
|
||||
eq(admin),
|
||||
isNull(),
|
||||
eq("T")
|
||||
);
|
||||
}
|
||||
|
||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService,
|
||||
pointHistoryRepository, PublishMode.DIRECT, redisTemplate);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
@Test
|
||||
void createPostRespectsRateLimit() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
|
||||
when(postRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(1L);
|
||||
PostService service = new PostService(
|
||||
postRepo,
|
||||
userRepo,
|
||||
catRepo,
|
||||
tagRepo,
|
||||
lotteryRepo,
|
||||
pollPostRepo,
|
||||
pollVoteRepo,
|
||||
notifService,
|
||||
subService,
|
||||
commentService,
|
||||
commentRepo,
|
||||
reactionRepo,
|
||||
subRepo,
|
||||
notificationRepo,
|
||||
postReadService,
|
||||
imageUploader,
|
||||
taskScheduler,
|
||||
emailSender,
|
||||
context,
|
||||
pointService,
|
||||
postChangeLogService,
|
||||
pointHistoryRepository,
|
||||
PublishMode.DIRECT,
|
||||
redisTemplate
|
||||
);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
|
||||
assertThrows(RateLimitException.class,
|
||||
() -> service.createPost("alice", 1L, "t", "c", List.of(1L),
|
||||
null, null, null, null, null, null, null, null, null));
|
||||
}
|
||||
when(postRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(1L);
|
||||
|
||||
@Test
|
||||
void deletePostRemovesPointHistoriesAndRecalculatesPoints() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
assertThrows(RateLimitException.class, () ->
|
||||
service.createPost(
|
||||
"alice",
|
||||
1L,
|
||||
"t",
|
||||
"c",
|
||||
List.of(1L),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService,
|
||||
pointHistoryRepository, PublishMode.DIRECT, redisTemplate);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
@Test
|
||||
void deletePostRemovesPointHistoriesAndRecalculatesPoints() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
|
||||
Post post = new Post();
|
||||
post.setId(10L);
|
||||
User author = new User();
|
||||
author.setId(20L);
|
||||
author.setRole(Role.USER);
|
||||
post.setAuthor(author);
|
||||
PostService service = new PostService(
|
||||
postRepo,
|
||||
userRepo,
|
||||
catRepo,
|
||||
tagRepo,
|
||||
lotteryRepo,
|
||||
pollPostRepo,
|
||||
pollVoteRepo,
|
||||
notifService,
|
||||
subService,
|
||||
commentService,
|
||||
commentRepo,
|
||||
reactionRepo,
|
||||
subRepo,
|
||||
notificationRepo,
|
||||
postReadService,
|
||||
imageUploader,
|
||||
taskScheduler,
|
||||
emailSender,
|
||||
context,
|
||||
pointService,
|
||||
postChangeLogService,
|
||||
pointHistoryRepository,
|
||||
PublishMode.DIRECT,
|
||||
redisTemplate
|
||||
);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
|
||||
User historyUser = new User();
|
||||
historyUser.setId(30L);
|
||||
Post post = new Post();
|
||||
post.setId(10L);
|
||||
User author = new User();
|
||||
author.setId(20L);
|
||||
author.setRole(Role.USER);
|
||||
post.setAuthor(author);
|
||||
|
||||
PointHistory history = new PointHistory();
|
||||
history.setUser(historyUser);
|
||||
history.setPost(post);
|
||||
User historyUser = new User();
|
||||
historyUser.setId(30L);
|
||||
|
||||
when(postRepo.findById(10L)).thenReturn(Optional.of(post));
|
||||
when(userRepo.findByUsername("author")).thenReturn(Optional.of(author));
|
||||
when(commentRepo.findByPostAndParentIsNullOrderByCreatedAtAsc(post)).thenReturn(List.of());
|
||||
when(reactionRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(subRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(notificationRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(pointHistoryRepository.findByPost(post)).thenReturn(List.of(history));
|
||||
when(pointService.recalculateUserPoints(historyUser)).thenReturn(0);
|
||||
PointHistory history = new PointHistory();
|
||||
history.setUser(historyUser);
|
||||
history.setPost(post);
|
||||
|
||||
service.deletePost(10L, "author");
|
||||
when(postRepo.findById(10L)).thenReturn(Optional.of(post));
|
||||
when(userRepo.findByUsername("author")).thenReturn(Optional.of(author));
|
||||
when(commentRepo.findByPostAndParentIsNullOrderByCreatedAtAsc(post)).thenReturn(List.of());
|
||||
when(reactionRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(subRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(notificationRepo.findByPost(post)).thenReturn(List.of());
|
||||
when(pointHistoryRepository.findByPost(post)).thenReturn(List.of(history));
|
||||
when(pointService.recalculateUserPoints(historyUser)).thenReturn(0);
|
||||
|
||||
ArgumentCaptor<List<PointHistory>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(pointHistoryRepository).saveAll(captor.capture());
|
||||
List<PointHistory> savedHistories = captor.getValue();
|
||||
assertEquals(1, savedHistories.size());
|
||||
PointHistory savedHistory = savedHistories.get(0);
|
||||
assertNull(savedHistory.getPost());
|
||||
assertNotNull(savedHistory.getDeletedAt());
|
||||
assertTrue(savedHistory.getDeletedAt().isBefore(LocalDateTime.now().plusSeconds(1)));
|
||||
service.deletePost(10L, "author");
|
||||
|
||||
verify(pointService).recalculateUserPoints(historyUser);
|
||||
verify(userRepo).saveAll(any());
|
||||
}
|
||||
ArgumentCaptor<List<PointHistory>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(pointHistoryRepository).saveAll(captor.capture());
|
||||
List<PointHistory> savedHistories = captor.getValue();
|
||||
assertEquals(1, savedHistories.size());
|
||||
PointHistory savedHistory = savedHistories.get(0);
|
||||
assertNull(savedHistory.getPost());
|
||||
assertNotNull(savedHistory.getDeletedAt());
|
||||
assertTrue(savedHistory.getDeletedAt().isBefore(LocalDateTime.now().plusSeconds(1)));
|
||||
|
||||
@Test
|
||||
void finalizeLotteryNotifiesAuthor() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
verify(pointService).recalculateUserPoints(historyUser);
|
||||
verify(userRepo).saveAll(any());
|
||||
}
|
||||
|
||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService,
|
||||
pointHistoryRepository, PublishMode.DIRECT, redisTemplate);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
@Test
|
||||
void finalizeLotteryNotifiesAuthor() {
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = mock(TagRepository.class);
|
||||
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||
PollPostRepository pollPostRepo = mock(PollPostRepository.class);
|
||||
PollVoteRepository pollVoteRepo = mock(PollVoteRepository.class);
|
||||
NotificationService notifService = mock(NotificationService.class);
|
||||
SubscriptionService subService = mock(SubscriptionService.class);
|
||||
CommentService commentService = mock(CommentService.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||
PostReadService postReadService = mock(PostReadService.class);
|
||||
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
EmailSender emailSender = mock(EmailSender.class);
|
||||
ApplicationContext context = mock(ApplicationContext.class);
|
||||
PointService pointService = mock(PointService.class);
|
||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||
|
||||
User author = new User();
|
||||
author.setId(1L);
|
||||
User winner = new User();
|
||||
winner.setId(2L);
|
||||
PostService service = new PostService(
|
||||
postRepo,
|
||||
userRepo,
|
||||
catRepo,
|
||||
tagRepo,
|
||||
lotteryRepo,
|
||||
pollPostRepo,
|
||||
pollVoteRepo,
|
||||
notifService,
|
||||
subService,
|
||||
commentService,
|
||||
commentRepo,
|
||||
reactionRepo,
|
||||
subRepo,
|
||||
notificationRepo,
|
||||
postReadService,
|
||||
imageUploader,
|
||||
taskScheduler,
|
||||
emailSender,
|
||||
context,
|
||||
pointService,
|
||||
postChangeLogService,
|
||||
pointHistoryRepository,
|
||||
PublishMode.DIRECT,
|
||||
redisTemplate
|
||||
);
|
||||
when(context.getBean(PostService.class)).thenReturn(service);
|
||||
|
||||
LotteryPost lp = new LotteryPost();
|
||||
lp.setId(1L);
|
||||
lp.setAuthor(author);
|
||||
lp.setTitle("L");
|
||||
lp.setPrizeCount(1);
|
||||
lp.getParticipants().add(winner);
|
||||
User author = new User();
|
||||
author.setId(1L);
|
||||
User winner = new User();
|
||||
winner.setId(2L);
|
||||
|
||||
when(lotteryRepo.findById(1L)).thenReturn(Optional.of(lp));
|
||||
LotteryPost lp = new LotteryPost();
|
||||
lp.setId(1L);
|
||||
lp.setAuthor(author);
|
||||
lp.setTitle("L");
|
||||
lp.setPrizeCount(1);
|
||||
lp.getParticipants().add(winner);
|
||||
|
||||
service.finalizeLottery(1L);
|
||||
when(lotteryRepo.findById(1L)).thenReturn(Optional.of(lp));
|
||||
|
||||
verify(notifService).createNotification(eq(winner), eq(NotificationType.LOTTERY_WIN), eq(lp), isNull(), isNull(), eq(author), isNull(), isNull());
|
||||
verify(notifService).createNotification(eq(author), eq(NotificationType.LOTTERY_DRAW), eq(lp), isNull(), isNull(), isNull(), isNull(), isNull());
|
||||
}
|
||||
service.finalizeLottery(1L);
|
||||
|
||||
verify(notifService).createNotification(
|
||||
eq(winner),
|
||||
eq(NotificationType.LOTTERY_WIN),
|
||||
eq(lp),
|
||||
isNull(),
|
||||
isNull(),
|
||||
eq(author),
|
||||
isNull(),
|
||||
isNull()
|
||||
);
|
||||
verify(notifService).createNotification(
|
||||
eq(author),
|
||||
eq(NotificationType.LOTTERY_DRAW),
|
||||
eq(lp),
|
||||
isNull(),
|
||||
isNull(),
|
||||
isNull(),
|
||||
isNull(),
|
||||
isNull()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,59 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.*;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ReactionServiceTest {
|
||||
@Test
|
||||
void reactToPostSendsEmailEveryFive() {
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
MessageRepository messageRepo = mock(MessageRepository.class);
|
||||
NotificationService notif = mock(NotificationService.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
ReactionService service = new ReactionService(reactionRepo, userRepo, postRepo, commentRepo, messageRepo, notif, email);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setUsername("bob");
|
||||
User author = new User();
|
||||
author.setId(2L);
|
||||
author.setEmail("a@a.com");
|
||||
Post post = new Post();
|
||||
post.setId(3L);
|
||||
post.setAuthor(author);
|
||||
@Test
|
||||
void reactToPostSendsEmailEveryFive() {
|
||||
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||
UserRepository userRepo = mock(UserRepository.class);
|
||||
PostRepository postRepo = mock(PostRepository.class);
|
||||
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||
MessageRepository messageRepo = mock(MessageRepository.class);
|
||||
NotificationService notif = mock(NotificationService.class);
|
||||
EmailSender email = mock(EmailSender.class);
|
||||
ReactionService service = new ReactionService(
|
||||
reactionRepo,
|
||||
userRepo,
|
||||
postRepo,
|
||||
commentRepo,
|
||||
messageRepo,
|
||||
notif,
|
||||
email
|
||||
);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(
|
||||
service,
|
||||
"websiteUrl",
|
||||
"https://ex.com"
|
||||
);
|
||||
|
||||
when(userRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
||||
when(postRepo.findById(3L)).thenReturn(Optional.of(post));
|
||||
when(reactionRepo.findByUserAndPostAndType(user, post, ReactionType.LIKE)).thenReturn(Optional.empty());
|
||||
when(reactionRepo.save(any(Reaction.class))).thenAnswer(i -> i.getArgument(0));
|
||||
when(reactionRepo.countReceived(author.getUsername())).thenReturn(5L);
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setUsername("bob");
|
||||
User author = new User();
|
||||
author.setId(2L);
|
||||
author.setEmail("a@a.com");
|
||||
Post post = new Post();
|
||||
post.setId(3L);
|
||||
post.setAuthor(author);
|
||||
|
||||
service.reactToPost("bob", 3L, ReactionType.LIKE);
|
||||
when(userRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
||||
when(postRepo.findById(3L)).thenReturn(Optional.of(post));
|
||||
when(reactionRepo.findByUserAndPostAndType(user, post, ReactionType.LIKE)).thenReturn(
|
||||
Optional.empty()
|
||||
);
|
||||
when(reactionRepo.save(any(Reaction.class))).thenAnswer(i -> i.getArgument(0));
|
||||
when(reactionRepo.countReceived(author.getUsername())).thenReturn(5L);
|
||||
|
||||
verify(email).sendEmail("a@a.com", "你有新的互动", "https://ex.com/messages");
|
||||
verify(notif).sendCustomPush(author, "你有新的互动", "https://ex.com/messages");
|
||||
}
|
||||
service.reactToPost("bob", 3L, ReactionType.LIKE);
|
||||
|
||||
verify(email).sendEmail("a@a.com", "你有新的互动", "https://ex.com/messages");
|
||||
verify(notif).sendCustomPush(author, "你有新的互动", "https://ex.com/messages");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,51 +1,66 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.model.PostStatus;
|
||||
import com.openisle.repository.CommentRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import com.openisle.repository.CategoryRepository;
|
||||
import com.openisle.repository.TagRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.model.PostStatus;
|
||||
import com.openisle.repository.CategoryRepository;
|
||||
import com.openisle.repository.CommentRepository;
|
||||
import com.openisle.repository.PostRepository;
|
||||
import com.openisle.repository.TagRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
class SearchServiceTest {
|
||||
|
||||
@Test
|
||||
void globalSearchDeduplicatesPosts() {
|
||||
UserRepository userRepo = Mockito.mock(UserRepository.class);
|
||||
PostRepository postRepo = Mockito.mock(PostRepository.class);
|
||||
CommentRepository commentRepo = Mockito.mock(CommentRepository.class);
|
||||
CategoryRepository categoryRepo = Mockito.mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = Mockito.mock(TagRepository.class);
|
||||
SearchService service = new SearchService(userRepo, postRepo, commentRepo, categoryRepo, tagRepo);
|
||||
@Test
|
||||
void globalSearchDeduplicatesPosts() {
|
||||
UserRepository userRepo = Mockito.mock(UserRepository.class);
|
||||
PostRepository postRepo = Mockito.mock(PostRepository.class);
|
||||
CommentRepository commentRepo = Mockito.mock(CommentRepository.class);
|
||||
CategoryRepository categoryRepo = Mockito.mock(CategoryRepository.class);
|
||||
TagRepository tagRepo = Mockito.mock(TagRepository.class);
|
||||
SearchService service = new SearchService(
|
||||
userRepo,
|
||||
postRepo,
|
||||
commentRepo,
|
||||
categoryRepo,
|
||||
tagRepo
|
||||
);
|
||||
|
||||
Post post1 = new Post();
|
||||
post1.setId(1L);
|
||||
post1.setTitle("hello");
|
||||
Post post2 = new Post();
|
||||
post2.setId(2L);
|
||||
post2.setTitle("world");
|
||||
Post post1 = new Post();
|
||||
post1.setId(1L);
|
||||
post1.setTitle("hello");
|
||||
Post post2 = new Post();
|
||||
post2.setId(2L);
|
||||
post2.setTitle("world");
|
||||
|
||||
Mockito.when(postRepo.findByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseAndStatus(
|
||||
Mockito.anyString(), Mockito.anyString(), Mockito.eq(PostStatus.PUBLISHED)))
|
||||
.thenReturn(List.of(post1));
|
||||
Mockito.when(postRepo.findByTitleContainingIgnoreCaseAndStatus(Mockito.anyString(), Mockito.eq(PostStatus.PUBLISHED)))
|
||||
.thenReturn(List.of(post1, post2));
|
||||
Mockito.when(commentRepo.findByContentContainingIgnoreCase(Mockito.anyString()))
|
||||
.thenReturn(List.of());
|
||||
Mockito.when(userRepo.findByUsernameContainingIgnoreCase(Mockito.anyString()))
|
||||
.thenReturn(List.of());
|
||||
Mockito.when(
|
||||
postRepo.findByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseAndStatus(
|
||||
Mockito.anyString(),
|
||||
Mockito.anyString(),
|
||||
Mockito.eq(PostStatus.PUBLISHED)
|
||||
)
|
||||
).thenReturn(List.of(post1));
|
||||
Mockito.when(
|
||||
postRepo.findByTitleContainingIgnoreCaseAndStatus(
|
||||
Mockito.anyString(),
|
||||
Mockito.eq(PostStatus.PUBLISHED)
|
||||
)
|
||||
).thenReturn(List.of(post1, post2));
|
||||
Mockito.when(commentRepo.findByContentContainingIgnoreCase(Mockito.anyString())).thenReturn(
|
||||
List.of()
|
||||
);
|
||||
Mockito.when(userRepo.findByUsernameContainingIgnoreCase(Mockito.anyString())).thenReturn(
|
||||
List.of()
|
||||
);
|
||||
|
||||
List<SearchService.SearchResult> results = service.globalSearch("h");
|
||||
List<SearchService.SearchResult> results = service.globalSearch("h");
|
||||
|
||||
assertEquals(2, results.size());
|
||||
assertEquals(1L, results.get(0).id());
|
||||
assertEquals(2L, results.get(1).id());
|
||||
}
|
||||
assertEquals(2, results.size());
|
||||
assertEquals(1L, results.get(0).id());
|
||||
assertEquals(2L, results.get(1).id());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.openisle.exception.FieldException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UsernameValidatorTest {
|
||||
|
||||
@Test
|
||||
void rejectsEmptyUsername() {
|
||||
UsernameValidator validator = new UsernameValidator();
|
||||
assertThrows(FieldException.class, () -> validator.validate(""));
|
||||
assertThrows(FieldException.class, () -> validator.validate(null));
|
||||
}
|
||||
@Test
|
||||
void rejectsEmptyUsername() {
|
||||
UsernameValidator validator = new UsernameValidator();
|
||||
assertThrows(FieldException.class, () -> validator.validate(""));
|
||||
assertThrows(FieldException.class, () -> validator.validate(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void allowsShortUsername() {
|
||||
UsernameValidator validator = new UsernameValidator();
|
||||
assertDoesNotThrow(() -> validator.validate("a"));
|
||||
}
|
||||
@Test
|
||||
void allowsShortUsername() {
|
||||
UsernameValidator validator = new UsernameValidator();
|
||||
assertDoesNotThrow(() -> validator.validate("a"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user