Skip to content

Commit

Permalink
Rename XmlPredicate2 to XmlPredicate
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Jan 3, 2025
1 parent febb9fb commit 4f60c2b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ru.tinkoff.tcb.circe.bson.*
import ru.tinkoff.tcb.predicatedsl.Keyword
import ru.tinkoff.tcb.predicatedsl.form.FormPredicate
import ru.tinkoff.tcb.predicatedsl.json.JsonPredicate
import ru.tinkoff.tcb.predicatedsl.xml.XmlPredicate2
import ru.tinkoff.tcb.predicatedsl.xml.XmlPredicate
import ru.tinkoff.tcb.protocol.bson.*
import ru.tinkoff.tcb.protocol.json.*
import ru.tinkoff.tcb.protocol.schema.*
Expand Down Expand Up @@ -160,11 +160,11 @@ final case class JLensRequest(

@derive(decoder, encoder)
final case class XPathRequest(
headers: Map[String, String],
query: Map[JsonOptic, Map[Keyword.Json, Json]] = Map.empty,
body: XmlPredicate2,
extractors: Map[String, XmlExtractor] = Map.empty,
inlineCData: Boolean = false
headers: Map[String, String],
query: Map[JsonOptic, Map[Keyword.Json, Json]] = Map.empty,
body: XmlPredicate,
extractors: Map[String, XmlExtractor] = Map.empty,
inlineCData: Boolean = false
) extends HttpStubRequest {
override def checkBody(rBody: RequestBody): Boolean =
extractXML(rBody).map(body).getOrElse(false)
Expand Down Expand Up @@ -333,7 +333,7 @@ final case class JLensPart(headers: Map[String, String], body: JsonPredicate) ex
}

@derive(decoder, encoder)
final case class XPathPart(headers: Map[String, String], body: XmlPredicate2) extends RequestPart {
final case class XPathPart(headers: Map[String, String], body: XmlPredicate) extends RequestPart {
override def checkBody(value: String): Boolean = Try(SafeXML.loadString(value)).map(body).getOrElse(false)

override def extractJson(body: String): Option[Json] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ru.tinkoff.tcb.bson.derivation.bsonDecoder
import ru.tinkoff.tcb.bson.derivation.bsonEncoder
import ru.tinkoff.tcb.circe.bson.*
import ru.tinkoff.tcb.predicatedsl.json.JsonPredicate
import ru.tinkoff.tcb.predicatedsl.xml.XmlPredicate2
import ru.tinkoff.tcb.predicatedsl.xml.XmlPredicate
import ru.tinkoff.tcb.protocol.schema.*
import ru.tinkoff.tcb.utils.xml.SafeXML
import ru.tinkoff.tcb.utils.xml.XMLString
Expand Down Expand Up @@ -67,7 +67,7 @@ final case class JLensResponseSpec(code: Option[Int], body: Option[JsonPredicate
}

@derive(decoder, encoder)
final case class XPathResponseSpec(code: Option[Int], body: Option[XmlPredicate2]) extends ResponseSpec {
final case class XPathResponseSpec(code: Option[Int], body: Option[XmlPredicate]) extends ResponseSpec {
override def checkBody(data: String): Boolean =
Try(SafeXML.loadString(data)).exists(nx => body.forall(_(nx)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ru.tinkoff.tcb.bson.derivation.bsonDecoder
import ru.tinkoff.tcb.bson.derivation.bsonEncoder
import ru.tinkoff.tcb.circe.bson.*
import ru.tinkoff.tcb.predicatedsl.json.JsonPredicate
import ru.tinkoff.tcb.predicatedsl.xml.XmlPredicate2
import ru.tinkoff.tcb.predicatedsl.xml.XmlPredicate
import ru.tinkoff.tcb.protocol.schema.*
import ru.tinkoff.tcb.utils.xml.SafeXML
import ru.tinkoff.tcb.utils.xml.XMLString
Expand Down Expand Up @@ -94,7 +94,7 @@ final case class JLensInput(payload: JsonPredicate) extends ScenarioInput {
}

@derive(decoder, encoder)
final case class XPathInput(payload: XmlPredicate2) extends ScenarioInput {
final case class XPathInput(payload: XmlPredicate) extends ScenarioInput {
override def checkMessage(message: String): Boolean =
extractXML(message).exists(payload(_))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,39 @@ import ru.tinkoff.tcb.utils.circe.optics.JsonOptic
import ru.tinkoff.tcb.utils.json.JObject
import ru.tinkoff.tcb.xpath.SXpath

abstract class XmlPredicate2 extends (NodeSeq => Boolean) {
def definition: XmlPredicate2.Spec
abstract class XmlPredicate extends (NodeSeq => Boolean) {
def definition: XmlPredicate.Spec

override def hashCode(): Int = definition.hashCode()

override def equals(obj: Any): Boolean = obj match {
case xp: XmlPredicate2 => xp.definition == definition
case xp: XmlPredicate => xp.definition == definition
case _ => false
}
}

object XmlPredicate2 {
object XmlPredicate {
type Spec = Map[SXpath, Map[Keyword.Xml, Json]]

implicit val xmlPredicateDecoder: Decoder[XmlPredicate2] =
implicit val xmlPredicateDecoder: Decoder[XmlPredicate] =
Decoder[Spec].emap(apply(_).toEither.leftMap(_.toList.mkString(", ")))

implicit val xmlPredicateEncoder: Encoder[XmlPredicate2] =
implicit val xmlPredicateEncoder: Encoder[XmlPredicate] =
Encoder[Spec].contramap(_.definition)

implicit val xmlPredicateSchema: Schema[XmlPredicate2] =
implicitly[Schema[Spec]].as[XmlPredicate2]
implicit val xmlPredicateSchema: Schema[XmlPredicate] =
implicitly[Schema[Spec]].as[XmlPredicate]

implicit val xmlPredicateBsonDecoder: BsonDecoder[XmlPredicate2] =
implicit val xmlPredicateBsonDecoder: BsonDecoder[XmlPredicate] =
BsonDecoder[Spec].afterReadTry(
apply(_).toEither.leftMap(errs => new BsonInvalidOperationException(errs.toList.mkString(", "))).toTry
)

implicit val xmlPredicateBsonEncoder: BsonEncoder[XmlPredicate2] =
implicit val xmlPredicateBsonEncoder: BsonEncoder[XmlPredicate] =
BsonEncoder[Spec].beforeWrite(_.definition)

implicit val xmlPredicateRootOptionFields: RootOptionFields[XmlPredicate2] =
RootOptionFields.mk[XmlPredicate2](RootOptionFields[Spec].fields, RootOptionFields[Spec].isOptionItself)
implicit val xmlPredicateRootOptionFields: RootOptionFields[XmlPredicate] =
RootOptionFields.mk[XmlPredicate](RootOptionFields[Spec].fields, RootOptionFields[Spec].isOptionItself)

/**
* @param description
Expand All @@ -70,7 +70,7 @@ object XmlPredicate2 {
*/
def apply(
description: Spec
): ValidatedNel[PredicateConstructionError, XmlPredicate2] =
): ValidatedNel[PredicateConstructionError, XmlPredicate] =
description.toVector
.map { case (xPath, spec) =>
spec.toVector
Expand All @@ -83,7 +83,7 @@ object XmlPredicate2 {
.reduceOption(_ |+| _)
.getOrElse(Validated.valid((_: NodeSeq) => true))
.map(f =>
new XmlPredicate2 {
new XmlPredicate {
override val definition: Map[SXpath, Map[Keyword.Xml, Json]] = description

override def apply(xml: NodeSeq): Boolean = f(xml)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import ru.tinkoff.tcb.predicatedsl.Keyword
import ru.tinkoff.tcb.predicatedsl.SpecificationError
import ru.tinkoff.tcb.xpath.*

class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
class XmlPredicateSpec extends AnyFunSuite with Matchers with EitherValues {
test("XmlPredicate should produce validator from correct specification") {
val spec = Json.obj(
"/tag1" := Json.obj("==" := "test")
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toEither
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toEither

sut shouldBe Symbol("right")
}
Expand All @@ -27,7 +27,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/tag1" := Json.obj(">=" := "test")
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toEither
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toEither

sut shouldBe Left(
NonEmptyList.one(
Expand All @@ -42,7 +42,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
// "/root/tag2" := Json.obj("==" := 42)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><root><tag1>test</tag1><tag2>42</tag2></root></wrapper>)) shouldBe Some(true)
sut.map(_(<wrapper><root><tag1>peka</tag1><tag2>42</tag2></root></wrapper>)) shouldBe Some(false)
Expand All @@ -54,7 +54,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/root/tag2" := Json.obj("!=" := 42)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><root><tag1>test</tag1><tag2>42</tag2></root></wrapper>)) shouldBe Some(false)
sut.map(_(<wrapper><root><tag1>peka</tag1><tag2>99</tag2></root></wrapper>)) shouldBe Some(true)
Expand All @@ -65,7 +65,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj(">" := 42)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>43</f></wrapper>)) shouldBe Some(true)
sut.map(_(<wrapper><f>42</f></wrapper>)) shouldBe Some(false)
Expand All @@ -76,7 +76,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj(">=" := 42)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>43</f></wrapper>)) shouldBe Some(true)
sut.map(_(<wrapper><f>42</f></wrapper>)) shouldBe Some(true)
Expand All @@ -88,7 +88,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj("<" := 42)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>42</f></wrapper>)) shouldBe Some(false)
sut.map(_(<wrapper><f>41</f></wrapper>)) shouldBe Some(true)
Expand All @@ -99,7 +99,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj("<=" := 42)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>43</f></wrapper>)) shouldBe Some(false)
sut.map(_(<wrapper><f>42</f></wrapper>)) shouldBe Some(true)
Expand All @@ -111,7 +111,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj(">" := 40, "<=" := 45, "!=" := 43)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>39</f></wrapper>)) shouldBe Some(false)
sut.map(_(<wrapper><f>40</f></wrapper>)) shouldBe Some(false)
Expand All @@ -128,7 +128,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"f" := Json.obj("~=" := "\\d{4,}")
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>123</f></wrapper>)) shouldBe Some(false)
sut.map(_(<wrapper><f>1234</f></wrapper>)) shouldBe Some(true)
Expand All @@ -141,7 +141,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj("size" := 4)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>123</f></wrapper>)) shouldBe Some(false)
sut.map(_(<wrapper><f>1234</f></wrapper>)) shouldBe Some(true)
Expand All @@ -153,7 +153,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj("exists" := true)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>123</f></wrapper>)) shouldBe Some(true)
sut.map(_(<wrapper><f/></wrapper>)) shouldBe Some(true)
Expand All @@ -165,7 +165,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
"/f" := Json.obj("exists" := false)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><f>123</f></wrapper>)) shouldBe Some(false)
sut.map(_(<wrapper><f/></wrapper>)) shouldBe Some(false)
Expand All @@ -181,7 +181,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><data><![CDATA[test]]></data></wrapper>)) shouldBe Some(true)
sut.map(_(<wrapper><data><![CDATA[test]]> </data></wrapper>)) shouldBe Some(false)
Expand All @@ -197,7 +197,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper><data><![CDATA[1234]]></data></wrapper>)) shouldBe Some(true)
sut.map(_(<wrapper><data><![CDATA[123f]]></data></wrapper>)) shouldBe Some(false)
Expand All @@ -215,7 +215,7 @@ class XmlPredicateSpec2 extends AnyFunSuite with Matchers with EitherValues {
)
)

val sut = XmlPredicate2(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption
val sut = XmlPredicate(spec.as[Map[SXpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(<wrapper>
<json>
Expand Down

0 comments on commit 4f60c2b

Please sign in to comment.