Skip to content

Commit

Permalink
Specs for getting multiple statistics green
Browse files Browse the repository at this point in the history
But boy does that need refactoring...
  • Loading branch information
manuelkiessling committed Jan 20, 2016
1 parent 7263743 commit c30cc64
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 22 deletions.
10 changes: 7 additions & 3 deletions api/app/controllers/Statistics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class Statistics(statisticsRepository: Repository[StatisticsModel, String]) exte
(JsPath \ "numberOf500").write[Int]
)(unlift(StatisticsModel.unapply))

def show(testcaseId: String) = Action {
val statistics = statisticsRepository.getOneById(testcaseId)
Ok(Json.toJson(statistics))
def show(testcaseId: String, n: Int) = Action {
if (n == 1) {
Ok(Json.toJson(List(statisticsRepository.getOneById(testcaseId))))
} else {
Ok(Json.toJson(statisticsRepository.getNById(testcaseId, n)))
}

}

}
23 changes: 20 additions & 3 deletions api/app/repositories/CassandraRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package repositories

import com.datastax.driver.core.querybuilder.QueryBuilder
import com.datastax.driver.core.querybuilder.QueryBuilder._
import com.datastax.driver.core.{Row, Session}
import com.datastax.driver.core.{ResultSet, Row, Session}
import models.Model
import scala.collection.JavaConversions._

abstract class CassandraRepository[M <: Model, I](session: Session, tablename: String, partitionKeyName: String)
extends Repository[M, I] {
def rowToModel(row: Row): M

def getOneRowBySinglePartitionKeyId(partitionKeyValue: I): Row = {
def getOneRowBySinglePartitionKeyValue(partitionKeyValue: I): Row = {
val selectStmt =
select()
.from(tablename)
Expand All @@ -20,9 +21,25 @@ abstract class CassandraRepository[M <: Model, I](session: Session, tablename: S
val row = resultSet.one()
row
}

def getNBySinglePartitionKeyValue(partitionKeyValue: I, n: Int): ResultSet = {
val selectStmt =
select()
.from(tablename)
.where(QueryBuilder.eq(partitionKeyName, partitionKeyValue))
.limit(n)

session.execute(selectStmt)
}

override def getOneById(id: I): M = {
val row = getOneRowBySinglePartitionKeyId(id)
val row = getOneRowBySinglePartitionKeyValue(id)
rowToModel(row)
}

override def getNById(id: I, n: Int): List[M] = {
val rows = getNBySinglePartitionKeyValue(id, n).all().toList
rows.map(row => rowToModel(row))
}

}
1 change: 1 addition & 0 deletions api/app/repositories/Repository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import models.Model

abstract trait Repository[M <: Model, I] {
def getOneById(id: I): M
def getNById(id: I, n: Int): List[M]
}
2 changes: 1 addition & 1 deletion api/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ GET / controllers.Application.index
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)

# REST API
GET /testresults/:testcaseId/statistics/ controllers.Statistics.show(testcaseId: String)
GET /testresults/:testcaseId/statistics/latest/ controllers.Statistics.show(testcaseId: String, n: Int ?= 1)
14 changes: 10 additions & 4 deletions api/test/ApplicationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class MockStatisticsRepository extends Repository[StatisticsModel, String] {
override def getOneById(id: String): StatisticsModel = {
StatisticsModel("mocked-testresult-" + id, 987, 123, 456, 789)
}

override def getNById(id: String, n: Int): List[StatisticsModel] = {
List(
StatisticsModel("mocked-testresult-" + id, 987, 123, 456, 789)
)
}
}

class FakeApplicationComponents(context: Context) extends AppComponents(context) {
Expand Down Expand Up @@ -54,19 +60,19 @@ class ApplicationSpec extends PlaySpec with OneAppPerSuite {
contentAsString(home) must include ("Your new application is ready.")
}

"return a JSON object with statistics for a given testcase id" in {
val Some(response) = route(FakeRequest(GET, "/testresults/abcd/statistics/"))
"return a JSON array with the latest statistics entry for a given testcase id" in {
val Some(response) = route(FakeRequest(GET, "/testresults/abcd/statistics/latest/?n=1"))

status(response) mustBe OK
contentType(response) mustBe Some("application/json")
charset(response) mustBe Some("utf-8")
contentAsString(response) mustBe
"""
|{"testresultId":"mocked-testresult-abcd",
|[{"testresultId":"mocked-testresult-abcd",
|"runtimeMilliseconds":987,
|"numberOf200":123,
|"numberOf400":456,
|"numberOf500":789}
|"numberOf500":789}]
|""".stripMargin.replace("\n", "")
}
}
Expand Down
49 changes: 38 additions & 11 deletions api/test/IntegrationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IntegrationSpec extends PlaySpec with OneBrowserPerSuite with OneServerPer
|INSERT INTO statistics
| (testcase_id, testresult_id, datetime_run, runtime_milliseconds, number_of_200, number_of_400, number_of_500)
| VALUES
| ('testcase1', 'testresult1', '2016-01-07 03:32:12+0000', 333, 123, 456, 789);
| ('testcase1', 'testresult3', '2016-01-07 03:32:12+0000', 333, 123, 456, 789);
""".stripMargin
)

Expand All @@ -39,7 +39,7 @@ class IntegrationSpec extends PlaySpec with OneBrowserPerSuite with OneServerPer
|INSERT INTO statistics
| (testcase_id, testresult_id, datetime_run, runtime_milliseconds, number_of_200, number_of_400, number_of_500)
| VALUES
| ('testcase2', 'testresult1', '2016-01-07 04:32:12+0000', 333, 123, 456, 789);
| ('testcase2', 'testresult3', '2016-01-07 04:32:12+0000', 333, 123, 456, 789);
""".stripMargin
)

Expand All @@ -48,7 +48,7 @@ class IntegrationSpec extends PlaySpec with OneBrowserPerSuite with OneServerPer
|INSERT INTO statistics
| (testcase_id, testresult_id, datetime_run, runtime_milliseconds, number_of_200, number_of_400, number_of_500)
| VALUES
| ('testcase1', 'testresult1', '2016-01-07 02:32:12+0000', 222, 123, 456, 789);
| ('testcase1', 'testresult2', '2016-01-07 02:32:12+0000', 222, 122, 452, 782);
""".stripMargin
)
}
Expand All @@ -68,16 +68,43 @@ class IntegrationSpec extends PlaySpec with OneBrowserPerSuite with OneServerPer
pageSource must include ("Your new application is ready.")
}

"return a JSON object with the latest statistics for a given testcase id" in {
go to "http://localhost:" + port + "/testresults/testcase1/statistics/"
"return a JSON array with the latest statistics entry for a given testcase id" in {
go to "http://localhost:" + port + "/testresults/testcase1/statistics/latest/?n=1"
pageSource mustBe
"""
|{"testresultId":"testresult1",
|"runtimeMilliseconds":333,
|"numberOf200":123,
|"numberOf400":456,
|"numberOf500":789}
|""".stripMargin.replace("\n", "")
|[
| {
| "testresultId":"testresult3",
| "runtimeMilliseconds":333,
| "numberOf200":123,
| "numberOf400":456,
| "numberOf500":789
| }
|]
|""".stripMargin.replace("\n", "").replace(" ", "")
}

"return a JSON array with the latest N statistics entries for a given testcase id" in {
go to "http://localhost:" + port + "/testresults/testcase1/statistics/latest/?n=2"
pageSource mustBe
"""
|[
| {
| "testresultId":"testresult3",
| "runtimeMilliseconds":333,
| "numberOf200":123,
| "numberOf400":456,
| "numberOf500":789
| },
| {
| "testresultId":"testresult2",
| "runtimeMilliseconds":222,
| "numberOf200":122,
| "numberOf400":452,
| "numberOf500":782
| }
|]
|""".stripMargin.replace("\n", "").replace(" ", "")
}

}
Expand Down

0 comments on commit c30cc64

Please sign in to comment.