From 70e467b694a2c8c0d0dcdc8e2cd5e3c0d6abcfa2 Mon Sep 17 00:00:00 2001 From: Jordan Sim-Smith Date: Thu, 8 Aug 2024 15:51:54 +1200 Subject: [PATCH] Implement dynamodb wiring with dagger --- immersion_tracker_api/BUILD.bazel | 10 +++++ .../immersiontracker/DynamoDbModule.java | 28 +++++++++++++ .../immersiontracker/GetProgressHandler.java | 16 ++++++-- .../immersiontracker/Hello.java | 13 ------ .../ImmersionTrackerComponent.java | 8 ---- .../ImmersionTrackerFactory.java | 32 +++++++++++++++ ...=> GetProgressHandlerIntegrationTest.java} | 41 +++++++++++++------ 7 files changed, 111 insertions(+), 37 deletions(-) create mode 100644 immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/DynamoDbModule.java delete mode 100644 immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/Hello.java delete mode 100644 immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerComponent.java create mode 100644 immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerFactory.java rename immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/{MainIntegrationTest.java => GetProgressHandlerIntegrationTest.java} (51%) diff --git a/immersion_tracker_api/BUILD.bazel b/immersion_tracker_api/BUILD.bazel index 764399e..f5f3cca 100644 --- a/immersion_tracker_api/BUILD.bazel +++ b/immersion_tracker_api/BUILD.bazel @@ -13,6 +13,10 @@ java_library( ), deps = [ ":dagger", + "@maven//:com_google_code_findbugs_jsr305", + "@maven//:com_google_guava_guava", + "@maven//:software_amazon_awssdk_dynamodb", + "@maven//:software_amazon_awssdk_dynamodb_enhanced", ], ) @@ -23,6 +27,9 @@ java_binary( deps = [ ":lib", "@maven//:com_amazonaws_aws_lambda_java_core", + "@maven//:com_google_guava_guava", + "@maven//:software_amazon_awssdk_dynamodb", + "@maven//:software_amazon_awssdk_dynamodb_enhanced", ], ) @@ -40,7 +47,10 @@ java_test_suite( test_suffixes = ["IntegrationTest.java"], runtime_deps = JUNIT5_DEPS, deps = [ + ":get-progress-handler", + ":lib", "//lib/testcontainers:lib", + "@maven//:com_amazonaws_aws_lambda_java_core", "@maven//:org_assertj_assertj_core", "@maven//:org_junit_jupiter_junit_jupiter_api", "@maven//:org_testcontainers_junit_jupiter", diff --git a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/DynamoDbModule.java b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/DynamoDbModule.java new file mode 100644 index 0000000..5f2ac51 --- /dev/null +++ b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/DynamoDbModule.java @@ -0,0 +1,28 @@ +package com.jordansimsmith.immersiontracker; + +import dagger.Module; +import dagger.Provides; +import java.net.URI; +import javax.annotation.Nullable; +import javax.inject.Named; +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; + +@Module +public class DynamoDbModule { + + @Provides + public DynamoDbClient dynamoDbClient(@Nullable @Named("dynamoDbEndpoint") URI dynamoDbEndpoint) { + var builder = DynamoDbClient.builder(); + if (dynamoDbEndpoint != null) { + builder.endpointOverride(dynamoDbEndpoint); + } + + return builder.build(); + } + + @Provides + public DynamoDbEnhancedClient dynamoDbEnhancedClient(DynamoDbClient dynamoDbClient) { + return DynamoDbEnhancedClient.builder().dynamoDbClient(dynamoDbClient).build(); + } +} diff --git a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/GetProgressHandler.java b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/GetProgressHandler.java index bc6bbfe..6379147 100644 --- a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/GetProgressHandler.java +++ b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/GetProgressHandler.java @@ -2,16 +2,26 @@ import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; +import com.google.common.annotations.VisibleForTesting; +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; public class GetProgressHandler implements RequestHandler { - private final Hello hello; + private final DynamoDbClient dynamoDbClient; + private final DynamoDbEnhancedClient dynamoDbEnhancedClient; public GetProgressHandler() { - this.hello = DaggerImmersionTrackerComponent.create().hello(); + this(ImmersionTrackerFactory.create()); + } + + @VisibleForTesting + GetProgressHandler(ImmersionTrackerFactory factory) { + this.dynamoDbEnhancedClient = factory.dynamoDbEnhancedClient(); + this.dynamoDbClient = factory.dynamoDbClient(); } @Override public String handleRequest(Object s, Context context) { - return hello.hello(); + return dynamoDbClient.listTables().toString(); } } diff --git a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/Hello.java b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/Hello.java deleted file mode 100644 index a7e320c..0000000 --- a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/Hello.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.jordansimsmith.immersiontracker; - -import javax.inject.Inject; - -public class Hello { - - @Inject - public Hello() {} - - public String hello() { - return "hello, world"; - } -} diff --git a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerComponent.java b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerComponent.java deleted file mode 100644 index f53f83c..0000000 --- a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerComponent.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.jordansimsmith.immersiontracker; - -import dagger.Component; - -@Component -public interface ImmersionTrackerComponent { - Hello hello(); -} diff --git a/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerFactory.java b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerFactory.java new file mode 100644 index 0000000..841895c --- /dev/null +++ b/immersion_tracker_api/src/main/java/com/jordansimsmith/immersiontracker/ImmersionTrackerFactory.java @@ -0,0 +1,32 @@ +package com.jordansimsmith.immersiontracker; + +import dagger.BindsInstance; +import dagger.Component; +import java.net.URI; +import javax.inject.Named; +import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; + +@Component(modules = {DynamoDbModule.class}) +public interface ImmersionTrackerFactory { + + DynamoDbClient dynamoDbClient(); + + DynamoDbEnhancedClient dynamoDbEnhancedClient(); + + @Component.Builder + interface Builder { + @BindsInstance + Builder dynamoDbEndpoint(@Named("dynamoDbEndpoint") URI dynamoDbEndpoint); + + ImmersionTrackerFactory build(); + } + + static ImmersionTrackerFactory create() { + return DaggerImmersionTrackerFactory.builder().build(); + } + + static ImmersionTrackerFactory.Builder builder() { + return DaggerImmersionTrackerFactory.builder(); + } +} diff --git a/immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/MainIntegrationTest.java b/immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/GetProgressHandlerIntegrationTest.java similarity index 51% rename from immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/MainIntegrationTest.java rename to immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/GetProgressHandlerIntegrationTest.java index f577fde..c909155 100644 --- a/immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/MainIntegrationTest.java +++ b/immersion_tracker_api/src/test/java/com/jordansimsmith/immersiontracker/GetProgressHandlerIntegrationTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import com.jordansimsmith.testcontainers.DynamoDbContainer; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -11,21 +12,21 @@ import software.amazon.awssdk.services.dynamodb.model.*; @Testcontainers -public class MainIntegrationTest { +public class GetProgressHandlerIntegrationTest { - @Container DynamoDbContainer dynamodb = new DynamoDbContainer(); + private DynamoDbClient dynamodbClient; + private DynamoDbEnhancedClient dynamoDbEnhancedClient; - @Test - void test() throws Exception { - assertThat(dynamodb.isRunning()).isTrue(); - assertThat(dynamodb.getHost()).isEqualTo("localhost"); - - var endpoint = dynamodb.getEndpoint(); + private GetProgressHandler getProgressHandler; - var client = DynamoDbClient.builder().endpointOverride(endpoint).build(); - var enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(client).build(); + @Container DynamoDbContainer dynamoDbContainer = new DynamoDbContainer(); - assertThat(client.listTables().tableNames()).isEmpty(); + @BeforeEach + void setUp() { + dynamodbClient = + DynamoDbClient.builder().endpointOverride(dynamoDbContainer.getEndpoint()).build(); + dynamoDbEnhancedClient = + DynamoDbEnhancedClient.builder().dynamoDbClient(dynamodbClient).build(); var req = CreateTableRequest.builder() @@ -42,8 +43,22 @@ void test() throws Exception { .writeCapacityUnits(1L) .build()) .build(); - var res = client.createTable(req); + dynamodbClient.createTable(req); + + var factory = + ImmersionTrackerFactory.builder().dynamoDbEndpoint(dynamoDbContainer.getEndpoint()).build(); + getProgressHandler = new GetProgressHandler(factory); + } - assertThat(client.listTables().tableNames()).contains("my_table"); + @Test + void test1() { + getProgressHandler.handleRequest(null, null); + assertThat(dynamodbClient.listTables().tableNames()).contains("my_table"); + } + + @Test + void test2() { + getProgressHandler.handleRequest(null, null); + assertThat(dynamodbClient.listTables().tableNames()).doesNotContain("my_table_2"); } }