eaa963752881b9e05a988327300701a310ba4d1a
[Sone.git] / SoneChangeDetectorTest.java
1 package net.pterodactylus.sone.core;
2
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;
9
10 import java.util.HashSet;
11
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;
17
18 import org.junit.Before;
19 import org.junit.Test;
20
21 /**
22  * Unit test for {@link SoneChangeDetector}.
23  */
24 public class SoneChangeDetectorTest {
25
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);
43
44         @Before
45         public void setupPosts() {
46                 when(oldSone.getPosts()).thenReturn(asList(oldPost, removedPost));
47                 when(newSone.getPosts()).thenReturn(asList(oldPost, newPost));
48         }
49
50         @Before
51         public void setupPostProcessors() {
52                 soneChangeDetector.onNewPosts(newPostProcessor);
53                 soneChangeDetector.onRemovedPosts(removedPostProcessor);
54         }
55
56         @Before
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)));
63         }
64
65         @Before
66         public void setupPostReplyProcessors() {
67                 soneChangeDetector.onNewPostReplies(newPostReplyProcessor);
68                 soneChangeDetector.onRemovedPostReplies(removedPostReplyProcessor);
69         }
70
71         @Test
72         public void changeDetectorDetectsChanges() {
73                 soneChangeDetector.detectChanges(newSone);
74
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);
81
82                 verify(newPostReplyProcessor).processPostReply(newPostReply);
83                 verify(newPostReplyProcessor, never()).processPostReply(oldPostReply);
84                 verify(newPostReplyProcessor, never()).processPostReply(
85                                 removedPostReply);
86                 verify(removedPostReplyProcessor).processPostReply(removedPostReply);
87                 verify(removedPostReplyProcessor, never()).processPostReply(
88                                 oldPostReply);
89                 verify(removedPostReplyProcessor, never()).processPostReply(
90                                 newPostReply);
91         }
92
93         @Test
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));
104         }
105
106 }