From 6e1a004d61f68e2a4fc1812738437aca43cc7cd1 Mon Sep 17 00:00:00 2001 From: Ted <82504888+ted791029@users.noreply.github.com> Date: Tue, 25 Jul 2023 22:54:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=A6=E4=BD=9C=E6=88=BF=E4=B8=BBIsReady=20a?= =?UTF-8?q?lways=20True=20(#130)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 根據jwt subject is user identity 修正 根據第一次code review修正 實作房主IsReady always True * 根據第二次code review修正 --------- Co-authored-by: Ted --- .../application/usecases/CreateRoomUsecase.kt | 5 ++- .../kotlin/tw/waterballsa/gaas/domain/Room.kt | 5 ++- .../gaas/spring/it/AbstractSpringBootTest.kt | 5 ++- .../it/controllers/RoomControllerTest.kt | 37 +++++++++++-------- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/CreateRoomUsecase.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/CreateRoomUsecase.kt index f903be0b..6eece9e3 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/CreateRoomUsecase.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/CreateRoomUsecase.kt @@ -39,9 +39,10 @@ class CreateRoomUsecase( } } - private fun Request.createRoom(hostPlayer: Player): Room { + private fun Request.createRoom(host: Player): Room { val gameRegistration = findGameRegistrationById(gameId) - return roomRepository.createRoom(toRoom(gameRegistration, hostPlayer)) + host.ready() + return roomRepository.createRoom(toRoom(gameRegistration, host)) } private fun findGameRegistrationById(gameId: String) = diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/domain/Room.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/domain/Room.kt index bfa94c0c..271e743c 100644 --- a/domain/src/main/kotlin/tw/waterballsa/gaas/domain/Room.kt +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/domain/Room.kt @@ -61,7 +61,10 @@ class Room( private fun changeHost() { players.firstOrNull() - ?.let { host = it } + ?.let { + host = it + host.ready() + } } private fun findPlayer(playerId: Player.Id): Player? = players.find { it.id == playerId } diff --git a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/AbstractSpringBootTest.kt b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/AbstractSpringBootTest.kt index b190ae7a..6df142f3 100644 --- a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/AbstractSpringBootTest.kt +++ b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/AbstractSpringBootTest.kt @@ -12,6 +12,7 @@ import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.ResultActions import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder import tw.waterballsa.gaas.domain.User +import java.nio.charset.StandardCharsets.UTF_8 @SpringBootTest @AutoConfigureMockMvc @@ -40,10 +41,10 @@ abstract class AbstractSpringBootTest { .build() protected fun ResultActions.getBody(type: Class): T = - andReturn().response.contentAsString.let { objectMapper.readValue(it, type) } + String(andReturn().response.contentAsByteArray, UTF_8).let { objectMapper.readValue(it, type) } protected fun ResultActions.getBody(type: TypeReference): T = - andReturn().response.contentAsString.let { objectMapper.readValue(it, type) } + String(andReturn().response.contentAsByteArray, UTF_8).let { objectMapper.readValue(it, type) } protected fun Any.toJson(): String = objectMapper.writeValueAsString(this) diff --git a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt index e7ed1e56..527aeff4 100644 --- a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt +++ b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt @@ -3,6 +3,8 @@ package tw.waterballsa.gaas.spring.it.controllers import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test @@ -20,6 +22,7 @@ import tw.waterballsa.gaas.domain.GameRegistration import tw.waterballsa.gaas.domain.Room import tw.waterballsa.gaas.domain.Room.Player import tw.waterballsa.gaas.domain.User +import tw.waterballsa.gaas.spring.controllers.RoomController.CreateRoomViewModel import tw.waterballsa.gaas.spring.it.AbstractSpringBootTest import tw.waterballsa.gaas.spring.models.TestCreateRoomRequest import tw.waterballsa.gaas.spring.models.TestGetRoomsRequest @@ -54,14 +57,14 @@ class RoomControllerTest @Autowired constructor( fun givenUserIsInTheLobby_WhenUserCreateARoom_ThenShouldSucceed() { val request = createRoomRequest() createRoom(testUser, request) - .thenCreateRoomSuccessfully(request) + .thenCreateRoomSuccessfully() } @Test fun givenUserIsInTheLobby_WhenUserCreateARoomWithValidPassword_ThenShouldSucceed() { val request = createRoomRequest("1234") createRoom(testUser, request) - .thenCreateRoomSuccessfully(request) + .thenCreateRoomSuccessfully() } @Test @@ -84,7 +87,7 @@ class RoomControllerTest @Autowired constructor( fun givenUserAlreadyCreatedARoom_WhenUserCreateAnotherRoom_ThenShouldFail() { val request = createRoomRequest("1234") createRoom(testUser, request) - .thenCreateRoomSuccessfully(request) + .thenCreateRoomSuccessfully() createRoom(testUser, request) .andExpect(status().isBadRequest) .andExpect(jsonPath("$.message").value("A user can only create one room at a time.")) @@ -448,19 +451,21 @@ class RoomControllerTest @Autowired constructor( return leaveRoom(leaveUser) } - private fun ResultActions.thenCreateRoomSuccessfully(request: TestCreateRoomRequest) { - request.let { + private fun ResultActions.thenCreateRoomSuccessfully() { + val roomView = getBody(CreateRoomViewModel::class.java) + val room = roomRepository.findById(roomView.id)!! + room.let { andExpect(status().isCreated) - .andExpect(jsonPath("$.id").exists()) - .andExpect(jsonPath("$.name").value(it.name)) - .andExpect(jsonPath("$.game.id").value(testGame.id!!.value)) - .andExpect(jsonPath("$.game.name").value(testGame.displayName)) - .andExpect(jsonPath("$.host.id").value(testUser.id!!.value)) - .andExpect(jsonPath("$.host.nickname").value(testUser.nickname)) - .andExpect(jsonPath("$.isLocked").value(!it.password.isNullOrEmpty())) - .andExpect(jsonPath("$.currentPlayers").value(1)) - .andExpect(jsonPath("$.minPlayers").value(it.minPlayers)) - .andExpect(jsonPath("$.maxPlayers").value(it.maxPlayers)) + assertEquals(roomView.name, it.name) + assertEquals(roomView.game.id, it.game.id!!.value) + assertEquals(roomView.game.name, it.game.displayName) + assertEquals(roomView.host.id, it.host.id!!.value) + assertEquals(roomView.host.nickname, it.host.nickname) + assertEquals(roomView.currentPlayers, it.players.size) + assertEquals(roomView.minPlayers, it.minPlayers) + assertEquals(roomView.maxPlayers, it.maxPlayers) + assertTrue(it.host.readiness) + assertTrue(it.players.first().readiness) } } @@ -484,6 +489,8 @@ class RoomControllerTest @Autowired constructor( val room = roomRepository.findById(testRoom.roomId!!)!! assertFalse(room.hasPlayer(player.id)) assertFalse(room.isHost(player.id)) + assertTrue(room.host.readiness) + assertTrue(room.players.first().readiness) } private fun createUser(