package SuMoC.backend.user.service; import SuMoC.backend.user.dtos.FriendshipDTO; import SuMoC.backend.user.dtos.MinimalUserDTO; import SuMoC.backend.user.enums.FriendShipStatus; import SuMoC.backend.user.models.Friendship; import SuMoC.backend.user.models.User; import SuMoC.backend.user.repository.FriendshipRepository; import SuMoC.backend.user.repository.UserRepository; import jakarta.transaction.Transactional; 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 java.util.List; import static SuMoC.backend.Factories.UserTestFactory.uniqueUser; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @ActiveProfiles("test") @Transactional public class FriendshipServiceTest { @Autowired private FriendshipService friendshipService; @Autowired private FriendshipRepository friendshipRepository; @Autowired private UserRepository userRepository; private User sender; private User friend; @BeforeEach void setUp() { sender = userRepository.save(uniqueUser(1)); friend = userRepository.save(uniqueUser(2)); } // ------------------------------------------------------------------------- // requestFriendship // ------------------------------------------------------------------------- @Test void requestFriendship_Success() { friendshipService.requestFriendship(sender, friend.getId()); Friendship senderToFriend = friendshipRepository.findByUserAndFriend(sender, friend); Friendship friendToSender = friendshipRepository.findByUserAndFriend(friend, sender); assertNotNull(senderToFriend); assertNotNull(friendToSender); assertEquals(FriendShipStatus.REQUESTED, senderToFriend.getStatus()); assertEquals(FriendShipStatus.PENDING, friendToSender.getStatus()); } @Test void requestFriendship_InvalidStates_Throw() { // friend not found assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, 99999L)); // sender is blocked friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.BLOCKED)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // already rejected friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.REJECTED)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // already pending: both sides (REQUESTED + PENDING) friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.REQUESTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.PENDING)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // already pending: only sender side exists, reverse missing -> unexpected throw friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.REQUESTED)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // already pending reverse: both sides (PENDING + REQUESTED) friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REQUESTED)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // already pending reverse: only sender side exists, reverse missing -> unexpected throw friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // already friends: both sides ACCEPTED friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // already friends: only sender side exists, reverse missing -> unexpected throw friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // corrupt state: REJECTED + unexpected second row friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REJECTED)); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // PENDING check: relation to Friend != REQUESTED, rel to Sender == PENDING friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.PENDING)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // ACCEPTED check: relation to Friend != ACCEPTED, rel to Sender == ACCEPTED friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.REQUESTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.requestFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); } @Test void requestFriendship_AfterRejection_Success() { // friend previously rejected senders request only the REJECTED row remains friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REJECTED)); friendshipService.requestFriendship(sender, friend.getId()); Friendship senderToFriend = friendshipRepository.findByUserAndFriend(sender, friend); Friendship friendToSender = friendshipRepository.findByUserAndFriend(friend, sender); assertNotNull(senderToFriend); assertNotNull(friendToSender); assertEquals(FriendShipStatus.REQUESTED, senderToFriend.getStatus()); assertEquals(FriendShipStatus.PENDING, friendToSender.getStatus()); } // ------------------------------------------------------------------------- // acceptFriendship // ------------------------------------------------------------------------- @Test void acceptFriendship_Success() { friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REQUESTED)); friendshipService.acceptFriendship(sender, friend.getId()); assertEquals(FriendShipStatus.ACCEPTED, friendshipRepository.findByUserAndFriend(sender, friend).getStatus()); assertEquals(FriendShipStatus.ACCEPTED, friendshipRepository.findByUserAndFriend(friend, sender).getStatus()); } @Test void acceptFriendship_InvalidStates_Throw() { // friend not found assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, 99999L)); // no relationship at all assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, friend.getId())); // only one side exists friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); friendshipRepository.flush(); assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // wrong status (already accepted) friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, friend.getId())); // wrong status: sender is REQUESTED instead of PENDING (swapped) friendshipRepository.deleteAllInBatch(); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.REQUESTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.PENDING)); assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); // wrong status: both ACCEPTED friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, friend.getId())); // relation to Friend != PENDING, relation to Sender == REQUESTED friendshipRepository.deleteAllInBatch(); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REQUESTED)); assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // relation to Friend == PENDING, relation to sender != REQUESTED friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.acceptFriendship(sender, friend.getId())); } // ------------------------------------------------------------------------- // rejectFriendship // ------------------------------------------------------------------------- @Test void rejectFriendship_Success() { friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REQUESTED)); friendshipService.rejectFriendship(sender, friend.getId()); assertNull(friendshipRepository.findByUserAndFriend(sender, friend)); assertEquals(FriendShipStatus.REJECTED, friendshipRepository.findByUserAndFriend(friend, sender).getStatus()); } @Test void rejectFriendship_InvalidStates_Throw() { // friend not found assertThrows(RuntimeException.class, () -> friendshipService.rejectFriendship(sender, 99999L)); // no relationship assertThrows(RuntimeException.class, () -> friendshipService.rejectFriendship(sender, friend.getId())); // wrong status friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.rejectFriendship(sender, friend.getId())); // only one side exists (sender has PENDING, friend missing) friendshipRepository.deleteAllInBatch(); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); assertThrows(RuntimeException.class, () -> friendshipService.rejectFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); // only one side exists (friend has REQUESTED, sender missing) friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REQUESTED)); assertThrows(RuntimeException.class, () -> friendshipService.rejectFriendship(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); // wrong status: sender is REQUESTED instead of PENDING friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.REQUESTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.PENDING)); assertThrows(RuntimeException.class, () -> friendshipService.rejectFriendship(sender, friend.getId())); } // ------------------------------------------------------------------------- // unfriend // ------------------------------------------------------------------------- @Test void unfriend_Success() { friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); friendshipService.unfriend(sender, friend.getId()); assertNull(friendshipRepository.findByUserAndFriend(sender, friend)); assertNull(friendshipRepository.findByUserAndFriend(friend, sender)); } @Test void unfriend_InvalidStates_Throw() { // friend not found assertThrows(RuntimeException.class, () -> friendshipService.unfriend(sender, 99999L)); // no relationship assertThrows(RuntimeException.class, () -> friendshipService.unfriend(sender, friend.getId())); // wrong status friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.PENDING)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.REQUESTED)); assertThrows(RuntimeException.class, () -> friendshipService.unfriend(sender, friend.getId())); // only one side exists (only sender has ACCEPTED) friendshipRepository.deleteAllInBatch(); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.unfriend(sender, friend.getId())); friendshipRepository.deleteAllInBatch(); // only one side exists (only friend has ACCEPTED) friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.unfriend(sender, friend.getId())); } // ------------------------------------------------------------------------- // blockUser // ------------------------------------------------------------------------- @Test void blockUser_CreatesBlocked() { // no prior relationship friendshipService.blockUser(sender, friend.getId()); assertNull(friendshipRepository.findByUserAndFriend(sender, friend)); assertEquals(FriendShipStatus.BLOCKED, friendshipRepository.findByUserAndFriend(friend, sender).getStatus()); } @Test void blockUser_DeletesPriorRelationship() { // friends -> block friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); friendshipService.blockUser(sender, friend.getId()); assertNull(friendshipRepository.findByUserAndFriend(sender, friend)); assertEquals(FriendShipStatus.BLOCKED, friendshipRepository.findByUserAndFriend(friend, sender).getStatus()); friendshipRepository.deleteAllInBatch(); friendshipRepository.flush(); // pending request -> block friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.REQUESTED)); friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.PENDING)); friendshipService.blockUser(sender, friend.getId()); assertNull(friendshipRepository.findByUserAndFriend(sender, friend)); assertEquals(FriendShipStatus.BLOCKED, friendshipRepository.findByUserAndFriend(friend, sender).getStatus()); } @Test void blockUser_FriendNotFound_Throws() { assertThrows(RuntimeException.class, () -> friendshipService.blockUser(sender, 99999L)); } // ------------------------------------------------------------------------- // unblockUser // ------------------------------------------------------------------------- @Test void unblockUser_Success() { friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.BLOCKED)); friendshipService.unblockUser(sender, friend.getId()); assertNull(friendshipRepository.findByUserAndFriend(friend, sender)); } @Test void unblockUser_InvalidStates_Throw() { // friend not found assertThrows(RuntimeException.class, () -> friendshipService.unblockUser(sender, 99999L)); // no relationship assertThrows(RuntimeException.class, () -> friendshipService.unblockUser(sender, friend.getId())); // wrong status friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.ACCEPTED)); assertThrows(RuntimeException.class, () -> friendshipService.unblockUser(sender, friend.getId())); } // ------------------------------------------------------------------------- // getFriendships / getAcceptedFriendships // ------------------------------------------------------------------------- @Test void getFriendships_ReturnsAllEntries_WithCorrectNames() { User third = userRepository.save(uniqueUser(3)); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(sender, third, FriendShipStatus.REQUESTED)); List all = friendshipService.getFriendships(sender.getId()); assertEquals(2, all.size()); // accepted -> private name all.stream() .filter(f -> f.getFriend().getId().equals(friend.getId())) .findFirst() .ifPresentOrElse( f -> assertEquals(friend.getPrivateName(), f.getFriend().getDisplayName()), () -> fail("Accepted friendship not found") ); // requested -> public name all.stream() .filter(f -> f.getFriend().getId().equals(third.getId())) .findFirst() .ifPresentOrElse( f -> assertEquals(third.getPublicName(), f.getFriend().getDisplayName()), () -> fail("Requested friendship not found") ); } @Test void getFriendships_Empty() { assertTrue(friendshipService.getFriendships(sender.getId()).isEmpty()); } @Test void getAcceptedFriendships_OnlyReturnsAccepted_WithPrivateName() { User third = userRepository.save(uniqueUser(3)); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(sender, third, FriendShipStatus.REQUESTED)); List result = friendshipService.getAcceptedFriendships(sender.getId()); assertEquals(1, result.size()); assertEquals(FriendShipStatus.ACCEPTED, result.get(0).getStatus()); assertEquals(friend.getPrivateName(), result.get(0).getFriend().getDisplayName()); } @Test void getAcceptedFriendships_Empty() { assertTrue(friendshipService.getAcceptedFriendships(sender.getId()).isEmpty()); } // ------------------------------------------------------------------------- // getBlockedUsers // ------------------------------------------------------------------------- @Test void getBlockedUsers_ReturnsBlockedUsers() { friendshipRepository.save(new Friendship(friend, sender, FriendShipStatus.BLOCKED)); List result = friendshipService.getBlockedUsers(sender.getId()); assertEquals(1, result.size()); assertEquals(friend.getId(), result.get(0).getId()); } @Test void getBlockedUsers_Empty() { assertTrue(friendshipService.getBlockedUsers(sender.getId()).isEmpty()); } // ------------------------------------------------------------------------- // getAcceptedFriends // ------------------------------------------------------------------------- @Test void getAcceptedFriends_ReturnsOnlyAccepted() { User third = userRepository.save(uniqueUser(3)); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); friendshipRepository.save(new Friendship(sender, third, FriendShipStatus.ACCEPTED)); List accepted = friendshipService.getAcceptedFriends(sender); assertEquals(2, accepted.size()); assertTrue(accepted.stream().anyMatch(u -> u.getId().equals(friend.getId()))); assertTrue(accepted.stream().anyMatch(u -> u.getId().equals(third.getId()))); } @Test void getAcceptedFriends_Empty() { assertTrue(friendshipService.getAcceptedFriends(sender).isEmpty()); } // ------------------------------------------------------------------------- // getPrivateNameIfFriendshipAccepted // ------------------------------------------------------------------------- @Test void getPrivateNameIfFriendshipAccepted_PrivateOrPublicName() { User third = userRepository.save(uniqueUser(3)); friendshipRepository.save(new Friendship(sender, friend, FriendShipStatus.ACCEPTED)); // sender and third are NOT friends List result = friendshipService.getPrivateNameIfFriendshipAccepted(sender, List.of(friend.getId(), third.getId())); assertEquals(2, result.size()); FriendshipService.NameWithFriendship friendResult = result.stream() .filter(r -> r.id().equals(friend.getId())).findFirst().orElseThrow(); assertEquals(friend.getPrivateName(), friendResult.name()); assertTrue(friendResult.friendship()); FriendshipService.NameWithFriendship thirdResult = result.stream() .filter(r -> r.id().equals(third.getId())).findFirst().orElseThrow(); assertEquals(third.getPublicName(), thirdResult.name()); assertFalse(thirdResult.friendship()); } @Test void getPrivateNameIfFriendshipAccepted_EmptyList() { assertTrue(friendshipService.getPrivateNameIfFriendshipAccepted(sender, List.of()).isEmpty()); } @Test void getPrivateNameIfFriendshipAccepted_UserNotFound_Throws() { assertThrows(RuntimeException.class, () -> friendshipService.getPrivateNameIfFriendshipAccepted(sender, List.of(99999L))); } // ------------------------------------------------------------------------- // getByFriendcode // ------------------------------------------------------------------------- @Test void requestFriendshipByFriendcode_Success() { friendshipService.requestFriendshipByFriendcode(sender, friend.getFriendCode()); assertEquals(FriendShipStatus.REQUESTED, friendshipRepository.findByUserAndFriend(sender, friend).getStatus()); assertEquals(FriendShipStatus.PENDING, friendshipRepository.findByUserAndFriend(friend, sender).getStatus()); } @Test void requestFriendshipByFriendcode_NotFound_Throws() { assertThrows(RuntimeException.class, () -> friendshipService.requestFriendshipByFriendcode(sender, "INVALID_CODE")); } @Test void requestFriendshipByFriendcode_Self_Throws() { assertThrows(RuntimeException.class, () -> friendshipService.requestFriendshipByFriendcode(sender, sender.getFriendCode())); } }