Skip to content

Commit

Permalink
fix: AttributeConverter 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
Hwanvely committed Aug 23, 2024
1 parent fbd1563 commit 48425dc
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.LocalDateTime;
import java.util.List;

import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
Expand Down Expand Up @@ -37,46 +38,47 @@ public class AppleGame extends BaseEntity {
private Long sessionId;
private Long ownerId;
@Lob
private Board board;
@Convert(converter = AppleGameBoardConverter.class)
private AppleGameBoard appleGameBoard;
private int score = 0;
private LocalDateTime finishedAt;

public AppleGame(Board board, Long ownerId) {
this.board = board;
public AppleGame(AppleGameBoard appleGameBoard, Long ownerId) {
this.appleGameBoard = appleGameBoard;
this.ownerId = ownerId;
this.finishedAt = willFinishAt();
}

public AppleGame(Board board, Long ownerId, LocalDateTime finishedAt) {
this.board = board;
public AppleGame(AppleGameBoard appleGameBoard, Long ownerId, LocalDateTime finishedAt) {
this.appleGameBoard = appleGameBoard;
this.ownerId = ownerId;
this.finishedAt = finishedAt;
}

public AppleGame(Board board, Long ownerId, LocalDateTime finishedAt, int score) {
this.board = board;
public AppleGame(AppleGameBoard appleGameBoard, Long ownerId, LocalDateTime finishedAt, int score) {
this.appleGameBoard = appleGameBoard;
this.ownerId = ownerId;
this.finishedAt = finishedAt;
this.score = score;
}

public static AppleGame ofRandomized(Long ownerId) {
return new AppleGame(new Board(DEFAULT_HEIGHT, DEFAULT_WIDTH), ownerId);
return new AppleGame(new AppleGameBoard(DEFAULT_HEIGHT, DEFAULT_WIDTH), ownerId);
}

public void restart() {
validateOngoing();
this.board = board.reset();
this.appleGameBoard = appleGameBoard.reset();
this.score = 0;
this.createdAt = now();
this.finishedAt = willFinishAt();
}

public void removeApplesIn(Range range) {
validateOngoing();
List<Apple> removed = board.removeApplesIn(range);
List<Apple> removed = appleGameBoard.removeApplesIn(range);
if (removed.stream().anyMatch(Apple::isGolden)) {
board = board.reset();
appleGameBoard = appleGameBoard.reset();
}
score += removed.size();
}
Expand All @@ -102,7 +104,7 @@ private LocalDateTime willFinishAt() {
}

public List<List<Apple>> getApples() {
return board.getApples();
return appleGameBoard.getApples();
}

public void increase(int amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Board {
public class AppleGameBoard {

private static final int REMOVABLE_SUM = 10;

private List<List<Apple>> apples;

public Board(List<List<Apple>> apples) {
public AppleGameBoard(List<List<Apple>> apples) {
validateSquared(apples);
this.apples = apples.stream()
.map(ArrayList::new)
.collect(Collectors.toList());
}

public Board(int height, int width) {
public AppleGameBoard(int height, int width) {
this(ApplesFactory.createRandomized(height, width));
}

Expand All @@ -39,8 +39,8 @@ private void validateSquared(List<List<Apple>> apples) {
}
}

public Board reset() {
return new Board(this.getHeight(), this.getWidth());
public AppleGameBoard reset() {
return new AppleGameBoard(this.getHeight(), this.getWidth());
}

protected List<Apple> removeApplesIn(Range range) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

@Converter(autoApply = true)
public class BoardConverter implements AttributeConverter<Board, String> {
public class AppleGameBoardConverter implements AttributeConverter<AppleGameBoard, String> {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

Expand All @@ -19,18 +19,18 @@ public class BoardConverter implements AttributeConverter<Board, String> {
}

@Override
public String convertToDatabaseColumn(Board board) {
public String convertToDatabaseColumn(AppleGameBoard appleGameBoard) {
try {
return OBJECT_MAPPER.writeValueAsString(board);
return OBJECT_MAPPER.writeValueAsString(appleGameBoard);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@Override
public Board convertToEntityAttribute(String dbJson) {
public AppleGameBoard convertToEntityAttribute(String dbJson) {
try {
return OBJECT_MAPPER.readValue(dbJson, Board.class);
return OBJECT_MAPPER.readValue(dbJson, AppleGameBoard.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.snackgame.server.applegame.controller.dto.GameResultResponse;
import com.snackgame.server.applegame.controller.dto.RangeRequest;
import com.snackgame.server.applegame.domain.game.AppleGame;
import com.snackgame.server.applegame.domain.game.AppleGameBoard;
import com.snackgame.server.applegame.domain.game.AppleGames;
import com.snackgame.server.applegame.domain.game.Board;
import com.snackgame.server.game.metadata.Metadata;
import com.snackgame.server.game.session.event.SessionEndEvent;

Expand All @@ -32,11 +32,11 @@ public AppleGame startGameFor(Long memberId) {

public Optional<AppleGame> placeMoves(Long memberId, Long sessionId, List<RangeRequest> rangeRequests) {
AppleGame game = appleGames.getBy(memberId, sessionId);
Board previous = game.getBoard();
AppleGameBoard previous = game.getAppleGameBoard();

rangeRequests.forEach(request -> game.removeApplesIn(request.toRange()));

if (!game.getBoard().equals(previous)) {
if (!game.getAppleGameBoard().equals(previous)) {
return Optional.of(game);
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SnackgameController(
fun placeMoves(
@Authenticated member: Member,
@PathVariable sessionId: Long,
@RequestBody @Valid requests: List<StreakRequest>
@RequestBody requests: List<StreakRequest>
): ResponseEntity<SnackgameResponse> = snackgameService.placeMoves(member.id, sessionId, requests)
.map { game ->
ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.snackgame.server.game.snackgame.snack.EmptySnack
import com.snackgame.server.game.snackgame.snack.Snack
import java.util.stream.Collectors


class Board() {
private var snacks: MutableList<MutableList<Snack>> = arrayListOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.ObjectMapper
import javax.persistence.AttributeConverter
import javax.persistence.Converter

@Converter(autoApply = true)

class BoardConverter : AttributeConverter<Board, String> {

override fun convertToDatabaseColumn(board: Board): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.snackgame.server.game.metadata.Metadata.SNACK_GAME
import com.snackgame.server.game.session.domain.Session
import com.snackgame.server.game.snackgame.snack.Snack
import java.time.Duration
import javax.persistence.Convert
import javax.persistence.Entity
import javax.persistence.Lob

@Entity
class Snackgame(
ownerId: Long,
@Lob
@Convert(converter = BoardConverter::class)
val board: Board,
timeLimit: Duration = SESSION_TIME + SPARE_TIME,
score: Int = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import com.snackgame.server.game.session.domain.Session
import com.snackgame.server.game.session.service.dto.SessionResponse
import com.snackgame.server.game.snackgame.domain.Snackgame

class SnackgameResponse(
session: Session,

data class SnackgameResponse(
val session: Session,
val board: List<List<SnackResponse>>
) : SessionResponse(session) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class BoardConverterTest {
class AppleGameBoardConverterTest {

private final BoardConverter converter = new BoardConverter();
private final AppleGameBoardConverter converter = new AppleGameBoardConverter();

@Test
void 게임판을_JSON으로_변환한다() {
Board board = TestFixture.TWO_BY_FOUR();
AppleGameBoard appleGameBoard = TestFixture.TWO_BY_FOUR();

String serialized = converter.convertToDatabaseColumn(board);
String serialized = converter.convertToDatabaseColumn(appleGameBoard);

assertThat(serialized).isEqualTo(TestFixture.TWO_BY_FOUR_AS_JSON);
}

@Test
void 황금사과를_포함한_게임판을_JSON으로_변환한다() {
Board board = TestFixture.TWO_BY_TWO_WITH_GOLDEN_APPLE();
AppleGameBoard appleGameBoard = TestFixture.TWO_BY_TWO_WITH_GOLDEN_APPLE();

String serialized = converter.convertToDatabaseColumn(board);
String serialized = converter.convertToDatabaseColumn(appleGameBoard);

assertThat(serialized).isEqualTo(TestFixture.TWO_BY_TWO_WITH_GOLDEN_APPLE_AS_JSON);
}

@Test
void JSON_게임판을_사과객체들로_변환한다() {
Board expected = TestFixture.TWO_BY_FOUR();
AppleGameBoard expected = TestFixture.TWO_BY_FOUR();

var deserialized = converter.convertToEntityAttribute(TestFixture.TWO_BY_FOUR_AS_JSON);

Expand All @@ -45,7 +45,7 @@ class BoardConverterTest {

@Test
void 황금사과를_포함한_게임판을_역직렬화한다() {
Board expected = TestFixture.TWO_BY_TWO_WITH_GOLDEN_APPLE();
AppleGameBoard expected = TestFixture.TWO_BY_TWO_WITH_GOLDEN_APPLE();

var deserialized = converter.convertToEntityAttribute(TestFixture.TWO_BY_TWO_WITH_GOLDEN_APPLE_AS_JSON);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class BoardTest {
class AppleGameBoardTest {

@Test
void 게임판_높이는_1이상이어야_한다() {
int height = 0;
int width = 8;

assertThatThrownBy(() -> new Board(height, width))
assertThatThrownBy(() -> new AppleGameBoard(height, width))
.isInstanceOf(InvalidBoardSizeException.class)
.hasMessage("잘못된 크기의 게임판입니다");
}
Expand All @@ -37,7 +37,7 @@ class BoardTest {
int height = 1;
int width = 0;

assertThatThrownBy(() -> new Board(height, width))
assertThatThrownBy(() -> new AppleGameBoard(height, width))
.isInstanceOf(InvalidBoardSizeException.class)
.hasMessage("잘못된 크기의 게임판입니다");
}
Expand All @@ -47,7 +47,7 @@ class BoardTest {
int height = 4;
int width = 8;

var board = new Board(height, width);
var board = new AppleGameBoard(height, width);

assertThat(board.getApples()).hasSize(height);
assertThat(board.getApples()).allSatisfy(
Expand All @@ -59,7 +59,7 @@ class BoardTest {
void 게임판을_초기화해도_같은_크기를_가진다() {
int height = 4;
int width = 8;
var board = new Board(height, width);
var board = new AppleGameBoard(height, width);

var reset = board.reset();

Expand All @@ -73,7 +73,7 @@ class BoardTest {
void 게임판을_초기화하면_사과들이_바뀐다() {
int height = 4;
int width = 8;
var board = new Board(height, width);
var board = new AppleGameBoard(height, width);

var reset = board.reset();

Expand Down Expand Up @@ -132,7 +132,7 @@ class BoardTest {

@Test
void 하나를_황금사과로_만든다() {
var board = new Board(10, 8);
var board = new AppleGameBoard(10, 8);

long goldenAppleCount = board.getApples().stream()
.flatMap(Collection::stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class AppleGameTest {
new Coordinate(1, 3)
);
game.removeApplesIn(range);
var previousBoard = game.getBoard();
var previousBoard = game.getAppleGameBoard();
var previousCreatedAt = game.getCreatedAt();

game.restart();

assertThat(game.getBoard()).isNotEqualTo(previousBoard);
assertThat(game.getAppleGameBoard()).isNotEqualTo(previousBoard);
assertThat(game.getScore()).isZero();
assertThat(game.getCreatedAt()).isAfter(previousCreatedAt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.snackgame.server.applegame.domain.apple.EmptyApple;
import com.snackgame.server.applegame.domain.apple.PlainApple;
import com.snackgame.server.applegame.domain.game.Board;
import com.snackgame.server.applegame.domain.game.AppleGameBoard;

public class TestFixture {

Expand All @@ -15,8 +15,8 @@ public class TestFixture {
* <p>[1, 1, _, 5]</p>
* <p>[9, 2, _, 2]</p>
*/
public static Board TWO_BY_FOUR() {
return new Board(List.of(
public static AppleGameBoard TWO_BY_FOUR() {
return new AppleGameBoard(List.of(
List.of(PlainApple.of(1), PlainApple.of(1), EmptyApple.get(), PlainApple.of(5)),
List.of(PlainApple.of(9), PlainApple.of(2), EmptyApple.get(), PlainApple.of(2))
));
Expand All @@ -27,8 +27,8 @@ public static Board TWO_BY_FOUR() {
* <p>[<b>9</b>, 2]</p>
* 9는 황금사과이다.
*/
public static Board TWO_BY_TWO_WITH_GOLDEN_APPLE() {
return new Board(List.of(
public static AppleGameBoard TWO_BY_TWO_WITH_GOLDEN_APPLE() {
return new AppleGameBoard(List.of(
List.of(PlainApple.of(1), PlainApple.of(8)),
List.of(PlainApple.of(9).golden(), PlainApple.of(2))
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ void setUp() {

AppleGame current = appleGames.getBy(땡칠().getId(), previous.getSessionId());
assertThat(current.getCreatedAt()).isAfter(previous.getCreatedAt());
assertThat(current.getBoard())
assertThat(current.getAppleGameBoard())
.usingRecursiveComparison()
.isNotEqualTo(previous.getBoard());
.isNotEqualTo(previous.getAppleGameBoard());
}

@Test
Expand Down
Loading

0 comments on commit 48425dc

Please sign in to comment.