1 package net.pterodactylus.sone.core;
3 import static java.util.Arrays.asList;
4 import static org.mockito.ArgumentMatchers.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 public class SoneChangeDetectorTest {
26 private final Sone oldSone = mock(Sone.class);
27 private final Sone newSone = mock(Sone.class);
28 private final SoneChangeDetector soneChangeDetector =
29 new SoneChangeDetector(oldSone);
30 private final Post oldPost = mock(Post.class);
31 private final Post removedPost = mock(Post.class);
32 private final Post newPost = mock(Post.class);
33 private final PostProcessor newPostProcessor = mock(PostProcessor.class);
34 private final PostProcessor removedPostProcessor =
35 mock(PostProcessor.class);
36 private final PostReply oldPostReply = mock(PostReply.class);
37 private final PostReply removedPostReply = mock(PostReply.class);
38 private final PostReply newPostReply = mock(PostReply.class);
39 private final PostReplyProcessor newPostReplyProcessor =
40 mock(PostReplyProcessor.class);
41 private final PostReplyProcessor removedPostReplyProcessor =
42 mock(PostReplyProcessor.class);
45 public void setupPosts() {
46 when(oldSone.getPosts()).thenReturn(asList(oldPost, removedPost));
47 when(newSone.getPosts()).thenReturn(asList(oldPost, newPost));
51 public void setupPostProcessors() {
52 soneChangeDetector.onNewPosts(newPostProcessor);
53 soneChangeDetector.onRemovedPosts(removedPostProcessor);
57 public void setupPostReplies() {
58 when(oldSone.getReplies()).thenReturn(
59 new HashSet<PostReply>(
60 asList(oldPostReply, removedPostReply)));
61 when(newSone.getReplies()).thenReturn(
62 new HashSet<PostReply>(asList(oldPostReply, newPostReply)));
66 public void setupPostReplyProcessors() {
67 soneChangeDetector.onNewPostReplies(newPostReplyProcessor);
68 soneChangeDetector.onRemovedPostReplies(removedPostReplyProcessor);
72 public void changeDetectorDetectsChanges() {
73 soneChangeDetector.detectChanges(newSone);
75 verify(newPostProcessor).processPost(newPost);
76 verify(newPostProcessor, never()).processPost(oldPost);
77 verify(newPostProcessor, never()).processPost(removedPost);
78 verify(removedPostProcessor).processPost(removedPost);
79 verify(removedPostProcessor, never()).processPost(oldPost);
80 verify(removedPostProcessor, never()).processPost(newPost);
82 verify(newPostReplyProcessor).processPostReply(newPostReply);
83 verify(newPostReplyProcessor, never()).processPostReply(oldPostReply);
84 verify(newPostReplyProcessor, never()).processPostReply(
86 verify(removedPostReplyProcessor).processPostReply(removedPostReply);
87 verify(removedPostReplyProcessor, never()).processPostReply(
89 verify(removedPostReplyProcessor, never()).processPostReply(
94 public void changeDetectorDoesNotNotifyAnyProcessorIfProcessorsUnset() {
95 soneChangeDetector.onNewPosts(null);
96 soneChangeDetector.onRemovedPosts(null);
97 soneChangeDetector.onNewPostReplies(null);
98 soneChangeDetector.onRemovedPostReplies(null);
99 soneChangeDetector.detectChanges(newSone);
100 verify(newPostProcessor, never()).processPost(any(Post.class));
101 verify(removedPostProcessor, never()).processPost(any(Post.class));
102 verify(newPostReplyProcessor, never()).processPostReply(any(PostReply.class));
103 verify(removedPostReplyProcessor, never()).processPostReply(any(PostReply.class));