Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public interface BlockRepository extends JpaRepository<Block, Long> {

@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BlockBlockingResponse> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}