PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/questions/src/test/scala/com/cyrusinnovation/inquisition/response/service/ResponseServiceTest.scala

https://github.com/cyrusinnovation/inquisition
Scala | 128 lines | 106 code | 22 blank | 0 comment | 0 complexity | 0c32dac3600143df61f5a3db3fb28a63 MD5 | raw file
  1. package com.cyrusinnovation.inquisition.response.service
  2. import org.junit.runner.RunWith
  3. import org.scalatest.junit.JUnitRunner
  4. import org.scalatest.matchers.ShouldMatchers
  5. import org.scalatest.{BeforeAndAfterEach, FunSuite}
  6. import com.mongodb.casbah.commons.MongoDBObject
  7. import com.mongodb.casbah.MongoConnection
  8. import com.cyrusinnovation.inquisition.response.mongodb.MongoResponseRepository
  9. import com.cyrusinnovation.inquisition.response.Response
  10. import com.cyrusinnovation.inquisition.questions.Question
  11. import org.joda.time.DateTime
  12. import com.cyrusinnovation.inquisition.questions.mongodb.{MongoTestConstants, MongoQuestionRepository}
  13. import java.security.InvalidParameterException
  14. @RunWith(classOf[JUnitRunner])
  15. class ResponseServiceTest extends FunSuite with ShouldMatchers with BeforeAndAfterEach {
  16. val con = MongoConnection()
  17. val TestDbName = "test_inquisition"
  18. val db = con(TestDbName)
  19. val questionRepository = new MongoQuestionRepository(db)
  20. val repository = new MongoResponseRepository(db, questionRepository)
  21. val service = new ResponseServiceImpl(repository)
  22. override def beforeEach() {
  23. db("questions").remove(MongoDBObject())
  24. }
  25. def createSavedQuestion(responses: List[Response] = List[Response]()): Question = {
  26. val question = new Question(None, "title " + System.nanoTime(), "testUser", "body", List(), responses, "")
  27. questionRepository.save(question)
  28. }
  29. def createSavedResponse(question: Question): Response = {
  30. val response = new Response(None, title = "title", creatorUsername = "testUser", body = "body")
  31. service.save(response, question.id.get, "testUser")
  32. }
  33. test("can save a new response") {
  34. val question = createSavedQuestion()
  35. val savedResponse = createSavedResponse(question)
  36. val retrievedResponse = repository.getResponse(savedResponse.id.get).get._2
  37. savedResponse should be(retrievedResponse)
  38. }
  39. test("saving a new response throws exception if question id is null") {
  40. val response = new Response(None, title = "title", creatorUsername = "testUser", body = "body")
  41. evaluating {
  42. service.save(response, null, "testUser")
  43. } should produce[IllegalArgumentException]
  44. }
  45. test("saving a new response throws exception if question id does not exist") {
  46. val response = new Response(None, title = "title", creatorUsername = "testUser", body = "body")
  47. evaluating {
  48. service.save(response, MongoTestConstants.DeadObjectIdString, "testUser")
  49. } should produce[IllegalArgumentException]
  50. }
  51. test("saving a new response throws exception if response is null") {
  52. val response = new Response(None, title = "title", creatorUsername = "testUser", body = "body")
  53. evaluating {
  54. service.save(response, MongoTestConstants.DeadObjectIdString, "testUser")
  55. } should produce[IllegalArgumentException]
  56. }
  57. test("saving a new response throws exception if creatorUsername is null") {
  58. val question = createSavedQuestion()
  59. val response = new Response(None, title = "title", creatorUsername = "testUser", body = "body")
  60. evaluating {
  61. service.save(response, question.id.get, "")
  62. } should produce[IllegalArgumentException]
  63. }
  64. test("saving a new response throws exception if creatorUsername is empty") {
  65. val question = createSavedQuestion()
  66. val response = new Response(None, title = "title", creatorUsername = "testUser", body = "body")
  67. evaluating {
  68. service.save(response, question.id.get, null)
  69. } should produce[IllegalArgumentException]
  70. }
  71. test("update a response") {
  72. val question = createSavedQuestion()
  73. val response = createSavedResponse(question)
  74. val expectedResponse = response.copy(body = "Updated Body")
  75. val responseUnderTest = service.update(response.copy(body = "Updated Body"), "testUser")
  76. responseUnderTest should be(expectedResponse)
  77. }
  78. test("only the response creator can upate a response") {
  79. val question = createSavedQuestion()
  80. val response = createSavedResponse(question)
  81. evaluating {
  82. service.update(response.copy(body = "Updated Body"), "wrongUser")
  83. } should produce[InvalidParameterException]
  84. }
  85. test("can find question by id") {
  86. val question = createSavedQuestion()
  87. val response = createSavedResponse(question)
  88. val retrievedResponse = service.findById(response.id.get)
  89. retrievedResponse should be(response)
  90. }
  91. test("exception thrown when id is null") {
  92. evaluating {
  93. service.findById(null)
  94. } should produce[IllegalArgumentException]
  95. }
  96. test("exception thrown when id is empty") {
  97. evaluating {
  98. service.findById("")
  99. } should produce[IllegalArgumentException]
  100. }
  101. test("exception thrown when id does not exist") {
  102. evaluating {
  103. service.findById(MongoTestConstants.DeadObjectIdString)
  104. } should produce[IllegalArgumentException]
  105. }
  106. }