Browse Source

feat: configurable products size

master
Christof Nolle 1 year ago
parent
commit
58da316894

+ 26
- 18
src/main/scala/de/codingchallenge/Routes.scala View File

@@ -10,24 +10,26 @@ import de.codingchallenge.configuration.Environment
import com.typesafe.scalalogging.LazyLogging
import de.codingchallenge.services.ArticleExportService

class Routes(articleExportService: ArticleExportService,
actorSystem: ActorSystem,
actorMaterializer: ActorMaterializer,
environment: Environment)
extends LazyLogging {
class Routes(
articleExportService: ArticleExportService,
actorSystem: ActorSystem,
actorMaterializer: ActorMaterializer,
environment: Environment)
extends LazyLogging {

implicit val as: ActorSystem = actorSystem
implicit val mat: Materializer = actorMaterializer

var on: Boolean = true


val serviceRoutes: Route =
pathSuffix("readinessProbe") {
get {
if(on) {
if (on) {
complete {
HttpEntity(ContentTypes.`application/json`, ByteString(de.codingchallenge.BuildInfo.toJson))
HttpEntity(
ContentTypes.`application/json`,
ByteString(de.codingchallenge.BuildInfo.toJson))
}
} else {
complete(StatusCodes.ServiceUnavailable)
@@ -36,26 +38,32 @@ class Routes(articleExportService: ArticleExportService,
} ~ path("health") {
get {
complete {
HttpEntity(ContentTypes.`application/json`, ByteString(de.codingchallenge.BuildInfo.toJson))
HttpEntity(
ContentTypes.`application/json`,
ByteString(de.codingchallenge.BuildInfo.toJson))
}
}
}

val exportRoute: Route =
path("articles") {
get {
onSuccess(articleExportService.exportArticles()) { res =>
complete(res)
pathPrefix("articles") {
{
path(IntNumber) { productsSize =>
get {
onSuccess(articleExportService.exportArticles(productsSize)) { res =>
complete(res)
}
}
}
}
}

val routes: Route =
pathPrefix("inoio-coding-challenge") {
serviceRoutes
} ~ pathPrefix("inoio-coding-challenge" / "export") {
exportRoute
} ~ serviceRoutes
pathPrefix("inoio-coding-challenge") {
serviceRoutes
} ~ pathPrefix("inoio-coding-challenge" / "export") {
exportRoute
} ~ serviceRoutes

val rejectRoute: Route = reject


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

@@ -18,8 +18,6 @@ class ArticleExportService(
) extends LazyLogging {
implicit val m: Materializer = mat

val productsSize: Int = 20

/**
* Streams articles to generate a product export.
* Filters unavailable articles
@@ -31,7 +29,7 @@ class ArticleExportService(
*
* @return
*/
def exportArticles(): Future[HttpResponse] = productExportRepository.add(Source.fromGraph[ProductExport, NotUsed](
def exportArticles(productsSize: Int): Future[HttpResponse] = productExportRepository.add(Source.fromGraph[ProductExport, NotUsed](
articleRepository
.getArticles(productsSize)
.filter(_.stock > 0)

+ 5
- 4
src/test/scala/de/codingchallenge/services/ArticleExportServiceSpec.scala View File

@@ -18,6 +18,7 @@ import scala.concurrent.{Await, Future}
class ArticleExportServiceSpec extends BaseSpec with AkkaSpec {

trait TestSetup extends ProductExportFixture with ArticleFixture {
val productsSize = 10
val articleRepositoryMock = mock[ArticleRepository]
val productExportRepositoryMock = mock[ProductExportRepository]
val service =
@@ -32,7 +33,7 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec {
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))

Await.result(service.exportArticles(), 5.second)
Await.result(service.exportArticles(productsSize), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
@@ -45,7 +46,7 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec {
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))

Await.result(service.exportArticles(), 5.second)
Await.result(service.exportArticles(productsSize), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
@@ -60,7 +61,7 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec {
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))

Await.result(service.exportArticles(), 5.second)
Await.result(service.exportArticles(productsSize), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())
@@ -78,7 +79,7 @@ class ArticleExportServiceSpec extends BaseSpec with AkkaSpec {
when(productExportRepositoryMock.add(any(), any()))
.thenReturn(Future.successful(HttpResponse()))

Await.result(service.exportArticles(), 5.second)
Await.result(service.exportArticles(productsSize), 5.second)
val sourceCaptor: ArgumentCaptor[Source[ProductExport, NotUsed]] =
ArgumentCaptor.forClass(classOf[Source[ProductExport, NotUsed]])
verify(productExportRepositoryMock).add(sourceCaptor.capture(), any())

Loading…
Cancel
Save