Skip to content

Commit 0dd631c

Browse files
committed
WIP set up test stack
1 parent a5cbe4c commit 0dd631c

5 files changed

Lines changed: 151 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ project/plugins/project/
1717
dynamodb-local/
1818
dynamodb-local-common/
1919
dynamodb-local-schedule-lambda/
20+
dynamodb-local-live-activities/
2021

2122
# Scala-IDE specific
2223
.scala_dependencies

build.sbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ lazy val reportExtractor = lambda("reportextractor", "reportextractor", Some("co
461461
lazy val liveactivities = lambda("liveactivities", "liveactivities", Some("com.gu.liveactivities.LambdaLocalRun"))
462462
.dependsOn(common)
463463
.dependsOn(apiModels % "test->test", apiModels % "compile->compile")
464+
.settings(LocalDynamoDBLiveActivities.settings)
464465
.settings(
465466
libraryDependencies ++= Seq(
466467
"com.turo" % "pushy" % "0.13.10",
@@ -472,4 +473,10 @@ lazy val liveactivities = lambda("liveactivities", "liveactivities", Some("com.g
472473
// Hopefully this workaround can be removed once play-json-extensions either updates to Play 3.0 or is merged into play-json
473474
ExclusionRule(organization = "com.typesafe.play")
474475
),
476+
fork := true,
477+
startDynamoDBLocal := startDynamoDBLocal.dependsOn(Test / compile).value,
478+
Test / test := (Test / test).dependsOn(startDynamoDBLocal).value,
479+
Test / testOnly := (Test / testOnly).dependsOn(startDynamoDBLocal).evaluated,
480+
Test / testQuick := (Test / testQuick).dependsOn(startDynamoDBLocal).evaluated,
481+
Test / testOptions += dynamoDBLocalTestCleanup.value,
475482
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.gu.liveactivities
2+
3+
import aws.AsyncDynamo
4+
import com.amazonaws.auth.{
5+
AWSCredentials,
6+
AWSCredentialsProvider,
7+
AWSCredentialsProviderChain
8+
}
9+
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration
10+
import com.amazonaws.regions.Regions
11+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsyncClientBuilder
12+
import com.amazonaws.services.dynamodbv2.model.{
13+
CreateTableRequest,
14+
DeleteTableRequest
15+
}
16+
import org.specs2.mutable.Specification
17+
import org.specs2.specification.{BeforeAfterAll, Scope}
18+
19+
trait DynamodbSpecification extends Specification with BeforeAfterAll {
20+
21+
sequential
22+
23+
val TableName: String
24+
25+
def createTableRequest: CreateTableRequest
26+
27+
val TestEndpoint = "http://localhost:8002"
28+
29+
override def beforeAll(): Unit = {
30+
awsClient.createTable(createTableRequest)
31+
}
32+
33+
override def afterAll(): Unit = {
34+
awsClient.deleteTable(new DeleteTableRequest(TableName))
35+
}
36+
37+
private def awsClient = {
38+
val chain = new AWSCredentialsProviderChain(new AWSCredentialsProvider {
39+
override def refresh(): Unit = {}
40+
41+
override def getCredentials: AWSCredentials = new AWSCredentials {
42+
override def getAWSAccessKeyId: String = ""
43+
44+
override def getAWSSecretKey: String = ""
45+
}
46+
})
47+
48+
val client = AmazonDynamoDBAsyncClientBuilder.standard()
49+
.withCredentials(chain)
50+
.withEndpointConfiguration( new EndpointConfiguration(TestEndpoint, Regions.EU_WEST_1.getName) )
51+
.build
52+
53+
client
54+
}
55+
56+
trait AsyncDynamoScope extends Scope {
57+
val asyncClient = new AsyncDynamo(awsClient)
58+
}
59+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.gu.liveactivities
2+
3+
4+
import com.amazonaws.services.dynamodbv2.model._
5+
import org.specs2.concurrent.ExecutionEnv
6+
import org.specs2.mock.Mockito
7+
import tracking.Repository.RepositoryResult
8+
9+
import scala.concurrent.Future
10+
import scala.jdk.CollectionConverters._
11+
12+
13+
class LiveActivityChannelRepositoryTest (implicit ev: ExecutionEnv) extends DynamodbSpecification with Mockito {
14+
15+
override val TableName = "test-table"
16+
17+
"LiveActivityChannelRepository" should {
18+
"connect to local DynamoDB and create table" in new RepositoryScope {
19+
// This test will pass if the table is created successfully in beforeAll()
20+
1 must beEqualTo(1)
21+
}
22+
23+
"save a new channel mapping for an id if it does not exist" in new RepositoryScope with ExampleData {
24+
repository.saveMapping(footballMapping).flatMap { _ =>
25+
repository.getMappingByActivityId(footballMapping.liveActivityId)
26+
} must beEqualTo(Right(Some(footballMapping))).await
27+
}
28+
29+
"Error if trying to save a new channel mapping for an id that already exists" in new RepositoryScope with ExampleData {
30+
repository.saveMapping(footballMapping).flatMap { _ =>
31+
repository.getMappingByActivityId(footballMapping.liveActivityId)
32+
} must beEqualTo(Right(Some(footballMapping))).await
33+
}
34+
35+
// "delete a channel mapping for an activity id if it exists" in new RepositoryScope with ExampleData {
36+
// }
37+
//
38+
// "get a channel mapping for an activity id if it exists" in new RepositoryScope with ExampleData {
39+
// }
40+
}
41+
42+
trait RepositoryScope extends AsyncDynamoScope {
43+
val repository = new LiveActivityChannelRepository(asyncClient, TableName)
44+
}
45+
46+
trait ExampleData {
47+
val footballData = FootballLiveActivity(
48+
homeTeam = "HomeTeamName",
49+
awayTeam = "AwayTeamName",
50+
articleUrl = "https://www.theguardian.com/football/test-article-id"
51+
)
52+
53+
val footballMapping = LiveActivityMapping(
54+
liveActivityId = "football-1234567",
55+
channelId = "test-channel-id",
56+
data = Some(footballData)
57+
)
58+
}
59+
60+
override def createTableRequest: CreateTableRequest = {
61+
val IdField = "liveActivityId"
62+
63+
new CreateTableRequest(TableName, List(new KeySchemaElement(IdField, KeyType.HASH)).asJava)
64+
.withAttributeDefinitions(List(
65+
new AttributeDefinition(IdField, ScalarAttributeType.S)
66+
).asJava)
67+
.withProvisionedThroughput(new ProvisionedThroughput(5L, 5L))
68+
}
69+
70+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import com.localytics.sbt.dynamodb.DynamoDBLocalKeys
2+
import com.localytics.sbt.dynamodb.DynamoDBLocalKeys._
3+
import sbt._
4+
5+
object LocalDynamoDBLiveActivities {
6+
7+
val settings: Seq[Setting[_]] = DynamoDBLocalKeys.baseDynamoDBSettings ++ Seq(
8+
dynamoDBLocalDownloadDir := file("dynamodb-local-live-activities"),
9+
dynamoDBLocalInMemory := true,
10+
dynamoDBLocalVersion := "latest",
11+
// dynamoDBLocalSharedDB := true, // Add this line
12+
dynamoDBLocalPort := 8002
13+
)
14+
}

0 commit comments

Comments
 (0)