Guest User

Untitled

a guest
Jul 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. package com.agemooij.test
  2.  
  3. import java.util.Date
  4.  
  5. import org.junit.runner.RunWith
  6. import org.scalatest.junit.JUnitRunner
  7. import org.scalatest.FlatSpec
  8. import org.scalatest.matchers.ShouldMatchers
  9.  
  10. import com.mongodb.casbah.Imports._
  11. import com.bumnetworks.salat._
  12. import com.bumnetworks.salat.global._
  13.  
  14. case class Tweet (
  15. val id: Long,
  16. val user: String,
  17. val mentions: List[String] = Nil
  18. )
  19.  
  20. case class Item (
  21. val src: String,
  22. val subtype: String,
  23. val createdAt: Date,
  24. val addedAt: Date,
  25. val text: String,
  26. val links: List[String] = Nil,
  27. val tags: List[String] = Nil,
  28. val users: List[String],
  29. val tweet: Option[Tweet] = None,
  30. val nrOfUsers: Int
  31. )
  32.  
  33. @RunWith(classOf[JUnitRunner])
  34. class ItemRepositoryTest extends FlatSpec with ShouldMatchers {
  35.  
  36. "Serialization of a nested case class instance with Salat" should "work as expected" in {
  37. object ItemSerializer extends Grater(classOf[Item])
  38.  
  39. val tweet = Tweet(id = 123456789L, user = "agemooij")
  40. val item = Item (
  41. src = "Twitter",
  42. subtype = "favorite",
  43. createdAt = new Date(),
  44. addedAt = new Date(),
  45. text = "the text",
  46. links = Nil,
  47. tags = List("tag1", "tag2", "tag3", "tag4"),
  48. users = List("agemooij", "snidag"),
  49. tweet = Some(tweet),
  50. nrOfUsers = 2
  51. )
  52.  
  53. val itemAsMongo: MongoDBObject = ItemSerializer.asDBObject(item)
  54.  
  55. println("Item as mongo:")
  56. println(itemAsMongo.asDBObject)
  57. println("Users list:")
  58. println(itemAsMongo.get("users"))
  59.  
  60. assert(itemAsMongo.as[String]("src") === "Twitter")
  61.  
  62. val itemDeserialized: Item = ItemSerializer.asObject(itemAsMongo)
  63.  
  64. assert(item === itemDeserialized)
  65. assert(itemDeserialized.tweet === Some(tweet))
  66.  
  67. itemDeserialized.tweet match {
  68. case Some(tweeeet) => {
  69. println("Tweeeet: " + tweet)
  70.  
  71. assert(tweeeet === tweet)
  72. assert(tweeeet.id === 123456789L)
  73. assert(tweeeet.user === "agemooij")
  74. assert(tweeeet.mentions === Nil)
  75. }
  76. case _ => throw new IllegalStateException("The deserialized item did not contain a deserialized tweet.")
  77. }
  78. }
  79. }
  80.  
  81. /*
  82. Resulting output:
  83.  
  84. {
  85. "_typeHint" : "com.agemooij.test.Item" ,
  86. "src" : "Twitter" ,
  87. "subtype" : "favorite" ,
  88. "createdAt" : { "$date" : "2011-01-06T17:13:52Z"} ,
  89. "addedAt" : { "$date" : "2011-01-06T17:13:52Z"} ,
  90. "text" : "the text" ,
  91. "links" : [ ] ,
  92. "tags" : [ "tag1" , "tag2" , "tag3" , "tag4"] ,
  93. "users" : [ "agemooij" , "snidag"] ,
  94. "tweet" : [ 123456789 , "agemooij" , [ ]] , <=== huh ??
  95. "nrOfUsers" : 2
  96. }
  97. */
Add Comment
Please, Sign In to add comment