Browse Source

chore: test/comments

master
Christof Nolle 2 years ago
parent
commit
dd9d703653

+ 11
- 1
src/main/scala/de/codingchallenge/services/ArticleExportService.scala View File

@@ -20,6 +20,17 @@ class ArticleExportService(

val productsSize: Int = 100

/**
* Streams articles to generate a product export.
* Filters unavailable articles
* accumulates articles until id change (groupBy does not close the substream and waits for more articles which match
* the predicate)
* Accumulated articles are combined (we might use a semigroup here) to a tuple of the article with the lowest price
* and the sum of stock values)
* Result is transformed to a 'ProductExport' which later on will be processed by the productExportRepository
*
* @return
*/
def exportArticles(): Future[HttpResponse] = productExportRepository.add(Source.fromGraph(
articleRepository
.getArticles(productsSize)
@@ -27,7 +38,6 @@ class ArticleExportService(
.map(a => (a, a.stock))
.via(new AccumulateWhileUnchanged[(Article, Int), String](_._1.productId))
.map(_.reduce[(Article, Int)] {
case ((a1, c1), (a2, c2)) if a1.price < a2.price => (a1, c1 + c2)
case ((a1, c1), (a2, c2)) if a1.price > a2.price => (a2, c1 + c2)
case ((a1, c1), (_, c2)) => (a1, c1 + c2)
})

+ 1
- 2
src/test/scala/de/codingchallenge/repositories/ArticleRepositorySpec.scala View File

@@ -3,14 +3,13 @@ package de.codingchallenge.repositories
import akka.stream.scaladsl.Sink
import de.codingchallenge.models.Article
import de.codingchallenge.{AkkaSpec, BaseSpec}
import org.scalatest.{BeforeAndAfterAll, MustMatchers, WordSpec}

import scala.concurrent.duration._
import scala.concurrent.Await

/**
* Honestly this is kind of an integration tests running against the Demo Server.
* I will leave it here for convenience
* I will leave it here for convenience.
*/
class ArticleRepositorySpec extends BaseSpec with AkkaSpec {


+ 12
- 2
src/test/scala/de/codingchallenge/services/ArticleExportServiceSpec.scala View File

@@ -23,6 +23,18 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec{
}

"The ArticleExportService" must {
"ignore unavailable articles" in new TestSetup{
doReturn(Source(List(articleUnavailable)), Nil: _*)
.when(articleRepositoryMock)
.getArticles(100)
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))

Await.result(service.exportArticles(), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, _]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, _]])
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)
@@ -34,8 +46,6 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec{
val sourceCaptor: ArgumentCaptor[Source[ProductExport, _]] = ArgumentCaptor.forClass(classOf[Source[ProductExport, _]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
Await.result(sourceCaptor.getValue.runWith(Sink.head), 1.second) mustBe productExport


}
}
}

Loading…
Cancel
Save