1 package net.pterodactylus.sone.core;
3 import static java.util.Arrays.asList;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.never;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.when;
10 import java.util.HashSet;
12 import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
13 import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
14 import net.pterodactylus.sone.data.Post;
15 import net.pterodactylus.sone.data.PostReply;
16 import net.pterodactylus.sone.data.Sone;
18 import org.junit.Before;
19 import org.junit.Test;
22 * Unit test for {@link SoneChangeDetector}.
24 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26 public class SoneChangeDetectorTest {
28 private final Sone oldSone = mock(Sone.class);
29 private final Sone newSone = mock(Sone.class);
30 private final SoneChangeDetector soneChangeDetector =
31 new SoneChangeDetector(oldSone);
32 private final Post oldPost = mock(Post.class);
33 private final Post removedPost = mock(Post.class);
34 private final Post newPost = mock(Post.class);
35 private final PostProcessor newPostProcessor = mock(PostProcessor.class);
36 private final PostProcessor removedPostProcessor =
37 mock(PostProcessor.class);
38 private final PostReply oldPostReply = mock(PostReply.class);
39 private final PostReply removedPostReply = mock(PostReply.class);
40 private final PostReply newPostReply = mock(PostReply.class);
41 private final PostReplyProcessor newPostReplyProcessor =
42 mock(PostReplyProcessor.class);
43 private final PostReplyProcessor removedPostReplyProcessor =
44 mock(PostReplyProcessor.class);
47 public void setupPosts() {
48 when(oldSone.getPosts()).thenReturn(asList(oldPost, removedPost));
49 when(newSone.getPosts()).thenReturn(asList(oldPost, newPost));
53 public void setupPostProcessors() {
54 soneChangeDetector.onNewPosts(newPostProcessor);
55 soneChangeDetector.onRemovedPosts(removedPostProcessor);
59 public void setupPostReplies() {
60 when(oldSone.getReplies()).thenReturn(
61 new HashSet<PostReply>(
62 asList(oldPostReply, removedPostReply)));
63 when(newSone.getReplies()).thenReturn(
64 new HashSet<PostReply>(asList(oldPostReply, newPostReply)));
68 public void setupPostReplyProcessors() {
69 soneChangeDetector.onNewPostReplies(newPostReplyProcessor);
70 soneChangeDetector.onRemovedPostReplies(removedPostReplyProcessor);
74 public void changeDetectorDetectsChanges() {
75 soneChangeDetector.detectChanges(newSone);
77 verify(newPostProcessor).processPost(newPost);
78 verify(newPostProcessor, never()).processPost(oldPost);
79 verify(newPostProcessor, never()).processPost(removedPost);
80 verify(removedPostProcessor).processPost(removedPost);
81 verify(removedPostProcessor, never()).processPost(oldPost);
82 verify(removedPostProcessor, never()).processPost(newPost);
84 verify(newPostReplyProcessor).processPostReply(newPostReply);
85 verify(newPostReplyProcessor, never()).processPostReply(oldPostReply);
86 verify(newPostReplyProcessor, never()).processPostReply(
88 verify(removedPostReplyProcessor).processPostReply(removedPostReply);
89 verify(removedPostReplyProcessor, never()).processPostReply(
91 verify(removedPostReplyProcessor, never()).processPostReply(
96 public void changeDetectorDoesNotNotifyAnyProcessorIfProcessorsUnset() {
97 soneChangeDetector.onNewPosts(null);
98 soneChangeDetector.onRemovedPosts(null);
99 soneChangeDetector.onNewPostReplies(null);
100 soneChangeDetector.onRemovedPostReplies(null);
101 soneChangeDetector.detectChanges(newSone);
102 verify(newPostProcessor, never()).processPost(any(Post.class));
103 verify(removedPostProcessor, never()).processPost(any(Post.class));
104 verify(newPostReplyProcessor, never()).processPostReply(any(PostReply.class));
105 verify(removedPostReplyProcessor, never()).processPostReply(any(PostReply.class));