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+ }
0 commit comments