You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.http4sdemo
  2. import cats.syntax.all._
  3. import monix.bio.IO
  4. import monix.bio.Task
  5. import monix.bio.UIO
  6. import monix.reactive.Observable
  7. import org.http4s.Method
  8. import org.http4s.Request
  9. import org.http4s.Uri
  10. import org.http4s.implicits._
  11. import wow.doge.http4sdemo.MonixBioSuite
  12. import wow.doge.http4sdemo.dto.Book
  13. import wow.doge.http4sdemo.dto.BookSearchMode
  14. import wow.doge.http4sdemo.dto.BookUpdate
  15. import wow.doge.http4sdemo.services.LibraryService
  16. import wow.doge.http4sdemo.services.NoopLibraryService
  17. class LibraryControllerSpec extends MonixBioSuite {
  18. val Root = Uri(path = "")
  19. test("get books success") {
  20. import org.http4s.circe.CirceEntityCodec._
  21. val book = Book(1, "book1", "adsgq342dsdc", 1, date)
  22. val service = new NoopLibraryService {
  23. override def getBooks: Observable[Book] =
  24. Observable.fromIterable(book :: Nil)
  25. override def getBookById(id: Int): Task[Option[Book]] =
  26. Task.some(book)
  27. }
  28. for {
  29. _ <- UIO.unit
  30. routes = Http4sdemoRoutes.libraryRoutes(service)
  31. res <- routes
  32. .run(Request[Task](Method.GET, uri"/api/get/books"))
  33. .value
  34. .hideErrors
  35. body <- res.map(_.as[List[Book]]).sequence
  36. _ <- UIO(assertEquals(body, Some(List(book))))
  37. // _ <- logger2.debug(body.toString).hideErrors
  38. } yield ()
  39. }
  40. test("update book error") {
  41. import org.http4s.circe.CirceEntityCodec._
  42. val service = new NoopLibraryService {
  43. override def updateBook(id: Int, updateData: BookUpdate) =
  44. IO.raiseError(
  45. LibraryService.EntityDoesNotExist(s"Book with id=$id does not exist")
  46. )
  47. }
  48. for {
  49. _ <- UIO.unit
  50. reqBody = BookUpdate(Some("blah"), None)
  51. routes = Http4sdemoRoutes.libraryRoutes(service)
  52. res <- routes
  53. .run(
  54. Request[Task](Method.PATCH, Root / "api" / "update" / "book" / "1")
  55. .withEntity(reqBody)
  56. )
  57. .value
  58. .hideErrors
  59. body <- res.map(_.as[LibraryService.Error]).sequence
  60. _ <- UIO(
  61. assertEquals(
  62. body,
  63. Some(
  64. LibraryService.EntityDoesNotExist("Book with id=1 does not exist")
  65. )
  66. )
  67. )
  68. // _ <- logger.debug(res.toString).hideErrors
  69. // _ <- logger.debug(body.toString).hideErrors
  70. } yield ()
  71. }
  72. test("get books by author name") {
  73. import org.http4s.circe.CirceEntityCodec._
  74. val value = "blah"
  75. val books =
  76. List(Book(1, "book1", value, 1, date), Book(2, "book1", value, 1, date))
  77. val service = new NoopLibraryService {
  78. override def searchBook(mode: BookSearchMode, value: String) =
  79. mode match {
  80. case BookSearchMode.BookTitle =>
  81. Observable.fromIterable(books)
  82. case BookSearchMode.AuthorName =>
  83. Observable.fromIterable(books)
  84. }
  85. }
  86. for {
  87. _ <- UIO.unit
  88. // logger2 = logger.withConstContext(
  89. // Map("Test" -> "get books by author name")
  90. // )
  91. routes = Http4sdemoRoutes.libraryRoutes(service)
  92. request = Request[Task](
  93. Method.GET,
  94. Root / "api" / "get" / "book"
  95. withQueryParams Map(
  96. "mode" -> BookSearchMode.AuthorName.entryName,
  97. "value" -> "blah"
  98. )
  99. )
  100. // _ <- logger2.info(s"Request -> $request")
  101. res <- routes.run(request).value.hideErrors
  102. body <- res.map(_.as[List[Book]]).sequence
  103. _ <- UIO.pure(body).assertEquals(Some(books))
  104. // _ <- logger2.debug(s"Response body -> $body").hideErrors
  105. } yield ()
  106. }
  107. test("get books by book title") {
  108. import org.http4s.circe.CirceEntityCodec._
  109. val value = "blah"
  110. val books =
  111. List(Book(1, "book1", value, 1, date), Book(2, "book1", value, 1, date))
  112. val service = new NoopLibraryService {
  113. override def searchBook(mode: BookSearchMode, value: String) =
  114. mode match {
  115. case BookSearchMode.BookTitle =>
  116. Observable.fromIterable(books)
  117. case BookSearchMode.AuthorName =>
  118. Observable.fromIterable(books)
  119. }
  120. }
  121. for {
  122. _ <- UIO.unit
  123. // logger2 = logger.withConstContext(
  124. // Map("Test" -> "get books by book title")
  125. // )
  126. routes = Http4sdemoRoutes.libraryRoutes(service)
  127. request = Request[Task](
  128. Method.GET,
  129. Root / "api" / "get" / "book"
  130. withQueryParams Map(
  131. "mode" -> BookSearchMode.BookTitle.entryName,
  132. "value" -> "blah"
  133. )
  134. )
  135. // _ <- logger2.info(s"Request -> $request")
  136. res <- routes.run(request).value.hideErrors
  137. body <- res.map(_.as[List[Book]]).sequence
  138. _ <- UIO.pure(body).assertEquals(Some(books))
  139. // _ <- logger2.debug(s"Response body -> $body").hideErrors
  140. } yield ()
  141. }
  142. }