Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Specs2, consolidate on ScalaTest #292

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def baseProject(module: String, majorMinorVersion: String) = Project(s"$module-p
"-deprecation",
"-Xfatal-warnings"
),
libraryDependencies += scalaTest,
publishTo := sonatypePublishToBundle.value,
sonatypeReleaseSettings
)
Expand All @@ -86,7 +87,6 @@ def faciaJson_playJsonVersion(majorMinorVersion: String) = baseProject("facia-js
libraryDependencies ++= Seq(
awsSdk,
commonsIo,
specs2,
"com.typesafe.play" %% "play-json" % exactPlayJsonVersions(majorMinorVersion),
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0",
scalaLogging
Expand All @@ -99,7 +99,7 @@ def fapiClient_playJsonVersion(majorMinorVersion: String) = baseProject("fapi-c
contentApi,
contentApiDefault,
commercialShared,
scalaTest,
scalaTestMockito,
mockito
)
)
Expand Down
32 changes: 16 additions & 16 deletions facia-json/src/test/scala/com/gu/facia/client/ApiClientSpec.scala
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package com.gu.facia.client

import org.specs2.mutable.Specification
import scala.concurrent.Future
import scala.concurrent.Await
import com.gu.facia.client.lib.ResourcesHelper
import scala.concurrent.duration.Duration
import org.scalatest.OptionValues
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class ApiClientSpec extends Specification {
class ApiClientSpec extends AnyFlatSpec with Matchers with OptionValues with ScalaFutures with IntegrationPatience {
object FakeS3Client extends S3Client with ResourcesHelper {
override def get(bucket: String, path: String): Future[FaciaResult] = Future {
slurpOrDie(path)
}
}

val client = ApiClient("not used", "DEV", FakeS3Client)
val client: ApiClient = ApiClient("not used", "DEV", FakeS3Client)

"ApiClient" should {
"fetch the config" in {
/** Nasty. PlaySpecification gives you a way of avoiding using Await ... TODO, maybe import that? */
val config = Await.result(client.config, Duration.Inf)
"ApiClient" should "fetch the config" in {
val config = client.config.futureValue

(config.collections.size mustEqual 334) and (config.fronts.size mustEqual 79)
}
config.collections should have size 334
config.fronts should have size 79
}

"fetch a collection" in {
val collection = Await.result(client.collection("2409-31b3-83df0-de5a"), Duration.Inf)
it should "fetch a collection" in {
val collectionOpt = client.collection("2409-31b3-83df0-de5a").futureValue

collection must beSome.which(_.live must haveLength(8))
}
collectionOpt.value.live should have size 8
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.gu.facia.client.models

import org.specs2.mutable.Specification
import play.api.libs.json.{JsString, JsSuccess, JsError, Json}
import com.gu.facia.client.lib.ResourcesHelper
import org.joda.time.{DateTimeZone, DateTime}
import org.joda.time.{DateTime, DateTimeZone}
import org.scalatest.OptionValues
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.{JsError, JsString, JsSuccess, Json}

class CollectionSpec extends Specification with ResourcesHelper {
class CollectionSpec extends AnyFlatSpec with Matchers with OptionValues with ResourcesHelper {
def getCollectionFixture(path: String) = Json.fromJson[CollectionJson](
Json.parse(slurpOrDie(path).get)
) match {
Expand All @@ -14,105 +16,95 @@ class CollectionSpec extends Specification with ResourcesHelper {
throw new RuntimeException("ARGH")
}

"Collection" should {
"deserialize" in {
val collection = getCollectionFixture("DEV/frontsapi/collection/2409-31b3-83df0-de5a/collection.json")

collection.live must haveLength(8)

collection.live.find(_.id == "football/quiz/2014/jun/11/world-cup-2014-the-ultimate-world-cup-trivia-quiz") must
beSome.which({ front =>
(front.frontPublicationDate mustEqual 1402500092818L) and (front.meta mustEqual Some(TrailMetaData.withDefaults(
("headline", JsString("The ultimate World Cup trivia quiz")),
("group", JsString("0"))
)))
})

collection.lastUpdated mustEqual new DateTime(2014, 6, 12, 8, 30, 20, 67, DateTimeZone.UTC)

collection.updatedBy mustEqual "Katherine Le Ruez"

collection.updatedEmail mustEqual "katherine.leruez@guardian.co.uk"
}

"deserialize content with image src widths and heights attached" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/uk-alpha/news/regular-stories/collection2.json")

collection.live.lift(1) must beSome.which({ item =>
item.safeMeta.imageSrcWidth mustEqual Some("940") and (item.safeMeta.imageSrcHeight mustEqual Some("564"))
})
}

"deserialize content with slideshows, where captions are optional" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/uk-alpha/news/regular-stories/collection-with-captions.json")

collection.live.lift(0) must beSome.which({ item =>
item.safeMeta.slideshow mustEqual Some(
List(
SlideshowAsset(
"http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/8/4/1407146294410/PrinceWilliamCatherineDuche.jpg",
"940",
"720"
),
SlideshowAsset(
"http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/8/4/1407140556976/AustraliansingerKylieMinogu.jpg",
"940",
"720",
Some("Kylie Minogue")
)
)
)
})
}

"deserialize content without metadata" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/754c-8e8c-fad9-a927/collection.json")

collection.live.lift(2) must beSome.which({ item =>
item.meta mustEqual None
})
}

"deserialize content with supporting items" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/uk-alpha/news/regular-stories/collection.json")

collection.live.headOption must beSome.which({ item =>
item.safeMeta.supporting must beSome.which({ supportingContent =>
supportingContent must haveLength(3)

supportingContent.lift(1) must beSome.which({ item =>
item.id mustEqual "internal-code/content/442568601"
item.publishedBy mustEqual Some("user1")

item.meta must beSome.which({ meta =>
meta.group mustEqual Some("2")

meta.headline mustEqual Some("Special report: Scores killed in deadliest assault in Gaza so far")
})
})
})
})

"be able to serialize and deserialize the same object" in {
val collectionJson = CollectionJson(
live = List(Trail("id-123", DateTime.now.getMillis, None, None)),
draft = None,
treats = None,
lastUpdated = DateTime.now(),
updatedBy = "A test",
updatedEmail = "a test email",
displayName = None,
href = None,
previously = None,
targetedTerritory = None
"Collection" should "deserialize" in {
val collection = getCollectionFixture("DEV/frontsapi/collection/2409-31b3-83df0-de5a/collection.json")

collection.live should have size 8

val front = collection.live.find(_.id == "football/quiz/2014/jun/11/world-cup-2014-the-ultimate-world-cup-trivia-quiz").value

front.frontPublicationDate shouldBe 1402500092818L
front.meta.value shouldBe TrailMetaData.withDefaults(
("headline", JsString("The ultimate World Cup trivia quiz")),
("group", JsString("0"))
)

collection.lastUpdated shouldBe new DateTime(2014, 6, 12, 8, 30, 20, 67, DateTimeZone.UTC)

collection.updatedBy shouldBe "Katherine Le Ruez"

collection.updatedEmail shouldBe "katherine.leruez@guardian.co.uk"
}

it should "deserialize content with image src widths and heights attached" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/uk-alpha/news/regular-stories/collection2.json")

val itemMeta = collection.live.lift(1).value.safeMeta
itemMeta.imageSrcWidth.value shouldBe "940"
itemMeta.imageSrcHeight.value shouldBe "564"
}

it should "deserialize content with slideshows, where captions are optional" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/uk-alpha/news/regular-stories/collection-with-captions.json")

val item = collection.live.lift(0).value
item.safeMeta.slideshow shouldBe Some(
List(
SlideshowAsset(
"http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/8/4/1407146294410/PrinceWilliamCatherineDuche.jpg",
"940",
"720"
),
SlideshowAsset(
"http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/8/4/1407140556976/AustraliansingerKylieMinogu.jpg",
"940",
"720",
Some("Kylie Minogue")
)
)
)
}

it should "deserialize content without metadata" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/754c-8e8c-fad9-a927/collection.json")

collection.live.lift(2).value.meta shouldBe None
}

it should "deserialize content with supporting items" in {
val collection = getCollectionFixture("PROD/frontsapi/collection/uk-alpha/news/regular-stories/collection.json")

val supportingContent = collection.live.headOption.value.safeMeta.supporting.value
supportingContent should have size 3

val collectionJsonAsString = Json.stringify(Json.toJson(collectionJson))
val newCollectionJson = Json.parse(collectionJsonAsString).as[CollectionJson]
val item = supportingContent.lift(1).value
item.id shouldBe "internal-code/content/442568601"
item.publishedBy shouldBe Some("user1")

newCollectionJson.lastUpdated should beEqualTo (collectionJson.lastUpdated)
newCollectionJson should beEqualTo (collectionJson)
}
}
val meta = item.meta.value
meta.group shouldBe Some("2")

meta.headline shouldBe Some("Special report: Scores killed in deadliest assault in Gaza so far")
}

it should "be able to serialize and deserialize the same object" in {
val collectionJson = CollectionJson(
live = List(Trail("id-123", DateTime.now.getMillis, None, None)),
draft = None,
treats = None,
lastUpdated = DateTime.now(),
updatedBy = "A test",
updatedEmail = "a test email",
displayName = None,
href = None,
previously = None,
targetedTerritory = None
)

val collectionJsonAsString = Json.stringify(Json.toJson(collectionJson))
val newCollectionJson = Json.parse(collectionJsonAsString).as[CollectionJson]

newCollectionJson.lastUpdated shouldBe collectionJson.lastUpdated
newCollectionJson shouldBe collectionJson
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package com.gu.facia.client.models

import com.gu.facia.client.lib.ResourcesHelper
import org.specs2.mutable.Specification
import org.scalatest.OptionValues
import play.api.libs.json.Json
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ConfigSpec extends Specification with ResourcesHelper {
"Config" should {
"deserialize" in {
class ConfigSpec extends AnyFlatSpec with Matchers with OptionValues with ResourcesHelper {
"Config" should "deserialize" in {
val config = Json.fromJson[ConfigJson](Json.parse(slurpOrDie("DEV/frontsapi/config/config.json").get)).get

config.collections.get("uk/commentisfree/most-viewed/regular-stories") must beSome.which({ collection =>
(collection.displayName must beSome.which(_ == "Most popular")) and
(collection.`type` must beSome.which(_ == "news/most-popular")) and
(collection.uneditable must beSome.which(identity)) and
(collection.backfill must beSome.which({ backfill =>
(backfill.`type` == "capi") and
(backfill.query == "uk/commentisfree?show-most-viewed=true&show-editors-picks=false&hide-recent-content=true")
}))
})
val collection = config.collections.get("uk/commentisfree/most-viewed/regular-stories").value

config.fronts.get("lifeandstyle/home-and-garden") must beSome.which({ front =>
(front.collections must haveLength(2)) and
(front.title must beSome.which(_ == "Home, interiors and gardening news, comment and advice")) and
(front.description must beSome.which(_ == "Latest news, comment and advice on homes, interior design, " +
"decorating and gardening from the Guardian, the world's leading liberal voice"))
})
collection.displayName.value shouldBe "Most popular"
collection.`type`.value shouldBe "news/most-popular"
collection.uneditable.value shouldBe true
val backfill = collection.backfill.value
backfill.`type` shouldBe "capi"
backfill.query shouldBe "uk/commentisfree?show-most-viewed=true&show-editors-picks=false&hide-recent-content=true"

val front = config.fronts.get("lifeandstyle/home-and-garden").value

front.collections should have length 2
front.title.value shouldBe "Home, interiors and gardening news, comment and advice"
front.description.value shouldBe "Latest news, comment and advice on homes, interior design, " +
"decorating and gardening from the Guardian, the world's leading liberal voice"
}
"deserialize territories" in {

it should "deserialize territories" in {
val config = Json.fromJson[ConfigJson](Json.parse(slurpOrDie("DEV/frontsapi/config/config.json").get)).get

config.collections.get("au/sport/golf/regular-stories") must beSome.which({ collection =>
collection.targetedTerritory.get mustEqual(EU27Territory)
})
config.collections.get("au/sport/golf/regular-stories").value.targetedTerritory.value shouldBe EU27Territory
}
"deserialize unsupported territories as unknown" in {

it should "deserialize unsupported territories as unknown" in {

val configJson = """{
| "displayName" : "Golf",
Expand All @@ -46,19 +46,15 @@ class ConfigSpec extends Specification with ResourcesHelper {
| "targetedTerritory": "Made-Up-Territory"
| }""".stripMargin
val parsed = Json.fromJson[CollectionConfigJson](Json.parse(configJson))
parsed.asOpt must beSome.which({ config =>
config.targetedTerritory must beSome(UnknownTerritory: TargetedTerritory)
})
parsed.asOpt.value.targetedTerritory.value shouldBe UnknownTerritory
}

"serialize territories" in {
val config = Json.fromJson[ConfigJson](Json.parse(slurpOrDie("DEV/frontsapi/config/config.json").get)).get
it should "serialize territories" in {
val config = Json.fromJson[ConfigJson](Json.parse(slurpOrDie("DEV/frontsapi/config/config.json").get)).get

config.collections.get("uk/commentisfree/most-viewed/regular-stories") must beSome.which({ collection =>
val collectionWithTerritory = collection.copy(targetedTerritory = Some(NZTerritory))
val json = Json.toJson(collectionWithTerritory).toString()
json mustEqual """{"displayName":"Most popular","backfill":{"type":"capi","query":"uk/commentisfree?show-most-viewed=true&show-editors-picks=false&hide-recent-content=true"},"type":"news/most-popular","uneditable":true,"targetedTerritory":"NZ"}"""
})
}
val collection = config.collections.get("uk/commentisfree/most-viewed/regular-stories").value
val collectionWithTerritory = collection.copy(targetedTerritory = Some(NZTerritory))
val json = Json.toJson(collectionWithTerritory).toString()
json shouldBe """{"displayName":"Most popular","backfill":{"type":"capi","query":"uk/commentisfree?show-most-viewed=true&show-editors-picks=false&hide-recent-content=true"},"type":"news/most-popular","uneditable":true,"targetedTerritory":"NZ"}"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.gu.facia.api

import org.scalatest.concurrent.ScalaFutures
import org.scalatest._
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

import scala.concurrent.ExecutionContext.Implicits.global

class ResponseTest extends FreeSpec with Matchers with ScalaFutures {
class ResponseTest extends AnyFreeSpec with Matchers with ScalaFutures {

"Response.filter" - {
"should return the same result if the filter function returns true" in {
Expand Down
Loading
Loading