|
| 1 | +package com.faforever.api.data.hook; |
| 2 | + |
| 3 | +import com.faforever.api.avatar.AvatarAssignmentRepository; |
| 4 | +import com.faforever.api.data.domain.Avatar; |
| 5 | +import com.faforever.api.data.domain.AvatarAssignment; |
| 6 | +import com.faforever.api.data.domain.Player; |
| 7 | +import com.faforever.api.error.ApiException; |
| 8 | +import com.faforever.api.error.ErrorCode; |
| 9 | +import com.yahoo.elide.annotation.LifeCycleHookBinding.Operation; |
| 10 | +import com.yahoo.elide.annotation.LifeCycleHookBinding.TransactionPhase; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 16 | + |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +import static com.faforever.api.error.ApiExceptionMatcher.hasErrorCode; |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +@ExtendWith(MockitoExtension.class) |
| 26 | +class PlayerAvatarValidationHookTest { |
| 27 | + |
| 28 | + private static final int PLAYER_ID = 7; |
| 29 | + private static final int AVATAR_ID = 42; |
| 30 | + |
| 31 | + @Mock |
| 32 | + private AvatarAssignmentRepository avatarAssignmentRepository; |
| 33 | + |
| 34 | + private PlayerAvatarValidationHook instance; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + instance = new PlayerAvatarValidationHook(avatarAssignmentRepository); |
| 39 | + } |
| 40 | + |
| 41 | + private static Player playerWithAvatar(Integer avatarId) { |
| 42 | + Avatar avatar = new Avatar(); |
| 43 | + avatar.setId(avatarId); |
| 44 | + Player player = new Player(); |
| 45 | + player.setId(PLAYER_ID); |
| 46 | + player.setCurrentAvatar(avatar); |
| 47 | + return player; |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void allowsAssignedAvatar() { |
| 52 | + when(avatarAssignmentRepository.findOneByAvatarIdAndPlayerId(AVATAR_ID, PLAYER_ID)) |
| 53 | + .thenReturn(Optional.of(new AvatarAssignment())); |
| 54 | + |
| 55 | + instance.execute(Operation.UPDATE, TransactionPhase.PRECOMMIT, playerWithAvatar(AVATAR_ID), null, Optional.empty()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void rejectsUnassignedAvatar() { |
| 60 | + when(avatarAssignmentRepository.findOneByAvatarIdAndPlayerId(AVATAR_ID, PLAYER_ID)) |
| 61 | + .thenReturn(Optional.empty()); |
| 62 | + |
| 63 | + ApiException result = assertThrows(ApiException.class, () -> |
| 64 | + instance.execute(Operation.UPDATE, TransactionPhase.PRECOMMIT, playerWithAvatar(AVATAR_ID), null, Optional.empty())); |
| 65 | + assertThat(result, hasErrorCode(ErrorCode.AVATAR_NOT_ASSIGNED)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void allowsClearingAvatar() { |
| 70 | + Player player = new Player(); |
| 71 | + player.setId(PLAYER_ID); |
| 72 | + |
| 73 | + instance.execute(Operation.UPDATE, TransactionPhase.PRECOMMIT, player, null, Optional.empty()); |
| 74 | + |
| 75 | + verifyNoInteractions(avatarAssignmentRepository); |
| 76 | + } |
| 77 | +} |
0 commit comments