Howto test Custom Json Objects with Spray Routing

2019-07-01 11:02发布

I'm creating a Rest API with spray-routing on top of mongodb for some CRUD operations, this all works fine, expect whenever I try to test it with specs2 the following specification

class RestServiceSpec extends Specification with Specs2RouteTest with RoutingRestService

  // database initialization removed for clarity

  "The rest service" should
    "have a player called 'Theo TestPlayer' in the db" in {
      Get("/api/1.0/player/" + player1._id) ~> restRoute ~> check {
        entityAs[Player] must be equalTo(player1)
      }
    }
  }

// some more specs removed for clarity
}

it will fail with the following error:

MalformedContent(invalid ObjectId ["51308c134820cf957c4c51ca"],Some(java.lang.IllegalArgumentException: invalid ObjectId ["51308c134820cf957c4c51ca"])) (Specs2Interface.scala:25)

I have no idea where to look as the reference to the source file and line number point to a generic failTest(msg:String) method

some more info:

I have a case class that I persist to Mongo using SalatDAO

case class Player(@Key("_id") _id:ObjectId = new ObjectId(), name:String, email:String, age:Int) {}

where ObjectId() a class is that wraps mongodb's ID generation to get this (un)marshalled through spray_json I created some jsonFormats

object MyJsonProtocol {
  implicit val objectIdFormat = new JsonFormat[ObjectId] {
    def write(o:ObjectId) = JsString(o.toString)
    def read(value:JsValue) = new ObjectId(value.toString())
  }
  implicit val PlayerFormat = jsonFormat(Player, "_id", "name", "email", "age")

and the relevant part of my route (removed error handling and logging):

  path("player" / "\\w+".r) {id:String =>
    get {
      respondWithMediaType(`application/json`) {
        complete {
          PlayerCRUD.getById(id) 
        }
      }
    } ~

2条回答
Evening l夕情丶
2楼-- · 2019-07-01 11:13
 implicit object ObjectIdJsonFormat extends JsonFormat[ObjectId] {
    def write(obj: ObjectId): JsValue = JsString(obj.toString)

    def read(json: JsValue): ObjectId = json match {
      case JsString(str) => new ObjectId(str)
      case _ => throw new DeserializationException(" string expected")
    }
  }
查看更多
放荡不羁爱自由
3楼-- · 2019-07-01 11:19

As nobody seems to know, I changed the _id from being a ObjectId() to just a string, and having a helpermethod to create it from new ObjectId().toString where needed

查看更多
登录 后发表回答