-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: update user info * refactor: ktlint * refactor: nickname validation * refactor: jwt property extension * refactor: remove view model setter * refactor: coding style * refactor: jwt property extension * refactor: coding style * refactor: coding style * refactor: coding style
- Loading branch information
Showing
12 changed files
with
226 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/UpdateUserUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package tw.waterballsa.gaas.application.usecases | ||
|
||
import tw.waterballsa.gaas.application.eventbus.EventBus | ||
import tw.waterballsa.gaas.application.repositories.UserRepository | ||
import tw.waterballsa.gaas.domain.User | ||
import tw.waterballsa.gaas.events.UserUpdatedEvent | ||
import tw.waterballsa.gaas.exceptions.NotFoundException.Companion.notFound | ||
import tw.waterballsa.gaas.exceptions.PlatformException | ||
import javax.inject.Named | ||
|
||
@Named | ||
class UpdateUserUseCase( | ||
private val userRepository: UserRepository, | ||
private val eventBus: EventBus, | ||
) { | ||
fun execute(request: Request, presenter: Presenter) { | ||
with(request) { | ||
validateNicknameDuplicated(nickname) | ||
val user = findUserByEmail(email) | ||
user.changeNickname(nickname) | ||
val updatedUser = userRepository.update(user) | ||
|
||
val event = updatedUser.toUserUpdatedEvent() | ||
presenter.present(event) | ||
eventBus.broadcast(event) | ||
} | ||
} | ||
|
||
private fun validateNicknameDuplicated(nickname: String) { | ||
if (userRepository.existsUserByNickname(nickname)) { | ||
throw PlatformException("invalid nickname: duplicated") | ||
} | ||
} | ||
|
||
private fun findUserByEmail(email: String) = | ||
userRepository.findByEmail(email) | ||
?: throw notFound(User::class).identifyBy("email", email) | ||
|
||
data class Request(val email: String, val nickname: String) | ||
} | ||
|
||
private fun User.toUserUpdatedEvent(): UserUpdatedEvent = | ||
UserUpdatedEvent(id!!, email, nickname) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
domain/src/main/kotlin/tw/waterballsa/gaas/events/UserUpdatedEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package tw.waterballsa.gaas.events | ||
|
||
import tw.waterballsa.gaas.domain.User | ||
|
||
class UserUpdatedEvent( | ||
val id: User.Id, | ||
val email: String, | ||
val nickname: String, | ||
) : DomainEvent() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...g/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/presenter/UpdateUserPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package tw.waterballsa.gaas.spring.controllers.presenter | ||
|
||
import tw.waterballsa.gaas.application.usecases.Presenter | ||
import tw.waterballsa.gaas.events.DomainEvent | ||
import tw.waterballsa.gaas.events.UserUpdatedEvent | ||
import tw.waterballsa.gaas.spring.controllers.viewmodel.UpdateUserViewModel | ||
import tw.waterballsa.gaas.spring.extensions.getEvent | ||
|
||
class UpdateUserPresenter : Presenter { | ||
lateinit var viewModel: UpdateUserViewModel | ||
private set | ||
|
||
override fun present(vararg events: DomainEvent) { | ||
viewModel = events.getEvent(UserUpdatedEvent::class)!!.toViewModel() | ||
} | ||
|
||
private fun UserUpdatedEvent.toViewModel(): UpdateUserViewModel = | ||
UpdateUserViewModel(id, email, nickname) | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...g/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/UpdateUserViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package tw.waterballsa.gaas.spring.controllers.viewmodel | ||
|
||
import tw.waterballsa.gaas.domain.User | ||
|
||
data class UpdateUserViewModel( | ||
val id: User.Id, | ||
val email: String, | ||
val nickname: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
package tw.waterballsa.gaas.spring.it.controllers | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.http.MediaType | ||
import org.springframework.test.web.servlet.ResultActions | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import tw.waterballsa.gaas.application.repositories.UserRepository | ||
import tw.waterballsa.gaas.domain.User | ||
import tw.waterballsa.gaas.spring.controllers.UpdateUserRequest | ||
import tw.waterballsa.gaas.spring.controllers.viewmodel.UpdateUserViewModel | ||
import tw.waterballsa.gaas.spring.it.AbstractSpringBootTest | ||
|
||
class UserControllerTest @Autowired constructor( | ||
|
@@ -34,17 +39,70 @@ class UserControllerTest @Autowired constructor( | |
.thenUserNotFound() | ||
} | ||
|
||
@Test | ||
fun givenUserNamedNeverever_whenChangeUserNicknameToMyNickName_thenUserNicknameShouldBeMyNickName() { | ||
givenUserNickname("Neverever") | ||
.whenChangeUserNickname(UpdateUserRequest("my nick name")) | ||
.thenUserNicknameShouldBeChanged("my nick name") | ||
} | ||
|
||
@Test | ||
fun givenUserNamedNeverever_whenChangeUserNicknameTo周杰倫_thenUserNicknameShouldBe周杰倫() { | ||
givenUserNickname("Neverever") | ||
.whenChangeUserNickname(UpdateUserRequest("周杰倫")) | ||
.thenUserNicknameShouldBeChanged("周杰倫") | ||
} | ||
|
||
@Test | ||
fun givenUserNamedNeverever_whenChangeUserNicknameTooShort_thenShouldChangeNicknameFailed() { | ||
givenUserNickname("Neverever") | ||
.whenChangeUserNickname(UpdateUserRequest("abc")) | ||
.thenShouldChangeNicknameFailed("invalid nickname: too short") | ||
} | ||
|
||
@Test | ||
fun givenUserNamedNeverever_whenChangeUserNicknameTooLong_thenShouldChangeNicknameFailed() { | ||
givenUserNickname("Neverever") | ||
.whenChangeUserNickname(UpdateUserRequest("This is a very long nickname")) | ||
.thenShouldChangeNicknameFailed("invalid nickname: too long") | ||
} | ||
|
||
@Test | ||
fun givenUserNamedNeverever_whenAnotherUserChangeToNeverever_thenShouldChangeNicknameFailed() { | ||
givenUserNickname("Neverever") | ||
givenAnotherUserNickname("周杰倫") | ||
.whenChangeUserNickname(UpdateUserRequest("Neverever")) | ||
.thenShouldChangeNicknameFailed("invalid nickname: duplicated") | ||
} | ||
|
||
private fun givenUserDoesNotLogIn(): User = this.mockUser | ||
|
||
private fun givenUserHasLoggedIn(): User { | ||
return userRepository.createUser(mockUser) | ||
} | ||
|
||
private fun givenUserNickname(nickname: String): User { | ||
val user = User("[email protected]", nickname, mockUser.identities) | ||
return userRepository.createUser(user) | ||
} | ||
|
||
private fun givenAnotherUserNickname(nickname: String): User { | ||
val user = User("[email protected]", nickname, mockUser.identities) | ||
return userRepository.createUser(user) | ||
} | ||
|
||
private fun User.whenGetUserSelf(): ResultActions { | ||
val jwt = identities.first().toJwt() | ||
return mockMvc.perform(get("/users/me").withJwt(jwt)) | ||
return mockMvc.perform(get("/users/me").withJwt(toJwt())) | ||
} | ||
|
||
private fun User.whenChangeUserNickname(updateUserRequest: UpdateUserRequest): ResultActions = | ||
mockMvc.perform( | ||
put("/users/me") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(updateUserRequest.toJson()) | ||
.withJwt(toJwt()) | ||
) | ||
|
||
private fun ResultActions.thenGetUserSuccessfully() { | ||
this.andExpect(status().isOk) | ||
.andExpect(jsonPath("$.id").value(mockUser.id!!.value)) | ||
|
@@ -59,4 +117,18 @@ class UserControllerTest @Autowired constructor( | |
.andExpect(jsonPath("$.nickname").doesNotExist()) | ||
} | ||
|
||
private fun ResultActions.thenUserNicknameShouldBeChanged(nickname: String) { | ||
val user = andExpect(status().isOk) | ||
.getBody(UpdateUserViewModel::class.java) | ||
|
||
userRepository.findById(user.id) | ||
.also { assertThat(it).isNotNull } | ||
.also { assertThat(it!!.nickname).isEqualTo(nickname) } | ||
} | ||
|
||
private fun ResultActions.thenShouldChangeNicknameFailed(message: String) { | ||
andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.message").value(message)) | ||
} | ||
|
||
} |