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.

ArticleExportServiceSpec.scala 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package de.codingchallenge.services
  2. import akka.NotUsed
  3. import akka.http.scaladsl.model.HttpResponse
  4. import akka.stream.scaladsl.{Sink, Source}
  5. import de.codingchallenge.fixtures.{ArticleFixture, ProductExportFixture}
  6. import de.codingchallenge.models.{Article, ProductExport}
  7. import de.codingchallenge.repositories.{ArticleRepository, ProductExportRepository}
  8. import de.codingchallenge.{AkkaSpec, BaseSpec}
  9. import org.mockito.ArgumentCaptor
  10. import scala.concurrent.duration._
  11. import scala.concurrent.{Await, Future}
  12. import org.mockito.Mockito._
  13. import org.mockito.ArgumentMatchers._
  14. import org.mockito.internal.verification.argumentmatching.ArgumentMatchingTool
  15. import de.codingchallenge.csv.CsvOps._
  16. class ArticleExportServiceSpec extends BaseSpec with AkkaSpec{
  17. trait TestSetup extends ProductExportFixture with ArticleFixture{
  18. val articleRepositoryMock = mock[ArticleRepository]
  19. val productExportRepositoryMock = mock[ProductExportRepository]
  20. val service = new ArticleExportService(articleRepositoryMock, productExportRepositoryMock, materializer)
  21. }
  22. "The ArticleExportService" must {
  23. "ignore unavailable articles" in new TestSetup{
  24. doReturn(Source(List(articleUnavailable)), Nil: _*)
  25. .when(articleRepositoryMock)
  26. .getArticles(100)
  27. when(productExportRepositoryMock.add(any(), any()))
  28. .thenReturn(Future.successful(HttpResponse()))
  29. Await.result(service.exportArticles(), 5.second)
  30. val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
  31. verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
  32. Await.result(sourceCaptor.getValue.runWith(Sink.headOption), 1.second) mustBe None
  33. }
  34. "pass the expected source to the product export repository" in new TestSetup{
  35. doReturn(Source(List(articleUnavailable, cheapestArticle, anotherArticle)), Nil: _*)
  36. .when(articleRepositoryMock)
  37. .getArticles(100)
  38. when(productExportRepositoryMock.add(any(), any()))
  39. .thenReturn(Future.successful(HttpResponse()))
  40. Await.result(service.exportArticles(), 5.second)
  41. val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
  42. verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
  43. Await.result(sourceCaptor.getValue.runWith(Sink.head), 1.second) mustBe productExport
  44. }
  45. "contain only one entry per product" in new TestSetup with ArticleFixture {
  46. val articles = sampleData.map(_.csvToOptOf[Article].get).toList
  47. doReturn(Source(articles), Nil: _*)
  48. .when(articleRepositoryMock)
  49. .getArticles(100)
  50. when(productExportRepositoryMock.add(any(), any()))
  51. .thenReturn(Future.successful(HttpResponse()))
  52. Await.result(service.exportArticles(), 5.second)
  53. val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
  54. verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
  55. val results = Await.result(sourceCaptor.getValue.runWith(Sink.seq), 1.second)
  56. results.foreach{
  57. r => results.count(_.productId == r.productId) == 1
  58. }
  59. }
  60. "find the cheapest element of a group" in new TestSetup with ArticleFixture {
  61. val articles = sampleGroup.map(_.csvToOptOf[Article].get).toList
  62. doReturn(Source(articles), Nil: _*)
  63. .when(articleRepositoryMock)
  64. .getArticles(100)
  65. when(productExportRepositoryMock.add(any(), any()))
  66. .thenReturn(Future.successful(HttpResponse()))
  67. Await.result(service.exportArticles(), 5.second)
  68. val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
  69. verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
  70. val results = Await.result(sourceCaptor.getValue.runWith(Sink.seq), 1.second)
  71. results.head mustBe ProductExport(
  72. productId = "P-C7PeyUjD",
  73. name = "BMSNWL HFH",
  74. description = "",
  75. price = 45.85f,
  76. stockSum = 26
  77. )
  78. }
  79. }
  80. }