routing casbah salat spray-json testkit

Cómo probar objetos Custom Json con Spray Routing



casbah salat (2)

Como nadie parece saber, cambié el _id de ser un ObjectId () a solo una cadena, y tengo un método de ayuda para crearlo desde el nuevo ObjectId (). ToString donde sea necesario

Estoy creando una API de reposo con enrutamiento por aspersión sobre mongodb para algunas operaciones CRUD, todo funciona bien, espero cada vez que intente probarlo con specs2 la siguiente especificación

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 }

fallará con el siguiente error:

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

No tengo idea de dónde buscar, ya que la referencia al archivo fuente y al número de línea apuntan a un método genérico failTest (msg: String)

algo más de información:

Tengo una clase de caso que persisto a Mongo usando SalatDAO

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

donde ObjectId () una clase es la que envuelve la generación de ID de mongodb para que esto (no) se organice a través de spray_json Creé algunos 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")

y la parte relevante de mi ruta (se eliminó el manejo y registro de errores):

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


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") } }