-
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.
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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,12 @@ | ||
package controllers | ||
|
||
import com.google.inject.Inject | ||
import play.api.mvc.{Action, Controller} | ||
import services.SandwichService | ||
|
||
class SandwichController @Inject()(sandwichService: SandwichService) extends Controller{ | ||
def sandwiches() = Action { | ||
val sandwiches = sandwichService.sandwiches | ||
Ok(views.html.sandwiches(sandwiches)) | ||
} | ||
} |
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,3 @@ | ||
package models | ||
|
||
case class Sandwich(name: String, price: BigDecimal, description: String) |
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,14 @@ | ||
package services | ||
|
||
import com.google.inject.ImplementedBy | ||
import models.Sandwich | ||
|
||
class RealSandwichService extends SandwichService { | ||
//just an empty list | ||
override def sandwiches(): List[Sandwich] = List() | ||
} | ||
|
||
@ImplementedBy(classOf[RealSandwichService]) | ||
trait SandwichService { | ||
def sandwiches() : List[Sandwich] | ||
} |
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 @@ | ||
@(sandwiches: List[Sandwich]) | ||
|
||
@main("Sandwiches") { | ||
<h1>Have a look at today's sandwiches</h1> | ||
@if(sandwiches.isEmpty) { | ||
<p>Sorry, we're sold out</p> | ||
} else { | ||
<p>Please choose a sandwich</p> | ||
@for(s <- sandwiches) { | ||
<li>@s.name, @s.description, £@s.price</li> | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package controllers | ||
|
||
import models.Sandwich | ||
import org.scalatestplus.play.PlaySpec | ||
import org.scalatestplus.play.guice.GuiceOneAppPerTest | ||
import play.api.test.FakeRequest | ||
import play.api.test.Helpers.{status, _} | ||
import services.SandwichService | ||
|
||
class SandwichControllerSpec extends PlaySpec with GuiceOneAppPerTest { | ||
"SandwichController" should { | ||
"Have some basic information and be accessible at the correct route" in { | ||
// Need to specify Host header to get through AllowedHostsFilter | ||
val request = FakeRequest(GET, "/sandwiches").withHeaders("Host" -> "localhost") | ||
val home = route(app, request).get | ||
|
||
//sanitation | ||
status(home) mustBe OK | ||
contentType(home) mustBe Some("text/html") | ||
contentAsString(home) must include("<title>Sandwiches</title>") | ||
contentAsString(home) must include("<h1>Have a look at today's sandwiches</h1>") | ||
} | ||
|
||
"give a helpful message when sold out" in { | ||
val controller = new SandwichController(FakeNoSandwichService) | ||
val result = controller.sandwiches().apply(FakeRequest()) | ||
contentAsString(result) must include("<p>Sorry, we're sold out</p>") | ||
} | ||
|
||
"show a single sandwich when only one is available" in { | ||
val controller = new SandwichController(FakeSingleSandwichService) | ||
val result = controller.sandwiches().apply(FakeRequest()) | ||
|
||
contentAsString(result) must not include("<p>Sorry, we're sold out</p>") | ||
contentAsString(result) must include ("Ham") | ||
contentAsString(result) must include ("Very tasty") | ||
contentAsString(result) must include ("£1.55") | ||
} | ||
|
||
"show multiple sandwiches when more than one is available" in { | ||
val controller = new SandwichController(FakeMultiSandwichService) | ||
val result = controller.sandwiches().apply(FakeRequest()) | ||
|
||
contentAsString(result) must not include("<p>Sorry, we're sold out</p>") | ||
contentAsString(result) must include ("Ham") | ||
contentAsString(result) must include ("Very tasty") | ||
contentAsString(result) must include ("£1.55") | ||
contentAsString(result) must include ("Cheese") | ||
contentAsString(result) must include ("Cheese tastic") | ||
contentAsString(result) must include ("£2.55") | ||
contentAsString(result) must include ("Egg") | ||
contentAsString(result) must include ("Fresh") | ||
contentAsString(result) must include ("£1.15") | ||
} | ||
} | ||
} | ||
|
||
object FakeNoSandwichService extends SandwichService { | ||
override def sandwiches(): List[Sandwich] = List() | ||
} | ||
|
||
object FakeSingleSandwichService extends SandwichService { | ||
override def sandwiches(): List[Sandwich] = List(Sandwich("Ham", 1.55, "Very tasty")) | ||
} | ||
|
||
object FakeMultiSandwichService extends SandwichService { | ||
val ham = Sandwich("Ham", 1.55, "Very tasty") | ||
val cheese = Sandwich("Cheese", 2.55, "Cheese tastic") | ||
val egg = Sandwich("Egg", 1.15, "Fresh") | ||
override def sandwiches(): List[Sandwich] = List(ham, cheese, egg) | ||
} |