Skip to content

Commit

Permalink
backet controller + test scenarios for post
Browse files Browse the repository at this point in the history
  • Loading branch information
andonescu committed Feb 21, 2016
1 parent 05cb851 commit 5fb8030
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 12 deletions.
35 changes: 35 additions & 0 deletions app/ro/andonescu/shoppingbasket/controllers/BasketController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ro.andonescu.shoppingbasket.controllers

import com.google.inject.Inject
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.JsError
import play.api.mvc.{Action, Controller}
import ro.andonescu.shoppingbasket.controllers.forms.PostBasketForms
import ro.andonescu.shoppingbasket.controllers.forms.formatters.BasketFormsFormatters._

import scala.concurrent.{Future, ExecutionContext}

/**
* Created by andonescu on 21.02.2016.
*/
class BasketController @Inject()(val messagesApi: MessagesApi)
(implicit ec: ExecutionContext) extends Controller with I18nSupport {


/**
* Handles the creation of a new shopping basket
*/
def post() = Action.async { implicit request =>
request.body.asJson.map {
json =>
json.validate[PostBasketForms].map {
form =>


???
}.recoverTotal(e => Future.successful(BadRequest("Detected error: " + JsError.toJson(e))))
}.getOrElse {
Future.successful(BadRequest("Expecting Json Data"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}
import ro.andonescu.shoppingbasket.controllers.mappers.{ProductViewMapper, ProductCollectionMappers}
import ro.andonescu.shoppingbasket.services.ProductService
import ro.andonescu.shoppingbasket.controllers.views.formaters._
import ro.andonescu.shoppingbasket.controllers.views.formatters._

import scala.concurrent.ExecutionContext

/**
* Created by andonescu on 20.02.2016.
*/

class ProductsController @Inject()(val productService: ProductService, val messagesApi: MessagesApi)
(implicit ec: ExecutionContext) extends Controller with I18nSupport {
class ProductController @Inject()(val productService: ProductService, val messagesApi: MessagesApi)
(implicit ec: ExecutionContext) extends Controller with I18nSupport {

def products(page: Int, pageSize: Int, available: Option[Boolean] = None) = Action.async { implicit request =>
productService.products(page, pageSize, available).map { data =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ro.andonescu.shoppingbasket.controllers.forms

/**
* Created by andonescu on 21.02.2016.
*/
case class PostBasketProductForm(id : String)
case class PostBasketItemForm(product: PostBasketProductForm, capacity: Int)
case class PostBasketForms(items: Seq[PostBasketItemForm])
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ro.andonescu.shoppingbasket.controllers.forms.formatters

import play.api.libs.json.Json
import ro.andonescu.shoppingbasket.controllers.forms.{PostBasketForms, PostBasketItemForm, PostBasketProductForm}

/**
* Created by andonescu on 21.02.2016.
*/
object BasketFormsFormatters {
implicit val postBasketProductFormFormat = Json.format[PostBasketProductForm]
implicit val postBasketItemFormFormat = Json.format[PostBasketItemForm]
implicit val postBasketFormsFormat = Json.format[PostBasketForms]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class ProductViewMapper(req: Request[_]) extends Mapper[Product, ProductCo
obj.price,
obj.currency,
obj.isAvailable,
Link.self(ro.andonescu.shoppingbasket.controllers.routes.ProductsController.product(obj.id), req)
Link.self(ro.andonescu.shoppingbasket.controllers.routes.ProductController.product(obj.id), req)
)

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ro.andonescu.shoppingbasket.controllers.views.formaters
package ro.andonescu.shoppingbasket.controllers.views.formatters

import play.api.libs.json.{Writes, Json}
import ro.andonescu.shoppingbasket.controllers.views.{CollectionView, PageInfoView}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ro.andonescu.shoppingbasket.controllers.views.formaters
package ro.andonescu.shoppingbasket.controllers.views.formatters

import play.api.libs.json.{Json, Writes}
import ro.andonescu.shoppingbasket.controllers.views.Link
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ro.andonescu.shoppingbasket.controllers.views.formaters
package ro.andonescu.shoppingbasket.controllers.views.formatters

import play.api.libs.json.Json
import ro.andonescu.shoppingbasket.controllers.views.{ProductCollectionView, Link}
Expand All @@ -7,7 +7,7 @@ import ro.andonescu.shoppingbasket.controllers.views.{ProductCollectionView, Lin
* Created by ionut on 20.02.2016.
*/
object ProductCollectionViewFormatter {
import ro.andonescu.shoppingbasket.controllers.views.formaters.LinkFormatter._
import ro.andonescu.shoppingbasket.controllers.views.formatters.LinkFormatter._

implicit val jsonProductCollectionView = Json.writes[ProductCollectionView]
}
9 changes: 6 additions & 3 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Endpoints
GET /products ro.andonescu.shoppingbasket.controllers.ProductsController.products(page: Int ?= 1, pageSize: Int ?= 50, available: Option[Boolean])
GET /products/:id ro.andonescu.shoppingbasket.controllers.ProductsController.product(id: String)
# Product
GET /products ro.andonescu.shoppingbasket.controllers.ProductController.products(page: Int ?= 1, pageSize: Int ?= 50, available: Option[Boolean])
GET /products/:id ro.andonescu.shoppingbasket.controllers.ProductController.product(id: String)

# Shopping Basket
POST /shoppingbaskets ro.andonescu.shoppingbasket.controllers.BasketController.post()
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package ro.andonescu.shoppingbasket.controllers

import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
import play.api.libs.json.Json
import play.api.test.{FakeHeaders, FakeRequest, PlaySpecification, WithApplication}

/**
* Created by andonescu on 21.02.2016.
*/
@RunWith(classOf[JUnitRunner])
class BasketControllerTest extends PlaySpecification {


"BasketController#post" should {

"return not create a shopping basket for no json" in new WithApplication {
val response = route(FakeRequest(POST, "/shoppingbaskets"))


response must beSome.which(status(_) == BAD_REQUEST)
}



"return create a shopping basket request with a single available item" in new WithApplication {

//TODO: override the id from the json
val jsonString =
""" {
| "items": [
| {
| "product": {
| "id": "334s"
| },
| "capacity": 2
| }
| ]
| }
""".stripMargin


val response = route(
FakeRequest(
POST,
"/shoppingbaskets",
FakeHeaders(Seq(("Content-Type", "application/json"))),
Json.parse(jsonString))
)


response must beSome.which(status(_) == BAD_REQUEST)
}

"return an error if no product is available in the request" in new WithApplication {

val jsonString =
""" {
| "items": [
| {
| "capacity": 2
| }
| ]
| }
""".stripMargin


val response = route(
FakeRequest(
POST,
"/shoppingbaskets",
FakeHeaders(Seq(("Content-Type", "application/json"))),
Json.parse(jsonString))
)


response must beSome.which(status(_) == BAD_REQUEST)
}

"return an error if the product does not exists" in new WithApplication {

val jsonString =
""" {
| "items": [
| {
| "product": {
| "id": "334s-ando-product-ha"
| },
| "capacity": 2
| }
| ]
| }
""".stripMargin


val response = route(
FakeRequest(
POST,
"/shoppingbaskets",
FakeHeaders(Seq(("Content-Type", "application/json"))),
Json.parse(jsonString))
)


response must beSome.which(status(_) == BAD_REQUEST)
}

"return return an error if the product is not available" in new WithApplication {

//TODO: override the id from the json
val jsonString =
""" {
| "items": [
| {
| "product": {
| "id": "334s"
| },
| "capacity": 2
| }
| ]
| }
""".stripMargin


val response = route(
FakeRequest(
POST,
"/shoppingbaskets",
FakeHeaders(Seq(("Content-Type", "application/json"))),
Json.parse(jsonString))
)


response must beSome.which(status(_) == BAD_REQUEST)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import play.api.test._
* Created by andonescu on 20.02.2016.
*/
@RunWith(classOf[JUnitRunner])
class ProductsControllerTest extends PlaySpecification{
class ProductControllerTest extends PlaySpecification{


"ProductsController#products" should {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ro.andonescu.shoppingbasket.services

import org.junit.runner.RunWith
import org.specs2.concurrent.ExecutionEnv
import org.specs2.mock.Mockito
import org.specs2.mutable._
import org.specs2.runner.JUnitRunner
import ro.andonescu.shoppingbasket.dao.ProductRepository
import ro.andonescu.shoppingbasket.dao.entities.Product
import ro.andonescu.shoppingbasket.dao.entities.gen.ProductGen
Expand All @@ -13,6 +15,7 @@ import scala.concurrent.Future
/**
* Created by andonescu on 21.02.2016.
*/
@RunWith(classOf[JUnitRunner])
class ProductServiceTest extends Specification with Mockito {


Expand Down

0 comments on commit 5fb8030

Please sign in to comment.