diff --git a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/ScenarioOutput.scala b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/ScenarioOutput.scala index b527156a..8931b042 100644 --- a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/ScenarioOutput.scala +++ b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/ScenarioOutput.scala @@ -6,6 +6,8 @@ import com.github.dwickern.macros.NameOf.* import derevo.circe.decoder import derevo.circe.encoder import derevo.derive +import io.circe.Decoder +import io.circe.Encoder import io.circe.Json import sttp.tapir.derevo.schema import sttp.tapir.generic.Configuration as TapirConfig @@ -16,6 +18,8 @@ import ru.tinkoff.tcb.bson.derivation.bsonEncoder import ru.tinkoff.tcb.circe.bson.* import ru.tinkoff.tcb.protocol.json.* import ru.tinkoff.tcb.protocol.schema.* +import ru.tinkoff.tcb.utils.transformation.json.JsonTransformations +import ru.tinkoff.tcb.utils.transformation.xml.XmlTransformation import ru.tinkoff.tcb.utils.xml.XMLString @derive( @@ -28,6 +32,7 @@ import ru.tinkoff.tcb.utils.xml.XMLString @BsonDiscriminator("mode") sealed trait ScenarioOutput { def delay: Option[FiniteDuration] + def isTemplate: Boolean } object ScenarioOutput { @@ -45,16 +50,44 @@ object ScenarioOutput { final case class RawOutput( payload: String, delay: Option[FiniteDuration] -) extends ScenarioOutput +) extends ScenarioOutput { + val isTemplate = false +} -@derive(decoder, encoder) final case class JsonOutput( payload: Json, - delay: Option[FiniteDuration] + delay: Option[FiniteDuration], + isTemplate: Boolean = true ) extends ScenarioOutput -@derive(decoder, encoder) +object JsonOutput { + implicit val joEncoder: Encoder.AsObject[JsonOutput] = + Encoder.forProduct2( + nameOf[JsonOutput](_.payload), + nameOf[JsonOutput](_.delay) + )(jo => (jo.payload, jo.delay)) + + implicit val joDecoder: Decoder[JsonOutput] = + Decoder.forProduct2(nameOf[JsonOutput](_.payload), nameOf[JsonOutput](_.delay))((pl, dl) => + JsonOutput(pl, dl, pl.isTemplate) + ) +} + final case class XmlOutput( payload: XMLString, - delay: Option[FiniteDuration] + delay: Option[FiniteDuration], + isTemplate: Boolean = true ) extends ScenarioOutput + +object XmlOutput { + implicit val xoEncoder: Encoder.AsObject[XmlOutput] = + Encoder.forProduct2( + nameOf[XmlOutput](_.payload), + nameOf[XmlOutput](_.delay) + )(jo => (jo.payload, jo.delay)) + + implicit val xoDecoder: Decoder[XmlOutput] = + Decoder.forProduct2(nameOf[XmlOutput](_.payload), nameOf[XmlOutput](_.delay))((pl, dl) => + XmlOutput(pl, dl, pl.toNode.isTemplate) + ) +} diff --git a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/scenario/ScenarioEngine.scala b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/scenario/ScenarioEngine.scala index ea7d454c..4eb69f9b 100644 --- a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/scenario/ScenarioEngine.scala +++ b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/scenario/ScenarioEngine.scala @@ -173,16 +173,21 @@ final class ScenarioEngine( dest.request.body.fold { rt.body( out match { - case RawOutput(payload, _) => payload - case JsonOutput(payload, _) => payload.substitute(data).substitute(xdata).noSpaces - case XmlOutput(payload, _) => payload.toNode.substitute(data).substitute(xdata).mkString + case RawOutput(payload, _) => payload + case JsonOutput(payload, _, isT) => + if (isT) payload.substitute(data).substitute(xdata).noSpaces else payload.noSpaces + case XmlOutput(payload, _, isT) => + if (isT) payload.toNode.substitute(data).substitute(xdata).mkString else payload.asString } ) } { drb => val bodyJson = out match { - case RawOutput(payload, _) => Json.fromString(payload) - case JsonOutput(payload, _) => payload.substitute(data).substitute(xdata) - case XmlOutput(payload, _) => Json.fromString(payload.toNode.substitute(data).substitute(xdata).mkString) + case RawOutput(payload, _) => Json.fromString(payload) + case JsonOutput(payload, _, isT) => if (isT) payload.substitute(data).substitute(xdata) else payload + case XmlOutput(payload, _, isT) => + if (isT) + Json.fromString(payload.toNode.substitute(data).substitute(xdata).mkString) + else Json.fromString(payload.asString) } rt.body(