Skip to content

feat: replace external dependencies with mocks in unit tests #1946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ public void test_ttlNotExpired_sameResult_ttlExpired_differentResult() throws In
Assertions.assertThat(result2.getResult()).isEqualTo(result1.getResult());
Assertions.assertThat(result3.getResult()).isNotEqualTo(result2.getResult());
}

}
15 changes: 2 additions & 13 deletions powertools-idempotency/powertools-idempotency-dynamodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,10 @@

<!-- Test dependencies -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<!-- >2.2.0 is not compatible with Java 11 anymore -->
<!-- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocalHistory.html -->
<version>2.2.0</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<!--Needed when building locally on M1 Mac-->
<dependency>
<groupId>io.github.ganadist.sqlite4java</groupId>
<artifactId>libsqlite4java-osx-aarch64</artifactId>
<version>1.0.392</version>
<scope>test</scope>
<type>dylib</type>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this file be deleted now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! I've deleted the DynamoDBConfig.java file as it was only used for DynamoDB Local server setup, which is no longer needed with our new mock-based approach.

Original file line number Diff line number Diff line change
Expand Up @@ -14,89 +14,20 @@

package software.amazon.lambda.powertools.idempotency.persistence.dynamodb;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.URI;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
import software.amazon.awssdk.services.dynamodb.model.BillingMode;
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
import software.amazon.awssdk.services.dynamodb.model.KeyType;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;

public class DynamoDBConfig {
class DynamoDBConfig {
protected static final String TABLE_NAME = "idempotency_table";
protected static DynamoDBProxyServer dynamoProxy;
protected static DynamoDbClient client;

@BeforeAll
public static void setupDynamo() {
int port = getFreePort();
try {
dynamoProxy = ServerRunner.createServerFromCommandLineArgs(new String[] {
"-inMemory",
"-port",
Integer.toString(port)
});
dynamoProxy.start();
} catch (Exception e) {
throw new RuntimeException();
}

client = DynamoDbClient.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.region(Region.EU_WEST_1)
.endpointOverride(URI.create("http://localhost:" + port))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create("FAKE", "FAKE")))
.build();

client.createTable(CreateTableRequest.builder()
.tableName(TABLE_NAME)
.keySchema(KeySchemaElement.builder().keyType(KeyType.HASH).attributeName("id").build())
.attributeDefinitions(
AttributeDefinition.builder().attributeName("id").attributeType(ScalarAttributeType.S).build())
.billingMode(BillingMode.PAY_PER_REQUEST)
.build());

DescribeTableResponse response = client
.describeTable(DescribeTableRequest.builder().tableName(TABLE_NAME).build());
if (response == null) {
throw new RuntimeException("Table was not created within expected time");
}
}

@AfterAll
public static void teardownDynamo() {
try {
dynamoProxy.stop();
} catch (Exception e) {
throw new RuntimeException();
}
}

private static int getFreePort() {
try {
ServerSocket socket = new ServerSocket(0);
int port = socket.getLocalPort();
socket.close();
return port;
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}

@Mock
protected DynamoDbClient client;

@BeforeEach
void setupMocks() {
MockitoAnnotations.openMocks(this);
}
}
}
Loading