优化目录结构

This commit is contained in:
WilliamColton
2025-08-03 01:27:28 +08:00
parent d63081955e
commit c08723574d
222 changed files with 2 additions and 25 deletions

View File

@@ -0,0 +1,37 @@
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.exception.RateLimitException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
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);
ImageUploader imageUploader = mock(ImageUploader.class);
CommentService service = new CommentService(commentRepo, postRepo, userRepo,
notifService, subService, reactionRepo, subRepo, nRepo, imageUploader);
when(commentRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(3L);
assertThrows(RateLimitException.class,
() -> service.addComment("alice", 1L, "hi"));
}
}

View File

@@ -0,0 +1,23 @@
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.*;
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();
verify(client).putObject(any(PutObjectRequest.class));
assertTrue(url.matches("http://cos.example.com/dynamic_assert/[a-f0-9]{32}\\.png"));
}
}

View File

@@ -0,0 +1,194 @@
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 static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
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");
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));
service.markRead("alice", List.of(10L, 11L));
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");
User user = new User();
user.setId(2L);
user.setUsername("bob");
when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user));
Notification n = new Notification();
when(nRepo.findByUserOrderByCreatedAtDesc(user)).thenReturn(List.of(n));
List<Notification> list = service.listNotifications("bob", null);
assertEquals(1, list.size());
verify(nRepo).findByUserOrderByCreatedAtDesc(user);
}
@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");
when(uRepo.findByUsername("carl")).thenReturn(Optional.of(user));
when(nRepo.countByUserAndRead(user, false)).thenReturn(5L);
long count = service.countUnread("carl");
assertEquals(5L, count);
verify(nRepo).countByUserAndRead(user, false);
}
@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);
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
service.createRegisterRequestNotifications(applicant, "reason");
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");
User admin = new User();
admin.setId(10L);
User user = new User();
user.setId(20L);
when(uRepo.findByRole(Role.ADMIN)).thenReturn(List.of(admin));
service.createActivityRedeemNotifications(user, "contact");
verify(nRepo).deleteByTypeAndFromUser(NotificationType.ACTIVITY_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");
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);
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");
User owner = new User();
User viewer = new User();
Post post = new Post();
when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
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));
}
}

View File

@@ -0,0 +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.*;
class PasswordValidatorTest {
@Test
void lowStrengthRequiresSixChars() {
PasswordValidator validator = new PasswordValidator(PasswordStrength.LOW);
assertThrows(FieldException.class, () -> validator.validate("12345"));
assertDoesNotThrow(() -> validator.validate("123456"));
}
@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"));
}
@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"));
}
}

View File

@@ -0,0 +1,83 @@
package com.openisle.service;
import com.openisle.model.*;
import com.openisle.repository.*;
import com.openisle.exception.RateLimitException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Optional;
import java.util.List;
import static org.mockito.Mockito.*;
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);
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);
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo,
notifService, subService, commentService, commentRepo,
reactionRepo, subRepo, notificationRepo, postReadService,
imageUploader, PublishMode.DIRECT);
Post post = new Post();
post.setId(1L);
User author = new User();
author.setId(1L);
author.setRole(Role.USER);
post.setAuthor(author);
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());
service.deletePost(1L, "alice");
verify(postReadService).deleteByPost(post);
verify(postRepo).delete(post);
}
@Test
void createPostRespectsRateLimit() {
PostRepository postRepo = mock(PostRepository.class);
UserRepository userRepo = mock(UserRepository.class);
CategoryRepository catRepo = mock(CategoryRepository.class);
TagRepository tagRepo = mock(TagRepository.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);
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo,
notifService, subService, commentService, commentRepo,
reactionRepo, subRepo, notificationRepo, postReadService,
imageUploader, PublishMode.DIRECT);
when(postRepo.countByAuthorAfter(eq("alice"), any())).thenReturn(1L);
assertThrows(RateLimitException.class,
() -> service.createPost("alice", 1L, "t", "c", List.of(1L)));
}
}

View File

@@ -0,0 +1,44 @@
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.*;
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);
NotificationService notif = mock(NotificationService.class);
EmailSender email = mock(EmailSender.class);
ReactionService service = new ReactionService(reactionRepo, userRepo, postRepo, commentRepo, 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);
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);
service.reactToPost("bob", 3L, ReactionType.LIKE);
verify(email).sendEmail("a@a.com", "你有新的互动", "https://ex.com/messages");
verify(notif).sendCustomPush(author, "你有新的互动", "https://ex.com/messages");
}
}

View File

@@ -0,0 +1,51 @@
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.*;
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);
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());
List<SearchService.SearchResult> results = service.globalSearch("h");
assertEquals(2, results.size());
assertEquals(1L, results.get(0).id());
assertEquals(2L, results.get(1).id());
}
}

View File

@@ -0,0 +1,22 @@
package com.openisle.service;
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 allowsShortUsername() {
UsernameValidator validator = new UsernameValidator();
assertDoesNotThrow(() -> validator.validate("a"));
}
}