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.

166 lines
5.0 KiB

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