From 62828ea81428d7b868426375d37af661834c268b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 21 Feb 2019 07:18:38 +0100 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20return=20null=20to=20function=20?= =?utf8?q?not=20allowing=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/database/memory/MemoryPost.java | 11 +---------- .../net/pterodactylus/sone/database/memory/MemoryPostTest.kt | 11 +++++++++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java index 75eae49..5c294c0 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java @@ -21,13 +21,10 @@ import static com.google.common.base.Optional.fromNullable; import java.util.UUID; -import javax.annotation.Nullable; - import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.SoneProvider; -import com.google.common.base.Function; import com.google.common.base.Optional; /** @@ -123,13 +120,7 @@ class MemoryPost implements Post { */ @Override public Optional getRecipient() { - return Optional.fromNullable(recipientId).transform(new Function() { - @Nullable - @Override - public Sone apply(String input) { - return soneProvider.getSone(input); - } - }); + return recipientId == null ? Optional.absent() : fromNullable(soneProvider.getSone(recipientId)); } /** diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryPostTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryPostTest.kt index 611ad70..90344e1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryPostTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryPostTest.kt @@ -9,10 +9,17 @@ import java.util.* class MemoryPostTest { + private val memoryDatabase = mock() + @Test fun `memory post returns empty optional for post without recipient`() { - val postDatabase = mock() - val memoryPost = MemoryPost(postDatabase, postDatabase, UUID.randomUUID().toString(), "soneId", null, 123, "text") + val memoryPost = MemoryPost(memoryDatabase, memoryDatabase, UUID.randomUUID().toString(), "soneId", null, 123, "text") + assertThat(memoryPost.recipient, equalTo(absent())) + } + + @Test + fun `empty optional is returned if recipient is set but non-existent`() { + val memoryPost = MemoryPost(memoryDatabase, memoryDatabase, UUID.randomUUID().toString(), "soneId", "recipientId", 123, "text") assertThat(memoryPost.recipient, equalTo(absent())) } -- 2.7.4