This is an automated email from the ASF dual-hosted git repository. rouazana pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 2b8c1fba817bc162718d813129a7a217cee43c2b Author: Antoine Duprat <adup...@linagora.com> AuthorDate: Mon Feb 25 15:28:41 2019 +0100 JAMES-2669 Define object storage provider as an enum --- .../ObjectStorageBlobConfiguration.java | 22 ++++++------ .../ObjectStorageDependenciesModule.java | 4 +-- .../objectstorage/ObjectStorageProvider.java | 41 ++++++++++++++++++++++ .../ObjectStorageBlobConfigurationTest.java | 6 ++-- .../ObjectStorageBlobStoreModuleTest.java | 6 ++-- .../objectstorage/ObjectStorageProviderTest.java | 41 ++++++++++++++++++++++ .../objectstorage/guice/DockerSwiftTestRule.java | 3 +- 7 files changed, 103 insertions(+), 20 deletions(-) diff --git a/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfiguration.java b/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfiguration.java index 586771c..01903e0 100644 --- a/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfiguration.java +++ b/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfiguration.java @@ -74,7 +74,7 @@ public class ObjectStorageBlobConfiguration { Builder.RequireAuthConfiguration requireAuthConfiguration = builder() .codec(payloadCodecFactory) - .swift() + .provider(ObjectStorageProvider.from(provider)) .container(ContainerName.of(namespace)); return defineAuthApi(configuration, authApi, requireAuthConfiguration) @@ -97,7 +97,7 @@ public class ObjectStorageBlobConfiguration { public static Builder.RequirePayloadCodec builder() { - return payloadCodec -> () -> container -> new Builder.RequireAuthConfiguration(payloadCodec,"swift", container); + return payloadCodec -> provider -> container -> new Builder.RequireAuthConfiguration(payloadCodec, provider, container); } public interface Builder { @@ -108,7 +108,7 @@ public class ObjectStorageBlobConfiguration { @FunctionalInterface public interface RequireProvider { - RequireContainerName swift(); + RequireContainerName provider(ObjectStorageProvider provider); } @FunctionalInterface @@ -119,10 +119,10 @@ public class ObjectStorageBlobConfiguration { public static class RequireAuthConfiguration { private final PayloadCodecFactory payloadCodec; - private final String provider; + private final ObjectStorageProvider provider; private final ContainerName container; - private RequireAuthConfiguration(PayloadCodecFactory payloadCodec, String provider, ContainerName container) { + private RequireAuthConfiguration(PayloadCodecFactory payloadCodec, ObjectStorageProvider provider, ContainerName container) { this.payloadCodec = payloadCodec; this.provider = provider; this.container = container; @@ -144,7 +144,7 @@ public class ObjectStorageBlobConfiguration { public static class ReadyToBuild { private final PayloadCodecFactory payloadCodecFactory; - private final String provider; + private final ObjectStorageProvider provider; private final ContainerName container; private final String authApiName; private final Optional<SwiftTempAuthObjectStorage.Configuration> tempAuth; @@ -153,7 +153,8 @@ public class ObjectStorageBlobConfiguration { private Optional<String> aesSalt; private Optional<char[]> aesPassword; - public ReadyToBuild(PayloadCodecFactory payloadCodecFactory, String provider, ContainerName container, String authApiName, + public ReadyToBuild(PayloadCodecFactory payloadCodecFactory, ObjectStorageProvider provider, + ContainerName container, String authApiName, Optional<SwiftTempAuthObjectStorage.Configuration> tempAuth, Optional<SwiftKeystone2ObjectStorage.Configuration> keystone2Configuration, Optional<SwiftKeystone3ObjectStorage.Configuration> keystone3Configuration) { @@ -205,7 +206,7 @@ public class ObjectStorageBlobConfiguration { private final PayloadCodecFactory payloadCodec; private final String authApi; private final ContainerName namespace; - private final String provider; + private final ObjectStorageProvider provider; private final Optional<SwiftTempAuthObjectStorage.Configuration> tempAuth; private final Optional<SwiftKeystone2ObjectStorage.Configuration> keystone2Configuration; private final Optional<SwiftKeystone3ObjectStorage.Configuration> keystone3Configuration; @@ -213,7 +214,8 @@ public class ObjectStorageBlobConfiguration { private Optional<char[]> aesPassword; @VisibleForTesting - ObjectStorageBlobConfiguration(PayloadCodecFactory payloadCodec, String provider, ContainerName namespace, + ObjectStorageBlobConfiguration(PayloadCodecFactory payloadCodec, ObjectStorageProvider provider, + ContainerName namespace, Optional<String> aesSalt, Optional<char[]> aesPassword, String authApi, Optional<SwiftTempAuthObjectStorage.Configuration> tempAuth, @@ -238,7 +240,7 @@ public class ObjectStorageBlobConfiguration { return namespace; } - public String getProvider() { + public ObjectStorageProvider getProvider() { return provider; } diff --git a/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageDependenciesModule.java b/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageDependenciesModule.java index 8cb907a..209ad4b 100644 --- a/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageDependenciesModule.java +++ b/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageDependenciesModule.java @@ -44,8 +44,6 @@ import com.google.inject.Scopes; public class ObjectStorageDependenciesModule extends AbstractModule { - private static final String OBJECTSTORAGE_PROVIDER_SWIFT = "swift"; - @Override protected void configure() { bind(BlobId.Factory.class).to(HashBlobId.Factory.class).in(Scopes.SINGLETON); @@ -75,7 +73,7 @@ public class ObjectStorageDependenciesModule extends AbstractModule { } private ObjectStorageBlobsDAOBuilder.RequireContainerName selectDaoBuilder(ObjectStorageBlobConfiguration configuration) { - if (!configuration.getProvider().equals(OBJECTSTORAGE_PROVIDER_SWIFT)) { + if (configuration.getProvider() != ObjectStorageProvider.SWIFT) { throw new IllegalArgumentException("unknown provider " + configuration.getProvider()); } switch (configuration.getAuthApi()) { diff --git a/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageProvider.java b/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageProvider.java new file mode 100644 index 0000000..d89151d --- /dev/null +++ b/server/container/guice/blob-objectstorage-guice/src/main/java/org/apache/james/modules/objectstorage/ObjectStorageProvider.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.james.modules.objectstorage; + +import java.util.Arrays; + +import org.apache.commons.configuration.ConfigurationException; + +public enum ObjectStorageProvider { + SWIFT("swift"); + + private final String name; + + ObjectStorageProvider(String name) { + this.name = name; + } + + public static ObjectStorageProvider from(String provider) throws ConfigurationException { + return Arrays.stream(values()) + .filter(value -> value.name.equals(provider)) + .findFirst() + .orElseThrow(() -> new ConfigurationException("Unknown object storage provider: " + provider)); + } +} diff --git a/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfigurationTest.java b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfigurationTest.java index 7157ccf..8dabe90 100644 --- a/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfigurationTest.java +++ b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobConfigurationTest.java @@ -176,7 +176,7 @@ class ObjectStorageBlobConfigurationTest { .isEqualTo( ObjectStorageBlobConfiguration.builder() .codec(PayloadCodecFactory.DEFAULT) - .swift() + .provider(ObjectStorageProvider.SWIFT) .container(ContainerName.of("foo")) .tempAuth(SwiftTempAuthObjectStorage.configBuilder() .endpoint(URI.create("http://swift/endpoint")) @@ -206,7 +206,7 @@ class ObjectStorageBlobConfigurationTest { .isEqualTo( ObjectStorageBlobConfiguration.builder() .codec(PayloadCodecFactory.DEFAULT) - .swift() + .provider(ObjectStorageProvider.SWIFT) .container(ContainerName.of("foo")) .keystone2(SwiftKeystone2ObjectStorage.configBuilder() .endpoint(URI.create("http://swift/endpoint")) @@ -235,7 +235,7 @@ class ObjectStorageBlobConfigurationTest { .isEqualTo( ObjectStorageBlobConfiguration.builder() .codec(PayloadCodecFactory.DEFAULT) - .swift() + .provider(ObjectStorageProvider.SWIFT) .container(ContainerName.of("foo")) .keystone3(SwiftKeystone3ObjectStorage.configBuilder() .endpoint(URI.create("http://swift/endpoint")) diff --git a/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobStoreModuleTest.java b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobStoreModuleTest.java index 3678f22..19b2d87 100644 --- a/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobStoreModuleTest.java +++ b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageBlobStoreModuleTest.java @@ -73,7 +73,7 @@ class ObjectStorageBlobStoreModuleTest { ObjectStorageBlobConfiguration tmpAuth = ObjectStorageBlobConfiguration.builder() .codec(PayloadCodecFactory.DEFAULT) - .swift() + .provider(ObjectStorageProvider.SWIFT) .container(generateContainerName()) .tempAuth(SwiftTempAuthObjectStorage.configBuilder() .endpoint(dockerSwift.swiftEndpoint()) @@ -86,7 +86,7 @@ class ObjectStorageBlobStoreModuleTest { .build(); ObjectStorageBlobConfiguration keystone2 = ObjectStorageBlobConfiguration.builder() .codec(PayloadCodecFactory.DEFAULT) - .swift() + .provider(ObjectStorageProvider.SWIFT) .container(generateContainerName()) .keystone2(SwiftKeystone2ObjectStorage.configBuilder() .endpoint(dockerSwift.keystoneV2Endpoint()) @@ -97,7 +97,7 @@ class ObjectStorageBlobStoreModuleTest { .build(); ObjectStorageBlobConfiguration keystone3 = ObjectStorageBlobConfiguration.builder() .codec(PayloadCodecFactory.DEFAULT) - .swift() + .provider(ObjectStorageProvider.SWIFT) .container(generateContainerName()) .keystone3(SwiftKeystone3ObjectStorage.configBuilder() .endpoint(dockerSwift.keystoneV3Endpoint()) diff --git a/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageProviderTest.java b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageProviderTest.java new file mode 100644 index 0000000..2899956 --- /dev/null +++ b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/ObjectStorageProviderTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.james.modules.objectstorage; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.commons.configuration.ConfigurationException; +import org.junit.jupiter.api.Test; + +class ObjectStorageProviderTest { + + @Test + void fromShouldThrowWhenUnkownObjectStorage() { + assertThatThrownBy(() -> ObjectStorageProvider.from("unknown")) + .isInstanceOf(RuntimeException.class) + .hasMessage("Unknown object storage provider: unknown"); + } + + @Test + void fromShouldReturnThenObjectStorage() throws ConfigurationException { + assertThat(ObjectStorageProvider.from("swift")).isEqualTo(ObjectStorageProvider.SWIFT); + } +} \ No newline at end of file diff --git a/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/guice/DockerSwiftTestRule.java b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/guice/DockerSwiftTestRule.java index d813f85..f3ad780 100644 --- a/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/guice/DockerSwiftTestRule.java +++ b/server/container/guice/blob-objectstorage-guice/src/test/java/org/apache/james/modules/objectstorage/guice/DockerSwiftTestRule.java @@ -33,6 +33,7 @@ import org.apache.james.blob.objectstorage.swift.SwiftKeystone2ObjectStorage; import org.apache.james.blob.objectstorage.swift.TenantName; import org.apache.james.blob.objectstorage.swift.UserName; import org.apache.james.modules.objectstorage.ObjectStorageBlobConfiguration; +import org.apache.james.modules.objectstorage.ObjectStorageProvider; import org.apache.james.modules.objectstorage.PayloadCodecFactory; import org.apache.james.utils.GuiceProbe; import org.junit.runner.Description; @@ -107,7 +108,7 @@ public class DockerSwiftTestRule implements GuiceModuleTestRule { ContainerName containerName = ContainerName.of(UUID.randomUUID().toString()); ObjectStorageBlobConfiguration configuration = ObjectStorageBlobConfiguration.builder() .codec(payloadCodecFactory) - .swift() + .provider(ObjectStorageProvider.SWIFT) .container(containerName) .keystone2(authConfiguration) .aesSalt("c603a7327ee3dcbc031d8d34b1096c605feca5e1") --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org