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

Canonical collections #84

Merged
merged 3 commits into from
Mar 18, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ case class FrontJson(
imageHeight: Option[Int],
isImageDisplayed: Option[Boolean],
priority: Option[String],
isHidden: Option[Boolean]
isHidden: Option[Boolean],
canonical: Option[String]
)

object ConfigJson {
Expand Down
28 changes: 25 additions & 3 deletions fapi-client/src/main/scala/com/gu/facia/api/models/front.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ case class Front(
frontImage: Option[FrontImage],
isImageDisplayed: Boolean,
priority: FrontPriority,
isHidden: Boolean)
isHidden: Boolean,
canonicalCollection: String)

object Front {
private def getFrontPriority(frontJson: FrontJson): FrontPriority =
Expand All @@ -39,7 +40,7 @@ object Front {
imageWidth <- frontJson.imageWidth
} yield FrontImage(imageUrl, imageHeight, imageWidth)

def fromFrontJson(id: String, frontJson: FrontJson): Front =
def fromFrontJson(id: String, frontJson: FrontJson): Front = {
Front(
id,
frontJson.collections,
Expand All @@ -51,8 +52,29 @@ object Front {
getImageUrl(frontJson),
frontJson.isImageDisplayed.getOrElse(false),
getFrontPriority(frontJson),
frontJson.isHidden.getOrElse(false)
frontJson.isHidden.getOrElse(false),
canonicalCollection(id, frontJson)
)
}

/**
* If we're on a network front, try hard-coded headlines ids, otherwise use editorially
* chosen canonical container if present, falling back to the first available collection.
* We should never have a front with no containers so final fallback is a placeholder.
*/
private def canonicalCollection(id: String, frontJson: FrontJson): String = {
val frontHeadlineCollections = id match {
// PROD CODE
case "uk" => List("uk-alpha/news/regular-stories", "f3d7d2bc-e667-4a86-974f-fe27daeaebcc")
case "us" => List("us-alpha/news/regular-stories")
case "au" => List("au-alpha/news/regular-stories")
case _ => Nil
}
frontHeadlineCollections.find(frontJson.collections.contains)
.orElse(frontJson.canonical.filter(frontJson.collections.contains))
.orElse(frontJson.collections.headOption)
.getOrElse("no collections")
}

def frontsFromConfig(configJson: ConfigJson): Set[Front] = {
configJson.fronts
Expand Down
52 changes: 52 additions & 0 deletions fapi-client/src/test/scala/com/gu/facia/api/models/FrontTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.gu.facia.api.models

import com.gu.facia.client.models.FrontJson
import org.scalatest._
import org.scalatest.mock.MockitoSugar

class FrontTest extends FreeSpec with ShouldMatchers with MockitoSugar with OneInstancePerTest {
"fromFrontJson" - {
"when generating the canonical field" - {
val frontJson = FrontJson(
collections = List("collection1", "collection2", "collection3", "collection4"),
None, None, None, None, None, None, None, None, None, None, None, None
)

"uses the fronts config field if present" in {
val frontJsonWithCanonical = frontJson.copy(canonical = Some("collection2"))
Front.fromFrontJson("frontId", frontJsonWithCanonical).canonicalCollection should equal("collection2")
}

"in the absence of a fronts config field, takes the first present collection" in {
Front.fromFrontJson("frontId", frontJson).canonicalCollection should equal("collection1")
}

"on the uk front, takes the editorially-chosen PROD uk headlines collection if it is present" in {
val ukFrontJson = frontJson.copy(collections = frontJson.collections :+ "uk-alpha/news/regular-stories")
Front.fromFrontJson("uk", ukFrontJson).canonicalCollection should equal("uk-alpha/news/regular-stories")
}

"on the uk front, takes the first collection if the editorially-chosen uk headlines collection is not present" in {
Front.fromFrontJson("uk", frontJson).canonicalCollection should equal("collection1")
}

"on the us front, takes the editorially-chosen PROD us headlines collection if it is present" in {
val usFrontJson = frontJson.copy(collections = frontJson.collections :+ "us-alpha/news/regular-stories")
Front.fromFrontJson("us", usFrontJson).canonicalCollection should equal("us-alpha/news/regular-stories")
}

"on the us front, takes the first collection if the editorially-chosen us headlines collection is not present" in {
Front.fromFrontJson("us", frontJson).canonicalCollection should equal("collection1")
}

"on the au front, takes the editorially-chosen PROD au headlines collection if it is present" in {
val auFrontJson = frontJson.copy(collections = frontJson.collections :+ "au-alpha/news/regular-stories")
Front.fromFrontJson("au", auFrontJson).canonicalCollection should equal("au-alpha/news/regular-stories")
}

"on the au front, takes the first collection if the editorially-chosen au headlines collection is not present" in {
Front.fromFrontJson("au", frontJson).canonicalCollection should equal("collection1")
}
}
}
}