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 2019/01/11 09:58:17 UTC
[4/8] james-project git commit: JAMES-2630 Add Reactor utils
JAMES-2630 Add Reactor utils
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/92842f40
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/92842f40
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/92842f40
Branch: refs/heads/master
Commit: 92842f40a6efba09fcbdb9f1295bd2cd719be6d7
Parents: 08c2f45
Author: Gautier DI FOLCO <gd...@linagora.com>
Authored: Tue Jan 8 09:46:52 2019 +0100
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Fri Jan 11 16:57:08 2019 +0700
----------------------------------------------------------------------
.../org/apache/james/util/ReactorUtils.java | 27 ++++++++
.../org/apache/james/util/ReactorUtilsTest.java | 72 ++++++++++++++++++++
2 files changed, 99 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/92842f40/server/container/util/src/main/java/org/apache/james/util/ReactorUtils.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/main/java/org/apache/james/util/ReactorUtils.java b/server/container/util/src/main/java/org/apache/james/util/ReactorUtils.java
new file mode 100644
index 0000000..42e6d8e
--- /dev/null
+++ b/server/container/util/src/main/java/org/apache/james/util/ReactorUtils.java
@@ -0,0 +1,27 @@
+/****************************************************************
+ * 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.util;
+
+import reactor.core.publisher.Mono;
+
+public class ReactorUtils {
+ public static <T> Mono<T> executeAndEmpty(Runnable runnable) {
+ return Mono.fromRunnable(runnable).then(Mono.empty());
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/92842f40/server/container/util/src/test/java/org/apache/james/util/ReactorUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/test/java/org/apache/james/util/ReactorUtilsTest.java b/server/container/util/src/test/java/org/apache/james/util/ReactorUtilsTest.java
new file mode 100644
index 0000000..3c991e0
--- /dev/null
+++ b/server/container/util/src/test/java/org/apache/james/util/ReactorUtilsTest.java
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+import reactor.core.publisher.Mono;
+
+class ReactorUtilsTest {
+
+ @Nested
+ class ExecuteAndEmpty {
+ @Test
+ void shouldExecuteTheRunnableAndReturnEmpty() {
+ Counter counter = new Counter(1);
+
+ Mono<?> reactor = Mono.empty()
+ .switchIfEmpty(ReactorUtils.executeAndEmpty(() -> counter.increment(2)))
+ .map(FunctionalUtils.toFunction(any -> counter.increment(4)));
+
+ assertThat(reactor.hasElement().block()).isFalse();
+ assertThat(counter.getCounter()).isEqualTo(3);
+ }
+
+ @Test
+ void shouldNotExecuteTheRunnableAndReturnTheValue() {
+ Counter counter = new Counter(1);
+
+ Mono<?> reactor = Mono.just(42)
+ .switchIfEmpty(ReactorUtils.executeAndEmpty(() -> counter.increment(2)))
+ .map(FunctionalUtils.toFunction(any -> counter.increment(4)));
+
+ assertThat(reactor.hasElement().block()).isTrue();
+ assertThat(counter.getCounter()).isEqualTo(5);
+ }
+
+ private class Counter {
+ private Integer counter;
+
+ public Counter(Integer counter) {
+ this.counter = counter;
+ }
+
+ public void increment(Integer other) {
+ counter += other;
+ }
+
+ public Integer getCounter() {
+ return counter;
+ }
+ }
+ }
+}
\ 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