JAMES-2582 new configuration for choosing blob storage
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/e6691f50 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e6691f50 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e6691f50 Branch: refs/heads/master Commit: e6691f5074c6321d2e29cdc6894c316cb459d7bf Parents: e00ebe1 Author: tran tien duc <[email protected]> Authored: Thu Nov 1 11:06:44 2018 +0700 Committer: Antoine Duprat <[email protected]> Committed: Mon Nov 5 17:35:58 2018 +0100 ---------------------------------------------------------------------- .../BlobStoreChoosingConfiguration.java | 91 ++++++++++++++ .../BlobStoreChoosingConfigurationTest.java | 119 +++++++++++++++++++ 2 files changed, 210 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/e6691f50/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfiguration.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfiguration.java b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfiguration.java new file mode 100644 index 0000000..2d9fc5c --- /dev/null +++ b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfiguration.java @@ -0,0 +1,91 @@ +/**************************************************************** + * 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.objectstore; + +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang3.StringUtils; + +import com.google.common.base.MoreObjects; + +public class BlobStoreChoosingConfiguration { + + public enum BlobStoreImplName { + CASSANDRA("cassandra"), + SWIFT("swift"); + + static String supportedImplNames() { + return Stream.of(BlobStoreImplName.values()) + .map(BlobStoreImplName::getName) + .collect(Collectors.joining(",")); + } + + static BlobStoreImplName from(String name) { + return Stream.of(values()) + .filter(blobName -> blobName.getName().equalsIgnoreCase(name)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(String.format("%s is not a valid name of BlobStores, " + + "please use one of supported values in: %s", name, supportedImplNames()))); + } + + private final String name; + + BlobStoreImplName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + static final String BLOBSTORE_IMPLEMENTATION_PROPERTY = "objectstore.implementation"; + + static BlobStoreChoosingConfiguration from(Configuration configuration) { + BlobStoreImplName blobStoreImplName = Optional.ofNullable(configuration.getString(BLOBSTORE_IMPLEMENTATION_PROPERTY)) + .filter(StringUtils::isNotBlank) + .map(StringUtils::trim) + .map(BlobStoreImplName::from) + .orElseThrow(() -> new IllegalStateException(String.format("%s property is missing please use one of " + + "supported values in: %s", BLOBSTORE_IMPLEMENTATION_PROPERTY, BlobStoreImplName.supportedImplNames()))); + + return new BlobStoreChoosingConfiguration(blobStoreImplName); + } + + private final BlobStoreImplName implementation; + + BlobStoreChoosingConfiguration(BlobStoreImplName implementation) { + this.implementation = implementation; + } + + BlobStoreImplName getImplementation() { + return implementation; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("implementation", implementation) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/e6691f50/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfigurationTest.java b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfigurationTest.java new file mode 100644 index 0000000..34b4b7a --- /dev/null +++ b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/objectstore/BlobStoreChoosingConfigurationTest.java @@ -0,0 +1,119 @@ +/**************************************************************** + * 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.objectstore; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.junit.jupiter.api.Test; + +class BlobStoreChoosingConfigurationTest { + + private static final String SWIFT = "swift"; + private static final String CASSANDRA = "cassandra"; + + @Test + void fromShouldThrowWhenBlobStoreImplIsMissing() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + + assertThatThrownBy(() -> BlobStoreChoosingConfiguration.from(configuration)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("objectstore.implementation property is missing please use one of supported values in: cassandra,swift"); + } + + @Test + void fromShouldReturnSwiftWhenBlobStoreImplIsNull() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("objectstore.implementation", null); + + assertThatThrownBy(() -> BlobStoreChoosingConfiguration.from(configuration)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("objectstore.implementation property is missing please use one of supported values in: cassandra,swift"); + } + + @Test + void fromShouldThrowWhenBlobStoreImplIsEmpty() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("objectstore.implementation", ""); + + assertThatThrownBy(() -> BlobStoreChoosingConfiguration.from(configuration)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("objectstore.implementation property is missing please use one of supported values in: cassandra,swift"); + } + + @Test + void fromShouldThrowWhenBlobStoreImplIsNotInSupportedList() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("objectstore.implementation", "un_supported"); + + assertThatThrownBy(() -> BlobStoreChoosingConfiguration.from(configuration)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("un_supported is not a valid name of BlobStores, please use one of supported values in: cassandra,swift"); + } + + @Test + void fromShouldReturnConfigurationWhenBlobStoreImplIsCassandra() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("objectstore.implementation", CASSANDRA); + + assertThat( + BlobStoreChoosingConfiguration.from(configuration) + .getImplementation() + .getName()) + .isEqualTo(CASSANDRA); + } + + @Test + void fromShouldReturnConfigurationWhenBlobStoreImplIsSwift() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("objectstore.implementation", SWIFT); + + assertThat( + BlobStoreChoosingConfiguration.from(configuration) + .getImplementation() + .getName()) + .isEqualTo(SWIFT); + } + + @Test + void fromShouldReturnConfigurationWhenBlobStoreImplIsSupportedAndCaseInsensitive() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("objectstore.implementation", "SWifT"); + + assertThat( + BlobStoreChoosingConfiguration.from(configuration) + .getImplementation() + .getName()) + .isEqualTo(SWIFT); + } + + @Test + void fromShouldReturnConfigurationWhenBlobStoreImplIsSupportedAndHasExtraSpaces() { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + configuration.addProperty("objectstore.implementation", " cassandra "); + + assertThat( + BlobStoreChoosingConfiguration.from(configuration) + .getImplementation() + .getName()) + .isEqualTo(CASSANDRA); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
