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

Implement test resources property provider factory #231

Merged
merged 7 commits into from
May 3, 2023

Conversation

melix
Copy link
Collaborator

@melix melix commented Apr 28, 2023

This commit introduces a new annotation, @TestResourceProperties, which can be applied on a test in order to tell that the test needs to resolve one or more test resource properties before the application context is available.

For example, with:

@MicronautTest
@TestResourcesProperties(
    value = "redis.url"
)
class SomeTest { ... }

Then the value of the redis.url property will automatically be fetched from the test resources service and made available as if it had been done in a TestPropertyProvider. The difference is that this without this annotation, it was required to call the test resources client directly in the provider, which was error prone. In particular, the properties argument of the resolve call are not easy to figure out.

This annotation is therefore a convention to avoid having to call the client directly. However, in some cases it might be necessary to read a property in order to compute a different one which needs to be available in the test.

This can be done by adding a providers argument to the annotation:

@TestResourcesProperties(
    value = "rabbitmq.uri",
    providers = RabbitTest.RabbitMQProvider.class
)
class RabbitTest {
  // ...
      @ReflectiveAccess
      public static class RabbitMQProvider implements TestResourcesPropertyProvider {
          @Override
          public Map<String, String> provide(Map<String, Object> testProperties) {
              String uri = (String) testProperties.get("rabbitmq.uri");
              return Map.of(
                  "rabbitmq.servers.product-cluster.port", String.valueOf(URI.create(uri).getPort())
              );
          }
      }
}

The TestResourcesPropertyProvider type has access to all properties which are already resolved at the moment it is called, which includes the properties asked in @TestResourcesProperties.

This should make the implementation of micronaut-projects/micronaut-nats#321 easier.

This commit introduces a new annotation, `@TestResourceProperties`, which
can be applied on a test in order to tell that the test needs to resolve
one or more test resource properties _before_ the application context is
available.

For example, with:

```java
@MicronautTest
@TestResourcesProperties(
    value = "redis.url"
)
class SomeTest { ... }
```

Then the value of the `redis.url` property will automatically be
fetched from the test resources service and made available as if
it had been done in a `TestPropertyProvider`. The difference is
that this without this annotation, it was required to call the
test resources client directly in the provider, which was error
prone. In particular, the `properties` argument of the `resolve`
call are not easy to figure out.

This annotation is therefore a convention to avoid having to
call the client directly. However, in some cases it might be
necessary to read a property in order to compute a different
one which needs to be available in the test.

This can be done by adding a `providers` argument to the
annotation:

```java
@TestResourcesProperties(
    value = "rabbitmq.uri",
    providers = RabbitTest.RabbitMQProvider.class
)
class RabbitTest {
  // ...
      @ReflectiveAccess
      public static class RabbitMQProvider implements TestResourcesPropertyProvider {
          @OverRide
          public Map<String, String> provide(Map<String, Object> testProperties) {
              String uri = (String) testProperties.get("rabbitmq.uri");
              return Map.of(
                  "rabbitmq.servers.product-cluster.port", String.valueOf(URI.create(uri).getPort())
              );
          }
      }
}
```

The `TestResourcesPropertyProvider` type has access to all properties
which are already resolved at the moment it is called, which includes
the properties asked in `@TestResourcesProperties`.

This should make the implementation of micronaut-projects/micronaut-nats#321
easier.
@melix melix added the type: improvement A minor improvement to an existing feature label Apr 28, 2023
@melix melix added this to the 2.0.0-M3 milestone Apr 28, 2023
@melix melix requested a review from graemerocher April 28, 2023 14:36
@melix melix self-assigned this Apr 28, 2023
Copy link
Contributor

@graemerocher graemerocher left a comment

Choose a reason for hiding this comment

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

Concept looks fine needs more javadoc and some reference documentation explaining how to use it

@graemerocher
Copy link
Contributor

Some reference docs would be nice

@melix
Copy link
Collaborator Author

melix commented May 2, 2023

Some reference docs would be nice

Yes, but I'd like to finish the improvements first (there should be another PR on this topic, with scope management).

@melix melix merged commit fd82992 into master May 3, 2023
@melix melix deleted the cc/properties-annotation branch May 3, 2023 15:57
melix added a commit that referenced this pull request May 4, 2023
This commit adds user documentation for the new Micronaut Test
extensions available in test resources.

This is a follow up to #231 and #241
melix added a commit that referenced this pull request May 4, 2023
* Documentation for Micronaut test extensions

This commit adds user documentation for the new Micronaut Test
extensions available in test resources.

This is a follow up to #231 and #241

* Address review comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants