diff --git a/src/main/java/se/jensen/yuki/springboot/repository/MuteRepository.java b/src/main/java/se/jensen/yuki/springboot/repository/MuteRepository.java index c3e7ab8..acbe84c 100644 --- a/src/main/java/se/jensen/yuki/springboot/repository/MuteRepository.java +++ b/src/main/java/se/jensen/yuki/springboot/repository/MuteRepository.java @@ -46,7 +46,7 @@ public interface MuteRepository extends JpaRepository { @Modifying @Transactional - @Query(value = "INSERT INTO mutes (muting_id, muted_id, created_at) VALUES (:currentUserId, :targetUserId, CURRENT_TIMESTAMP)", nativeQuery = true) + @Query(value = "INSERT INTO mutes (muting_id, muted_id, created_at, updated_at) VALUES (:currentUserId, :targetUserId, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)", nativeQuery = true) void createMuteRelationship(@Param("currentUserId") Long currentUserId, @Param("targetUserId") Long targetUserId); @Modifying diff --git a/src/test/java/se/jensen/yuki/springboot/service/LikeServiceIntegrationTest.java b/src/test/java/se/jensen/yuki/springboot/service/LikeServiceIntegrationTest.java new file mode 100644 index 0000000..852fa99 --- /dev/null +++ b/src/test/java/se/jensen/yuki/springboot/service/LikeServiceIntegrationTest.java @@ -0,0 +1,219 @@ +package se.jensen.yuki.springboot.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Slice; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.transaction.annotation.Transactional; +import se.jensen.yuki.springboot.dto.like.CommentLikeResponse; +import se.jensen.yuki.springboot.dto.like.LikeResponse; +import se.jensen.yuki.springboot.dto.like.PostLikeResponse; +import se.jensen.yuki.springboot.model.Comment; +import se.jensen.yuki.springboot.model.Post; +import se.jensen.yuki.springboot.model.User; +import se.jensen.yuki.springboot.repository.*; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@ActiveProfiles("test") +@Transactional +class LikeServiceIntegrationTest { + + @Autowired + private LikeService likeService; + + @Autowired + private UserRepository userRepository; + + @Autowired + private PostRepository postRepository; + + @Autowired + private CommentRepository commentRepository; + + @Autowired + private PostLikeRepository postLikeRepository; + + @Autowired + private CommentLikeRepository commentLikeRepository; + + private User user; + private Post post; + private Comment comment; + + @BeforeEach + void setUp() { + + user = userRepository.save( + User.builder() + .username("user1") + .email("user1@test.com") + .password("pass") + .displayName("User1") + .bio("bio") + .role("USER") + .build() + ); + + post = postRepository.save( + Post.builder() + .content("test post") + .user(user) + .build() + ); + + comment = commentRepository.save( + Comment.builder() + .content("test comment") + .author(user) + .post(post) + .build() + ); + } + + // ----------------------- + // Post Like + // ----------------------- + + @Test + void likePost_shouldCreateLike() { + + LikeResponse res = + likeService.likePost(post.getId(), user.getId()); + + assertThat(res.liked()).isTrue(); + assertThat(res.likeCount()).isEqualTo(1); + + assertThat( + postLikeRepository.findByPostIdAndUserId( + post.getId(), user.getId() + ) + ).isPresent(); + } + + + @Test + void likePost_shouldNotDuplicate() { + + likeService.likePost(post.getId(), user.getId()); + + LikeResponse res = + likeService.likePost(post.getId(), user.getId()); + + assertThat(res.likeCount()).isEqualTo(1); + } + + + @Test + void unlikePost_shouldRemoveLike() { + + likeService.likePost(post.getId(), user.getId()); + + LikeResponse res = + likeService.unlikePost(post.getId(), user.getId()); + + assertThat(res.liked()).isFalse(); + assertThat(res.likeCount()).isEqualTo(0); + + assertThat( + postLikeRepository.findByPostIdAndUserId( + post.getId(), user.getId() + ) + ).isEmpty(); + } + + + // ----------------------- + // Post Like List + // ----------------------- + + @Test + void getPostLikes_shouldReturnList() { + + likeService.likePost(post.getId(), user.getId()); + + Slice slice = + likeService.getPostLikesByPostId( + post.getId(), + PageRequest.of(0, 10) + ); + + assertThat(slice).isNotNull(); + assertThat(slice.getContent()).hasSize(1); + + PostLikeResponse res = + slice.getContent().get(0); + + assertThat(res.userId()).isEqualTo(user.getId()); + } + + + // ----------------------- + // Comment Like + // ----------------------- + + @Test + void likeComment_shouldCreateLike() { + + LikeResponse res = + likeService.likeComment(comment.getId(), user.getId()); + + assertThat(res.liked()).isTrue(); + assertThat(res.likeCount()).isEqualTo(1); + + assertThat( + commentLikeRepository.findByCommentIdAndUserId( + comment.getId(), user.getId() + ) + ).isPresent(); + } + + + @Test + void unlikeComment_shouldRemoveLike() { + + likeService.likeComment(comment.getId(), user.getId()); + + LikeResponse res = + likeService.unlikeComment(comment.getId(), user.getId()); + + assertThat(res.liked()).isFalse(); + assertThat(res.likeCount()).isEqualTo(0); + + assertThat( + commentLikeRepository.findByCommentIdAndUserId( + comment.getId(), user.getId() + ) + ).isEmpty(); + } + + + // ----------------------- + // Comment Like List + // ----------------------- + + @Test + void getCommentLikes_shouldReturnList() { + + likeService.likeComment(comment.getId(), user.getId()); + + Slice slice = + likeService.getCommentLikesByCommentId( + comment.getId(), + PageRequest.of(0, 10) + ); + + assertThat(slice).isNotNull(); + assertThat(slice.getContent()).hasSize(1); + + CommentLikeResponse res = + slice.getContent().get(0); + + assertThat(res.userId()).isEqualTo(user.getId()); + } + +} diff --git a/src/test/java/se/jensen/yuki/springboot/service/MuteServiceIntegrationTest.java b/src/test/java/se/jensen/yuki/springboot/service/MuteServiceIntegrationTest.java new file mode 100644 index 0000000..9ec96a4 --- /dev/null +++ b/src/test/java/se/jensen/yuki/springboot/service/MuteServiceIntegrationTest.java @@ -0,0 +1,130 @@ +package se.jensen.yuki.springboot.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Slice; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.transaction.annotation.Transactional; +import se.jensen.yuki.springboot.dto.mute.MuteMutingResponse; +import se.jensen.yuki.springboot.dto.mute.MuteResponse; +import se.jensen.yuki.springboot.model.User; +import se.jensen.yuki.springboot.repository.UserRepository; + +import static org.assertj.core.api.Assertions.assertThat; + + +@SpringBootTest +@ActiveProfiles("test") +@Transactional +class MuteServiceIntegrationTest { + + @Autowired + private MuteService muteService; + + @Autowired + private UserRepository userRepository; + + + private User user1; + private User user2; + + + @BeforeEach + void setUp() { + + user1 = userRepository.save( + User.builder() + .username("user1") + .email("user1@test.com") + .password("pass") + .displayName("User1") + .bio("bio") + .role("USER") + .build() + ); + + user2 = userRepository.save( + User.builder() + .username("user2") + .email("user2@test.com") + .password("pass") + .displayName("User2") + .bio("bio") + .role("USER") + .build() + ); + } + + + // ----------------------- + // Mute + // ----------------------- + + @Test + void muteUser_shouldCreateMute() { + + MuteResponse res = + muteService.muteUser(user1.getId(), user2.getId()); + + assertThat(res).isNotNull(); + assertThat(res.mutedUserId()).isEqualTo(user2.getId()); + } + + + @Test + void muteUser_shouldNotDuplicate() { + + muteService.muteUser(user1.getId(), user2.getId()); + + MuteResponse res = + muteService.muteUser(user1.getId(), user2.getId()); + + assertThat(res).isNotNull(); + } + + + @Test + void unmuteUser_shouldRemoveMute() { + + muteService.muteUser(user1.getId(), user2.getId()); + + boolean result = + muteService.unmuteUser(user1.getId(), user2.getId()); + + assertThat(result).isTrue(); + + // 再度解除は false + boolean second = + muteService.unmuteUser(user1.getId(), user2.getId()); + + assertThat(second).isFalse(); + } + + + // ----------------------- + // List + // ----------------------- + + @Test + void getMutings_shouldReturnList() { + + muteService.muteUser(user1.getId(), user2.getId()); + + Slice slice = + muteService.getMutings( + user1.getId(), + PageRequest.of(0, 10) + ); + + assertThat(slice).isNotNull(); + assertThat(slice.getContent()).hasSize(1); + + MuteMutingResponse res = + slice.getContent().get(0); + + assertThat(res.mutedUserId()).isEqualTo(user2.getId()); + } +}