Skip to content

Commit

Permalink
Finalized app by making use of the repo and changing specs accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelkiessling committed Jan 18, 2016
1 parent 4e707cb commit 796b6c6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/controllers/Application.scala
Expand Up @@ -8,7 +8,8 @@ import repositories.Repository
class Application(productsRepository: Repository[ProductModel, Int]) extends Controller {

def index = Action {
Ok(views.html.index("Your new application is ready."))
val product = productsRepository.getOneById(1)
Ok(views.html.index(s"Your new application is ready. The name of product #1 is ${product.name}."))
}

}
35 changes: 33 additions & 2 deletions test/ApplicationSpec.scala
@@ -1,9 +1,40 @@
import java.io.File
import models.ProductModel
import play.api
import play.api.{Mode, Environment, ApplicationLoader}
import play.api.ApplicationLoader.Context
import play.api.test._
import play.api.test.Helpers._
import org.scalatestplus.play._
import repositories.Repository

class MockProductsRepository extends Repository[ProductModel, Int] {
override def getOneById(id: Int): ProductModel = {
ProductModel(1, "Mocked Chair")
}
}

class FakeApplicationComponents(context: Context) extends AppComponents(context) {
override lazy val productsRepository = new MockProductsRepository()
}

class FakeAppLoader extends ApplicationLoader {
override def load(context: Context): api.Application =
new FakeApplicationComponents(context).application
}

class ApplicationSpec extends PlaySpec with OneAppPerSuite {

override implicit lazy val app: api.Application = {
val appLoader = new FakeAppLoader
appLoader.load(
ApplicationLoader.createContext(
new Environment(
new File("."), ApplicationLoader.getClass.getClassLoader, Mode.Test)
)
)
}

"Application" should {

"send 404 on a bad request" in {
Expand All @@ -12,12 +43,12 @@ class ApplicationSpec extends PlaySpec with OneAppPerSuite {
status(wrongRoute) mustBe NOT_FOUND
}

"render the index page" in {
"render the index page and tell us about the first product" in {
val Some(home) = route(FakeRequest(GET, "/"))

status(home) mustBe OK
contentType(home) mustBe Some("text/html")
contentAsString(home) must include ("Your new application is ready.")
contentAsString(home) must include ("Your new application is ready. The name of product #1 is Mocked Chair.")
}
}

Expand Down
27 changes: 24 additions & 3 deletions test/IntegrationSpec.scala
@@ -1,14 +1,35 @@
import java.io.File
import cassandra.{CassandraConnector, CassandraConnectionUri}
import org.scalatest.BeforeAndAfter
import org.scalatestplus.play._
import play.api
import play.api.{Mode, Environment, ApplicationLoader}

class IntegrationSpec extends PlaySpec with OneBrowserPerSuite with OneServerPerSuite with HtmlUnitFactory {
class IntegrationSpec extends PlaySpec with OneBrowserPerSuite with OneServerPerSuite with HtmlUnitFactory with BeforeAndAfter {

before {
val uri = CassandraConnectionUri("cassandra://localhost:9042/test")
val session = CassandraConnector.createSessionAndInitKeyspace(uri)
val query = "INSERT INTO products (id, name) VALUES (1, 'Chair');"
session.execute(query)
session.close()
}

override implicit lazy val app: api.Application =
new AppLoader().load(
ApplicationLoader.createContext(
new Environment(
new File("."), ApplicationLoader.getClass.getClassLoader, Mode.Test)
)
)

"Application" should {

"work from within a browser" in {
"work from within a browser and tell us about the first product" in {

go to "http://localhost:" + port

pageSource must include ("Your new application is ready.")
pageSource must include ("Your new application is ready. The name of product #1 is Chair.")
}
}
}

0 comments on commit 796b6c6

Please sign in to comment.