From 67a5acf5af2ffec98abcf2df959edcc22ff53d21 Mon Sep 17 00:00:00 2001 From: Oscar Date: Wed, 27 Mar 2024 15:39:11 +0100 Subject: [PATCH] More refactors --- ...nController.kt => GetPokemonController.kt} | 31 ++++----------- .../usecase/GetPokemonByIdCommandHandler.kt | 14 +++++++ .../context/pokemon/domain/Pokemon.kt | 2 - .../domain/repository/PokemonRepository.kt | 4 +- .../pokemon/domain/service/PokemonCreator.kt | 17 --------- .../infrastructure/PokemonDataRepository.kt | 2 - .../datasource/ClickHouseDataSource.kt | 5 --- .../CreatePokemonControllerAcceptanceTest.kt | 4 +- .../domain/service/PokemonFinderTest.kt | 5 +-- .../PokemonApiDataSourceIntegrationTest.kt | 2 +- .../c/pozas/playground/kernel/DomainEvent.kt | 6 +++ .../usecase/CreatePokemonUseCaseTest.kt | 38 ------------------- 12 files changed, 33 insertions(+), 97 deletions(-) rename context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/controller/{PokemonController.kt => GetPokemonController.kt} (54%) create mode 100644 context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/usecase/GetPokemonByIdCommandHandler.kt delete mode 100644 context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/service/PokemonCreator.kt delete mode 100644 context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/datasource/ClickHouseDataSource.kt rename src/test/kotlin/oscar/c/pozas/playground/app/controller/PokemonControllerAcceptanceTest.kt => context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/app/controller/CreatePokemonControllerAcceptanceTest.kt (97%) rename src/test/kotlin/oscar/c/pozas/playground/domain/usecase/GetPokemonUseCaseTest.kt => context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/domain/service/PokemonFinderTest.kt (93%) rename {src/test/kotlin/oscar/c/pozas/playground/infrastructure/datasource => context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/infrastructure}/PokemonApiDataSourceIntegrationTest.kt (96%) create mode 100644 kernel/src/main/kotlin/oscar/c/pozas/playground/kernel/DomainEvent.kt delete mode 100644 src/test/kotlin/oscar/c/pozas/playground/domain/usecase/CreatePokemonUseCaseTest.kt diff --git a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/controller/PokemonController.kt b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/controller/GetPokemonController.kt similarity index 54% rename from context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/controller/PokemonController.kt rename to context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/controller/GetPokemonController.kt index 05f4ba1..895727e 100644 --- a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/controller/PokemonController.kt +++ b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/controller/GetPokemonController.kt @@ -5,28 +5,17 @@ import io.swagger.v3.oas.annotations.media.Content import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.responses.ApiResponses -import io.swagger.v3.oas.annotations.tags.Tag import org.springframework.http.HttpStatus -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.RequestBody -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.ResponseStatus -import org.springframework.web.bind.annotation.RestController -import oscar.c.pozas.playgroud.context.pokemon.app.controller.inputmodel.PokemonInputModel +import org.springframework.web.bind.annotation.* import oscar.c.pozas.playgroud.context.pokemon.app.controller.viewmodel.PokemonViewModel import oscar.c.pozas.playgroud.context.pokemon.app.controller.viewmodel.toViewModel -import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon -import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonCreator -import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonFinder +import oscar.c.pozas.playgroud.context.pokemon.app.usecase.GetPokemonByIdCommand +import oscar.c.pozas.playgroud.context.pokemon.app.usecase.GetPokemonByIdCommandHandler @RestController @RequestMapping("public/v1/pokemon") -@Tag(name = "Pokemon public API") -class PokemonController( - private val pokemonFinder: PokemonFinder, - private val pokemonCreator: PokemonCreator, +class GetPokemonController( + private val getPokemonById: GetPokemonByIdCommandHandler ) { @GetMapping("/{id}") @@ -50,11 +39,5 @@ class PokemonController( ] ) @ResponseStatus(HttpStatus.OK) - fun findById(@PathVariable("id") id: Int): PokemonViewModel = - pokemonFinder(Pokemon.Id(id))!!.toViewModel() - - @PostMapping("/") - @Operation(summary = "Create a new custom Pokemon") - @ResponseStatus(HttpStatus.CREATED) - fun create(@RequestBody input: PokemonInputModel) = pokemonCreator(input.toDomain()) -} + fun findById(@PathVariable("id") id: Int): PokemonViewModel = getPokemonById(GetPokemonByIdCommand(id)).toViewModel() +} \ No newline at end of file diff --git a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/usecase/GetPokemonByIdCommandHandler.kt b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/usecase/GetPokemonByIdCommandHandler.kt new file mode 100644 index 0000000..3510a23 --- /dev/null +++ b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/app/usecase/GetPokemonByIdCommandHandler.kt @@ -0,0 +1,14 @@ +package oscar.c.pozas.playgroud.context.pokemon.app.usecase + +import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon +import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonFinder + +class GetPokemonByIdCommandHandler(private val pokemonFinder: PokemonFinder) { + + operator fun invoke(command: GetPokemonByIdCommand): Pokemon { + val id = Pokemon.Id(command.id) + return pokemonFinder(id)!! + } +} + +data class GetPokemonByIdCommand(val id: Int) \ No newline at end of file diff --git a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/Pokemon.kt b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/Pokemon.kt index 695a591..7a89290 100644 --- a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/Pokemon.kt +++ b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/Pokemon.kt @@ -8,6 +8,4 @@ data class Pokemon( data class Id(val value: Int) data class Name(val value: String) - - } \ No newline at end of file diff --git a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/repository/PokemonRepository.kt b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/repository/PokemonRepository.kt index fc842e1..98be16a 100644 --- a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/repository/PokemonRepository.kt +++ b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/repository/PokemonRepository.kt @@ -4,7 +4,5 @@ import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon interface PokemonRepository { - fun create(pokemon: Pokemon): Pokemon - fun findById(id: Pokemon.Id): Pokemon? -} +} \ No newline at end of file diff --git a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/service/PokemonCreator.kt b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/service/PokemonCreator.kt deleted file mode 100644 index 96ad6a2..0000000 --- a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/domain/service/PokemonCreator.kt +++ /dev/null @@ -1,17 +0,0 @@ -package oscar.c.pozas.playgroud.context.pokemon.domain.service - -import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon -import oscar.c.pozas.playgroud.context.pokemon.domain.repository.PokemonRepository - -class PokemonCreator(private val repository: PokemonRepository) { - - operator fun invoke(pokemon: Pokemon) { - ensurePokemonNotExists() - - repository.create(pokemon) - } - - private fun ensurePokemonNotExists() { - // TODO: Implement me pls - } -} \ No newline at end of file diff --git a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/PokemonDataRepository.kt b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/PokemonDataRepository.kt index a706944..2ee06cc 100644 --- a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/PokemonDataRepository.kt +++ b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/PokemonDataRepository.kt @@ -13,8 +13,6 @@ class PokemonDataRepository( private val pokemonStoreDataSource: PokemonStoreDataSource ) : PokemonRepository { - override fun create(pokemon: Pokemon) = pokemonStoreDataSource.save(pokemon) - @Cacheable(value = ["pokemon"]) override fun findById(id: Pokemon.Id): Pokemon? = pokemonStoreDataSource.getById(id.value) ?: pokemonApiClientDataSource.get(id.value) diff --git a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/datasource/ClickHouseDataSource.kt b/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/datasource/ClickHouseDataSource.kt deleted file mode 100644 index d52f6d4..0000000 --- a/context/pokemon/src/main/kotlin/oscar/c/pozas/playgroud/context/pokemon/infrastructure/datasource/ClickHouseDataSource.kt +++ /dev/null @@ -1,5 +0,0 @@ -package oscar.c.pozas.playgroud.context.pokemon.infrastructure.datasource - -class ClickHouseDataSource { - -} \ No newline at end of file diff --git a/src/test/kotlin/oscar/c/pozas/playground/app/controller/PokemonControllerAcceptanceTest.kt b/context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/app/controller/CreatePokemonControllerAcceptanceTest.kt similarity index 97% rename from src/test/kotlin/oscar/c/pozas/playground/app/controller/PokemonControllerAcceptanceTest.kt rename to context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/app/controller/CreatePokemonControllerAcceptanceTest.kt index beeefb6..e177754 100644 --- a/src/test/kotlin/oscar/c/pozas/playground/app/controller/PokemonControllerAcceptanceTest.kt +++ b/context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/app/controller/CreatePokemonControllerAcceptanceTest.kt @@ -1,4 +1,4 @@ -package oscar.c.pozas.playground.app.controller +package oscar.c.pozas.playground.context.pokemon.app.controller import com.fasterxml.jackson.databind.ObjectMapper import com.github.tomakehurst.wiremock.client.WireMock.get @@ -22,7 +22,7 @@ import org.springframework.util.ResourceUtils @WireMockTest(httpPort = 8089) @AutoConfigureEmbeddedDatabase(type = POSTGRES, refresh = AFTER_EACH_TEST_METHOD) @ActiveProfiles("test") -class PokemonControllerAcceptanceTest { +class CreatePokemonControllerAcceptanceTest { @LocalServerPort protected var springBootPort = 0 // Port used during the test is injected diff --git a/src/test/kotlin/oscar/c/pozas/playground/domain/usecase/GetPokemonUseCaseTest.kt b/context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/domain/service/PokemonFinderTest.kt similarity index 93% rename from src/test/kotlin/oscar/c/pozas/playground/domain/usecase/GetPokemonUseCaseTest.kt rename to context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/domain/service/PokemonFinderTest.kt index 96bd192..7d9c17a 100644 --- a/src/test/kotlin/oscar/c/pozas/playground/domain/usecase/GetPokemonUseCaseTest.kt +++ b/context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/domain/service/PokemonFinderTest.kt @@ -1,4 +1,4 @@ -package oscar.c.pozas.playground.domain.usecase +package oscar.c.pozas.playground.context.pokemon.domain.service import io.mockk.MockKAnnotations import io.mockk.every @@ -14,10 +14,9 @@ import org.junit.jupiter.api.assertThrows import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon import oscar.c.pozas.playgroud.context.pokemon.domain.repository.PokemonRepository import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonFinder -import java.util.Optional @TestInstance(TestInstance.Lifecycle.PER_CLASS) -class GetPokemonUseCaseTest { +class PokemonFinderTest { @MockK private lateinit var repository: PokemonRepository diff --git a/src/test/kotlin/oscar/c/pozas/playground/infrastructure/datasource/PokemonApiDataSourceIntegrationTest.kt b/context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/infrastructure/PokemonApiDataSourceIntegrationTest.kt similarity index 96% rename from src/test/kotlin/oscar/c/pozas/playground/infrastructure/datasource/PokemonApiDataSourceIntegrationTest.kt rename to context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/infrastructure/PokemonApiDataSourceIntegrationTest.kt index ae1deec..e833db7 100644 --- a/src/test/kotlin/oscar/c/pozas/playground/infrastructure/datasource/PokemonApiDataSourceIntegrationTest.kt +++ b/context/pokemon/src/test/kotlin/oscar/c/pozas/playground/context/pokemon/infrastructure/PokemonApiDataSourceIntegrationTest.kt @@ -1,4 +1,4 @@ -package oscar.c.pozas.playground.infrastructure.datasource +package oscar.c.pozas.playground.context.pokemon.infrastructure import io.mockk.MockKAnnotations import io.mockk.every diff --git a/kernel/src/main/kotlin/oscar/c/pozas/playground/kernel/DomainEvent.kt b/kernel/src/main/kotlin/oscar/c/pozas/playground/kernel/DomainEvent.kt new file mode 100644 index 0000000..cfc11e3 --- /dev/null +++ b/kernel/src/main/kotlin/oscar/c/pozas/playground/kernel/DomainEvent.kt @@ -0,0 +1,6 @@ +package oscar.c.pozas.playground.kernel + +abstract class DomainEvent { + + abstract fun getName(): String +} \ No newline at end of file diff --git a/src/test/kotlin/oscar/c/pozas/playground/domain/usecase/CreatePokemonUseCaseTest.kt b/src/test/kotlin/oscar/c/pozas/playground/domain/usecase/CreatePokemonUseCaseTest.kt deleted file mode 100644 index d5f5251..0000000 --- a/src/test/kotlin/oscar/c/pozas/playground/domain/usecase/CreatePokemonUseCaseTest.kt +++ /dev/null @@ -1,38 +0,0 @@ -package oscar.c.pozas.playground.domain.usecase - -import io.mockk.MockKAnnotations -import io.mockk.every -import io.mockk.impl.annotations.InjectMockKs -import io.mockk.impl.annotations.MockK -import org.junit.jupiter.api.BeforeAll -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.api.assertDoesNotThrow -import oscar.c.pozas.playgroud.context.pokemon.domain.Pokemon -import oscar.c.pozas.playgroud.context.pokemon.domain.repository.PokemonRepository -import oscar.c.pozas.playgroud.context.pokemon.domain.service.PokemonCreator - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -class CreatePokemonUseCaseTest { - - @MockK - private lateinit var repository: PokemonRepository - - @InjectMockKs - private lateinit var pokemonCreator: PokemonCreator - - private val pokemon = Pokemon(id = Pokemon.Id(5), name = Pokemon.Name("Charmeleon")) - - @BeforeAll - fun setUp() { - MockKAnnotations.init(this) - every { repository.create(pokemon) }.returns(pokemon) - } - - @Test - fun `when provided a Pokemon then not throw exception`() { - assertDoesNotThrow { - pokemonCreator(pokemon) - } - } -}