Skip to content

Commit

Permalink
#8 test show template
Browse files Browse the repository at this point in the history
  • Loading branch information
FRosner committed Jul 13, 2016
1 parent d428fc7 commit 88083c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
9 changes: 4 additions & 5 deletions app/de/frosner/broccoli/controllers/TemplateController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import javax.inject.Inject
import de.frosner.broccoli.models.Template
import Template.templateWrites
import de.frosner.broccoli.services.TemplateService

import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}
import play.api.mvc.{Action, AnyContent, Controller}


class TemplateController @Inject() (templateService: TemplateService) extends Controller {

private val templates = templateService.templates

def list = Action {
def list: Action[AnyContent] = Action {
Ok(Json.toJson(templateService.templates))
}

def show(id: String) = Action {
templates.find(_.id == id).map(template => Ok(Json.toJson(template))).getOrElse(NotFound)
def show(id: String): Action[AnyContent] = Action {
templateService.template(id).map(template => Ok(Json.toJson(template))).getOrElse(NotFound)
}

}
36 changes: 34 additions & 2 deletions test/de/frosner/broccoli/controllers/TemplateControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import play.api.mvc.BodyParsers

class TemplateControllerSpec extends PlaySpecification {

"/templates" should {
"list templates" should {

"list all available templates" in { implicit ee: ExecutionEnv =>
val templateService = mock(classOf[TemplateService])
Expand All @@ -22,7 +22,7 @@ class TemplateControllerSpec extends PlaySpecification {
)
when(templateService.templates).thenReturn(Seq(template))
val controller = new TemplateController(templateService)

val result = controller.list.apply(FakeRequest())
status(result) must be equalTo 200
contentAsJson(result) must be equalTo JsArray(Seq(
Expand All @@ -36,4 +36,36 @@ class TemplateControllerSpec extends PlaySpecification {

}

"show template" should {

"return the template if it exists" in { implicit ee: ExecutionEnv =>
val templateService = mock(classOf[TemplateService])
val template = Template(
id = "id",
template = "template {{name}}",
description = "description"
)
when(templateService.template("id")).thenReturn(Some(template))
val controller = new TemplateController(templateService)

val result = controller.show("id").apply(FakeRequest())
status(result) must be equalTo 200
contentAsJson(result) must be equalTo JsObject(Map(
"id" -> JsString(template.id),
"parameters" -> JsArray(Seq(JsString("name"))),
"description" -> JsString(template.description)
))
}

"return 404 if the template does not exist" in {
val templateService = mock(classOf[TemplateService])
when(templateService.template("id")).thenReturn(None)
val controller = new TemplateController(templateService)

val result = controller.show("id").apply(FakeRequest())
status(result) must be equalTo 404
}

}

}

0 comments on commit 88083c8

Please sign in to comment.