From 135c38f390da1a2faf983d9b2b2f0fea6a5e74b5 Mon Sep 17 00:00:00 2001 From: vlad Date: Fri, 9 Sep 2016 15:15:04 -0700 Subject: Scalacheck properties check inside of scalatest + code formatting --- src/test/scala/com/drivergrp/core/AuthTest.scala | 42 ++++++++++++---------- src/test/scala/com/drivergrp/core/CoreTest.scala | 23 ++++++------ src/test/scala/com/drivergrp/core/FileTest.scala | 38 ++++++++++---------- .../scala/com/drivergrp/core/GeneratorsTest.scala | 2 +- src/test/scala/com/drivergrp/core/TimeTest.scala | 30 +++++++++++++++- 5 files changed, 83 insertions(+), 52 deletions(-) (limited to 'src/test/scala') diff --git a/src/test/scala/com/drivergrp/core/AuthTest.scala b/src/test/scala/com/drivergrp/core/AuthTest.scala index 7725a45..0e4841b 100644 --- a/src/test/scala/com/drivergrp/core/AuthTest.scala +++ b/src/test/scala/com/drivergrp/core/AuthTest.scala @@ -13,11 +13,13 @@ class AuthTest extends FlatSpec with Matchers with MockitoSugar with ScalatestRo "'authorize' directive" should "throw error is auth token is not in the request" in { Get("/naive/attempt") ~> - auth.directives.authorize(CanSignOutReport) { authToken => complete("Never going to be here") } ~> - check { - handled shouldBe false - rejections should contain (MissingHeaderRejection("WWW-Authenticate")) - } + auth.directives.authorize(CanSignOutReport) { authToken => + complete("Never going to be here") + } ~> + check { + handled shouldBe false + rejections should contain(MissingHeaderRejection("WWW-Authenticate")) + } } it should "throw error is authorized user is not having the requested permission" in { @@ -25,13 +27,15 @@ class AuthTest extends FlatSpec with Matchers with MockitoSugar with ScalatestRo val referenceAuthToken = AuthToken(Base64("I am a pathologist's token")) Post("/administration/attempt").addHeader( - RawHeader(auth.directives.AuthenticationTokenHeader, s"Macaroon ${referenceAuthToken.value.value}") + RawHeader(auth.directives.AuthenticationTokenHeader, s"Macaroon ${referenceAuthToken.value.value}") ) ~> - auth.directives.authorize(CanAssignRoles) { authToken => complete("Never going to get here") } ~> - check { - handled shouldBe false - rejections should contain (ValidationRejection("User does not have the required permission CanAssignRoles", None)) - } + auth.directives.authorize(CanAssignRoles) { authToken => + complete("Never going to get here") + } ~> + check { + handled shouldBe false + rejections should contain(ValidationRejection("User does not have the required permission CanAssignRoles", None)) + } } it should "pass and retrieve the token to client code, if token is in request and user has permission" in { @@ -39,14 +43,14 @@ class AuthTest extends FlatSpec with Matchers with MockitoSugar with ScalatestRo val referenceAuthToken = AuthToken(Base64("I am token")) Get("/valid/attempt/?a=2&b=5").addHeader( - RawHeader(auth.directives.AuthenticationTokenHeader, s"Macaroon ${referenceAuthToken.value.value}") + RawHeader(auth.directives.AuthenticationTokenHeader, s"Macaroon ${referenceAuthToken.value.value}") ) ~> - auth.directives.authorize(CanSignOutReport) { authToken => - complete("Alright, \"" + authToken.value.value + "\" is handled") - } ~> - check { - handled shouldBe true - responseAs[String] shouldBe "Alright, \"Macaroon I am token\" is handled" - } + auth.directives.authorize(CanSignOutReport) { authToken => + complete("Alright, \"" + authToken.value.value + "\" is handled") + } ~> + check { + handled shouldBe true + responseAs[String] shouldBe "Alright, \"Macaroon I am token\" is handled" + } } } diff --git a/src/test/scala/com/drivergrp/core/CoreTest.scala b/src/test/scala/com/drivergrp/core/CoreTest.scala index b944ebb..19e685c 100644 --- a/src/test/scala/com/drivergrp/core/CoreTest.scala +++ b/src/test/scala/com/drivergrp/core/CoreTest.scala @@ -31,23 +31,22 @@ class CoreTest extends FlatSpec with Matchers with MockitoSugar { "Id" should "have equality and ordering working correctly" in { - (Id[String](1234213L) === Id[String](1234213L)) should be (true) - (Id[String](1234213L) === Id[String](213414L)) should be (false) - (Id[String](213414L) === Id[String](1234213L)) should be (false) - + (Id[String](1234213L) === Id[String](1234213L)) should be(true) + (Id[String](1234213L) === Id[String](213414L)) should be(false) + (Id[String](213414L) === Id[String](1234213L)) should be(false) Seq(Id[String](4L), Id[String](3L), Id[String](2L), Id[String](1L)).sorted should contain - theSameElementsInOrderAs (Seq(Id[String](1L), Id[String](2L), Id[String](3L), Id[String](4L))) + theSameElementsInOrderAs(Seq(Id[String](1L), Id[String](2L), Id[String](3L), Id[String](4L))) } "Name" should "have equality and ordering working correctly" in { - (Name[String]("foo") === Name[String]("foo")) should be (true) - (Name[String]("foo") === Name[String]("bar")) should be (false) - (Name[String]("bar") === Name[String]("foo")) should be (false) + (Name[String]("foo") === Name[String]("foo")) should be(true) + (Name[String]("foo") === Name[String]("bar")) should be(false) + (Name[String]("bar") === Name[String]("foo")) should be(false) Seq(Name[String]("d"), Name[String]("cc"), Name[String]("a"), Name[String]("bbb")).sorted should contain - theSameElementsInOrderAs (Seq(Name[String]("a"), Name[String]("bbb"), Name[String]("cc"), Name[String]("d"))) + theSameElementsInOrderAs(Seq(Name[String]("a"), Name[String]("bbb"), Name[String]("cc"), Name[String]("d"))) } "Revision" should "have equality working correctly" in { @@ -55,8 +54,8 @@ class CoreTest extends FlatSpec with Matchers with MockitoSugar { val bla = Revision[String]("85569dab-a3dc-401b-9f95-d6fb4162674b") val foo = Revision[String]("f54b3558-bdcd-4646-a14b-8beb11f6b7c4") - (bla === bla) should be (true) - (bla === foo) should be (false) - (foo === bla) should be (false) + (bla === bla) should be(true) + (bla === foo) should be(false) + (foo === bla) should be(false) } } diff --git a/src/test/scala/com/drivergrp/core/FileTest.scala b/src/test/scala/com/drivergrp/core/FileTest.scala index adf38e2..2c9c2c9 100644 --- a/src/test/scala/com/drivergrp/core/FileTest.scala +++ b/src/test/scala/com/drivergrp/core/FileTest.scala @@ -39,17 +39,17 @@ class FileTest extends FlatSpec with Matchers with MockitoSugar { val s3ResultsMock = mock[ListObjectsV2Result] when(s3ResultsMock.getNextContinuationToken).thenReturn("continuationToken") - when(s3ResultsMock.isTruncated).thenReturn( - false, // before file created it is empty (zero pages) - true, false, // after file is uploaded it contains this one file (one page) - false) // after file is deleted it is empty (zero pages) again + when(s3ResultsMock.isTruncated).thenReturn(false, // before file created it is empty (zero pages) + true, + false, // after file is uploaded it contains this one file (one page) + false) // after file is deleted it is empty (zero pages) again when(s3ResultsMock.getObjectSummaries).thenReturn( - // before file created it is empty, `getObjectSummaries` is never called - List[S3ObjectSummary](s3ObjectSummaryMock).asJava, // after file is uploaded it contains this one file - List.empty[S3ObjectSummary].asJava) // after file is deleted it is empty again + // before file created it is empty, `getObjectSummaries` is never called + List[S3ObjectSummary](s3ObjectSummaryMock).asJava, // after file is uploaded it contains this one file + List.empty[S3ObjectSummary].asJava) // after file is deleted it is empty again val s3ObjectMetadataMock = mock[ObjectMetadata] - val amazonS3Mock = mock[AmazonS3] + val amazonS3Mock = mock[AmazonS3] when(amazonS3Mock.listObjectsV2(any[ListObjectsV2Request]())).thenReturn(s3ResultsMock) when(amazonS3Mock.putObject(testBucket, testFilePath.toString, sourceTestFile)).thenReturn(s3PutMock) when(amazonS3Mock.getObject(any[GetObjectRequest](), any[File]())).thenReturn(s3ObjectMetadataMock) @@ -62,17 +62,17 @@ class FileTest extends FlatSpec with Matchers with MockitoSugar { Await.result(s3Storage.upload(sourceTestFile, testFilePath), 10 seconds) val filesAfterUpload = Await.result(s3Storage.list(testDirPath).run, 10 seconds) - filesAfterUpload.size should be (1) + filesAfterUpload.size should be(1) val uploadedFileLine = filesAfterUpload.head - uploadedFileLine.name should be (Name[File](testFileName)) - uploadedFileLine.location should be (testFilePath) + uploadedFileLine.name should be(Name[File](testFileName)) + uploadedFileLine.location should be(testFilePath) uploadedFileLine.revision.id.length should be > 0 uploadedFileLine.lastModificationDate.millis should be > 0L val downloadedFile = Await.result(s3Storage.download(testFilePath).run, 10 seconds) downloadedFile shouldBe defined downloadedFile.foreach { - _.getAbsolutePath.endsWith(testFilePath.toString) should be (true) + _.getAbsolutePath.endsWith(testFilePath.toString) should be(true) } Await.result(s3Storage.delete(testFilePath), 10 seconds) @@ -98,16 +98,16 @@ class FileTest extends FlatSpec with Matchers with MockitoSugar { Await.result(fileStorage.upload(sourceTestFile, testFilePath), 10 seconds) val filesAfterUpload = Await.result(fileStorage.list(testDirPath).run, 10 seconds) - filesAfterUpload.size should be (1) + filesAfterUpload.size should be(1) val uploadedFileLine = filesAfterUpload.head - uploadedFileLine.name should be (Name[File]("uploadTestFile")) - uploadedFileLine.location should be (testFilePath) + uploadedFileLine.name should be(Name[File]("uploadTestFile")) + uploadedFileLine.location should be(testFilePath) uploadedFileLine.revision.id.length should be > 0 uploadedFileLine.lastModificationDate.millis should be > 0L val downloadedFile = Await.result(fileStorage.download(testFilePath).run, 10 seconds) downloadedFile shouldBe defined - downloadedFile.map(_.getAbsolutePath) should be (Some(testFilePath.toString)) + downloadedFile.map(_.getAbsolutePath) should be(Some(testFilePath.toString)) Await.result(fileStorage.delete(testFilePath), 10 seconds) @@ -117,9 +117,9 @@ class FileTest extends FlatSpec with Matchers with MockitoSugar { private def generateTestLocalFile(path: String): File = { val randomSourceFolderName = java.util.UUID.randomUUID().toString - val sourceTestFile = new File(Paths.get(path, randomSourceFolderName, "uploadTestFile").toString) - sourceTestFile.getParentFile.mkdirs() should be (true) - sourceTestFile.createNewFile() should be (true) + val sourceTestFile = new File(Paths.get(path, randomSourceFolderName, "uploadTestFile").toString) + sourceTestFile.getParentFile.mkdirs() should be(true) + sourceTestFile.createNewFile() should be(true) using(new java.io.PrintWriter(sourceTestFile)) { _.append("Test File Contents") } sourceTestFile } diff --git a/src/test/scala/com/drivergrp/core/GeneratorsTest.scala b/src/test/scala/com/drivergrp/core/GeneratorsTest.scala index 84a8635..631149e 100644 --- a/src/test/scala/com/drivergrp/core/GeneratorsTest.scala +++ b/src/test/scala/com/drivergrp/core/GeneratorsTest.scala @@ -47,7 +47,7 @@ class GeneratorsTest extends FlatSpec with Matchers with Assertions { it should "be able to generate proper UUIDs" in { nextUuid() should not be nextUuid() - nextUuid().toString.length should be (36) + nextUuid().toString.length should be(36) } it should "be able to generate new Revisions" in { diff --git a/src/test/scala/com/drivergrp/core/TimeTest.scala b/src/test/scala/com/drivergrp/core/TimeTest.scala index e6cca45..b928413 100644 --- a/src/test/scala/com/drivergrp/core/TimeTest.scala +++ b/src/test/scala/com/drivergrp/core/TimeTest.scala @@ -3,11 +3,18 @@ package com.drivergrp.core import java.util.TimeZone import com.drivergrp.core.time.{Time, _} +import org.scalacheck.{Arbitrary, Gen} import org.scalatest.{FlatSpec, Matchers} +import org.scalatest.prop.Checkers +import org.scalacheck.Arbitrary._ +import org.scalacheck.Prop.BooleanOperators import scala.concurrent.duration._ -class TimeTest extends FlatSpec with Matchers { +class TimeTest extends FlatSpec with Matchers with Checkers { + + implicit val arbitraryDuration = Arbitrary[Duration](Gen.chooseNum(0L, 9999999999L).map(_.milliseconds)) + implicit val arbitraryTime = Arbitrary[Time](Gen.chooseNum(0L, 9999999999L).map(millis => Time(millis))) "Time" should "have correct methods to compare" in { @@ -15,14 +22,24 @@ class TimeTest extends FlatSpec with Matchers { Time(123L).isAfter(Time(123L)) should be(false) Time(123L).isAfter(Time(234L)) should be(false) + check((a: Time, b: Time) => (a.millis > b.millis) ==> a.isAfter(b)) + Time(234L).isBefore(Time(123L)) should be(false) Time(123L).isBefore(Time(123L)) should be(false) Time(123L).isBefore(Time(234L)) should be(true) + + check { (a: Time, b: Time) => + (a.millis < b.millis) ==> a.isBefore(b) + } } it should "not modify time" in { Time(234L).millis should be(234L) + + check { millis: Long => + Time(millis).millis == millis + } } it should "support arithmetic with scala.concurrent.duration" in { @@ -30,12 +47,23 @@ class TimeTest extends FlatSpec with Matchers { Time(123L).advanceBy(0 minutes).millis should be(123L) Time(123L).advanceBy(1 second).millis should be(123L + Second) Time(123L).advanceBy(4 days).millis should be(123L + 4 * Days) + + check { (time: Time, duration: Duration) => + time.advanceBy(duration).millis == (time.millis + duration.toMillis) + } } it should "have ordering defined correctly" in { Seq(Time(321L), Time(123L), Time(231L)).sorted should contain theSameElementsInOrderAs Seq(Time(123L), Time(231L), Time(321L)) + + check { times: List[Time] => + times.sorted.sliding(2).filter(_.size == 2).forall { + case Seq(a, b) => + a.millis <= b.millis + } + } } it should "reset to the start of the period, e.g. month" in { -- cgit v1.2.3