Don’t hand in null to function not allowing it
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 21 Feb 2019 06:04:52 +0000 (07:04 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 21 Feb 2019 06:04:52 +0000 (07:04 +0100)
src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java
src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryPostTest.kt [new file with mode: 0644]

index e0f83c2..75eae49 100644 (file)
@@ -21,10 +21,13 @@ 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;
 
 /**
@@ -120,7 +123,13 @@ class MemoryPost implements Post {
         */
        @Override
        public Optional<Sone> getRecipient() {
-               return fromNullable(soneProvider.getSone(recipientId));
+               return Optional.fromNullable(recipientId).transform(new Function<String, Sone>() {
+                       @Nullable
+                       @Override
+                       public Sone apply(String input) {
+                               return soneProvider.getSone(input);
+                       }
+               });
        }
 
        /**
diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryPostTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryPostTest.kt
new file mode 100644 (file)
index 0000000..611ad70
--- /dev/null
@@ -0,0 +1,19 @@
+package net.pterodactylus.sone.database.memory
+
+import com.google.common.base.Optional.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+import java.util.*
+
+class MemoryPostTest {
+
+       @Test
+       fun `memory post returns empty optional for post without recipient`() {
+               val postDatabase = mock<MemoryDatabase>()
+               val memoryPost = MemoryPost(postDatabase, postDatabase, UUID.randomUUID().toString(), "soneId", null, 123, "text")
+               assertThat(memoryPost.recipient, equalTo(absent()))
+       }
+
+}