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 651044a1ea8784b44c39d40fd7d87777e06ef977 Author: Tran Tien Duc <[email protected]> AuthorDate: Mon Apr 22 15:51:54 2019 +0700 JAMES-2742 MailRepositoryPropertiesStore API + Contract + Memory Impl --- .../api/MailRepositoryProperties.java | 34 ++++-- .../api/MailRepositoryPropertiesStore.java | 29 +++++ .../api/MailRepositoryPropertiesStoreContract.java | 119 +++++++++++++++++++++ .../MemoryMailRepositoryPropertiesStore.java | 51 +++++++++ .../MemoryMailRepositoryPropertiesStoreTest.java | 39 +++++++ 5 files changed, 263 insertions(+), 9 deletions(-) diff --git a/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryProperties.java b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryProperties.java index 1822b0d..cd78e2b 100644 --- a/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryProperties.java +++ b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryProperties.java @@ -25,21 +25,37 @@ import com.google.common.base.MoreObjects; public class MailRepositoryProperties { - public static class Builder { - private boolean browsable; + static class Builder { - public Builder browsable(boolean browsable) { - this.browsable = browsable; - return this; + @FunctionalInterface + interface RequireBrowsable { + ReadyToBuild browsable(boolean browsable); + + default ReadyToBuild canBrowse() { + return browsable(true); + } + + default ReadyToBuild canNotBrowse() { + return browsable(false); + } } - MailRepositoryProperties build() { - return new MailRepositoryProperties(browsable); + static class ReadyToBuild { + + private final boolean browsable; + + ReadyToBuild(boolean browsable) { + this.browsable = browsable; + } + + MailRepositoryProperties build() { + return new MailRepositoryProperties(browsable); + } } } - public static Builder builder() { - return new Builder(); + static Builder.RequireBrowsable builder() { + return Builder.ReadyToBuild::new; } private final boolean browsable; diff --git a/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryPropertiesStore.java b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryPropertiesStore.java new file mode 100644 index 0000000..b129097 --- /dev/null +++ b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryPropertiesStore.java @@ -0,0 +1,29 @@ +/**************************************************************** + * 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.mailrepository.api; + +import org.reactivestreams.Publisher; + +public interface MailRepositoryPropertiesStore { + + Publisher<Void> store(MailRepositoryUrl url, MailRepositoryProperties properties); + + Publisher<MailRepositoryProperties> retrieve(MailRepositoryUrl url); +} diff --git a/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/api/MailRepositoryPropertiesStoreContract.java b/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/api/MailRepositoryPropertiesStoreContract.java new file mode 100644 index 0000000..72c99e2 --- /dev/null +++ b/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/api/MailRepositoryPropertiesStoreContract.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.mailrepository.api; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +import reactor.core.publisher.Mono; + +public interface MailRepositoryPropertiesStoreContract { + + MailRepositoryProperties NON_BROWSABLE = MailRepositoryProperties.builder() + .canNotBrowse() + .build(); + MailRepositoryProperties BROWSABLE = MailRepositoryProperties.builder() + .canBrowse() + .build(); + + MailRepositoryUrl URL_1 = MailRepositoryUrl.from("protocol://deletedMessages/user1"); + MailRepositoryUrl URL_2 = MailRepositoryUrl.from("protocol://deletedMessages/user2"); + MailRepositoryUrl URL_3 = MailRepositoryUrl.from("protocol://deletedMessages/user3"); + + MailRepositoryPropertiesStore testee(); + + @Test + default void storeShouldStorePropertiesWithBelongingUrl() { + Mono.from(testee().store(URL_1, NON_BROWSABLE)) + .block(); + + assertThat(Mono.from(testee().retrieve(URL_1)).block()) + .isEqualTo(NON_BROWSABLE); + } + + @Test + default void storeShouldOverrideTheResultWhenCalledMultipleTimes() { + Mono.from(testee().store(URL_1, NON_BROWSABLE)).block(); + Mono.from(testee().store(URL_1, BROWSABLE)).block(); + + assertThat(Mono.from(testee().retrieve(URL_1)).block()) + .isEqualTo(BROWSABLE); + } + + @Test + default void storeShouldNotThrowWhenStoringDifferentUrl() { + assertThatCode(() -> { + Mono.from(testee().store(URL_1, NON_BROWSABLE)).block(); + Mono.from(testee().store(URL_2, BROWSABLE)).block(); + }).doesNotThrowAnyException(); + } + + @Test + default void retrieveShouldReturnTheRightPropertiesByTheUrl() { + Mono.from(testee().store(URL_1, NON_BROWSABLE)).block(); + Mono.from(testee().store(URL_2, BROWSABLE)).block(); + + assertThat(Mono.from(testee().retrieve(URL_2)).block()) + .isEqualTo(BROWSABLE); + } + + @Test + default void retrieveShouldReturnEmptyWhenRetrievingByNonExistedUrl() { + Mono.from(testee().store(URL_1, NON_BROWSABLE)).block(); + Mono.from(testee().store(URL_2, BROWSABLE)).block(); + + assertThat(Mono.from(testee().retrieve(URL_3)).blockOptional()) + .isEmpty(); + } + + @Test + default void doingMappingOnTheResultShouldNotThrowWhenEmptyResult() { + assertThatCode(() -> + Mono.from(testee().retrieve(URL_1)) + .map(MailRepositoryProperties::isBrowsable) + .block()) + .doesNotThrowAnyException(); + } + + @Test + default void doingMappingOnTheResultShouldNotThrowAfterANullMappingPipeLineWhenEmptyResult() { + assertThatCode(() -> + Mono.from(testee().retrieve(URL_1)) + .map(properties -> (MailRepositoryProperties) null) + .map(MailRepositoryProperties::isBrowsable) + .block()) + .doesNotThrowAnyException(); + } + + @Test + default void doingMappingOnTheResultShouldThrowAfterANullMappingPipeLineWhenNotEmptyResult() { + Mono.from(testee().store(URL_1, NON_BROWSABLE)).block(); + + assertThatThrownBy(() -> + Mono.from(testee().retrieve(URL_1)) + .map(properties -> (MailRepositoryProperties) null) + .map(MailRepositoryProperties::isBrowsable) + .block()) + .isInstanceOf(NullPointerException.class); + } +} diff --git a/server/mailrepository/mailrepository-memory/src/main/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryPropertiesStore.java b/server/mailrepository/mailrepository-memory/src/main/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryPropertiesStore.java new file mode 100644 index 0000000..82f8dea --- /dev/null +++ b/server/mailrepository/mailrepository-memory/src/main/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryPropertiesStore.java @@ -0,0 +1,51 @@ +/**************************************************************** + * 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.mailrepository.memory; + +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.james.mailrepository.api.MailRepositoryProperties; +import org.apache.james.mailrepository.api.MailRepositoryPropertiesStore; +import org.apache.james.mailrepository.api.MailRepositoryUrl; +import org.reactivestreams.Publisher; + +import com.google.common.annotations.VisibleForTesting; + +import reactor.core.publisher.Mono; + +public class MemoryMailRepositoryPropertiesStore implements MailRepositoryPropertiesStore { + + private final ConcurrentHashMap<MailRepositoryUrl, MailRepositoryProperties> propertiesMap; + + @VisibleForTesting + MemoryMailRepositoryPropertiesStore() { + this.propertiesMap = new ConcurrentHashMap<>(); + } + + @Override + public Publisher<Void> store(MailRepositoryUrl url, MailRepositoryProperties properties) { + return Mono.fromRunnable(() -> propertiesMap.put(url, properties)); + } + + @Override + public Publisher<MailRepositoryProperties> retrieve(MailRepositoryUrl url) { + return Mono.fromSupplier(() -> propertiesMap.get(url)); + } +} diff --git a/server/mailrepository/mailrepository-memory/src/test/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryPropertiesStoreTest.java b/server/mailrepository/mailrepository-memory/src/test/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryPropertiesStoreTest.java new file mode 100644 index 0000000..33b6d66 --- /dev/null +++ b/server/mailrepository/mailrepository-memory/src/test/java/org/apache/james/mailrepository/memory/MemoryMailRepositoryPropertiesStoreTest.java @@ -0,0 +1,39 @@ +/**************************************************************** + * 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.mailrepository.memory; + +import org.apache.james.mailrepository.api.MailRepositoryPropertiesStore; +import org.apache.james.mailrepository.api.MailRepositoryPropertiesStoreContract; +import org.junit.jupiter.api.BeforeEach; + +class MemoryMailRepositoryPropertiesStoreTest implements MailRepositoryPropertiesStoreContract { + + private MemoryMailRepositoryPropertiesStore testee; + + @BeforeEach + void beforeEach() { + testee = new MemoryMailRepositoryPropertiesStore(); + } + + @Override + public MailRepositoryPropertiesStore testee() { + return testee; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
