Skip to content

Commit

Permalink
M6l2 biz (#25)
Browse files Browse the repository at this point in the history
* m6l1 - Prepare

* m6l1 - Chain of Responsibility

* M6l2 biz (#26)

* m6l1 - Chain of Responsibility

(cherry picked from commit 80c19ed85f4415940a9c475344925c421cf17992)

* m6l2 remove unknown failing test

---------

Co-authored-by: Sergey Okatov <[email protected]>
Co-authored-by: Sergey Okatov <[email protected]>
  • Loading branch information
3 people authored Jun 18, 2024
1 parent b2e81fb commit 9be9ab5
Show file tree
Hide file tree
Showing 31 changed files with 1,066 additions and 116 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class V2AdStubApiTest {
) { response ->
val responseObj = response.body<AdOffersResponse>()
assertEquals(200, response.status.value)
assertEquals("d-666-01", responseObj.ads?.first()?.id)
assertEquals("s-666-01", responseObj.ads?.first()?.id)
}

private inline fun <reified T: IRequest> v2TestApplication(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class V1AdStubApiTest {
) { response ->
val responseObj = response.body<AdOffersResponse>()
assertEquals(200, response.status.value)
assertEquals("d-666-01", responseObj.ads?.first()?.id)
assertEquals("s-666-01", responseObj.ads?.first()?.id)
}

private fun v1TestApplication(
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions ok-marketplace-be/ok-marketplace-biz/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ kotlin {
dependencies {
implementation(kotlin("stdlib-common"))

implementation(libs.cor)

implementation(project(":ok-marketplace-common"))
implementation(project(":ok-marketplace-stubs"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MkplContext> {
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()
}
Original file line number Diff line number Diff line change
@@ -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<MkplContext>.initStatus(title: String) = worker() {
this.title = title
this.description = """
Этот обработчик устанавливает стартовый статус обработки. Запускается только в случае не заданного статуса.
""".trimIndent()
on { state == MkplState.NONE }
handle { state = MkplState.RUNNING }
}
Original file line number Diff line number Diff line change
@@ -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<MkplContext>.operation(
title: String,
command: MkplCommand,
block: ICorChainDsl<MkplContext>.() -> Unit
) = chain {
block()
this.title = title
on { this.command == command && state == MkplState.RUNNING }
}
Original file line number Diff line number Diff line change
@@ -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<MkplContext>.stubs(title: String, block: ICorChainDsl<MkplContext>.() -> Unit) = chain {
block()
this.title = title
on { workMode == MkplWorkMode.STUB && state == MkplState.RUNNING }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<MkplContext>.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
}
}
}
Original file line number Diff line number Diff line change
@@ -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<MkplContext>.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
}
}
}
Original file line number Diff line number Diff line change
@@ -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<MkplContext>.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}"
)
)
}
}
Original file line number Diff line number Diff line change
@@ -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<MkplContext>.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))
}
}
}
Loading

0 comments on commit 9be9ab5

Please sign in to comment.