Skip to content

Commit 30697da

Browse files
committed
Update tests on dynamodb service class
1 parent 70e46a1 commit 30697da

2 files changed

Lines changed: 81 additions & 65 deletions

File tree

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package com.gu.liveactivities
22

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
3+
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient
4+
import software.amazon.awssdk.auth.credentials.{
5+
AwsCredentialsProviderChain,
6+
ProfileCredentialsProvider,
7+
DefaultCredentialsProvider,
158
}
9+
import software.amazon.awssdk.regions.Region.EU_WEST_1
1610
import org.specs2.mutable.Specification
1711
import org.specs2.specification.{BeforeAfterAll, Scope}
12+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
13+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
14+
import software.amazon.awssdk.services.dynamodb.model._
15+
import java.net.URI
16+
import software.amazon.awssdk.regions.Region
1817

1918
trait DynamodbSpecification extends Specification with BeforeAfterAll {
2019

@@ -27,32 +26,21 @@ trait DynamodbSpecification extends Specification with BeforeAfterAll {
2726
val TestEndpoint = "http://localhost:8002"
2827

2928
override def beforeAll(): Unit = {
30-
awsClient.createTable(createTableRequest)
29+
awsClient.createTable(createTableRequest).join()
3130
}
3231

3332
override def afterAll(): Unit = {
34-
awsClient.deleteTable(new DeleteTableRequest(TableName))
33+
awsClient.deleteTable(DeleteTableRequest.builder().tableName(TableName).build()).join()
3534
}
3635

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 = "testid"
43-
override def getAWSSecretKey: String = "testkey"
44-
}
45-
})
46-
47-
val client = AmazonDynamoDBAsyncClientBuilder.standard()
48-
.withCredentials(chain)
49-
.withEndpointConfiguration( new EndpointConfiguration(TestEndpoint, Regions.EU_WEST_1.getName) )
50-
.build
51-
52-
client
53-
}
36+
private def awsClient = DynamoDbAsyncClient
37+
.builder()
38+
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("TEST", "TEST")))
39+
.region(Region.EU_WEST_1)
40+
.endpointOverride(URI.create(TestEndpoint))
41+
.build()
5442

5543
trait AsyncDynamoScope extends Scope {
56-
val asyncClient = new AsyncDynamo(awsClient)
44+
val asyncClient = awsClient
5745
}
5846
}
Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.gu.liveactivities
22

3-
import com.amazonaws.services.dynamodbv2.model._
43
import org.specs2.concurrent.ExecutionEnv
54
import org.specs2.mock.Mockito
6-
import tracking.Repository.RepositoryResult
7-
import tracking.RepositoryError
5+
import software.amazon.awssdk.services.dynamodb.model._
86
import scala.jdk.CollectionConverters._
97
import com.gu.liveactivities.service.LiveActivityChannelRepository
108
import com.gu.liveactivities.models.FootballLiveActivity
119
import com.gu.liveactivities.models.LiveActivityMapping
1210
import com.gu.liveactivities.models.LiveActivityData
13-
import java.time.ZonedDateTime
1411
import com.gu.liveactivities.service.ChannelMappingsRepository
12+
import java.time.ZonedDateTime
13+
import com.gu.liveactivities.models.RepositoryException
14+
import com.gu.liveactivities.models.LiveActivityInvalidStateException
1515

1616

1717
class LiveActivityChannelRepositoryTest(implicit ev: ExecutionEnv)
@@ -21,41 +21,55 @@ class LiveActivityChannelRepositoryTest(implicit ev: ExecutionEnv)
2121
override val TableName = "test-table"
2222

2323
"LiveActivityChannelRepository" should {
24-
"connect to local DynamoDB and create table" in new RepositoryScope {
25-
// This test will pass if the table is created successfully in beforeAll()
26-
1 must beEqualTo(1)
27-
}
2824

2925
"save a new channel mapping for an id if it does not exist" in new RepositoryScope
3026
with ExampleData {
31-
repository.createMapping(footballMapping).flatMap { _ =>
27+
val footballMapping = createFootballMappingWithId("football-0001")
28+
repository.createMapping(
29+
footballMapping.id,
30+
footballMapping.channelId,
31+
footballMapping.eventData,
32+
footballMapping.competitionId).flatMap { _ =>
3233
repository.getMappingById(footballMapping.id)
33-
} must beEqualTo(Right(footballMapping)).await
34+
} must beEqualTo(footballMapping).await
3435
}
3536

36-
// "Error if trying to save a new channel mapping for an id that already exists" in new RepositoryScope
37-
// with ExampleData {
38-
// repository.createMapping(footballMapping).flatMap { _ =>
39-
// repository.createMapping(footballMapping)
40-
// } must beLike[ChannelMappingsRepository.Result[Unit]] {
41-
// case Left(RepositoryException(msg, _))
42-
// if msg.contains("ConditionalCheckFailed") =>
43-
// ok
44-
// }.await
45-
// }
37+
"Error if trying to save a new channel mapping for an id that already exists" in new RepositoryScope
38+
with ExampleData {
39+
val footballMapping = createFootballMappingWithId("football-0004")
40+
repository.createMapping(footballMapping.id,
41+
footballMapping.channelId,
42+
footballMapping.eventData,
43+
footballMapping.competitionId).flatMap { _ =>
44+
repository.createMapping(footballMapping.id,
45+
footballMapping.channelId,
46+
footballMapping.eventData,
47+
footballMapping.competitionId)
48+
} must throwA[RepositoryException].await
49+
}
4650

4751
"delete a channel mapping for an activity id if it exists" in new RepositoryScope
4852
with ExampleData {
49-
repository.createMapping(footballMapping).flatMap { _ =>
53+
val footballMapping = createFootballMappingWithId("football-0002")
54+
repository.createMapping(
55+
footballMapping.id,
56+
footballMapping.channelId,
57+
footballMapping.eventData,
58+
footballMapping.competitionId).flatMap { _ =>
5059
repository.deleteMappingById(footballMapping.id)
51-
} must beEqualTo(Right(())).await
60+
} must beEqualTo(()).await
5261
}
5362

5463
"get a channel mapping for an activity id if it exists" in new RepositoryScope
5564
with ExampleData {
56-
repository.createMapping(footballMapping).flatMap { _ =>
65+
val footballMapping = createFootballMappingWithId("football-0003")
66+
repository.createMapping(
67+
footballMapping.id,
68+
footballMapping.channelId,
69+
footballMapping.eventData,
70+
footballMapping.competitionId).flatMap { _ =>
5771
repository.getMappingById(footballMapping.id)
58-
} must beEqualTo(Right(footballMapping)).await
72+
} must beEqualTo(footballMapping).await
5973
}
6074
}
6175

@@ -70,7 +84,7 @@ class LiveActivityChannelRepositoryTest(implicit ev: ExecutionEnv)
7084
articleUrl = "https://www.theguardian.com/football/test-article-id"
7185
)
7286

73-
val footballMapping = LiveActivityMapping(
87+
val footballMappingTemplate = LiveActivityMapping(
7488
id = "football-1234567",
7589
channelId = "test-channel-id",
7690
isChannelActive = true,
@@ -80,21 +94,35 @@ class LiveActivityChannelRepositoryTest(implicit ev: ExecutionEnv)
8094
lastEventId = None,
8195
lastEventUpdate = None,
8296
)
97+
98+
def createFootballMappingWithId(id: String): LiveActivityMapping = {
99+
footballMappingTemplate.copy(id = id)
100+
}
83101
}
84102

85103
override def createTableRequest: CreateTableRequest = {
86104
val IdField = "id"
87105

88-
new CreateTableRequest(
89-
TableName,
90-
List(new KeySchemaElement(IdField, KeyType.HASH)).asJava
106+
val attrs: List[AttributeDefinition] = List(
107+
AttributeDefinition
108+
.builder()
109+
.attributeName(IdField)
110+
.attributeType(ScalarAttributeType.S)
111+
.build(),
91112
)
92-
.withAttributeDefinitions(
93-
List(
94-
new AttributeDefinition(IdField, ScalarAttributeType.S)
95-
).asJava
113+
val keySchema: List[KeySchemaElement] = List(
114+
KeySchemaElement.builder().attributeName(IdField).keyType(KeyType.HASH).build(),
115+
)
116+
117+
CreateTableRequest
118+
.builder()
119+
.tableName(TableName)
120+
.keySchema(keySchema.asJava)
121+
.attributeDefinitions(attrs.asJava)
122+
.provisionedThroughput(
123+
ProvisionedThroughput.builder().readCapacityUnits(5L).writeCapacityUnits(5L).build(),
96124
)
97-
.withProvisionedThroughput(new ProvisionedThroughput(5L, 5L))
125+
.build();
98126
}
99127

100128
}

0 commit comments

Comments
 (0)