PageRenderTime 64ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/questions/src/test/scala/com/cyrusinnovation/inquisition/questions/service/QuestionServiceImplTest.scala

https://github.com/cyrusinnovation/inquisition
Scala | 203 lines | 152 code | 51 blank | 0 comment | 2 complexity | ca710906c4af6bbb79e830d93f49d5c1 MD5 | raw file
  1. package com.cyrusinnovation.inquisition.questions.service
  2. import org.scalatest.matchers.ShouldMatchers
  3. import org.scalatest.{BeforeAndAfterEach, FunSuite}
  4. import com.cyrusinnovation.inquisition.questions.Question
  5. import com.mongodb.casbah.MongoConnection
  6. import org.junit.runner.RunWith
  7. import org.scalatest.junit.JUnitRunner
  8. import com.cyrusinnovation.inquisition.questions.mongodb.{MongoTestConstants, MongoQuestionRepository}
  9. import com.mongodb.casbah.commons.MongoDBObject
  10. import java.security.InvalidParameterException
  11. import java.lang.String
  12. import com.cyrusinnovation.inquisition.response.Response
  13. @RunWith(classOf[JUnitRunner])
  14. class QuestionServiceImplTest extends FunSuite with ShouldMatchers with BeforeAndAfterEach {
  15. val con = MongoConnection()
  16. val TestDbName = "test_inquisition"
  17. val db = con(TestDbName)
  18. val repository = new MongoQuestionRepository(db)
  19. val service = new QuestionServiceImpl(repository)
  20. override def beforeEach() {
  21. db("questions").remove(MongoDBObject())
  22. }
  23. def uniqueQuestion(title: String = "How do I use MongoDB?"): Question = {
  24. Question(id = None, title = title + " " + System.nanoTime(), creatorUsername = "tester", body = "The question body.")
  25. }
  26. test("should be able to save a question and get it back") {
  27. val q = uniqueQuestion()
  28. q.id should be(None)
  29. val savedQuestion = service.createQuestion(q)
  30. savedQuestion.id should not be (None)
  31. savedQuestion.id should be('defined)
  32. val retrievedQuestion = service.findById(savedQuestion.id.get)
  33. retrievedQuestion.id should be(savedQuestion.id)
  34. }
  35. test("should throw illegal argument exception if not found") {
  36. evaluating {
  37. service.findById(MongoTestConstants.DeadObjectIdString)
  38. } should produce[IllegalArgumentException]
  39. }
  40. test("should be able to create a question") {
  41. val q = uniqueQuestion()
  42. q.id should be(None)
  43. val savedQuestion = service.createQuestion(q)
  44. savedQuestion.id should not be (None)
  45. savedQuestion.id should be('defined)
  46. repository.findById(savedQuestion.id.get) should be('defined)
  47. }
  48. test("should be able to find recent questions") {
  49. val questions = List(uniqueQuestion(), uniqueQuestion("Why isn't IntelliJ working?"))
  50. val savedQuestions = questions.map(repository.save(_))
  51. val results = service.findRecent()
  52. savedQuestions.foreach(results.contains(_) should be(true))
  53. }
  54. test("should be able to find recent questions without passing time") {
  55. val questions = List(uniqueQuestion(), uniqueQuestion("Why isn't IntelliJ working?"))
  56. val savedQuestions = questions.map(repository.save(_))
  57. val results = service.findRecent()
  58. savedQuestions.foreach(results.contains(_) should be(true))
  59. }
  60. test("should not be able to find all questions") {
  61. val questions = List(uniqueQuestion(), uniqueQuestion("Why isn't IntelliJ working?"))
  62. questions.map(repository.save(_))
  63. val results = service.findRecent(1)
  64. results.size should be(1)
  65. }
  66. test("find question total none") {
  67. var count = service.findQuestionCount()
  68. count should equal(0)
  69. }
  70. test("find question total with 2 questions in the repo") {
  71. val questions = List(uniqueQuestion(), uniqueQuestion("Why isn't IntelliJ working?"))
  72. questions.map(repository.save(_))
  73. val count = service.findQuestionCount()
  74. count should equal(2)
  75. }
  76. test("delete a question with a given object id") {
  77. val savedQuestion = repository.save(uniqueQuestion())
  78. val savedQuestionId = savedQuestion.id.get
  79. service.deleteQuestion(savedQuestionId, savedQuestion.creatorUsername)
  80. val deletedQuestion = repository.findById(savedQuestionId)
  81. deletedQuestion should be(None)
  82. }
  83. test("delete a question with a given object id, but not the correct username") {
  84. val savedQuestion = repository.save(uniqueQuestion())
  85. val savedQuestionId = savedQuestion.id.get
  86. evaluating(service.deleteQuestion(savedQuestionId, "invalidusername")) should
  87. (produce[InvalidParameterException])
  88. }
  89. test("deleting a non-existant question does not throw an exception") {
  90. service.deleteQuestion(MongoTestConstants.DeadObjectIdString, "")
  91. }
  92. test("should be able to update a question body") {
  93. val newBodyText: String = "something else"
  94. val q = uniqueQuestion()
  95. q.id should be(None)
  96. val savedQuestion = service.createQuestion(q)
  97. service.updateQuestion(savedQuestion.copy(body = newBodyText), savedQuestion.creatorUsername)
  98. val updatedQuestion = service.findById(savedQuestion.id.get)
  99. updatedQuestion.id should equal(savedQuestion.id)
  100. updatedQuestion.body should equal(newBodyText)
  101. }
  102. test("should be able to update a question body with illegal user creator") {
  103. val newBodyText: String = "something else"
  104. val q = uniqueQuestion()
  105. q.id should be(None)
  106. val savedQuestion = service.createQuestion(q)
  107. evaluating {
  108. service.updateQuestion(savedQuestion.copy(body = newBodyText), MongoTestConstants.InvalidUserName)
  109. } should produce[IllegalArgumentException]
  110. }
  111. test("Should be able to update a question without removing all responses") {
  112. val r1: Response = new Response(None, title = "Title 1", creatorUsername = "42", body = "body text 1")
  113. val r2: Response = new Response(None, title = "Title 2", creatorUsername = "42", body = "body text 2")
  114. val q = uniqueQuestion().copy(responses = List(r1, r2))
  115. val savedQuestion = repository.save(q)
  116. savedQuestion.responses should equal(q.responses)
  117. val updatedQuestion = new Question(id = q.id, body = q.body, title = "Update Title", creatorUsername = q.creatorUsername)
  118. service.updateQuestion(updatedQuestion, updatedQuestion.creatorUsername)
  119. val actualQ = service.findById(savedQuestion.id.get)
  120. actualQ.responses should equal(savedQuestion.responses)
  121. }
  122. test("Should be able to update a question without removing all tags") {
  123. val tags = List("taga", "tagb", "tagc")
  124. val q = uniqueQuestion().copy(tags = tags)
  125. val savedQuestion = repository.save(q)
  126. savedQuestion.tags should equal(tags)
  127. val updatedQuestion = new Question(id = q.id, body = q.body, title = "Update Title", creatorUsername = q.creatorUsername)
  128. service.updateQuestion(updatedQuestion, updatedQuestion.creatorUsername)
  129. val actualQ = service.findById(savedQuestion.id.get)
  130. actualQ.tags should equal(savedQuestion.tags)
  131. }
  132. test("get a list of clients") {
  133. val client1 = "Boston Capital"
  134. val client2 = "NFL"
  135. val client3 = "Nordstroms"
  136. val expectedList = List(client2, client3)
  137. val questions = List(uniqueQuestion().copy(client = client1), uniqueQuestion().copy(client = client2),
  138. uniqueQuestion().copy(client = client3))
  139. questions.map(repository.save(_))
  140. val clientList = service.getClientList("n")
  141. clientList.corresponds(expectedList){_ == _} should be(true)
  142. clientList should equal(expectedList)
  143. }
  144. test("get a limit of 0 of questions without a response") {
  145. val questions = List(uniqueQuestion(), uniqueQuestion(), uniqueQuestion())
  146. val expectedList = questions.map(repository.save(_))
  147. val retrievedQuestions = service.findQuestionsWithoutResponses(0)
  148. retrievedQuestions should not be(null)
  149. retrievedQuestions.isEmpty should be(false)
  150. retrievedQuestions.corresponds(expectedList){_ == _} should be(true)
  151. }
  152. }