Repository navigation
Feature/integration tests #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<PostLikeResponse> 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<CommentLikeResponse> 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()); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+79
to
+85
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| muteService.muteUser(user1.getId(), user2.getId()); | |
| MuteResponse res = | |
| muteService.muteUser(user1.getId(), user2.getId()); | |
| assertThat(res).isNotNull(); | |
| // First mute call creates the relationship | |
| muteService.muteUser(user1.getId(), user2.getId()); | |
| // Second mute call should not create a duplicate | |
| MuteResponse res = | |
| muteService.muteUser(user1.getId(), user2.getId()); | |
| assertThat(res).isNotNull(); | |
| // Verify only one mute relationship exists | |
| Slice<MuteMutingResponse> slice = | |
| muteService.getMutings( | |
| user1.getId(), | |
| PageRequest.of(0, 10) | |
| ); | |
| assertThat(slice).isNotNull(); | |
| assertThat(slice.getContent()).hasSize(1); | |
| assertThat(slice.getContent().get(0).mutedUserId()).isEqualTo(user2.getId()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using a wildcard repository import in tests; it makes it harder to see which repositories are actually used and is inconsistent with the other integration tests in this package (which import repositories explicitly). Replace
se.jensen.yuki.springboot.repository.*with the specific repository imports used by this test.