Skip to content

Commit

Permalink
實作房主IsReady always True (#130)
Browse files Browse the repository at this point in the history
* 根據jwt subject is user identity 修正
根據第一次code review修正
實作房主IsReady always True

* 根據第二次code review修正

---------

Co-authored-by: Ted <[email protected]>
  • Loading branch information
ted791029 and Ted authored Jul 25, 2023
1 parent d60d3bb commit 6e1a004
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
5 changes: 4 additions & 1 deletion domain/src/main/kotlin/tw/waterballsa/gaas/domain/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,10 +41,10 @@ abstract class AbstractSpringBootTest {
.build()

protected fun <T> ResultActions.getBody(type: Class<T>): T =
andReturn().response.contentAsString.let { objectMapper.readValue(it, type) }
String(andReturn().response.contentAsByteArray, UTF_8).let { objectMapper.readValue(it, type) }

protected fun <T> ResultActions.getBody(type: TypeReference<T>): 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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."))
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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(
Expand Down

0 comments on commit 6e1a004

Please sign in to comment.