Add test for FreenetLinkPart
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneChangeDetectorTest.java
1 package net.pterodactylus.sone.core;
2
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;
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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25  */
26 public class SoneChangeDetectorTest {
27
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);
45
46         @Before
47         public void setupPosts() {
48                 when(oldSone.getPosts()).thenReturn(asList(oldPost, removedPost));
49                 when(newSone.getPosts()).thenReturn(asList(oldPost, newPost));
50         }
51
52         @Before
53         public void setupPostProcessors() {
54                 soneChangeDetector.onNewPosts(newPostProcessor);
55                 soneChangeDetector.onRemovedPosts(removedPostProcessor);
56         }
57
58         @Before
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)));
65         }
66
67         @Before
68         public void setupPostReplyProcessors() {
69                 soneChangeDetector.onNewPostReplies(newPostReplyProcessor);
70                 soneChangeDetector.onRemovedPostReplies(removedPostReplyProcessor);
71         }
72
73         @Test
74         public void changeDetectorDetectsChanges() {
75                 soneChangeDetector.detectChanges(newSone);
76
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);
83
84                 verify(newPostReplyProcessor).processPostReply(newPostReply);
85                 verify(newPostReplyProcessor, never()).processPostReply(oldPostReply);
86                 verify(newPostReplyProcessor, never()).processPostReply(
87                                 removedPostReply);
88                 verify(removedPostReplyProcessor).processPostReply(removedPostReply);
89                 verify(removedPostReplyProcessor, never()).processPostReply(
90                                 oldPostReply);
91                 verify(removedPostReplyProcessor, never()).processPostReply(
92                                 newPostReply);
93         }
94
95         @Test
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));
106         }
107
108 }