Browse Source

fix: unavailibility filter

put back into repo
check availability in tests
master
Christof Nolle 4 years ago
parent
commit
b5b6969bf2
  1. 2
      src/main/scala/de/codingchallenge/services/ArticleExportService.scala
  2. 56
      src/test/scala/de/codingchallenge/services/ArticleExportServiceSpec.scala

2
src/main/scala/de/codingchallenge/services/ArticleExportService.scala

@ -34,7 +34,7 @@ class ArticleExportService( @@ -34,7 +34,7 @@ class ArticleExportService(
def exportArticles(): Future[HttpResponse] = productExportRepository.add(Source.fromGraph[ProductExport, NotUsed](
articleRepository
.getArticles(productsSize)
// .filter(_.stock > 0)
.filter(_.stock > 0)
.map(a => (a, a.stock))
.via(new AccumulateWhileUnchanged[(Article, Int), String](_._1.productId))
.map(reduceTuples)

56
src/test/scala/de/codingchallenge/services/ArticleExportServiceSpec.scala

@ -3,49 +3,51 @@ package de.codingchallenge.services @@ -3,49 +3,51 @@ package de.codingchallenge.services
import akka.NotUsed
import akka.http.scaladsl.model.HttpResponse
import akka.stream.scaladsl.{Sink, Source}
import de.codingchallenge.csv.CsvOps._
import de.codingchallenge.fixtures.{ArticleFixture, ProductExportFixture}
import de.codingchallenge.models.{Article, ProductExport}
import de.codingchallenge.repositories.{ArticleRepository, ProductExportRepository}
import de.codingchallenge.{AkkaSpec, BaseSpec}
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import org.mockito.Mockito._
import org.mockito.ArgumentMatchers._
import org.mockito.internal.verification.argumentmatching.ArgumentMatchingTool
import de.codingchallenge.csv.CsvOps._
class ArticleExportServiceSpec extends BaseSpec with AkkaSpec{
class ArticleExportServiceSpec extends BaseSpec with AkkaSpec {
trait TestSetup extends ProductExportFixture with ArticleFixture{
trait TestSetup extends ProductExportFixture with ArticleFixture {
val articleRepositoryMock = mock[ArticleRepository]
val productExportRepositoryMock = mock[ProductExportRepository]
val service = new ArticleExportService(articleRepositoryMock, productExportRepositoryMock, materializer)
val service =
new ArticleExportService(articleRepositoryMock, productExportRepositoryMock, materializer)
}
"The ArticleExportService" must {
"ignore unavailable articles" in new TestSetup{
"ignore unavailable articles" in new TestSetup {
doReturn(Source(List(articleUnavailable)), Nil: _*)
.when(articleRepositoryMock)
.getArticles(100)
.getArticles(any())
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))
Await.result(service.exportArticles(), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
Await.result(sourceCaptor.getValue.runWith(Sink.headOption), 1.second) mustBe None
}
"pass the expected source to the product export repository" in new TestSetup{
doReturn(Source(List(articleUnavailable, cheapestArticle, anotherArticle)), Nil: _*)
.when(articleRepositoryMock)
.getArticles(100)
"pass the expected source to the product export repository" in new TestSetup {
doReturn(Source(List(articleUnavailable, cheapestArticle, anotherArticle)), Nil: _*)
.when(articleRepositoryMock)
.getArticles(any())
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))
Await.result(service.exportArticles(), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
Await.result(sourceCaptor.getValue.runWith(Sink.head), 1.second) mustBe productExport
}
@ -54,37 +56,39 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec{ @@ -54,37 +56,39 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec{
val articles = sampleData.map(_.csvToOptOf[Article].get).toList
doReturn(Source(articles), Nil: _*)
.when(articleRepositoryMock)
.getArticles(100)
.getArticles(any())
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))
Await.result(service.exportArticles(), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
val results = Await.result(sourceCaptor.getValue.runWith(Sink.seq), 1.second)
results.foreach{
r => results.count(_.productId == r.productId) == 1
}
results.foreach { r =>
results.count(_.productId == r.productId) == 1
}
}
"find the cheapest element of a group" in new TestSetup with ArticleFixture {
val articles = sampleGroup.map(_.csvToOptOf[Article].get).toList
doReturn(Source(articles), Nil: _*)
.when(articleRepositoryMock)
.getArticles(100)
.getArticles(any())
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))
Await.result(service.exportArticles(), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
val results = Await.result(sourceCaptor.getValue.runWith(Sink.seq), 1.second)
results.head mustBe ProductExport(
productId = "P-C7PeyUjD",
name = "BMSNWL HFH",
description = "",
price = 45.85f,
stockSum = 26
name = "MGT",
description = "swwfkg rzDawtwl vvvjebHd aenb aXkcwJyqsc",
price = 77.34f,
stockSum = 26
)
}
}

Loading…
Cancel
Save