Skip to content

Commit

Permalink
feat: 읽은 쪽지 로그 중복 쪽지 제거 (#541)
Browse files Browse the repository at this point in the history
* test: 이미 읽은 쪽지는 로깅하지 않는 테스트

* feat: 이미 읽은 쪽지는 로깅하지 않는 기능 추가

* chore: letter 패키지 수정과 충돌 해결 및 커밋
  • Loading branch information
zillionme authored Nov 8, 2023
1 parent 1056f33 commit 4a506a5
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public ResponseEntity<ExceptionResponse> handleTypeMismatchException(final Excep

return ResponseEntity.status(commonExceptionType.httpStatus()).body(exceptionResponse);
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ExceptionResponse> handleArgumentTypeMismatchException(final Exception e) {
final CommonExceptionType commonExceptionType = CommonExceptionType.INVALID_REQUEST_PARAMETERS;
final ExceptionResponse exceptionResponse = new ExceptionResponse(commonExceptionType.errorCode(), commonExceptionType.errorMessage());

log.info("error = {}", exceptionResponse);

return ResponseEntity.status(commonExceptionType.httpStatus()).body(exceptionResponse);
}

@ExceptionHandler(InternalException.class)
public ResponseEntity<ExceptionResponse> handleInternalException(final InternalException e) {
final BaseExceptionType internalExceptionType = e.exceptionType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static com.now.naaga.letter.exception.LetterExceptionType.NO_EXIST;

Expand Down Expand Up @@ -78,8 +79,17 @@ public Letter findLetter(final LetterReadCommand letterReadCommand) {
private void logReadLetter(final Player player,
final Letter letter) {
final Game gameInProgress = getGameInProgress(player.getId());
final ReadLetterLog readLetterLog = new ReadLetterLog(gameInProgress, letter);
readLetterLogRepository.save(readLetterLog);
boolean isAlreadyReadLetter = isAlreadyReadLetter(gameInProgress, letter);
if(!isAlreadyReadLetter) {
final ReadLetterLog readLetterLog = new ReadLetterLog(gameInProgress, letter);
readLetterLogRepository.save(readLetterLog);
}
}
private boolean isAlreadyReadLetter(final Game game,
final Letter letter) {
Optional<ReadLetterLog> readLetterInGame = readLetterLogRepository
.findByGameIdAndLetterId(game.getId(), letter.getId());
return readLetterInGame.isPresent();
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.now.naaga.letter.domain.letterlog.ReadLetterLog;
import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ReadLetterLogRepository extends JpaRepository<ReadLetterLog, Long> {

List<ReadLetterLog> findByGameId(Long gamedId);

Optional<ReadLetterLog> findByGameIdAndLetterId(Long gam, Long letterId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@ActiveProfiles("test")
Expand All @@ -63,7 +64,107 @@ class LetterServiceTest {

@Autowired
private LetterBuilder letterBuilder;


@Test
void 이미_읽은_쪽지의_경우_읽은_쪽지_로그를_기록하지_않는다() {
// given
final Player player = playerBuilder.init()
.build();

final Place destination = placeBuilder.init()
.position(잠실_루터회관_정문_좌표)
.build();

final Game game = gameBuilder.init()
.place(destination)
.player(player)
.startPosition(잠실역_교보문고_좌표)
.build();

final Player letterRegister = playerBuilder.init()
.build();

final Letter letter = letterBuilder.init()
.registeredPlayer(letterRegister)
.build();

LetterService mockReadLetterService = mock(LetterService.class);

// when
mockReadLetterService.findLetter(new LetterReadCommand(player.getId(), letter.getId()));
mockReadLetterService.findLetter(new LetterReadCommand(player.getId(), letter.getId()));

//then
verify(mockReadLetterService, atLeast(1)).findLetter(new LetterReadCommand(player.getId(), letter.getId()));
}

@Test
void 읽은_쪽지_로그를_정상적으로_기록한다() {
// given
final Player player = playerBuilder.init()
.build();

final Place destination = placeBuilder.init()
.position(잠실_루터회관_정문_좌표)
.build();

final Game game = gameBuilder.init()
.place(destination)
.player(player)
.startPosition(잠실역_교보문고_좌표)
.build();

final Player letterRegister = playerBuilder.init()
.build();

final Letter letter = letterBuilder.init()
.registeredPlayer(letterRegister)
.build();

// when
letterService.findLetter(new LetterReadCommand(player.getId(), letter.getId()));

// then
final List<ReadLetterLog> actual = readLetterLogRepository.findAll();
final long expected = actual.get(0).getLetter().getId();

assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).hasSize(1);
softAssertions.assertThat(expected).isEqualTo(letter.getId());
});
}


@Test
void 읽은쪽지로그에_데이터저장시_진행중인_게임이없으면_예외가_발생한다() {
// given && when
final Player player = playerBuilder.init()
.build();

final Place destination = placeBuilder.init()
.position(잠실_루터회관_정문_좌표)
.build();

final Game game = gameBuilder.init()
.place(destination)
.player(player)
.startPosition(잠실역_교보문고_좌표)
.gameStatus(GameStatus.DONE)
.build();

final Player letterRegister = playerBuilder.init()
.build();

final Letter letter = letterBuilder.init()
.registeredPlayer(letterRegister)
.build();

//then
final GameException gameException = assertThrows(
GameException.class, () -> letterService.findLetter(new LetterReadCommand(player.getId(), letter.getId())));
assertThat(gameException.exceptionType()).isEqualTo(NOT_EXIST_IN_PROGRESS);
}

@Transactional
@Test
void 쪽지를_정상적으로_생성하고_게임중_등록한_쪽지를_기록으로_남긴다() {
Expand Down

0 comments on commit 4a506a5

Please sign in to comment.