Skip to content

Commit

Permalink
Merge pull request #17 from /issues/15
Browse files Browse the repository at this point in the history
Fix #15: Dedup `supports` and `client` methods in `AWSClientExtension`
  • Loading branch information
madhead authored Jan 30, 2022
2 parents 194be96 + f417efe commit 5482e29
Show file tree
Hide file tree
Showing 72 changed files with 467 additions and 351 deletions.
12 changes: 0 additions & 12 deletions .editorconfig

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.gradle
build

.idea
.idea/*
!.idea/codeStyles
!.idea/inspectionProfiles
out

public
129 changes: 129 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/aws_junit5.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ JUnit 5 extensions for AWS
This repo contains few JUnit 5 https://junit.org/junit5/docs/current/user-guide/#extensions[extensions] that you could find useful for testing AWS-related code.
These extensions can be used to inject clients for AWS service mocks provided by tools like https://github.com/localstack/localstack[localstack] or https://aws.amazon.com/about-aws/whats-new/2018/08/use-amazon-dynamodb-local-more-easily-with-the-new-docker-image/[DynamoDB Local].
Both AWS Java SDK https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/welcome.html[v 2.x] and https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/welcome.html[v 1.x] are supported.
Currently these services are supported:
Currently, these services are supported:

- https://aws.amazon.com/dynamodb[DynamoDB]
- https://aws.amazon.com/kinesis/data-streams[Kinesis Data Streams]
Expand All @@ -16,6 +16,7 @@ Currently these services are supported:
- https://aws.amazon.com/ses[SES]
- https://aws.amazon.com/sns[SNS]
- https://aws.amazon.com/sqs[SQS]
- https://aws.amazon.com/lambda[Lambda]

== How do I use it?

Expand Down
12 changes: 6 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ tasks {
"https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/"
)
}
subprojects.forEach {
it.tasks.withType<Javadoc>().forEach {
source += it.source
classpath += it.classpath
includes += it.includes
excludes += it.excludes
subprojects.forEach { subproject ->
subproject.tasks.withType<Javadoc>().forEach { task ->
source += task.source
classpath += task.classpath
includes += task.includes
excludes += task.excludes
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void postProcessTestInstance(final Object testInstance, final ExtensionCo
}

private void inject(final Object testInstance, final Field field) throws Exception {
final boolean accessible = field.isAccessible();
@SuppressWarnings("deprecation") final boolean accessible = field.isAccessible();

try {
field.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.madhead.aws_junit5.common.impl;

import java.lang.reflect.Field;
import java.util.Map;

/**
* Dictionary-based {@link AWSClientExtension} implementation.
*/
public abstract class AWSClientExtensionBase extends AWSClientExtension {
abstract protected Map<Class<?>, ? extends AWSClientFactory<?>> factories();

@Override
protected boolean supports(final Field field) {
return factories().containsKey(field.getType());
}

@Override
protected Object client(final Field field) throws Exception {
return factories().get(field.getType()).client(field);
}
}
1 change: 1 addition & 0 deletions common/v1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ dependencies {
testImplementation("com.amazonaws:aws-java-sdk-sns")
testImplementation("com.amazonaws:aws-java-sdk-sqs")
testImplementation("com.amazonaws:aws-java-sdk-ses")
testImplementation("com.amazonaws:aws-java-sdk-lambda")
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package me.madhead.aws_junit5.common.v1;

import me.madhead.aws_junit5.common.AWSClient;
import me.madhead.aws_junit5.common.AWSEndpoint;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.util.StringUtils;
import me.madhead.aws_junit5.common.AWSClient;
import me.madhead.aws_junit5.common.AWSEndpoint;

import java.lang.reflect.Field;

/**
* Creates AWS Java SDK v 1.x clients of type {@link T}.
*
* @param <S>
* the type of an {@link AwsClientBuilder}, who actually creates AWS clients of type {@link T}.
* the type of {@link AwsClientBuilder}, who actually creates AWS clients of type {@link T}.
* @param <T>
* the type of AWS clients this factory creates.
*/
Expand All @@ -28,7 +28,7 @@ public AWSClientFactory(final AwsClientBuilder<S, T> awsClientBuilder) {
@Override
public T client(final Field field) throws Exception {
final AWSClient awsClientAnnotation = field.getAnnotation(AWSClient.class);
final AWSEndpoint endpoint = awsClientAnnotation.endpoint().newInstance();
final AWSEndpoint endpoint = awsClientAnnotation.endpoint().getDeclaredConstructor().newInstance();

validate(endpoint);

Expand All @@ -47,7 +47,8 @@ public T client(final Field field) throws Exception {

if (field.isAnnotationPresent(AWSAdvancedConfiguration.class)) {
final AWSAdvancedConfiguration awsAdvancedConfiguration = field.getAnnotation(AWSAdvancedConfiguration.class);
final ClientConfiguration clientConfiguration = awsAdvancedConfiguration.clientConfigurationFactory().newInstance().create();
final ClientConfiguration clientConfiguration =
awsAdvancedConfiguration.clientConfigurationFactory().getDeclaredConstructor().newInstance().create();

if (clientConfiguration != null) {
awsClientBuilder.withClientConfiguration(clientConfiguration);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.madhead.aws_junit5.common.v1;

import me.madhead.aws_junit5.common.AWSClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import me.madhead.aws_junit5.common.AWSClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -15,27 +15,27 @@ class AWSClientFactoryInvalidConfigurationTest {
private Object nullURLAWSEndpoint;

@Test
void testEmpty() throws Exception {
final IllegalArgumentException exception = Assertions.assertThrows(
void testEmpty() {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
@SuppressWarnings("unchecked") final AWSClientFactory clientFactory =
new AWSClientFactory(AmazonDynamoDBClientBuilder.standard());
@SuppressWarnings("unused") final Object client = clientFactory.client(this.getClass().getDeclaredField(
"emptyURLAWSEndpoint"));
final AWSClientFactory<?, ?> clientFactory =
new AWSClientFactory<>(AmazonDynamoDBClientBuilder.standard());

clientFactory.client(this.getClass().getDeclaredField("emptyURLAWSEndpoint"));
}
);
}

@Test
void testNull() throws Exception {
final IllegalArgumentException exception = Assertions.assertThrows(
void testNull() {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
@SuppressWarnings("unchecked") final AWSClientFactory clientFactory =
new AWSClientFactory(AmazonDynamoDBClientBuilder.standard());
@SuppressWarnings("unused") final Object client = clientFactory.client(this.getClass().getDeclaredField(
"nullURLAWSEndpoint"));
final AWSClientFactory<?, ?> clientFactory =
new AWSClientFactory<>(AmazonDynamoDBClientBuilder.standard());

clientFactory.client(this.getClass().getDeclaredField("nullURLAWSEndpoint"));
}
);
}
Expand Down
Loading

0 comments on commit 5482e29

Please sign in to comment.