Skip to content

Commit

Permalink
Implement dynamodb wiring with dagger
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansimsmith committed Aug 8, 2024
1 parent bc15ac2 commit 70e467b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 37 deletions.
10 changes: 10 additions & 0 deletions immersion_tracker_api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand All @@ -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",
],
)

Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, String> {
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();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -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");
}
}

0 comments on commit 70e467b

Please sign in to comment.