+ private final Sone sone = mock(Sone.class);
+
+ @Before
+ public void setupSone() {
+ when(sone.getId()).thenReturn(SONE_ID);
+ }
+
+ @Test
+ public void postRecipientsAreDetectedCorrectly() {
+ Post postWithRecipient = createPost(of(RECIPIENT_ID));
+ memoryDatabase.storePost(postWithRecipient);
+ 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;
+ }