-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backet controller + test scenarios for post
- Loading branch information
Showing
12 changed files
with
212 additions
and
12 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
app/ro/andonescu/shoppingbasket/controllers/BasketController.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/ro/andonescu/shoppingbasket/controllers/forms/PostBasketForms.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
13 changes: 13 additions & 0 deletions
13
app/ro/andonescu/shoppingbasket/controllers/forms/formatters/BasketFormsFormatters.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/formaters/CollectionViewFormatter.scala → .../formatters/CollectionViewFormatter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...llers/views/formaters/LinkFormatter.scala → ...lers/views/formatters/LinkFormatter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
test/ro/andonescu/shoppingbasket/controllers/BasketControllerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters