Skip to content

Commit

Permalink
Smart templating in scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Mar 15, 2023
1 parent 62faee5 commit 81e1328
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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 {
Expand All @@ -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)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 81e1328

Please sign in to comment.