/src/test/scala/com/technologyconversations/api/ServiceSpec.scala

https://github.com/vfarcic/books-service · Scala · 184 lines · 147 code · 37 blank · 0 comment · 1 complexity · d626bb93b1d2f704fdc0e2c432d32a8e MD5 · raw file

  1. package com.technologyconversations.api
  2. import com.mongodb.casbah.Imports._
  3. import com.mongodb.casbah.MongoClient
  4. import com.mongodb.casbah.commons.MongoDBObject
  5. import com.novus.salat._
  6. import com.novus.salat.global._
  7. import org.specs2.mutable.Specification
  8. import org.specs2.specification.BeforeExample
  9. import spray.http.{ContentType, HttpEntity}
  10. import spray.http.MediaTypes._
  11. import spray.routing.HttpService
  12. import spray.testkit.Specs2RouteTest
  13. import spray.http.StatusCodes._
  14. import spray.json._
  15. import DefaultJsonProtocol._
  16. import spray.httpx.SprayJsonSupport._
  17. import spray.httpx.SprayJsonSupport._
  18. class ServiceSpec extends Specification with Specs2RouteTest with HttpService with ServiceRoute with StaticRoute with BeforeExample {
  19. val client = MongoClient("localhost", 27017)
  20. val db = client("books")
  21. val collection = db("books")
  22. val apiUri = "/api/v1/books"
  23. val staticUri = "/test.html"
  24. val bookId = 1234
  25. def actorRefFactory = system
  26. def before = db.dropDatabase()
  27. sequential
  28. s"GET $staticUri" should {
  29. "return OK" in {
  30. Get(staticUri) ~> staticRoute ~> check {
  31. response.status must equalTo(OK)
  32. }
  33. }
  34. "return file content" in {
  35. Get(staticUri) ~> staticRoute ~> check {
  36. val content = responseAs[String]
  37. content must equalTo("This is just a test")
  38. }
  39. }
  40. }
  41. s"GET $apiUri" should {
  42. "return OK" in {
  43. Get(apiUri) ~> serviceRoute ~> check {
  44. response.status must equalTo(OK)
  45. }
  46. }
  47. "return all books" in {
  48. val expected = insertBooks(3).map { book =>
  49. BookReduced(book._id, book.title, book.author)
  50. }
  51. Get(apiUri) ~> serviceRoute ~> check {
  52. response.entity must not equalTo None
  53. val books = responseAs[List[BookReduced]]
  54. books must haveSize(expected.size)
  55. books must equalTo(expected)
  56. }
  57. }
  58. }
  59. s"GET $apiUri/_id/$bookId" should {
  60. val expected = Book(bookId, "Title", "Author", "Description")
  61. "return OK" in {
  62. insertBook(expected)
  63. Get(s"$apiUri/_id/$bookId") ~> serviceRoute ~> check {
  64. response.status must equalTo(OK)
  65. }
  66. }
  67. "return book" in {
  68. insertBook(expected)
  69. Get(s"$apiUri/_id/$bookId") ~> serviceRoute ~> check {
  70. response.entity must not equalTo None
  71. val book = responseAs[Book]
  72. book must equalTo(expected)
  73. }
  74. }
  75. }
  76. s"PUT $apiUri" should {
  77. val expected = Book(bookId, "PUT title", "Put author", "Put description")
  78. "return OK" in {
  79. Put(apiUri, expected) ~> serviceRoute ~> check {
  80. response.status must equalTo(OK)
  81. }
  82. }
  83. "return Book" in {
  84. Put(apiUri, expected) ~> serviceRoute ~> check {
  85. response.entity must not equalTo None
  86. val book = responseAs[Book]
  87. book must equalTo(expected)
  88. }
  89. }
  90. "insert book to the DB" in {
  91. Put(apiUri, expected) ~> serviceRoute ~> check {
  92. response.status must equalTo(OK)
  93. val book = getBook(bookId)
  94. book must equalTo(expected)
  95. }
  96. }
  97. "update book when it exists in the DB" in {
  98. collection.insert(grater[Book].asDBObject(expected))
  99. Put(apiUri, expected) ~> serviceRoute ~> check {
  100. response.status must equalTo(OK)
  101. val book = getBook(bookId)
  102. book must equalTo(expected)
  103. }
  104. }
  105. }
  106. s"DELETE $apiUri/_id/$bookId" should {
  107. val expected = Book(bookId, "Title", "Author", "Description")
  108. "return OK" in {
  109. insertBook(expected)
  110. Delete(s"$apiUri/_id/$bookId") ~> serviceRoute ~> check {
  111. response.status must equalTo(OK)
  112. }
  113. }
  114. "return book" in {
  115. insertBook(expected)
  116. Delete(s"$apiUri/_id/$bookId") ~> serviceRoute ~> check {
  117. response.entity must not equalTo None
  118. val book = responseAs[Book]
  119. book must equalTo(expected)
  120. }
  121. }
  122. "remove book from the DB" in {
  123. insertBook(expected)
  124. Delete(s"$apiUri/_id/$bookId") ~> serviceRoute ~> check {
  125. response.status must equalTo(OK)
  126. getBooks must haveSize(0)
  127. }
  128. }
  129. }
  130. def insertBook(book: Book) {
  131. collection.insert(grater[Book].asDBObject(book))
  132. }
  133. def insertBooks(quantity: Int): List[Book] = {
  134. val books = List.tabulate(quantity)(id => Book(id, s"Title $id", s"Author $id", s"Description $id"))
  135. for (book <- books) {
  136. collection.insert(grater[Book].asDBObject(book))
  137. }
  138. books
  139. }
  140. def getBook(id: Int): Book = {
  141. val dbObject = collection.findOne(MongoDBObject("_id" -> id))
  142. grater[Book].asObject(dbObject.get)
  143. }
  144. def getBooks: List[Book] = {
  145. collection.find().toList.map(grater[Book].asObject(_))
  146. }
  147. }