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;
/**
*/
@Override
public Optional<Sone> getRecipient() {
- return Optional.fromNullable(recipientId).transform(new Function<String, Sone>() {
- @Nullable
- @Override
- public Sone apply(String input) {
- return soneProvider.getSone(input);
- }
- });
+ return recipientId == null ? Optional.<Sone>absent() : fromNullable(soneProvider.getSone(recipientId));
}
/**
class MemoryPostTest {
+ private val memoryDatabase = mock<MemoryDatabase>()
+
@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")
+ 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()))
}