test: write tests for controllers #24

Merged
devitq merged 11 commits from feat/spring-tests into develop 2026-05-08 20:32:36 +00:00
6 changed files with 47 additions and 35 deletions
Showing only changes of commit e91ebb7bd0 - Show all commits
@@ -69,10 +69,11 @@ class UserRepository(
}
override fun findAll(): List<User> =
jdbc.query(
"SELECT id, name, email, provider, provider_id, created_at FROM users",
userEntityRowMapper,
).map { it.toDomain() }
jdbc
.query(
"SELECT id, name, email, provider, provider_id, created_at FROM users",
userEntityRowMapper,
).map { it.toDomain() }
override fun deleteById(id: UUID) {
jdbc.update("DELETE FROM users WHERE id = ?", id)
@@ -84,7 +85,11 @@ class UserRepository(
): User? {
val entities =
jdbc.query(
"SELECT id, name, email, provider, provider_id, created_at FROM users WHERE provider = ? AND provider_id = ?",
"""
SELECT id, name, email, provider, provider_id, created_at
FROM users
WHERE provider = ? AND provider_id = ?
""".trimIndent(),
userEntityRowMapper,
provider.name,
providerId,
2
@@ -57,10 +57,11 @@ class FilmController(
FilmResponse.fromDomain(
editFilmUseCase.edit(
id = id,
command = EditFilmCommand(
title = request.title,
description = request.description,
),
command =
EditFilmCommand(
title = request.title,
description = request.description,
),
),
copilot-pull-request-reviewer[bot] commented 2026-04-21 18:32:33 +00:00 (Migrated from github.com)
Review

searchByTitle returns null when a film isn't found, which produces a 200 with an empty body. Other not-found scenarios in this API return 404 via EntityNotFoundException/ApiExceptionHandler; consider returning a 404 (or 204) explicitly (e.g., ResponseEntity.notFound()), to keep error semantics consistent for clients.

`searchByTitle` returns `null` when a film isn't found, which produces a 200 with an empty body. Other not-found scenarios in this API return 404 via `EntityNotFoundException`/`ApiExceptionHandler`; consider returning a 404 (or 204) explicitly (e.g., `ResponseEntity.notFound()`), to keep error semantics consistent for clients.
)
4
@@ -104,12 +104,16 @@ class FilmLibraryController(
@PathVariable userId: UUID,
): List<FilmResponse> {
val userLibrary =
try {
runCatching {
getFilmLibraryUseCase.getLibrary(
GetFilmLibraryQuery(userId = userId),
)
} catch (e: EntityNotFoundException) {
null
}.getOrElse { exception ->
if (exception is EntityNotFoundException) {
null
} else {
throw exception
}
}
val allFilms = getAllFilmsUseCase.getAll()
@@ -46,14 +46,12 @@ class UserController(
)
@GetMapping
fun getAll(): List<UserResponse> =
getAllUsersUseCase.getAll().map { UserResponse.fromDomain(it) }
fun getAll(): List<UserResponse> = getAllUsersUseCase.getAll().map { UserResponse.fromDomain(it) }
@GetMapping("/{id}")
fun getById(
@PathVariable id: UUID,
): UserResponse =
UserResponse.fromDomain(getUserByIdUseCase.getById(id))
): UserResponse = UserResponse.fromDomain(getUserByIdUseCase.getById(id))
@PatchMapping("/{id}")
fun edit(
@@ -63,9 +61,10 @@ class UserController(
UserResponse.fromDomain(
editUserUseCase.edit(
id = id,
command = EditUserCommand(
name = request.name,
),
command =
EditUserCommand(
name = request.name,
),
),
)
@@ -36,15 +36,19 @@ class FilmService(
throw BlockedValueException(target = "Film", field = "description")
}
val film = Film(
id = idGenerator.generateId(),
title = command.title,
description = command.description,
)
val film =
Film(
id = idGenerator.generateId(),
title = command.title,
description = command.description,
)
return filmRepository.save(film)
}
override fun edit(id: UUID, command: EditFilmCommand): Film {
override fun edit(
id: UUID,
command: EditFilmCommand,
): Film {
if (filmConfig.isBlocked(command.title)) {
throw BlockedValueException(target = "Film", field = "title")
}
@@ -26,18 +26,18 @@ class UserService(
DeleteUserUseCase,
GetUserByIdUseCase,
GetAllUsersUseCase {
override fun create(command: CreateUserCommand): User {
if (userConfig.isBlocked(command.name)) {
throw BlockedValueException(target = "User", field = "name")
}
val user = User(
id = idGenerator.generateId(),
name = command.name,
email = command.email,
library = null,
)
val user =
User(
id = idGenerator.generateId(),
name = command.name,
email = command.email,
library = null,
)
return userRepository.save(user)
}
@@ -59,9 +59,8 @@ class UserService(
userRepository.deleteById(id)
}
override fun getById(id: UUID): User {
return userRepository.findById(id) ?: throw EntityNotFoundException(entity = "User", id = id.toString())
}
override fun getById(id: UUID): User =
userRepository.findById(id) ?: throw EntityNotFoundException(entity = "User", id = id.toString())
override fun getAll(): List<User> = userRepository.findAll()
}