PageRenderTime 32ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/syndor-feedbot/src/test/scala/syndor/feedbot/FeedUpdaterSpec.scala

http://github.com/HendraWijaya/syndor
Scala | 139 lines | 99 code | 33 blank | 7 comment | 0 complexity | 580695d41a96c54a200a33e1110d5e15 MD5 | raw file
  1. package syndor.feedbot
  2. import org.bson.types.ObjectId
  3. import org.scalatest.matchers.ShouldMatchers
  4. import org.scalatest.BeforeAndAfterAll
  5. import org.scalatest.FlatSpec
  6. import syndor.EnvironmentSupport._
  7. import syndor.model.Feed
  8. import syndor.model.FeedItem
  9. import syndor.model.Source
  10. import syndor.model.FetchStatus
  11. import syndor.TestHttpServer
  12. import akka.actor.Actor.actorOf
  13. import com.novus.salat.dao._
  14. import com.mongodb.casbah.Imports._
  15. import org.scalatest.BeforeAndAfterEach
  16. import java.text.SimpleDateFormat
  17. class FeedUpdaterSpec extends FlatSpec
  18. with ShouldMatchers
  19. with BeforeAndAfterAll
  20. with TestFeedSupport
  21. with TestHttpServerSupport {
  22. private val updater = actorOf[FeedUpdater]
  23. override def beforeAll() {
  24. updater.start()
  25. super.beforeAll()
  26. }
  27. override def afterAll() {
  28. try {
  29. super.afterAll()
  30. } finally {
  31. updater.stop()
  32. }
  33. }
  34. behavior of "feed updater"
  35. it should "read feed items" in {
  36. Feed.collection.count should be(0)
  37. val feed = insertFetchingFeed(httpServer.resolve("/resource/reuters_2011_10_23.xml").toExternalForm())
  38. Feed.collection.count should be(1)
  39. FeedItem.collection.count should be(0)
  40. (updater ? FeedUpdater.UpdateRequest(feed)).await.get
  41. // Sleep as long as timeout to make sure it has finished
  42. //Thread.sleep(FeedBotConfig.fetcherTimeoutInMillis + 10)
  43. FeedItem.collection.count should be(10)
  44. verifyFeedItems(FeedItem.find(MongoDBObject()).sort(orderBy = MongoDBObject("publishedDate" -> -1)))
  45. Feed.findOneByID(feed.id) map { updatedFeed =>
  46. updatedFeed.fetchStatus.fetching should be(false)
  47. updatedFeed.fetchStatus.success should be(true)
  48. updatedFeed.fetchStatus.message should be(None)
  49. updatedFeed.fetchStatus.fetchStart should be (feed.fetchStatus.fetchStart)
  50. updatedFeed.fetchStatus.fetchEnd should be > feed.fetchStatus.fetchEnd
  51. updatedFeed.fetchStatus.fetchStart should be < updatedFeed.fetchStatus.fetchEnd
  52. } orElse {
  53. fail("Unable to find updated feed with id %s".format(feed.id))
  54. }
  55. def verifyFeedItems(cursor: SalatMongoCursor[FeedItem]) {
  56. cursor.next should have(
  57. 'title("Clues to Gaddafi's death concealed from public view"),
  58. 'uri("http://www.reuters.com/article/2011/10/23/us-libya-gaddafi-finalhours-idUSTRE79M02W20111023?feedType=RSS&feedName=topNews"),
  59. 'link("http://feeds.reuters.com/~r/reuters/topNews/~3/zjpIRmrOdG8/us-libya-gaddafi-finalhours-idUSTRE79M02W20111023"))
  60. }
  61. }
  62. it should "ignore items that are already fetched" in {
  63. val feed1 = insertFetchingFeed(httpServer.resolve("/resource/reuters_2011_10_23.xml").toExternalForm())
  64. val feed2 = insertFetchingFeed(httpServer.resolve("/resource/reuters_2011_10_23.xml").toExternalForm())
  65. Feed.collection.count should be(2)
  66. FeedItem.collection.count should be(0)
  67. (updater ? FeedUpdater.UpdateRequest(feed1)).as[FeedUpdater.UpdateResponse].get
  68. (updater ? FeedUpdater.UpdateRequest(feed2)).as[FeedUpdater.UpdateResponse].get
  69. // Sleep as long as timeout to make sure it has finished
  70. //Thread.sleep(FeedBotConfig.fetcherTimeoutInMillis + 10)
  71. FeedItem.collection.count should be(10)
  72. }
  73. it should "fail when updating non existent resource" in {
  74. val feed = insertFetchingFeed(httpServer.resolve("/resource/non_existent.xml").toExternalForm())
  75. Feed.collection.count should be(1)
  76. FeedItem.collection.count should be(0)
  77. //(updater ? FeedUpdater.UpdateRequest(feed)).as[FeedUpdater.UpdateResponse].get
  78. evaluating {
  79. (updater ? FeedUpdater.UpdateRequest(feed)).await.get
  80. } should produce[IllegalStateException] // Should get here due to 404
  81. // Sleep as long as timeout to make sure it has finished
  82. //Thread.sleep(FeedBotConfig.fetcherTimeoutInMillis + 10)
  83. FeedItem.collection.count should be(0)
  84. Feed.findOneByID(feed.id) map { updatedFeed =>
  85. updatedFeed.fetchStatus.fetching should be(false)
  86. updatedFeed.fetchStatus.success should be(false)
  87. updatedFeed.fetchStatus.message should not be (None)
  88. updatedFeed.fetchStatus.fetchStart should be (feed.fetchStatus.fetchStart)
  89. updatedFeed.fetchStatus.fetchEnd should be > feed.fetchStatus.fetchEnd
  90. updatedFeed.fetchStatus.fetchStart should be < updatedFeed.fetchStatus.fetchEnd
  91. } orElse {
  92. fail("Unable to find updated feed with id %s".format(feed.id))
  93. }
  94. }
  95. def insertFetchingFeed(url: String): Feed = {
  96. val feed = Feed(
  97. sourceId = source.id,
  98. title = "Test",
  99. url = url,
  100. fetchStatus = FetchStatus(
  101. fetching = true,
  102. fetchStart = System.currentTimeMillis))
  103. Feed.insert(feed)
  104. return feed
  105. }
  106. }