Skip to content

Commit

Permalink
Reuse container in DynamoDBTestBase
Browse files Browse the repository at this point in the history
  • Loading branch information
patchwork01 committed Feb 20, 2025
1 parent 4e3c3e9 commit 8cde679
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,26 @@
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static sleeper.dynamodb.test.GenericContainerAwsV1ClientHelper.buildAwsV1Client;

@Testcontainers
public abstract class DynamoDBTestBase {
public static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBTestBase.class);

private static AmazonDynamoDB dynamoDBClientShared;

@Container
public static final DynamoDBContainer CONTAINER = new DynamoDBContainer();
public static final DynamoDBContainer CONTAINER = start();
private static final AmazonDynamoDB DYNAMO_CLIENT = buildAwsV1Client(CONTAINER, CONTAINER.getDynamoPort(), AmazonDynamoDBClientBuilder.standard());

@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
protected final AmazonDynamoDB dynamoDBClient = dynamoDBClientShared;

@BeforeAll
public static void initDynamoClient() {
dynamoDBClientShared = buildAwsV1Client(CONTAINER, CONTAINER.getDynamoPort(), AmazonDynamoDBClientBuilder.standard());
}

@AfterAll
public static void shutdownDynamoClient() {
dynamoDBClientShared.shutdown();
protected final AmazonDynamoDB dynamoClient = DYNAMO_CLIENT;

@SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
private static DynamoDBContainer start() {
DynamoDBContainer container = new DynamoDBContainer()
.withLogConsumer(outputFrame -> LOGGER.info(outputFrame.getUtf8StringWithoutLineEnding()))
.withEnv("DEBUG", "1");
container.start();
return container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ void shouldCreateRecordWithStringAttribute() {
.string(TEST_KEY, UUID.randomUUID().toString())
.string(TEST_VALUE, "value").build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(record);
}

Expand All @@ -49,9 +49,9 @@ void shouldCreateRecordWithIntAttribute() {
.string(TEST_KEY, UUID.randomUUID().toString())
.number(TEST_VALUE, 123).build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(record);
}

Expand All @@ -62,9 +62,9 @@ void shouldCreateRecordWithLongAttribute() {
.string(TEST_KEY, UUID.randomUUID().toString())
.number(TEST_VALUE, 123L).build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(record);
}

Expand All @@ -75,9 +75,9 @@ void shouldCreateRecordWithInstantAttribute() {
.string(TEST_KEY, UUID.randomUUID().toString())
.number(TEST_VALUE, Instant.now().toEpochMilli()).build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(record);
}

Expand All @@ -89,9 +89,9 @@ void shouldCreateRecordWithNaN() {
.string(TEST_KEY, key)
.number(TEST_VALUE, Double.NaN).build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(record);
}

Expand All @@ -103,9 +103,9 @@ void shouldCreateRecordWithNullNumber() {
.string(TEST_KEY, key)
.number(TEST_VALUE, null).build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(record);
}

Expand All @@ -118,9 +118,9 @@ void shouldUnsetExistingNumberAttributeWhenProvidingNull() {
.number(TEST_VALUE, 123)
.number(TEST_VALUE, null).build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(new DynamoDBRecordBuilder()
.string(TEST_KEY, key).build());
}
Expand All @@ -134,9 +134,9 @@ void shouldUnsetExistingStringAttributeWhenProvidingNull() {
.string(TEST_VALUE, "abc")
.string(TEST_VALUE, null).build();
// When
dynamoDBClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
dynamoClient.putItem(new PutItemRequest(TEST_TABLE_NAME, record));
// Then
ScanResult result = dynamoDBClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
ScanResult result = dynamoClient.scan(new ScanRequest().withTableName(TEST_TABLE_NAME));
assertThat(result.getItems()).containsExactly(new DynamoDBRecordBuilder()
.string(TEST_KEY, key).build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ void setup() {

@AfterEach
public void tearDown() {
dynamoDBClient.deleteTable(TEST_TABLE_NAME);
dynamoClient.deleteTable(TEST_TABLE_NAME);
}

public void createTable() {
initialiseTable(dynamoDBClient, TEST_TABLE_NAME,
initialiseTable(dynamoClient, TEST_TABLE_NAME,
List.of(
new AttributeDefinition(TEST_KEY, ScalarAttributeType.S)),
List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public class DynamoDBUtilsPagingIT extends DynamoDBTestBase {

@AfterEach
public void tearDown() {
dynamoDBClient.deleteTable(tableName);
dynamoClient.deleteTable(tableName);
}

@Nested
@DisplayName("Running Scan")
class RunningScan {
@BeforeEach
void setup() {
initialiseTable(dynamoDBClient, tableName,
initialiseTable(dynamoClient, tableName,
List.of(
new AttributeDefinition(TEST_KEY, ScalarAttributeType.S)),
List.of(
Expand All @@ -75,11 +75,11 @@ void shouldReturnPagedResultsWhenMoreRecordsThanLimit() {
.string(TEST_KEY, UUID.randomUUID().toString())
.string(TEST_VALUE, "value2").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, scan().withLimit(1)))
assertThat(streamPagedItems(dynamoClient, scan().withLimit(1)))
.containsExactlyInAnyOrder(record1, record2);
}

Expand All @@ -90,10 +90,10 @@ void shouldReturnOnePageOfResultsWhenFewerRecordsThanLimit() {
.string(TEST_KEY, UUID.randomUUID().toString())
.string(TEST_VALUE, "value1").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record1));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, scan().withLimit(2)))
assertThat(streamPagedItems(dynamoClient, scan().withLimit(2)))
.containsExactlyInAnyOrder(record1);
}

Expand All @@ -104,10 +104,10 @@ void shouldReturnPagedResultsWhenRecordsEqualToLimit() {
.string(TEST_KEY, UUID.randomUUID().toString())
.string(TEST_VALUE, "value1").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record1));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, scan().withLimit(1)))
assertThat(streamPagedItems(dynamoClient, scan().withLimit(1)))
.containsExactlyInAnyOrder(record1);
}

Expand All @@ -118,10 +118,10 @@ void shouldNotReturnEmptyResultWhenFewerRecordsThanLimit() {
.string(TEST_KEY, UUID.randomUUID().toString())
.string(TEST_VALUE, "value1").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record1));

// When/Then
assertThat(streamPagedResults(dynamoDBClient, scan().withLimit(2)))
assertThat(streamPagedResults(dynamoClient, scan().withLimit(2)))
.extracting(result -> result.getItems().size())
.containsExactly(1);
}
Expand All @@ -133,10 +133,10 @@ void shouldReturnEmptyResultForLastPageWhenRecordsEqualToLimit() {
.string(TEST_KEY, UUID.randomUUID().toString())
.string(TEST_VALUE, "value1").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record1));

// When/Then
assertThat(streamPagedResults(dynamoDBClient, scan().withLimit(1)))
assertThat(streamPagedResults(dynamoClient, scan().withLimit(1)))
.extracting(result -> result.getItems().size())
.containsExactly(1, 0);
}
Expand All @@ -154,19 +154,19 @@ void shouldReturnPagedResultsWhenLastPageHasFewerRecordsThanLimit() {
.string(TEST_KEY, UUID.randomUUID().toString())
.string(TEST_VALUE, "value3").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoDBClient.putItem(new PutItemRequest(tableName, record3));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record3));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, scan().withLimit(2)))
assertThat(streamPagedItems(dynamoClient, scan().withLimit(2)))
.containsExactlyInAnyOrder(record1, record2, record3);
}

@Test
void shouldReturnNoResultsWhenNoRecordsExist() {
// When/Then
assertThat(streamPagedItems(dynamoDBClient, scan().withLimit(1)))
assertThat(streamPagedItems(dynamoClient, scan().withLimit(1)))
.isEmpty();
}
}
Expand All @@ -176,7 +176,7 @@ void shouldReturnNoResultsWhenNoRecordsExist() {
class RunningQuery {
@BeforeEach
void setUp() {
initialiseTable(dynamoDBClient, tableName,
initialiseTable(dynamoClient, tableName,
List.of(
new AttributeDefinition(TEST_KEY, ScalarAttributeType.S),
new AttributeDefinition(TEST_VALUE, ScalarAttributeType.S)),
Expand All @@ -195,11 +195,11 @@ void shouldReturnPagedResultsWhenMoreRecordsThanLimit() {
.string(TEST_KEY, "test-key")
.string(TEST_VALUE, "test-value-2").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, queryForKey("test-key").withLimit(1)))
assertThat(streamPagedItems(dynamoClient, queryForKey("test-key").withLimit(1)))
.containsExactlyInAnyOrder(record1, record2);
}

Expand All @@ -213,11 +213,11 @@ void shouldReturnOnePageOfResultsWhenFewerRecordsThanLimit() {
.string(TEST_KEY, "test-key")
.string(TEST_VALUE, "test-value-2").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, queryForKey("test-key").withLimit(3)))
assertThat(streamPagedItems(dynamoClient, queryForKey("test-key").withLimit(3)))
.containsExactlyInAnyOrder(record1, record2);
}

Expand All @@ -231,11 +231,11 @@ void shouldReturnPagedResultsWhenRecordsEqualToLimit() {
.string(TEST_KEY, "test-key")
.string(TEST_VALUE, "test-value-2").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, queryForKey("test-key").withLimit(2)))
assertThat(streamPagedItems(dynamoClient, queryForKey("test-key").withLimit(2)))
.containsExactlyInAnyOrder(record1, record2);
}

Expand All @@ -249,11 +249,11 @@ void shouldNotReturnEmptyResultWhenFewerRecordsThanLimit() {
.string(TEST_KEY, "test-key")
.string(TEST_VALUE, "test-value-2").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));

// When/Then
assertThat(streamPagedResults(dynamoDBClient, queryForKey("test-key").withLimit(3)))
assertThat(streamPagedResults(dynamoClient, queryForKey("test-key").withLimit(3)))
.extracting(result -> result.getItems().size())
.containsExactly(2);
}
Expand All @@ -268,11 +268,11 @@ void shouldReturnEmptyResultForLastPageWhenRecordsEqualToLimit() {
.string(TEST_KEY, "test-key")
.string(TEST_VALUE, "test-value-2").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));

// When/Then
assertThat(streamPagedResults(dynamoDBClient, queryForKey("test-key").withLimit(2)))
assertThat(streamPagedResults(dynamoClient, queryForKey("test-key").withLimit(2)))
.extracting(result -> result.getItems().size())
.containsExactly(2, 0);
}
Expand All @@ -290,19 +290,19 @@ void shouldReturnPagedResultsWhenLastPageHasFewerRecordsThanLimit() {
.string(TEST_KEY, "test-key")
.string(TEST_VALUE, "test-value-3").build();

dynamoDBClient.putItem(new PutItemRequest(tableName, record1));
dynamoDBClient.putItem(new PutItemRequest(tableName, record2));
dynamoDBClient.putItem(new PutItemRequest(tableName, record3));
dynamoClient.putItem(new PutItemRequest(tableName, record1));
dynamoClient.putItem(new PutItemRequest(tableName, record2));
dynamoClient.putItem(new PutItemRequest(tableName, record3));

// When/Then
assertThat(streamPagedItems(dynamoDBClient, queryForKey("test-key").withLimit(4)))
assertThat(streamPagedItems(dynamoClient, queryForKey("test-key").withLimit(4)))
.containsExactlyInAnyOrder(record1, record2, record3);
}

@Test
void shouldReturnNoResultsWhenNoRecordsExist() {
// When/Then
assertThat(streamPagedItems(dynamoDBClient, queryForKey("not-a-key").withLimit(1)))
assertThat(streamPagedItems(dynamoClient, queryForKey("not-a-key").withLimit(1)))
.isEmpty();
}
}
Expand Down
Loading

0 comments on commit 8cde679

Please sign in to comment.