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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "pass sample data as expected" 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. Await.result(sourceCaptor.getValue.runWith(Sink.seq), 1.second).foreach { e =>
  56. e mustBe a[ProductExport]
  57. }
  58. }
  59. }
  60. }