From 50de832d1f3c5d8814b7c81c30fec2a1a55a0665 Mon Sep 17 00:00:00 2001 From: evgnep Date: Tue, 18 Jun 2024 08:28:42 +0300 Subject: [PATCH] M6l2 biz --- gradle/libs.versions.toml | 1 + .../commonTest/kotlin/stub/V2AdStubApiTest.kt | 2 +- .../jvmTest/kotlin/stub/V1AdStubApiTest.kt | 2 +- .../kotlin/mock/AdControllerV1StubTest.kt | 98 ------------- .../ok-marketplace-biz/build.gradle.kts | 2 + .../src/commonMain/kotlin/MkplAdProcessor.kt | 72 ++++++++++ .../commonMain/kotlin/general/InitStatus.kt | 15 ++ .../commonMain/kotlin/general/Operation.kt | 17 +++ .../src/commonMain/kotlin/general/Stubs.kt | 13 ++ .../marketplace/biz/MkplAdProcessor.kt | 16 --- .../kotlin/stubs/StubCreateSuccess.kt | 33 +++++ .../kotlin/stubs/StubDeleteSuccess.kt | 28 ++++ .../src/commonMain/kotlin/stubs/StubNoCase.kt | 26 ++++ .../kotlin/stubs/StubOffersSuccess.kt | 28 ++++ .../kotlin/stubs/StubReadSuccess.kt | 28 ++++ .../kotlin/stubs/StubSearchSuccess.kt | 25 ++++ .../kotlin/stubs/StubUpdateSuccess.kt | 35 +++++ .../stubs/StubValidationBadDescription.kt | 27 ++++ .../kotlin/stubs/StubValidationBadId.kt | 27 ++++ .../kotlin/stubs/StubValidationBadTitle.kt | 28 ++++ .../kotlin/stubs/StubValidationDbError.kt | 26 ++++ .../src/commonMain/kotlin/stubs/Stubs.kt | 13 ++ .../src/commonTest/kotlin/package.kt | 2 + .../kotlin/stub/AdCreateStubTest.kt | 122 ++++++++++++++++ .../kotlin/stub/AdDeleteStubTest.kt | 85 +++++++++++ .../kotlin/stub/AdOffersStubTest.kt | 98 +++++++++++++ .../commonTest/kotlin/stub/AdReadStubTest.kt | 85 +++++++++++ .../kotlin/stub/AdSearchStubTest.kt | 82 +++++++++++ .../kotlin/stub/AdUpdateStubTest.kt | 136 ++++++++++++++++++ .../src/commonMain/kotlin/MkplContext.kt | 1 + .../kotlin/helpers/MkplErrorsHelpers.kt | 9 ++ 31 files changed, 1066 insertions(+), 116 deletions(-) delete mode 100644 ok-marketplace-be/ok-marketplace-app-spring/src/test/kotlin/mock/AdControllerV1StubTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/InitStatus.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Operation.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Stubs.kt delete mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/ru/otus/otuskotlin/marketplace/biz/MkplAdProcessor.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubCreateSuccess.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubDeleteSuccess.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubNoCase.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubOffersSuccess.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubReadSuccess.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubSearchSuccess.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubUpdateSuccess.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadDescription.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadId.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadTitle.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationDbError.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/Stubs.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/package.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdCreateStubTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdDeleteStubTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdOffersStubTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdReadStubTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdSearchStubTest.kt create mode 100644 ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdUpdateStubTest.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d62fed3..276a67d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -37,6 +37,7 @@ coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", ve coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } coroutines-reactor = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-reactor", version.ref = "coroutines" } coroutines-reactive = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-reactive", version.ref = "coroutines" } +cor = { module = "ru.otus.otuskotlin.marketplace.libs:ok-marketplace-lib-cor" } jackson-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" } jackson-datatype = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310", version.ref = "jackson" } diff --git a/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/stub/V2AdStubApiTest.kt b/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/stub/V2AdStubApiTest.kt index 44a5e90..77c6e23 100644 --- a/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/stub/V2AdStubApiTest.kt +++ b/ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/stub/V2AdStubApiTest.kt @@ -126,7 +126,7 @@ class V2AdStubApiTest { ) { response -> val responseObj = response.body() assertEquals(200, response.status.value) - assertEquals("d-666-01", responseObj.ads?.first()?.id) + assertEquals("s-666-01", responseObj.ads?.first()?.id) } private inline fun v2TestApplication( diff --git a/ok-marketplace-be/ok-marketplace-app-ktor/src/jvmTest/kotlin/stub/V1AdStubApiTest.kt b/ok-marketplace-be/ok-marketplace-app-ktor/src/jvmTest/kotlin/stub/V1AdStubApiTest.kt index e23976c..2a99823 100644 --- a/ok-marketplace-be/ok-marketplace-app-ktor/src/jvmTest/kotlin/stub/V1AdStubApiTest.kt +++ b/ok-marketplace-be/ok-marketplace-app-ktor/src/jvmTest/kotlin/stub/V1AdStubApiTest.kt @@ -125,7 +125,7 @@ class V1AdStubApiTest { ) { response -> val responseObj = response.body() assertEquals(200, response.status.value) - assertEquals("d-666-01", responseObj.ads?.first()?.id) + assertEquals("s-666-01", responseObj.ads?.first()?.id) } private fun v1TestApplication( diff --git a/ok-marketplace-be/ok-marketplace-app-spring/src/test/kotlin/mock/AdControllerV1StubTest.kt b/ok-marketplace-be/ok-marketplace-app-spring/src/test/kotlin/mock/AdControllerV1StubTest.kt deleted file mode 100644 index 4aa6b65..0000000 --- a/ok-marketplace-be/ok-marketplace-app-spring/src/test/kotlin/mock/AdControllerV1StubTest.kt +++ /dev/null @@ -1,98 +0,0 @@ -package ru.otus.otuskotlin.markeplace.app.spring.mock - -import org.assertj.core.api.Assertions.assertThat -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest -import org.springframework.http.MediaType -import org.springframework.test.web.reactive.server.WebTestClient -import org.springframework.web.reactive.function.BodyInserters -import ru.otus.otuskotlin.markeplace.app.spring.config.AdConfig -import ru.otus.otuskotlin.markeplace.app.spring.controllers.AdControllerV1Fine -import ru.otus.otuskotlin.marketplace.api.v1.models.* -import ru.otus.otuskotlin.marketplace.common.MkplContext -import ru.otus.otuskotlin.marketplace.common.models.MkplDealSide -import ru.otus.otuskotlin.marketplace.common.models.MkplState -import ru.otus.otuskotlin.marketplace.mappers.v1.* -import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub -import kotlin.test.Test - -// Temporary simple test with stubs -@WebFluxTest(AdControllerV1Fine::class, AdConfig::class) -internal class AdControllerV1StubTest { - @Autowired - private lateinit var webClient: WebTestClient - - @Test - fun createAd() = testStubAd( - "/v1/ad/create", - AdCreateRequest(), - MkplContext(adResponse = MkplAdStub.get(), state = MkplState.FINISHING) - .toTransportCreate().copy(responseType = "create") - ) - - @Test - fun readAd() = testStubAd( - "/v1/ad/read", - AdReadRequest(), - MkplContext(adResponse = MkplAdStub.get(), state = MkplState.FINISHING) - .toTransportRead().copy(responseType = "read") - ) - - @Test - fun updateAd() = testStubAd( - "/v1/ad/update", - AdUpdateRequest(), - MkplContext(adResponse = MkplAdStub.get(), state = MkplState.FINISHING) - .toTransportUpdate().copy(responseType = "update") - ) - - @Test - fun deleteAd() = testStubAd( - "/v1/ad/delete", - AdDeleteRequest(), - MkplContext(adResponse = MkplAdStub.get(), state = MkplState.FINISHING) - .toTransportDelete().copy(responseType = "delete") - ) - - @Test - fun searchAd() = testStubAd( - "/v1/ad/search", - AdSearchRequest(), - MkplContext( - adsResponse = MkplAdStub.prepareSearchList("ad search", MkplDealSide.DEMAND).toMutableList(), - state = MkplState.FINISHING - ) - .toTransportSearch().copy(responseType = "search") - ) - - @Test - fun offersAd() = testStubAd( - "/v1/ad/offers", - AdOffersRequest(), - MkplContext( - adResponse = MkplAdStub.get(), - adsResponse = MkplAdStub.prepareSearchList("ad search", MkplDealSide.DEMAND).toMutableList(), - state = MkplState.FINISHING - ) - .toTransportOffers().copy(responseType = "offers") - ) - - private inline fun testStubAd( - url: String, - requestObj: Req, - responseObj: Res, - ) { - webClient - .post() - .uri(url) - .contentType(MediaType.APPLICATION_JSON) - .body(BodyInserters.fromValue(requestObj)) - .exchange() - .expectStatus().isOk - .expectBody(Res::class.java) - .value { - println("RESPONSE: $it") - assertThat(it).isEqualTo(responseObj) - } - } -} diff --git a/ok-marketplace-be/ok-marketplace-biz/build.gradle.kts b/ok-marketplace-be/ok-marketplace-biz/build.gradle.kts index ee7ee48..de5fb1b 100644 --- a/ok-marketplace-be/ok-marketplace-biz/build.gradle.kts +++ b/ok-marketplace-be/ok-marketplace-biz/build.gradle.kts @@ -10,6 +10,8 @@ kotlin { dependencies { implementation(kotlin("stdlib-common")) + implementation(libs.cor) + implementation(project(":ok-marketplace-common")) implementation(project(":ok-marketplace-stubs")) } diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt new file mode 100644 index 0000000..47d9958 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/MkplAdProcessor.kt @@ -0,0 +1,72 @@ +package ru.otus.otuskotlin.marketplace.biz + +import ru.otus.otuskotlin.marketplace.biz.general.initStatus +import ru.otus.otuskotlin.marketplace.biz.general.operation +import ru.otus.otuskotlin.marketplace.biz.general.stubs +import ru.otus.otuskotlin.marketplace.biz.stubs.* +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.MkplCorSettings +import ru.otus.otuskotlin.marketplace.common.models.MkplCommand +import ru.otus.otuskotlin.marketplace.cor.rootChain + +class MkplAdProcessor( + private val corSettings: MkplCorSettings = MkplCorSettings.NONE +) { + suspend fun exec(ctx: MkplContext) = businessChain.exec(ctx.also { it.corSettings = corSettings }) + + private val businessChain = rootChain { + initStatus("Инициализация статуса") + + operation("Создание объявления", MkplCommand.CREATE) { + stubs("Обработка стабов") { + stubCreateSuccess("Имитация успешной обработки", corSettings) + stubValidationBadTitle("Имитация ошибки валидации заголовка") + stubValidationBadDescription("Имитация ошибки валидации описания") + stubDbError("Имитация ошибки работы с БД") + stubNoCase("Ошибка: запрошенный стаб недопустим") + } + } + operation("Получить объявление", MkplCommand.READ) { + stubs("Обработка стабов") { + stubReadSuccess("Имитация успешной обработки", corSettings) + stubValidationBadId("Имитация ошибки валидации id") + stubDbError("Имитация ошибки работы с БД") + stubNoCase("Ошибка: запрошенный стаб недопустим") + } + } + operation("Изменить объявление", MkplCommand.UPDATE) { + stubs("Обработка стабов") { + stubUpdateSuccess("Имитация успешной обработки", corSettings) + stubValidationBadId("Имитация ошибки валидации id") + stubValidationBadTitle("Имитация ошибки валидации заголовка") + stubValidationBadDescription("Имитация ошибки валидации описания") + stubDbError("Имитация ошибки работы с БД") + stubNoCase("Ошибка: запрошенный стаб недопустим") + } + } + operation("Удалить объявление", MkplCommand.DELETE) { + stubs("Обработка стабов") { + stubDeleteSuccess("Имитация успешной обработки", corSettings) + stubValidationBadId("Имитация ошибки валидации id") + stubDbError("Имитация ошибки работы с БД") + stubNoCase("Ошибка: запрошенный стаб недопустим") + } + } + operation("Поиск объявлений", MkplCommand.SEARCH) { + stubs("Обработка стабов") { + stubSearchSuccess("Имитация успешной обработки", corSettings) + stubValidationBadId("Имитация ошибки валидации id") + stubDbError("Имитация ошибки работы с БД") + stubNoCase("Ошибка: запрошенный стаб недопустим") + } + } + operation("Поиск подходящих предложений для объявления", MkplCommand.OFFERS) { + stubs("Обработка стабов") { + stubOffersSuccess("Имитация успешной обработки", corSettings) + stubValidationBadId("Имитация ошибки валидации id") + stubDbError("Имитация ошибки работы с БД") + stubNoCase("Ошибка: запрошенный стаб недопустим") + } + } + }.build() +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/InitStatus.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/InitStatus.kt new file mode 100644 index 0000000..a1d68f6 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/InitStatus.kt @@ -0,0 +1,15 @@ +package ru.otus.otuskotlin.marketplace.biz.general + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.MkplState + +fun ICorChainDsl.initStatus(title: String) = worker() { + this.title = title + this.description = """ + Этот обработчик устанавливает стартовый статус обработки. Запускается только в случае не заданного статуса. + """.trimIndent() + on { state == MkplState.NONE } + handle { state = MkplState.RUNNING } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Operation.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Operation.kt new file mode 100644 index 0000000..cd22539 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Operation.kt @@ -0,0 +1,17 @@ +package ru.otus.otuskotlin.marketplace.biz.general + +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.MkplCommand +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.chain + +fun ICorChainDsl.operation( + title: String, + command: MkplCommand, + block: ICorChainDsl.() -> Unit +) = chain { + block() + this.title = title + on { this.command == command && state == MkplState.RUNNING } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Stubs.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Stubs.kt new file mode 100644 index 0000000..ceedbfe --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/general/Stubs.kt @@ -0,0 +1,13 @@ +package ru.otus.otuskotlin.marketplace.biz.general + +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.models.MkplWorkMode +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.chain + +fun ICorChainDsl.stubs(title: String, block: ICorChainDsl.() -> Unit) = chain { + block() + this.title = title + on { workMode == MkplWorkMode.STUB && state == MkplState.RUNNING } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/ru/otus/otuskotlin/marketplace/biz/MkplAdProcessor.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/ru/otus/otuskotlin/marketplace/biz/MkplAdProcessor.kt deleted file mode 100644 index 26ae0f8..0000000 --- a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/ru/otus/otuskotlin/marketplace/biz/MkplAdProcessor.kt +++ /dev/null @@ -1,16 +0,0 @@ -package ru.otus.otuskotlin.marketplace.biz - -import ru.otus.otuskotlin.marketplace.common.MkplContext -import ru.otus.otuskotlin.marketplace.common.MkplCorSettings -import ru.otus.otuskotlin.marketplace.common.models.MkplDealSide -import ru.otus.otuskotlin.marketplace.common.models.MkplState -import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub - -@Suppress("unused", "RedundantSuspendModifier") -class MkplAdProcessor(val corSettings: MkplCorSettings) { - suspend fun exec(ctx: MkplContext) { - ctx.adResponse = MkplAdStub.get() - ctx.adsResponse = MkplAdStub.prepareSearchList("ad search", MkplDealSide.DEMAND).toMutableList() - ctx.state = MkplState.RUNNING - } -} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubCreateSuccess.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubCreateSuccess.kt new file mode 100644 index 0000000..66a1fd7 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubCreateSuccess.kt @@ -0,0 +1,33 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.MkplCorSettings +import ru.otus.otuskotlin.marketplace.common.models.MkplDealSide +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.models.MkplVisibility +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.logging.common.LogLevel +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub + +fun ICorChainDsl.stubCreateSuccess(title: String, corSettings: MkplCorSettings) = worker { + this.title = title + this.description = """ + Кейс успеха для создания объявления + """.trimIndent() + on { stubCase == MkplStubs.SUCCESS && state == MkplState.RUNNING } + val logger = corSettings.loggerProvider.logger("stubOffersSuccess") + handle { + logger.doWithLogging(id = this.requestId.asString(), LogLevel.DEBUG) { + state = MkplState.FINISHING + val stub = MkplAdStub.prepareResult { + adRequest.title.takeIf { it.isNotBlank() }?.also { this.title = it } + adRequest.description.takeIf { it.isNotBlank() }?.also { this.description = it } + adRequest.adType.takeIf { it != MkplDealSide.NONE }?.also { this.adType = it } + adRequest.visibility.takeIf { it != MkplVisibility.NONE }?.also { this.visibility = it } + } + adResponse = stub + } + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubDeleteSuccess.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubDeleteSuccess.kt new file mode 100644 index 0000000..cb242c3 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubDeleteSuccess.kt @@ -0,0 +1,28 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.MkplCorSettings +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.logging.common.LogLevel +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub + +fun ICorChainDsl.stubDeleteSuccess(title: String, corSettings: MkplCorSettings) = worker { + this.title = title + this.description = """ + Кейс успеха для удаления объявления + """.trimIndent() + on { stubCase == MkplStubs.SUCCESS && state == MkplState.RUNNING } + val logger = corSettings.loggerProvider.logger("stubOffersSuccess") + handle { + logger.doWithLogging(id = this.requestId.asString(), LogLevel.DEBUG) { + state = MkplState.FINISHING + val stub = MkplAdStub.prepareResult { + adRequest.title.takeIf { it.isNotBlank() }?.also { this.title = it } + } + adResponse = stub + } + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubNoCase.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubNoCase.kt new file mode 100644 index 0000000..c806bcb --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubNoCase.kt @@ -0,0 +1,26 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.helpers.fail +import ru.otus.otuskotlin.marketplace.common.models.MkplError +import ru.otus.otuskotlin.marketplace.common.models.MkplState + +fun ICorChainDsl.stubNoCase(title: String) = worker { + this.title = title + this.description = """ + Валидируем ситуацию, когда запрошен кейс, который не поддерживается в стабах + """.trimIndent() + on { state == MkplState.RUNNING } + handle { + fail( + MkplError( + code = "validation", + field = "stub", + group = "validation", + message = "Wrong stub case is requested: ${stubCase.name}" + ) + ) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubOffersSuccess.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubOffersSuccess.kt new file mode 100644 index 0000000..c64e2fa --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubOffersSuccess.kt @@ -0,0 +1,28 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.MkplCorSettings +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.logging.common.LogLevel +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub + +fun ICorChainDsl.stubOffersSuccess(title: String, corSettings: MkplCorSettings) = worker { + this.title = title + this.description = """ + Кейс успеха для получения предложений для объявления + """.trimIndent() + on { stubCase == MkplStubs.SUCCESS && state == MkplState.RUNNING } + val logger = corSettings.loggerProvider.logger("stubOffersSuccess") + handle { + logger.doWithLogging(id = this.requestId.asString(), LogLevel.DEBUG) { + state = MkplState.FINISHING + adResponse = MkplAdStub.prepareResult { + adRequest.id.takeIf { it != MkplAdId.NONE }?.also { this.id = it } + } + adsResponse.addAll(MkplAdStub.prepareOffersList(adResponse.title, MkplDealSide.SUPPLY)) + } + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubReadSuccess.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubReadSuccess.kt new file mode 100644 index 0000000..2b0ba0a --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubReadSuccess.kt @@ -0,0 +1,28 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.MkplCorSettings +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.logging.common.LogLevel +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub + +fun ICorChainDsl.stubReadSuccess(title: String, corSettings: MkplCorSettings) = worker { + this.title = title + this.description = """ + Кейс успеха для чтения объявления + """.trimIndent() + on { stubCase == MkplStubs.SUCCESS && state == MkplState.RUNNING } + val logger = corSettings.loggerProvider.logger("stubOffersSuccess") + handle { + logger.doWithLogging(id = this.requestId.asString(), LogLevel.DEBUG) { + state = MkplState.FINISHING + val stub = MkplAdStub.prepareResult { + adRequest.title.takeIf { it.isNotBlank() }?.also { this.title = it } + } + adResponse = stub + } + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubSearchSuccess.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubSearchSuccess.kt new file mode 100644 index 0000000..44f1fac --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubSearchSuccess.kt @@ -0,0 +1,25 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.MkplCorSettings +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.logging.common.LogLevel +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub + +fun ICorChainDsl.stubSearchSuccess(title: String, corSettings: MkplCorSettings) = worker { + this.title = title + this.description = """ + Кейс успеха для поиска объявлений + """.trimIndent() + on { stubCase == MkplStubs.SUCCESS && state == MkplState.RUNNING } + val logger = corSettings.loggerProvider.logger("stubOffersSuccess") + handle { + logger.doWithLogging(id = this.requestId.asString(), LogLevel.DEBUG) { + state = MkplState.FINISHING + adsResponse.addAll(MkplAdStub.prepareSearchList(adFilterRequest.searchString, MkplDealSide.DEMAND)) + } + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubUpdateSuccess.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubUpdateSuccess.kt new file mode 100644 index 0000000..16d4996 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubUpdateSuccess.kt @@ -0,0 +1,35 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.MkplCorSettings +import ru.otus.otuskotlin.marketplace.common.models.MkplAdId +import ru.otus.otuskotlin.marketplace.common.models.MkplDealSide +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.models.MkplVisibility +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.logging.common.LogLevel +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub + +fun ICorChainDsl.stubUpdateSuccess(title: String, corSettings: MkplCorSettings) = worker { + this.title = title + this.description = """ + Кейс успеха для изменения объявления + """.trimIndent() + on { stubCase == MkplStubs.SUCCESS && state == MkplState.RUNNING } + val logger = corSettings.loggerProvider.logger("stubOffersSuccess") + handle { + logger.doWithLogging(id = this.requestId.asString(), LogLevel.DEBUG) { + state = MkplState.FINISHING + val stub = MkplAdStub.prepareResult { + adRequest.id.takeIf { it != MkplAdId.NONE }?.also { this.id = it } + adRequest.title.takeIf { it.isNotBlank() }?.also { this.title = it } + adRequest.description.takeIf { it.isNotBlank() }?.also { this.description = it } + adRequest.adType.takeIf { it != MkplDealSide.NONE }?.also { this.adType = it } + adRequest.visibility.takeIf { it != MkplVisibility.NONE }?.also { this.visibility = it } + } + adResponse = stub + } + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadDescription.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadDescription.kt new file mode 100644 index 0000000..2dbc703 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadDescription.kt @@ -0,0 +1,27 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.helpers.fail +import ru.otus.otuskotlin.marketplace.common.models.MkplError +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs + +fun ICorChainDsl.stubValidationBadDescription(title: String) = worker { + this.title = title + this.description = """ + Кейс ошибки валидации для описания объявления + """.trimIndent() + on { stubCase == MkplStubs.BAD_DESCRIPTION && state == MkplState.RUNNING } + handle { + fail( + MkplError( + group = "validation", + code = "validation-description", + field = "description", + message = "Wrong description field" + ) + ) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadId.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadId.kt new file mode 100644 index 0000000..98e1130 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadId.kt @@ -0,0 +1,27 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.helpers.fail +import ru.otus.otuskotlin.marketplace.common.models.MkplError +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs + +fun ICorChainDsl.stubValidationBadId(title: String) = worker { + this.title = title + this.description = """ + Кейс ошибки валидации для идентификатора объявления + """.trimIndent() + on { stubCase == MkplStubs.BAD_ID && state == MkplState.RUNNING } + handle { + fail( + MkplError( + group = "validation", + code = "validation-id", + field = "id", + message = "Wrong id field" + ) + ) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadTitle.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadTitle.kt new file mode 100644 index 0000000..6d9952f --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationBadTitle.kt @@ -0,0 +1,28 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.helpers.fail +import ru.otus.otuskotlin.marketplace.common.models.MkplError +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs + +fun ICorChainDsl.stubValidationBadTitle(title: String) = worker { + this.title = title + this.description = """ + Кейс ошибки валидации для заголовка + """.trimIndent() + + on { stubCase == MkplStubs.BAD_TITLE && state == MkplState.RUNNING } + handle { + fail( + MkplError( + group = "validation", + code = "validation-title", + field = "title", + message = "Wrong title field" + ) + ) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationDbError.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationDbError.kt new file mode 100644 index 0000000..bb57f8c --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/StubValidationDbError.kt @@ -0,0 +1,26 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.worker +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.helpers.fail +import ru.otus.otuskotlin.marketplace.common.models.MkplError +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs + +fun ICorChainDsl.stubDbError(title: String) = worker { + this.title = title + this.description = """ + Кейс ошибки базы данных + """.trimIndent() + on { stubCase == MkplStubs.DB_ERROR && state == MkplState.RUNNING } + handle { + fail( + MkplError( + group = "internal", + code = "internal-db", + message = "Internal error" + ) + ) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/Stubs.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/Stubs.kt new file mode 100644 index 0000000..c49e5a3 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonMain/kotlin/stubs/Stubs.kt @@ -0,0 +1,13 @@ +package ru.otus.otuskotlin.marketplace.biz.stubs + +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.MkplState +import ru.otus.otuskotlin.marketplace.common.models.MkplWorkMode +import ru.otus.otuskotlin.marketplace.cor.ICorChainDsl +import ru.otus.otuskotlin.marketplace.cor.chain + +fun ICorChainDsl.stubs(title: String, block: ICorChainDsl.() -> Unit) = chain { + block() + this.title = title + on { workMode == MkplWorkMode.STUB && state == MkplState.RUNNING } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/package.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/package.kt new file mode 100644 index 0000000..41ea8c3 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/package.kt @@ -0,0 +1,2 @@ +package ru.otus.otuskotlin.marketplace.biz + diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdCreateStubTest.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdCreateStubTest.kt new file mode 100644 index 0000000..3ca2312 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdCreateStubTest.kt @@ -0,0 +1,122 @@ +package ru.otus.otuskotlin.marketplace.biz.stub + +import kotlinx.coroutines.test.runTest +import ru.otus.otuskotlin.marketplace.biz.MkplAdProcessor +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub +import kotlin.test.Test +import kotlin.test.assertEquals + +class AdCreateStubTest { + + private val processor = MkplAdProcessor() + val id = MkplAdId("666") + val title = "title 666" + val description = "desc 666" + val dealSide = MkplDealSide.DEMAND + val visibility = MkplVisibility.VISIBLE_PUBLIC + + @Test + fun create() = runTest { + + val ctx = MkplContext( + command = MkplCommand.CREATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.SUCCESS, + adRequest = MkplAd( + id = id, + title = title, + description = description, + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(MkplAdStub.get().id, ctx.adResponse.id) + assertEquals(title, ctx.adResponse.title) + assertEquals(description, ctx.adResponse.description) + assertEquals(dealSide, ctx.adResponse.adType) + assertEquals(visibility, ctx.adResponse.visibility) + } + + @Test + fun badTitle() = runTest { + val ctx = MkplContext( + command = MkplCommand.CREATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_TITLE, + adRequest = MkplAd( + id = id, + title = "", + description = description, + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("title", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + @Test + fun badDescription() = runTest { + val ctx = MkplContext( + command = MkplCommand.CREATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_DESCRIPTION, + adRequest = MkplAd( + id = id, + title = title, + description = "", + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("description", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + + @Test + fun databaseError() = runTest { + val ctx = MkplContext( + command = MkplCommand.CREATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.DB_ERROR, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("internal", ctx.errors.firstOrNull()?.group) + } + + @Test + fun badNoCase() = runTest { + val ctx = MkplContext( + command = MkplCommand.CREATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_ID, + adRequest = MkplAd( + id = id, + title = title, + description = description, + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("stub", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdDeleteStubTest.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdDeleteStubTest.kt new file mode 100644 index 0000000..08fc82f --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdDeleteStubTest.kt @@ -0,0 +1,85 @@ +package ru.otus.otuskotlin.marketplace.biz.stub + +import kotlinx.coroutines.test.runTest +import ru.otus.otuskotlin.marketplace.biz.MkplAdProcessor +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub +import kotlin.test.Test +import kotlin.test.assertEquals + +class AdDeleteStubTest { + + private val processor = MkplAdProcessor() + val id = MkplAdId("666") + + @Test + fun delete() = runTest { + + val ctx = MkplContext( + command = MkplCommand.DELETE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.SUCCESS, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + + val stub = MkplAdStub.get() + assertEquals(stub.id, ctx.adResponse.id) + assertEquals(stub.title, ctx.adResponse.title) + assertEquals(stub.description, ctx.adResponse.description) + assertEquals(stub.adType, ctx.adResponse.adType) + assertEquals(stub.visibility, ctx.adResponse.visibility) + } + + @Test + fun badId() = runTest { + val ctx = MkplContext( + command = MkplCommand.DELETE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_ID, + adRequest = MkplAd(), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("id", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + + @Test + fun databaseError() = runTest { + val ctx = MkplContext( + command = MkplCommand.DELETE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.DB_ERROR, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("internal", ctx.errors.firstOrNull()?.group) + } + + @Test + fun badNoCase() = runTest { + val ctx = MkplContext( + command = MkplCommand.DELETE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_TITLE, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("stub", ctx.errors.firstOrNull()?.field) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdOffersStubTest.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdOffersStubTest.kt new file mode 100644 index 0000000..ae9d009 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdOffersStubTest.kt @@ -0,0 +1,98 @@ +package ru.otus.otuskotlin.marketplace.biz.stub + +import kotlinx.coroutines.test.runTest +import ru.otus.otuskotlin.marketplace.biz.MkplAdProcessor +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +class AdOffersStubTest { + + private val processor = MkplAdProcessor() + val id = MkplAdId("777") + + @Test + fun offers() = runTest { + + val ctx = MkplContext( + command = MkplCommand.OFFERS, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.SUCCESS, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + + assertEquals(id, ctx.adResponse.id) + + with(MkplAdStub.get()) { + assertEquals(title, ctx.adResponse.title) + assertEquals(description, ctx.adResponse.description) + assertEquals(adType, ctx.adResponse.adType) + assertEquals(visibility, ctx.adResponse.visibility) + } + + assertTrue(ctx.adsResponse.size > 1) + val first = ctx.adsResponse.firstOrNull() ?: fail("Empty response list") + assertTrue(first.title.contains(ctx.adResponse.title)) + assertTrue(first.description.contains(ctx.adResponse.title)) + assertEquals(MkplDealSide.SUPPLY, first.adType) + assertEquals(MkplAdStub.get().visibility, first.visibility) + } + + @Test + fun badId() = runTest { + val ctx = MkplContext( + command = MkplCommand.OFFERS, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_ID, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("id", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + + @Test + fun databaseError() = runTest { + val ctx = MkplContext( + command = MkplCommand.OFFERS, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.DB_ERROR, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("internal", ctx.errors.firstOrNull()?.group) + } + + @Test + fun badNoCase() = runTest { + val ctx = MkplContext( + command = MkplCommand.OFFERS, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_TITLE, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("stub", ctx.errors.firstOrNull()?.field) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdReadStubTest.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdReadStubTest.kt new file mode 100644 index 0000000..c58ede7 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdReadStubTest.kt @@ -0,0 +1,85 @@ +package ru.otus.otuskotlin.marketplace.biz.stub + +import kotlinx.coroutines.test.runTest +import ru.otus.otuskotlin.marketplace.biz.MkplAdProcessor +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub +import kotlin.test.Test +import kotlin.test.assertEquals + +class AdReadStubTest { + + private val processor = MkplAdProcessor() + val id = MkplAdId("666") + + @Test + fun read() = runTest { + + val ctx = MkplContext( + command = MkplCommand.READ, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.SUCCESS, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + with (MkplAdStub.get()) { + assertEquals(id, ctx.adResponse.id) + assertEquals(title, ctx.adResponse.title) + assertEquals(description, ctx.adResponse.description) + assertEquals(adType, ctx.adResponse.adType) + assertEquals(visibility, ctx.adResponse.visibility) + } + } + + @Test + fun badId() = runTest { + val ctx = MkplContext( + command = MkplCommand.READ, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_ID, + adRequest = MkplAd(), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("id", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + + @Test + fun databaseError() = runTest { + val ctx = MkplContext( + command = MkplCommand.READ, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.DB_ERROR, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("internal", ctx.errors.firstOrNull()?.group) + } + + @Test + fun badNoCase() = runTest { + val ctx = MkplContext( + command = MkplCommand.READ, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_TITLE, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("stub", ctx.errors.firstOrNull()?.field) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdSearchStubTest.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdSearchStubTest.kt new file mode 100644 index 0000000..e7171fa --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdSearchStubTest.kt @@ -0,0 +1,82 @@ +package ru.otus.otuskotlin.marketplace.biz.stub + +import kotlinx.coroutines.test.runTest +import ru.otus.otuskotlin.marketplace.biz.MkplAdProcessor +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import ru.otus.otuskotlin.marketplace.stubs.MkplAdStub +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.test.fail + +class AdSearchStubTest { + + private val processor = MkplAdProcessor() + val filter = MkplAdFilter(searchString = "bolt") + + @Test + fun read() = runTest { + + val ctx = MkplContext( + command = MkplCommand.SEARCH, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.SUCCESS, + adFilterRequest = filter, + ) + processor.exec(ctx) + assertTrue(ctx.adsResponse.size > 1) + val first = ctx.adsResponse.firstOrNull() ?: fail("Empty response list") + assertTrue(first.title.contains(filter.searchString)) + assertTrue(first.description.contains(filter.searchString)) + with (MkplAdStub.get()) { + assertEquals(adType, first.adType) + assertEquals(visibility, first.visibility) + } + } + + @Test + fun badId() = runTest { + val ctx = MkplContext( + command = MkplCommand.SEARCH, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_ID, + adFilterRequest = filter, + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("id", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + + @Test + fun databaseError() = runTest { + val ctx = MkplContext( + command = MkplCommand.SEARCH, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.DB_ERROR, + adFilterRequest = filter, + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("internal", ctx.errors.firstOrNull()?.group) + } + + @Test + fun badNoCase() = runTest { + val ctx = MkplContext( + command = MkplCommand.SEARCH, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_TITLE, + adFilterRequest = filter, + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("stub", ctx.errors.firstOrNull()?.field) + } +} diff --git a/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdUpdateStubTest.kt b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdUpdateStubTest.kt new file mode 100644 index 0000000..2f4f900 --- /dev/null +++ b/ok-marketplace-be/ok-marketplace-biz/src/commonTest/kotlin/stub/AdUpdateStubTest.kt @@ -0,0 +1,136 @@ +package ru.otus.otuskotlin.marketplace.biz.stub + +import kotlinx.coroutines.test.runTest +import ru.otus.otuskotlin.marketplace.biz.MkplAdProcessor +import ru.otus.otuskotlin.marketplace.common.MkplContext +import ru.otus.otuskotlin.marketplace.common.models.* +import ru.otus.otuskotlin.marketplace.common.stubs.MkplStubs +import kotlin.test.Test +import kotlin.test.assertEquals + +class AdUpdateStubTest { + + private val processor = MkplAdProcessor() + val id = MkplAdId("777") + val title = "title 666" + val description = "desc 666" + val dealSide = MkplDealSide.DEMAND + val visibility = MkplVisibility.VISIBLE_PUBLIC + + @Test + fun create() = runTest { + + val ctx = MkplContext( + command = MkplCommand.UPDATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.SUCCESS, + adRequest = MkplAd( + id = id, + title = title, + description = description, + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(id, ctx.adResponse.id) + assertEquals(title, ctx.adResponse.title) + assertEquals(description, ctx.adResponse.description) + assertEquals(dealSide, ctx.adResponse.adType) + assertEquals(visibility, ctx.adResponse.visibility) + } + + @Test + fun badId() = runTest { + val ctx = MkplContext( + command = MkplCommand.UPDATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_ID, + adRequest = MkplAd(), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("id", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + + @Test + fun badTitle() = runTest { + val ctx = MkplContext( + command = MkplCommand.UPDATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_TITLE, + adRequest = MkplAd( + id = id, + title = "", + description = description, + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("title", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + @Test + fun badDescription() = runTest { + val ctx = MkplContext( + command = MkplCommand.UPDATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_DESCRIPTION, + adRequest = MkplAd( + id = id, + title = title, + description = "", + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("description", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } + + @Test + fun databaseError() = runTest { + val ctx = MkplContext( + command = MkplCommand.UPDATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.DB_ERROR, + adRequest = MkplAd( + id = id, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("internal", ctx.errors.firstOrNull()?.group) + } + + @Test + fun badNoCase() = runTest { + val ctx = MkplContext( + command = MkplCommand.UPDATE, + state = MkplState.NONE, + workMode = MkplWorkMode.STUB, + stubCase = MkplStubs.BAD_SEARCH_STRING, + adRequest = MkplAd( + id = id, + title = title, + description = description, + adType = dealSide, + visibility = visibility, + ), + ) + processor.exec(ctx) + assertEquals(MkplAd(), ctx.adResponse) + assertEquals("stub", ctx.errors.firstOrNull()?.field) + assertEquals("validation", ctx.errors.firstOrNull()?.group) + } +} diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt index 55f1a11..7ab6829 100644 --- a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/MkplContext.kt @@ -10,6 +10,7 @@ data class MkplContext( var state: MkplState = MkplState.NONE, val errors: MutableList = mutableListOf(), + var corSettings: MkplCorSettings = MkplCorSettings(), var workMode: MkplWorkMode = MkplWorkMode.PROD, var stubCase: MkplStubs = MkplStubs.NONE, var wsSession: IMkplWsSession = IMkplWsSession.NONE, diff --git a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/helpers/MkplErrorsHelpers.kt b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/helpers/MkplErrorsHelpers.kt index a42b867..f0e1e84 100644 --- a/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/helpers/MkplErrorsHelpers.kt +++ b/ok-marketplace-be/ok-marketplace-common/src/commonMain/kotlin/helpers/MkplErrorsHelpers.kt @@ -1,6 +1,8 @@ package ru.otus.otuskotlin.marketplace.common.helpers +import ru.otus.otuskotlin.marketplace.common.MkplContext import ru.otus.otuskotlin.marketplace.common.models.MkplError +import ru.otus.otuskotlin.marketplace.common.models.MkplState fun Throwable.asMkplError( code: String = "unknown", @@ -13,3 +15,10 @@ fun Throwable.asMkplError( message = message, exception = this, ) + +inline fun MkplContext.addError(vararg error: MkplError) = errors.addAll(error) + +inline fun MkplContext.fail(error: MkplError) { + addError(error) + state = MkplState.FAILING +}