package net.pterodactylus.sone.database.memory;
import static com.google.common.base.Optional.of;
+import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.AlbumImpl;
import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
import com.google.common.base.Optional;
@Test
public void postRecipientsAreDetectedCorrectly() {
- Post postWithRecipient = mock(Post.class);
- when(postWithRecipient.getSone()).thenReturn(sone);
- when(postWithRecipient.getRecipientId()).thenReturn(of(RECIPIENT_ID));
+ Post postWithRecipient = createPost(of(RECIPIENT_ID));
memoryDatabase.storePost(postWithRecipient);
- Post postWithoutRecipient = mock(Post.class);
- when(postWithoutRecipient.getSone()).thenReturn(sone);
- when(postWithoutRecipient.getRecipientId()).thenReturn(
- Optional.<String>absent());
+ Post postWithoutRecipient = createPost(Optional.<String>absent());
memoryDatabase.storePost(postWithoutRecipient);
assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID),
contains(postWithRecipient));
}
+ private Post createPost(Optional<String> recipient) {
+ Post postWithRecipient = mock(Post.class);
+ when(postWithRecipient.getId()).thenReturn(randomUUID().toString());
+ when(postWithRecipient.getSone()).thenReturn(sone);
+ when(postWithRecipient.getRecipientId()).thenReturn(recipient);
+ return postWithRecipient;
+ }
+
+ @Test
+ public void postRepliesAreManagedCorrectly() {
+ Post firstPost = createPost(Optional.<String>absent());
+ PostReply firstPostFirstReply = createPostReply(firstPost, 1000L);
+ Post secondPost = createPost(Optional.<String>absent());
+ PostReply secondPostFirstReply = createPostReply(secondPost, 1000L);
+ PostReply secondPostSecondReply = createPostReply(secondPost, 2000L);
+ memoryDatabase.storePost(firstPost);
+ memoryDatabase.storePost(secondPost);
+ memoryDatabase.storePostReply(firstPostFirstReply);
+ memoryDatabase.storePostReply(secondPostFirstReply);
+ memoryDatabase.storePostReply(secondPostSecondReply);
+ assertThat(memoryDatabase.getReplies(firstPost.getId()),
+ contains(firstPostFirstReply));
+ assertThat(memoryDatabase.getReplies(secondPost.getId()),
+ contains(secondPostFirstReply, secondPostSecondReply));
+ }
+
+ private PostReply createPostReply(Post post, long time) {
+ PostReply postReply = mock(PostReply.class);
+ when(postReply.getId()).thenReturn(randomUUID().toString());
+ when(postReply.getTime()).thenReturn(time);
+ when(postReply.getPost()).thenReturn(of(post));
+ final String postId = post.getId();
+ when(postReply.getPostId()).thenReturn(postId);
+ return postReply;
+ }
+
@Test
public void testBasicAlbumFunctionality() {
Album newAlbum = new AlbumImpl(mock(Sone.class));