You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/06/21 09:16:31 UTC
[james-project] 08/12: JAMES-3167 Add MailboxReactorUtils
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 013d7a5d91e05b69934041ce8614b3b865e71d6f
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Wed May 27 14:07:06 2020 +0200
JAMES-3167 Add MailboxReactorUtils
---
.../james/mailbox/store/MailboxReactorUtils.java | 52 +++++++++++++++++++++
.../mailbox/store/MailboxReactorUtilsTest.java | 54 ++++++++++++++++++++++
2 files changed, 106 insertions(+)
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/MailboxReactorUtils.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/MailboxReactorUtils.java
new file mode 100644
index 0000000..c8e3cd3
--- /dev/null
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/MailboxReactorUtils.java
@@ -0,0 +1,52 @@
+/****************************************************************
+ * 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.mailbox.store;
+
+import java.util.Optional;
+
+import org.apache.james.mailbox.exception.MailboxException;
+
+import reactor.core.publisher.Mono;
+
+public abstract class MailboxReactorUtils {
+ public static <T> T block(Mono<T> publisher) throws MailboxException {
+ try {
+ return publisher.block();
+ } catch (RuntimeException e) {
+ if (e.getCause() instanceof MailboxException) {
+ throw (MailboxException) e.getCause();
+ }
+
+ throw e;
+ }
+ }
+
+ public static <T> Optional<T> blockOptional(Mono<T> publisher) throws MailboxException {
+ try {
+ return publisher.blockOptional();
+ } catch (RuntimeException e) {
+ if (e.getCause() instanceof MailboxException) {
+ throw (MailboxException) e.getCause();
+ }
+
+ throw e;
+ }
+ }
+}
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/MailboxReactorUtilsTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/MailboxReactorUtilsTest.java
new file mode 100644
index 0000000..caf5e2f
--- /dev/null
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/MailboxReactorUtilsTest.java
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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.mailbox.store;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.mailbox.exception.MailboxException;
+import org.junit.jupiter.api.Test;
+
+import reactor.core.publisher.Mono;
+
+class MailboxReactorUtilsTest {
+ @Test
+ void blockJustShouldReturnTheValue() throws Exception {
+ assertThat(MailboxReactorUtils.block(Mono.just(42)))
+ .isEqualTo(42);
+ }
+
+ @Test
+ void blockEmptyShouldReturnNull() throws Exception {
+ assertThat((Integer) MailboxReactorUtils.block(Mono.empty()))
+ .isNull();
+ }
+
+ @Test
+ void blockErrorMailboxExceptionShouldThrowMailboxException() {
+ assertThatThrownBy(() -> MailboxReactorUtils.block(Mono.error(new MailboxException())))
+ .isInstanceOf(MailboxException.class);
+ }
+
+ @Test
+ void blockErrorRuntimeExceptionShouldThrowRuntimeException() {
+ assertThatThrownBy(() -> MailboxReactorUtils.block(Mono.error(new RuntimeException())))
+ .isInstanceOf(RuntimeException.class);
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org