diff --git a/src/main/java/mafia/mafiatogether/game/application/GameService.java b/src/main/java/mafia/mafiatogether/game/application/GameService.java index b7cdd57..89dfd9c 100644 --- a/src/main/java/mafia/mafiatogether/game/application/GameService.java +++ b/src/main/java/mafia/mafiatogether/game/application/GameService.java @@ -38,7 +38,7 @@ public GameStatusResponse findStatus(final String code) { private StatusType checkStatusChanged(final Game game) { game.setStatsSnapshot(); final StatusType statusType = game.getStatusType(Clock.systemDefaultZone().millis()); - if (game.isDeleted()){ + if (game.isDeleted()) { gameRepository.delete(game); return StatusType.WAIT; } @@ -81,4 +81,16 @@ public GameResultResponse findResult(final String code) { } return GameResultResponse.from(game); } + + public boolean isValid(final String code, final String name) { + Optional lobby = lobbyRepository.findById(code); + if (!lobby.isPresent()) { + return false; + } + Optional game = gameRepository.findById(code); + if (!game.isPresent()) { + return lobby.get().isParticipantExist(name); + } + return game.get().isPlayerExist(name); + } } diff --git a/src/main/java/mafia/mafiatogether/game/application/dto/response/GameExistResponse.java b/src/main/java/mafia/mafiatogether/game/application/dto/response/GameExistResponse.java new file mode 100644 index 0000000..86b6fcf --- /dev/null +++ b/src/main/java/mafia/mafiatogether/game/application/dto/response/GameExistResponse.java @@ -0,0 +1,6 @@ +package mafia.mafiatogether.game.application.dto.response; + +public record GameExistResponse( + boolean valid +) { +} diff --git a/src/main/java/mafia/mafiatogether/game/domain/Game.java b/src/main/java/mafia/mafiatogether/game/domain/Game.java index 9f82520..764dcdd 100644 --- a/src/main/java/mafia/mafiatogether/game/domain/Game.java +++ b/src/main/java/mafia/mafiatogether/game/domain/Game.java @@ -149,4 +149,8 @@ public boolean isStatusChanged() { public boolean isDeleted() { return status.getType().equals(StatusType.DELETED); } + + public boolean isPlayerExist(final String name) { + return players.contains(name); + } } diff --git a/src/main/java/mafia/mafiatogether/game/domain/PlayerCollection.java b/src/main/java/mafia/mafiatogether/game/domain/PlayerCollection.java index 1e92ac1..50cd40d 100644 --- a/src/main/java/mafia/mafiatogether/game/domain/PlayerCollection.java +++ b/src/main/java/mafia/mafiatogether/game/domain/PlayerCollection.java @@ -85,4 +85,8 @@ public void killTarget(final String target) { final Player targetPlayer = findByName(target); targetPlayer.kill(); } + + public boolean contains(final String name) { + return players.stream().anyMatch(player -> player.getName().equals(name)); + } } diff --git a/src/main/java/mafia/mafiatogether/game/ui/GameController.java b/src/main/java/mafia/mafiatogether/game/ui/GameController.java index bb1405c..879f1ae 100644 --- a/src/main/java/mafia/mafiatogether/game/ui/GameController.java +++ b/src/main/java/mafia/mafiatogether/game/ui/GameController.java @@ -2,8 +2,9 @@ import lombok.RequiredArgsConstructor; import mafia.mafiatogether.config.PlayerInfo; -import mafia.mafiatogether.game.application.GameService; import mafia.mafiatogether.config.PlayerInfoDto; +import mafia.mafiatogether.game.application.GameService; +import mafia.mafiatogether.game.application.dto.response.GameExistResponse; import mafia.mafiatogether.game.application.dto.response.GameInfoResponse; import mafia.mafiatogether.game.application.dto.response.GameResultResponse; import mafia.mafiatogether.game.application.dto.response.GameStatusResponse; @@ -48,4 +49,14 @@ public ResponseEntity findGameInfo( ) { return ResponseEntity.ok(gameService.findGameInfo(playerInfoDto.code(), playerInfoDto.name())); } + + + @GetMapping("/valid") + public ResponseEntity isValid( + @PlayerInfo final PlayerInfoDto playerInfoDto + ) { + return ResponseEntity.ok( + new GameExistResponse(gameService.isValid(playerInfoDto.code(), playerInfoDto.name())) + ); + } } diff --git a/src/main/java/mafia/mafiatogether/lobby/domain/Lobby.java b/src/main/java/mafia/mafiatogether/lobby/domain/Lobby.java index 4904905..60e37f3 100644 --- a/src/main/java/mafia/mafiatogether/lobby/domain/Lobby.java +++ b/src/main/java/mafia/mafiatogether/lobby/domain/Lobby.java @@ -62,4 +62,8 @@ public void validateToStart() { public void updateLastUpdateTime() { this.lastUpdateTime = Clock.systemDefaultZone().millis(); } + + public boolean isParticipantExist(final String name) { + return participants.contains(name); + } } diff --git a/src/main/java/mafia/mafiatogether/lobby/domain/ParticipantCollection.java b/src/main/java/mafia/mafiatogether/lobby/domain/ParticipantCollection.java index 7ff88ac..ac48ca6 100644 --- a/src/main/java/mafia/mafiatogether/lobby/domain/ParticipantCollection.java +++ b/src/main/java/mafia/mafiatogether/lobby/domain/ParticipantCollection.java @@ -36,4 +36,8 @@ private void validateParticipant(Participant participant, int total) { throw new GameException(ExceptionCode.INVALID_NAMES); } } + + public boolean contains(String name) { + return participants.stream().anyMatch(participant -> participant.getName().equals(name)); + } } diff --git a/src/test/java/mafia/mafiatogether/chat/ui/ChatControllerTest.java b/src/test/java/mafia/mafiatogether/chat/ui/ChatControllerTest.java index 3080057..3d4c211 100644 --- a/src/test/java/mafia/mafiatogether/chat/ui/ChatControllerTest.java +++ b/src/test/java/mafia/mafiatogether/chat/ui/ChatControllerTest.java @@ -29,7 +29,7 @@ public class ChatControllerTest extends ControllerTest { @BeforeEach void setTest() { - setRoom(); + setLobby(); setGame(); Chat chat = new Chat(CODE, new ArrayList<>()); diff --git a/src/test/java/mafia/mafiatogether/game/ui/GameControllerTest.java b/src/test/java/mafia/mafiatogether/game/ui/GameControllerTest.java index 4522a7e..b8029df 100644 --- a/src/test/java/mafia/mafiatogether/game/ui/GameControllerTest.java +++ b/src/test/java/mafia/mafiatogether/game/ui/GameControllerTest.java @@ -1,6 +1,7 @@ package mafia.mafiatogether.game.ui; import static org.assertj.core.api.SoftAssertions.assertSoftly; +import static org.hamcrest.Matchers.equalTo; import io.restassured.RestAssured; import io.restassured.http.ContentType; @@ -8,14 +9,14 @@ import java.util.Map; import mafia.mafiatogether.config.exception.ErrorResponse; import mafia.mafiatogether.config.exception.ExceptionCode; +import mafia.mafiatogether.game.application.dto.response.GameInfoResponse; +import mafia.mafiatogether.game.application.dto.response.GameStatusResponse; +import mafia.mafiatogether.game.application.dto.response.PlayerResponse; import mafia.mafiatogether.game.domain.Game; import mafia.mafiatogether.game.domain.Player; import mafia.mafiatogether.game.domain.status.StatusType; import mafia.mafiatogether.global.ControllerTest; -import mafia.mafiatogether.game.application.dto.response.PlayerResponse; import mafia.mafiatogether.job.domain.jobtype.JobType; -import mafia.mafiatogether.game.application.dto.response.GameInfoResponse; -import mafia.mafiatogether.game.application.dto.response.GameStatusResponse; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -25,8 +26,8 @@ class GameControllerTest extends ControllerTest { @BeforeEach - void setTest(){ - setRoom(); + void setTest() { + setLobby(); } @Test @@ -99,7 +100,7 @@ void setTest(){ } @Test - void 대기방에서_방장이_방의_정보를_찾는다(){ + void 대기방에서_방장이_방의_정보를_찾는다() { // given final String basic = Base64.getEncoder().encodeToString((CODE + ":" + PLAYER1_NAME).getBytes()); @@ -115,7 +116,7 @@ void setTest(){ /// then assertSoftly( - softly ->{ + softly -> { softly.assertThat(response.isMaster()).isTrue(); softly.assertThat(response.players()).hasSize(4); } @@ -238,4 +239,85 @@ private long countMafiaResponse(final String basic) { .filter(response -> response.job() != null && response.job().equals(JobType.MAFIA)) .count(); } + + @Test + void 게임이_진행되고_있을때_참가한_유저는_유효한지_검증한다() { + // given + setGame(); + final String basic = Base64.getEncoder().encodeToString((CODE + ":" + PLAYER1_NAME).getBytes()); + + // when & then + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .header("Authorization", "Basic " + basic) + .when().get("/games/valid") + .then().log().all() + .statusCode(HttpStatus.OK.value()) + .body("valid", equalTo(true)); + } + + @Test + void 로비만_존재하고_게임이_진행되지_않을때_참가한_유저가_유효한지_검증한다() { + // given + final String basic = Base64.getEncoder().encodeToString((CODE + ":" + PLAYER1_NAME).getBytes()); + + // when & then + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .header("Authorization", "Basic " + basic) + .when().get("/games/valid") + .then().log().all() + .statusCode(HttpStatus.OK.value()) + .body("valid", equalTo(true)); + } + + @Test + void 유저가_요청한_방이_없는지_검증한다() { + // given + final String code = "123456789fail"; + final String basic = Base64.getEncoder().encodeToString((code + ":" + PLAYER1_NAME).getBytes()); + + // when & then + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .header("Authorization", "Basic " + basic) + .when().get("/games/valid") + .then().log().all() + .statusCode(HttpStatus.OK.value()) + .body("valid", equalTo(false)); + } + + @Test + void 유저가_요청한_방에_존재하는지_검증한다() { + // given + final String invalidPlayer = "invalidPlayer"; + final String basic = Base64.getEncoder().encodeToString((CODE + ":" + invalidPlayer).getBytes()); + + // when & then + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .header("Authorization", "Basic " + basic) + .when().get("/games/valid") + .then().log().all() + .statusCode(HttpStatus.OK.value()) + .body("valid", equalTo(false)); + } + + + @Test + void 유저가_요청한_게임에_존재하는지_검증한다() { + // given + setGame(); + final String invalidPlayer = "invalidPlayer"; + final String basic = Base64.getEncoder().encodeToString((CODE + ":" + invalidPlayer).getBytes()); + + // when & then + RestAssured.given().log().all() + .contentType(ContentType.JSON) + .header("Authorization", "Basic " + basic) + .when().get("/games/valid") + .then().log().all() + .statusCode(HttpStatus.OK.value()) + .body("valid", equalTo(false)); + } } diff --git a/src/test/java/mafia/mafiatogether/global/ControllerTest.java b/src/test/java/mafia/mafiatogether/global/ControllerTest.java index d16cde3..f75f6ff 100644 --- a/src/test/java/mafia/mafiatogether/global/ControllerTest.java +++ b/src/test/java/mafia/mafiatogether/global/ControllerTest.java @@ -54,7 +54,7 @@ void setPort() { RestAssured.port = port; } - protected void setRoom() { + protected void setLobby() { final Lobby lobby = Lobby.create(CODE, LobbyInfo.of(5, 2, 1, 1)); lobby.joinPlayer(PLAYER1_NAME); lobby.joinPlayer(PLAYER2_NAME); @@ -89,6 +89,7 @@ private String findPlayerNameByJobType(PlayerCollection players, JobType jobType .findFirst().get() .getName(); } + @AfterEach void clearTest() { voteRepository.deleteById(CODE); diff --git a/src/test/java/mafia/mafiatogether/job/ui/JobControllerTest.java b/src/test/java/mafia/mafiatogether/job/ui/JobControllerTest.java index dba2754..5324b76 100644 --- a/src/test/java/mafia/mafiatogether/job/ui/JobControllerTest.java +++ b/src/test/java/mafia/mafiatogether/job/ui/JobControllerTest.java @@ -8,8 +8,8 @@ import java.util.Map; import mafia.mafiatogether.config.exception.ExceptionCode; import mafia.mafiatogether.global.ControllerTest; -import mafia.mafiatogether.job.domain.PlayerJobRepository; import mafia.mafiatogether.job.domain.JobTargetRepository; +import mafia.mafiatogether.job.domain.PlayerJobRepository; import mafia.mafiatogether.job.domain.jobtype.JobType; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; @@ -29,7 +29,7 @@ class JobControllerTest extends ControllerTest { @BeforeEach void setTest() { - setRoom(); + setLobby(); setGame(); } diff --git a/src/test/java/mafia/mafiatogether/vote/ui/VoteControllerTest.java b/src/test/java/mafia/mafiatogether/vote/ui/VoteControllerTest.java index 8b5ae6a..200f9ae 100644 --- a/src/test/java/mafia/mafiatogether/vote/ui/VoteControllerTest.java +++ b/src/test/java/mafia/mafiatogether/vote/ui/VoteControllerTest.java @@ -16,7 +16,7 @@ class VoteControllerTest extends ControllerTest { @BeforeEach void setTest(){ - setRoom(); + setLobby(); setGame(); }