From 4d1e2292a5d0c205343ce60f11074917f08c4e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20S=C3=A1nchez-Mariscal?= Date: Wed, 26 Jul 2023 09:59:01 +0200 Subject: [PATCH] Make LocalStorageOperations the primary bean (#314) --- object-storage-local/build.gradle.kts | 1 + .../local/LocalStorageOperations.java | 2 + .../local/LocalStoragePrimarySpec.groovy | 46 +++++++++++++++++++ src/main/docs/guide/local.adoc | 2 + 4 files changed, 51 insertions(+) create mode 100644 object-storage-local/src/test/groovy/io/micronaut/objectstorage/local/LocalStoragePrimarySpec.groovy diff --git a/object-storage-local/build.gradle.kts b/object-storage-local/build.gradle.kts index 2aa033fa..a7f0702b 100644 --- a/object-storage-local/build.gradle.kts +++ b/object-storage-local/build.gradle.kts @@ -8,6 +8,7 @@ dependencies { testImplementation(mnValidation.micronaut.validation) testImplementation(mnValidation.micronaut.validation.processor) testImplementation(projects.micronautObjectStorageTck) + testImplementation(projects.micronautObjectStorageAws) } micronautBuild { diff --git a/object-storage-local/src/main/java/io/micronaut/objectstorage/local/LocalStorageOperations.java b/object-storage-local/src/main/java/io/micronaut/objectstorage/local/LocalStorageOperations.java index d441fe3e..a374f62d 100644 --- a/object-storage-local/src/main/java/io/micronaut/objectstorage/local/LocalStorageOperations.java +++ b/object-storage-local/src/main/java/io/micronaut/objectstorage/local/LocalStorageOperations.java @@ -17,6 +17,7 @@ import io.micronaut.context.annotation.EachBean; import io.micronaut.context.annotation.Parameter; +import io.micronaut.context.annotation.Primary; import io.micronaut.context.annotation.Requires; import io.micronaut.core.annotation.NonNull; import io.micronaut.objectstorage.ObjectStorageException; @@ -53,6 +54,7 @@ @EachBean(LocalStorageConfiguration.class) @Requires(condition = ToggeableCondition.class) @Requires(beans = LocalStorageConfiguration.class) +@Primary public class LocalStorageOperations implements ObjectStorageOperations< LocalStorageOperations.LocalStorageFile, LocalStorageOperations.LocalStorageFile, diff --git a/object-storage-local/src/test/groovy/io/micronaut/objectstorage/local/LocalStoragePrimarySpec.groovy b/object-storage-local/src/test/groovy/io/micronaut/objectstorage/local/LocalStoragePrimarySpec.groovy new file mode 100644 index 00000000..f11fedda --- /dev/null +++ b/object-storage-local/src/test/groovy/io/micronaut/objectstorage/local/LocalStoragePrimarySpec.groovy @@ -0,0 +1,46 @@ +package io.micronaut.objectstorage.local + +import io.micronaut.context.ApplicationContext +import io.micronaut.context.annotation.Factory +import io.micronaut.context.annotation.Property +import io.micronaut.context.annotation.Requires +import io.micronaut.objectstorage.ObjectStorageOperations +import io.micronaut.test.annotation.MockBean +import io.micronaut.test.extensions.spock.annotation.MicronautTest +import jakarta.inject.Inject +import software.amazon.awssdk.services.s3.S3Client +import spock.lang.Specification + +@Property(name = "spec.name", value = SPEC_NAME) +@Property(name = "micronaut.object-storage.local.default.enabled", value = "true") +@Property(name = "micronaut.object-storage.aws.default.enabled", value = "true") +@MicronautTest +class LocalStoragePrimarySpec extends Specification { + + public static final String SPEC_NAME = 'LocalStoragePrimarySpec' + + @Inject + ApplicationContext ctx + + void "both local and aws storage are enabled"() { + when: + def beans = ctx.getBeansOfType(ObjectStorageOperations) + + then: + beans.size() == 2 + } + + void "local storage is primary"() { + when: + def operations = ctx.getBean(ObjectStorageOperations) + + then: + operations instanceof LocalStorageOperations + } + + @MockBean(S3Client) + S3Client s3Client() { + return Mock(S3Client) + } + +} diff --git a/src/main/docs/guide/local.adoc b/src/main/docs/guide/local.adoc index a2fd6871..54af3088 100644 --- a/src/main/docs/guide/local.adoc +++ b/src/main/docs/guide/local.adoc @@ -13,6 +13,8 @@ micronaut: enabled: true ---- +NOTE: When added to the classpath, api:objectstorage.local.LocalStorageOperations[] becomes the primary implementation of +api:objectstorage.ObjectStorageOperations[]. By default, it will create a temporary folder to store the files, but you can configure it to use a specific folder: