From 9dce55259eacb6c1bbebc10c0da8aaa67f2cdce8 Mon Sep 17 00:00:00 2001 From: YukiJanse Date: Fri, 6 Feb 2026 21:34:16 +0100 Subject: [PATCH 1/2] Added FollowService integration test and fixed a bug --- .../repository/FollowRepository.java | 2 +- .../service/FollowServiceIntegrationTest.java | 148 ++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/test/java/se/jensen/yuki/springboot/service/FollowServiceIntegrationTest.java diff --git a/src/main/java/se/jensen/yuki/springboot/repository/FollowRepository.java b/src/main/java/se/jensen/yuki/springboot/repository/FollowRepository.java index 7b0d093..8ab4312 100644 --- a/src/main/java/se/jensen/yuki/springboot/repository/FollowRepository.java +++ b/src/main/java/se/jensen/yuki/springboot/repository/FollowRepository.java @@ -79,7 +79,7 @@ CASE WHEN (SELECT COUNT(f3) FROM Follow f3 WHERE f3.follower.id = :userId AND f3 @Modifying @Transactional - @Query(value = "INSERT INTO follows (follower_id, followed_id, created_at) VALUES (:currentUserId, :targetUserId, CURRENT_TIMESTAMP)", nativeQuery = true) + @Query(value = "INSERT INTO follows (follower_id, followed_id, created_at, updated_at) VALUES (:currentUserId, :targetUserId, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)", nativeQuery = true) void createFollowRelationship(@Param("currentUserId") Long currentUserId, @Param("targetUserId") Long targetUserId); @Modifying diff --git a/src/test/java/se/jensen/yuki/springboot/service/FollowServiceIntegrationTest.java b/src/test/java/se/jensen/yuki/springboot/service/FollowServiceIntegrationTest.java new file mode 100644 index 0000000..152f8e6 --- /dev/null +++ b/src/test/java/se/jensen/yuki/springboot/service/FollowServiceIntegrationTest.java @@ -0,0 +1,148 @@ +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.test.context.ActiveProfiles; +import org.springframework.transaction.annotation.Transactional; +import se.jensen.yuki.springboot.dto.follow.FollowResponse; +import se.jensen.yuki.springboot.model.User; +import se.jensen.yuki.springboot.repository.FollowRepository; +import se.jensen.yuki.springboot.repository.UserRepository; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@ActiveProfiles("test") +@Transactional +public class FollowServiceIntegrationTest { + @Autowired + private FollowService followService; + + @Autowired + private UserRepository userRepository; + + @Autowired + private FollowRepository followRepository; + + + private User userA; + private User userB; + + + @BeforeEach + void setup() { + + followRepository.deleteAll(); + userRepository.deleteAll(); + + userA = userRepository.save( + User.builder() + .username("userA") + .email("a@test.com") + .password("pw") + .role("USER") + .displayName("A") + .bio("bio") + .build() + ); + + userB = userRepository.save( + User.builder() + .username("userB") + .email("b@test.com") + .password("pw") + .role("USER") + .displayName("B") + .bio("bio") + .build() + ); + } + + + // ---------- follow ---------- + + @Test + void followUser_whenNotFollowed_createsRelation() { + + FollowResponse response = + followService.followUser( + userA.getId(), + userB.getId() + ); + + assertThat(response).isNotNull(); + + assertThat( + followRepository.existsByFollowerIdAndFollowedId( + userA.getId(), + userB.getId() + ) + ).isTrue(); + } + + + @Test + void followUser_whenAlreadyFollowed_doesNotDuplicate() { + + // manually create the relationship to simulate the "already followed" state + followRepository.createFollowRelationship( + userA.getId(), + userB.getId() + ); + + long countBefore = followRepository.count(); + + FollowResponse response = + followService.followUser( + userA.getId(), + userB.getId() + ); + + long countAfter = followRepository.count(); + + assertThat(response).isNotNull(); + assertThat(countAfter).isEqualTo(countBefore); + } + + + // ---------- unfollow ---------- + + @Test + void unfollowUser_whenExists_deletes() { + + followRepository.createFollowRelationship( + userA.getId(), + userB.getId() + ); + + boolean result = + followService.unfollowUser( + userA.getId(), + userB.getId() + ); + + assertThat(result).isTrue(); + + assertThat( + followRepository.existsByFollowerIdAndFollowedId( + userA.getId(), + userB.getId() + ) + ).isFalse(); + } + + + @Test + void unfollowUser_whenNotExists_returnsFalse() { + + boolean result = + followService.unfollowUser( + userA.getId(), + userB.getId() + ); + + assertThat(result).isFalse(); + } +} From d84b38fcf9042467e5a2cb28c61b299af7b84b3b Mon Sep 17 00:00:00 2001 From: YukiJanse Date: Fri, 6 Feb 2026 21:46:19 +0100 Subject: [PATCH 2/2] Added BlockService integration test and fixed a bug --- .../repository/BlockRepository.java | 4 +- .../service/BlockServiceIntegrationTest.java | 158 ++++++++++++++++++ 2 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/test/java/se/jensen/yuki/springboot/service/BlockServiceIntegrationTest.java diff --git a/src/main/java/se/jensen/yuki/springboot/repository/BlockRepository.java b/src/main/java/se/jensen/yuki/springboot/repository/BlockRepository.java index f70abf5..721219d 100644 --- a/src/main/java/se/jensen/yuki/springboot/repository/BlockRepository.java +++ b/src/main/java/se/jensen/yuki/springboot/repository/BlockRepository.java @@ -46,11 +46,13 @@ public interface BlockRepository extends JpaRepository { @Modifying @Transactional - @Query(value = "INSERT INTO blocks (blocking_id, blocked_id, created_at) VALUES (:currentUserId, :targetUserId, CURRENT_TIMESTAMP)", nativeQuery = true) + @Query(value = "INSERT INTO blocks (blocking_id, blocked_id, created_at, updated_at) VALUES (:currentUserId, :targetUserId, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)", nativeQuery = true) void createBlockRelationship(@Param("currentUserId") Long currentUserId, @Param("targetUserId") Long targetUserId); @Modifying @Transactional @Query(value = "DELETE FROM blocks WHERE blocking_id = :currentUserId AND blocked_id = :targetUserId", nativeQuery = true) void deleteBlockRelationship(@Param("currentUserId") Long currentUserId, @Param("targetUserId") Long targetUserId); + + long countByBlockingIdAndBlockedId(Long id, Long id1); } diff --git a/src/test/java/se/jensen/yuki/springboot/service/BlockServiceIntegrationTest.java b/src/test/java/se/jensen/yuki/springboot/service/BlockServiceIntegrationTest.java new file mode 100644 index 0000000..c602307 --- /dev/null +++ b/src/test/java/se/jensen/yuki/springboot/service/BlockServiceIntegrationTest.java @@ -0,0 +1,158 @@ +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.block.BlockBlockingResponse; +import se.jensen.yuki.springboot.dto.block.BlockResponse; +import se.jensen.yuki.springboot.model.User; +import se.jensen.yuki.springboot.repository.BlockRepository; +import se.jensen.yuki.springboot.repository.UserRepository; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@ActiveProfiles("test") +@Transactional +class BlockServiceIntegrationTest { + + @Autowired + private BlockService blockService; + + @Autowired + private UserRepository userRepository; + + @Autowired + private BlockRepository blockRepository; + + private User userA; + private User userB; + + @BeforeEach + void setUp() { + + userA = userRepository.save( + User.builder() + .username("userA") + .email("a@test.com") + .password("pass") + .displayName("User A") + .bio("bioA") + .role("USER") + .build() + ); + + userB = userRepository.save( + User.builder() + .username("userB") + .email("b@test.com") + .password("pass") + .displayName("User B") + .bio("bioB") + .role("USER") + .build() + ); + } + + // ------------------------------- + // blockUser + // ------------------------------- + + @Test + void blockUser_shouldCreateNewBlock() { + + BlockResponse response = + blockService.blockUser(userA.getId(), userB.getId()); + + assertThat(response).isNotNull(); + assertThat(response.blockedUserId()).isEqualTo(userB.getId()); + + boolean exists = + blockRepository.existsByBlockingIdAndBlockedId( + userA.getId(), userB.getId()); + + assertThat(exists).isTrue(); + } + + + @Test + void blockUser_shouldNotDuplicateBlock() { + + // first + blockService.blockUser(userA.getId(), userB.getId()); + + // second + BlockResponse response = + blockService.blockUser(userA.getId(), userB.getId()); + + assertThat(response).isNotNull(); + + long count = + blockRepository.countByBlockingIdAndBlockedId( + userA.getId(), userB.getId()); + + assertThat(count).isEqualTo(1); + } + + + // ------------------------------- + // unblockUser + // ------------------------------- + + @Test + void unblockUser_shouldRemoveBlock() { + + blockService.blockUser(userA.getId(), userB.getId()); + + boolean result = + blockService.unblockUser(userA.getId(), userB.getId()); + + assertThat(result).isTrue(); + + boolean exists = + blockRepository.existsByBlockingIdAndBlockedId( + userA.getId(), userB.getId()); + + assertThat(exists).isFalse(); + } + + + @Test + void unblockUser_shouldReturnFalseWhenNotExists() { + + boolean result = + blockService.unblockUser(userA.getId(), userB.getId()); + + assertThat(result).isFalse(); + } + + + // ------------------------------- + // getBlockings + // ------------------------------- + + @Test + void getBlockings_shouldReturnBlockedUsers() { + + blockService.blockUser(userA.getId(), userB.getId()); + + Slice slice = + blockService.getBlockings( + userA.getId(), + PageRequest.of(0, 10) + ); + + assertThat(slice).isNotNull(); + assertThat(slice.getContent()).hasSize(1); + + BlockBlockingResponse res = + slice.getContent().get(0); + + assertThat(res.blockedUserId()).isEqualTo(userB.getId()); + } +}