Skip to content
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

supports inject parameters of lifecycle methods #87

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ LocalS3 for Junit5 provides a Java annotation `@LocalS3` helps you easily launch
```

When you annotate it on test classes or test methods, the LocalS3 extension automatically inject instances with
the following parameter types of test methods.
the following parameter types of test methods or lifecycle methods.

+ `AmazonS3`
+ `S3Client`
Expand Down Expand Up @@ -167,6 +167,29 @@ class AppTest {
}
```

Example 3: Inject instances to junit5 lifecycle methods.

```java
@LocalS3
class AppTest {

@BeforeAll
static void beforeAll(S3Client client) {
client.createBucket(b -> b.bucket("my-bucket"));
}

@Test
void test(S3Client client) {
client.headBucket(b -> b.bucket("my-bucket"));
}

@AfterAll
static void afterAll(S3Client client) {
client.deleteBucket(b -> b.bucket("my-bucket"));
}
}
```

#### Difference between `@LocalS3` on test classes and test methods

If `@LocalS3` is on a test class, the Junit5 extension will create a shared service for all test methods in the class
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

import java.lang.reflect.Method;
import java.util.Objects;

public abstract class AbstractLocalS3ParameterResolver implements ParameterResolver {
Expand All @@ -18,8 +19,10 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context)
throws ParameterResolutionException {

String methodName = context.getTestMethod().map(Method::toString).orElse("");
String s3OnMethodKey = context.getRequiredTestClass()
+ (context.getRequiredTestMethod() + LocalS3Extension.LOCAL_S3_PORT_STORE_SUFFIX);
+ (methodName + LocalS3Extension.LOCAL_S3_PORT_STORE_SUFFIX);
String s3OnClassKey = context.getRequiredTestClass() + LocalS3Extension.LOCAL_S3_PORT_STORE_SUFFIX;
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL);
Integer port = store.getOrDefault(s3OnMethodKey, Integer.TYPE, store.getOrDefault(s3OnClassKey, Integer.TYPE, null));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.robothy.s3.jupiter.extensions;

import lombok.SneakyThrows;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

Expand All @@ -25,8 +23,7 @@ protected Object resolve(int port) {
.forcePathStyle(true)
.endpointOverride(new URI(endpoint))
.region(Region.of("local"))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create("local-s3-access-key", "local-s3-secret-key")))
.credentialsProvider(AnonymousCredentialsProvider.create())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.robothy.s3.jupiter.extensions;

import com.amazonaws.services.s3.AmazonS3;
import com.robothy.s3.jupiter.LocalS3;
import com.robothy.s3.jupiter.LocalS3Endpoint;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;

import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

@LocalS3
public class ResolveBeforeAllParametersTest {

private static LocalS3Endpoint endpoint;

private static AmazonS3 s3;

private static S3Client s3Client;

@BeforeAll
static void setup(LocalS3Endpoint endpoint, AmazonS3 s3, S3Client s3Client) throws Exception {
ResolveBeforeAllParametersTest.endpoint = endpoint;
ResolveBeforeAllParametersTest.s3 = s3;
ResolveBeforeAllParametersTest.s3Client = s3Client;

s3.createBucket("my-bucket");
assertDoesNotThrow(() -> s3Client.headBucket(HeadBucketRequest.builder().bucket("my-bucket").build()));

S3Client clientWithInjectedEndpoint = S3Client.builder().region(Region.of("local"))
.endpointOverride(new URI(endpoint.endpoint()))
.forcePathStyle(true)
.credentialsProvider(AnonymousCredentialsProvider.create())
.build();
assertDoesNotThrow(() -> clientWithInjectedEndpoint.headBucket(HeadBucketRequest.builder().bucket("my-bucket").build()));
}

@Test
void testInjectedInstancesShareTheSameEndpoint(LocalS3Endpoint endpoint, AmazonS3 s3, S3Client s3Client) {
assertEquals(ResolveBeforeAllParametersTest.endpoint, endpoint);
assertDoesNotThrow(() -> s3.headBucket(new com.amazonaws.services.s3.model.HeadBucketRequest("my-bucket")));
assertDoesNotThrow(() -> s3Client.headBucket(b -> b.bucket("my-bucket")));
}

@AfterAll
static void cleanup(LocalS3Endpoint endpoint, AmazonS3 s3, S3Client s3Client) {
assertEquals(ResolveBeforeAllParametersTest.endpoint, endpoint);
assertDoesNotThrow(() -> s3.headBucket(new com.amazonaws.services.s3.model.HeadBucketRequest("my-bucket")));
assertDoesNotThrow(() -> s3Client.headBucket(HeadBucketRequest.builder().bucket("my-bucket").build()));
assertDoesNotThrow(() -> s3Client.deleteBucket(builder -> builder.bucket("my-bucket")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.robothy.s3.jupiter.extensions;

import com.amazonaws.services.s3.AmazonS3;
import com.robothy.s3.jupiter.LocalS3;
import com.robothy.s3.jupiter.LocalS3Endpoint;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

@LocalS3
public class ResolveBeforeEachParametersTest {

private LocalS3Endpoint endpoint;

@BeforeEach
void beforeEach(LocalS3Endpoint endpoint, AmazonS3 s3, S3Client s3Client) {
this.endpoint = endpoint;
assertDoesNotThrow(() -> s3.createBucket(new com.amazonaws.services.s3.model.CreateBucketRequest("my-bucket")));
assertDoesNotThrow(() -> s3Client.headBucket(builder -> builder.bucket("my-bucket")));
}

@Order(1)
@Test
void testInjectedInstancesShareTheSameEndpoint(LocalS3Endpoint endpoint, AmazonS3 s3, S3Client s3Client) {
assertEquals(this.endpoint, endpoint);
assertDoesNotThrow(() -> s3.headBucket(new com.amazonaws.services.s3.model.HeadBucketRequest("my-bucket")));
assertDoesNotThrow(() -> s3Client.headBucket(builder -> builder.bucket("my-bucket")));
}

@Order(2)
@Test
void testInjectedInstancesShareTheSameEndpoint2(LocalS3Endpoint endpoint, AmazonS3 s3, S3Client s3Client) {
assertEquals(this.endpoint, endpoint);
assertDoesNotThrow(() -> s3.headBucket(new com.amazonaws.services.s3.model.HeadBucketRequest("my-bucket")));
assertDoesNotThrow(() -> s3Client.headBucket(builder -> builder.bucket("my-bucket")));
}

@AfterEach
void afterEach(LocalS3Endpoint endpoint, AmazonS3 s3, S3Client client) {
assertEquals(this.endpoint, endpoint);
assertDoesNotThrow(() -> s3.headBucket(new com.amazonaws.services.s3.model.HeadBucketRequest("my-bucket")));
assertDoesNotThrow(() -> client.headBucket(HeadBucketRequest.builder().bucket("my-bucket").build()));
assertDoesNotThrow(() -> client.deleteBucket(builder -> builder.bucket("my-bucket")));
}

}
Loading