Skip to content
Permalink
Browse files
Finalized app by making use of the repo and changing specs accordingly
  • 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.
@@ -8,7 +8,8 @@ import repositories.Repository
class Application(productsRepository: Repository[ProductModel, Int]) extends Controller { class Application(productsRepository: Repository[ProductModel, Int]) extends Controller {


def index = Action { 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}."))
} }


} }
@@ -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._
import play.api.test.Helpers._ import play.api.test.Helpers._
import org.scalatestplus.play._ 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 { 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 { "Application" should {


"send 404 on a bad request" in { "send 404 on a bad request" in {
@@ -12,12 +43,12 @@ class ApplicationSpec extends PlaySpec with OneAppPerSuite {
status(wrongRoute) mustBe NOT_FOUND 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, "/")) val Some(home) = route(FakeRequest(GET, "/"))


status(home) mustBe OK status(home) mustBe OK
contentType(home) mustBe Some("text/html") 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.")
} }
} }


@@ -1,14 +1,35 @@
import java.io.File
import cassandra.{CassandraConnector, CassandraConnectionUri}
import org.scalatest.BeforeAndAfter
import org.scalatestplus.play._ 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 { "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 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.