-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update tests to use new caching ApiClient
- Loading branch information
Showing
9 changed files
with
79 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 36 additions & 11 deletions
47
facia-json/src/test/scala/com/gu/facia/client/ApiClientSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,58 @@ | ||
package com.gu.facia.client | ||
|
||
import com.gu.etagcaching.aws.s3.ObjectId | ||
import com.gu.etagcaching.fetching.{ETaggedData, Fetching, Missing, MissingOrETagged} | ||
import com.gu.facia.client.lib.ResourcesHelper | ||
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 | ||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.hashing.MurmurHash3 | ||
|
||
object FakeS3Fetching extends Fetching[ObjectId, Array[Byte]] with ResourcesHelper { | ||
private def pretendETagFor(bytes: Array[Byte]): String = MurmurHash3.bytesHash(bytes).toHexString | ||
|
||
override def fetch(objectId: ObjectId)(implicit ec: ExecutionContext): Future[MissingOrETagged[Array[Byte]]] = Future { | ||
slurpBytes(objectId.key).fold(Missing: MissingOrETagged[Array[Byte]]) { bytes => | ||
ETaggedData(pretendETagFor(bytes), bytes) | ||
} | ||
} | ||
|
||
override def fetchOnlyIfETagChanged(objectId: ObjectId, oldETag: String)(implicit ec: ExecutionContext): Future[Option[MissingOrETagged[Array[Byte]]]] = { | ||
fetch(objectId).map { | ||
case taggedData: ETaggedData[_] => | ||
Option.unless(oldETag == taggedData.eTag)(taggedData) // simulate a Not-Modified response, if there's no change in ETag | ||
case x => Some(x) | ||
} | ||
} | ||
} | ||
|
||
class ApiClientSpec extends AnyFlatSpec with Matchers with OptionValues with ScalaFutures with IntegrationPatience { | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
object FakeS3Client extends S3Client with ResourcesHelper { | ||
override def get(bucket: String, path: String): Future[FaciaResult] = Future { | ||
slurpOrDie(path) | ||
} | ||
} | ||
|
||
val client: ApiClient = ApiClient("not used", "DEV", FakeS3Client) | ||
val legacyClient: ApiClient = ApiClient("not used", "DEV", FakeS3Client) | ||
val cachingClient: ApiClient = ApiClient.withCaching("not used", Environment.Dev, FakeS3Fetching) | ||
|
||
"ApiClient" should "fetch the config" in { | ||
val config = client.config.futureValue | ||
for ((name, client) <- Map("legacy" -> legacyClient, "caching" -> cachingClient)) { | ||
s"$name ApiClient" should "fetch the config" in { | ||
val config = client.config.futureValue | ||
|
||
config.collections should have size 334 | ||
config.fronts should have size 79 | ||
} | ||
config.collections should have size 334 | ||
config.fronts should have size 79 | ||
} | ||
|
||
it should "fetch a collection" in { | ||
val collectionOpt = client.collection("2409-31b3-83df0-de5a").futureValue | ||
it should "fetch a collection" in { | ||
val collectionOpt = cachingClient.collection("2409-31b3-83df0-de5a").futureValue | ||
|
||
collectionOpt.value.live should have size 8 | ||
collectionOpt.value.live should have size 8 | ||
} | ||
} | ||
} |
9 changes: 1 addition & 8 deletions
9
facia-json/src/test/scala/com/gu/facia/client/lib/ApiTestClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
facia-json/src/test/scala/com/gu/facia/client/lib/ResourcesHelper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
package com.gu.facia.client.lib | ||
|
||
import com.gu.facia.client.FaciaSuccess | ||
import org.apache.commons.io.IOUtils | ||
|
||
import java.nio.charset.StandardCharsets.UTF_8 | ||
|
||
trait ResourcesHelper { | ||
def slurpBytes(path: String): Option[Array[Byte]] = | ||
Option(getClass.getClassLoader.getResource(path)).map(url => IOUtils.toByteArray(url.openStream())) | ||
|
||
def slurp(path: String): Option[String] = | ||
Option(getClass.getClassLoader.getResource(path)).map(scala.io.Source.fromURL(_).mkString) | ||
slurpBytes(path).map(bytes => new String(bytes, UTF_8)) | ||
|
||
def slurpOrDie(path: String) = slurp(path).map(_.getBytes).map(FaciaSuccess.apply).getOrElse { | ||
def slurpOrDie(path: String) = slurpBytes(path).map(FaciaSuccess.apply).getOrElse { | ||
throw new RuntimeException(s"Required resource $path not on class path") } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters