dbe70d58582a4d21347cf37e1609cfc6ba7716b1
[Sone.git] / src / test / java / net / pterodactylus / sone / core / CoreTest.java
1 package net.pterodactylus.sone.core;
2
3 import static org.mockito.ArgumentMatchers.eq;
4 import static org.mockito.Mockito.inOrder;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.verify;
7 import static org.mockito.Mockito.when;
8 import static org.mockito.hamcrest.MockitoHamcrest.argThat;
9
10 import net.pterodactylus.sone.core.Core.MarkPostKnown;
11 import net.pterodactylus.sone.core.Core.MarkReplyKnown;
12 import net.pterodactylus.sone.core.event.PostRemovedEvent;
13 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
14 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
15 import net.pterodactylus.sone.data.Post;
16 import net.pterodactylus.sone.data.PostReply;
17 import net.pterodactylus.sone.data.Sone;
18 import net.pterodactylus.sone.database.Database;
19 import net.pterodactylus.sone.freenet.wot.Identity;
20 import net.pterodactylus.sone.freenet.wot.IdentityManager;
21 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
22 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
23 import net.pterodactylus.util.config.Configuration;
24
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.eventbus.EventBus;
28 import org.hamcrest.Description;
29 import org.hamcrest.Matcher;
30 import org.hamcrest.TypeSafeDiagnosingMatcher;
31 import org.junit.Test;
32 import org.mockito.InOrder;
33
34 /**
35  * Unit test for {@link Core} and its subclasses.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class CoreTest {
40
41         @Test
42         public void markPostKnownMarksPostAsKnown() {
43                 Core core = mock(Core.class);
44                 Post post = mock(Post.class);
45                 MarkPostKnown markPostKnown = core.new MarkPostKnown(post);
46                 markPostKnown.run();
47                 verify(core).markPostKnown(eq(post));
48         }
49
50         @Test
51         public void markReplyKnownMarksReplyAsKnown() {
52                 Core core = mock(Core.class);
53                 PostReply postReply = mock(PostReply.class);
54                 MarkReplyKnown markReplyKnown = core.new MarkReplyKnown(postReply);
55                 markReplyKnown.run();
56                 verify(core).markReplyKnown(eq(postReply));
57         }
58
59         @Test
60         public void removingAnIdentitySendsRemovalEventsForAllSoneElements() {
61                 // given
62                 Configuration configuration = mock(Configuration.class);
63                 FreenetInterface freenetInterface = mock(FreenetInterface.class);
64                 IdentityManager identityManager = mock(IdentityManager.class);
65                 SoneDownloader soneDownloader = mock(SoneDownloader.class);
66                 ImageInserter imageInserter = mock(ImageInserter.class);
67                 UpdateChecker updateChecker = mock(UpdateChecker.class);
68                 WebOfTrustUpdater webOfTrustUpdater = mock(WebOfTrustUpdater.class);
69                 EventBus eventBus = mock(EventBus.class);
70                 Database database = mock(Database.class);
71                 Core core = new Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database);
72                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
73                 Identity identity = mock(Identity.class);
74                 when(identity.getId()).thenReturn("sone-id");
75                 Sone sone = mock(Sone.class);
76                 when(database.getSone("sone-id")).thenReturn(sone);
77                 PostReply postReply1 = mock(PostReply.class);
78                 PostReply postReply2 = mock(PostReply.class);
79                 when(sone.getReplies()).thenReturn(ImmutableSet.of(postReply1, postReply2));
80                 Post post1 = mock(Post.class);
81                 Post post2 = mock(Post.class);
82                 when(sone.getPosts()).thenReturn(ImmutableList.of(post1, post2));
83
84                 // when
85                 core.identityRemoved(new IdentityRemovedEvent(ownIdentity, identity));
86
87                 // then
88                 InOrder inOrder = inOrder(eventBus, database);
89                 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)));
90                 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)));
91                 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)));
92                 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)));
93                 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)));
94                 inOrder.verify(database).removeSone(sone);
95         }
96
97         private Matcher<Object> isPostRemoved(final Post post) {
98                 return new TypeSafeDiagnosingMatcher<Object>() {
99                         @Override
100                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
101                                 if (!(item instanceof PostRemovedEvent)) {
102                                         mismatchDescription.appendText("is not PostRemovedEvent");
103                                         return false;
104                                 }
105                                 if (((PostRemovedEvent) item).post() != post) {
106                                         mismatchDescription.appendText("post is ").appendValue(((PostRemovedEvent) item).post());
107                                         return false;
108                                 }
109                                 return true;
110                         }
111
112                         @Override
113                         public void describeTo(Description description) {
114                                 description.appendText("is PostRemovedEvent and post is ").appendValue(post);
115                         }
116                 };
117         }
118
119         private Matcher<Object> isPostReplyRemoved(final PostReply postReply) {
120                 return new TypeSafeDiagnosingMatcher<Object>() {
121                         @Override
122                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
123                                 if (!(item instanceof PostReplyRemovedEvent)) {
124                                         mismatchDescription.appendText("is not PostReplyRemovedEvent");
125                                         return false;
126                                 }
127                                 if (((PostReplyRemovedEvent) item).postReply() != postReply) {
128                                         mismatchDescription.appendText("post reply is ").appendValue(((PostReplyRemovedEvent) item).postReply());
129                                         return false;
130                                 }
131                                 return true;
132                         }
133
134                         @Override
135                         public void describeTo(Description description) {
136                                 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply);
137                         }
138                 };
139         }
140
141         private Matcher<Object> isSoneRemoved(final Sone sone) {
142                 return new TypeSafeDiagnosingMatcher<Object>() {
143                         @Override
144                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
145                                 if (!(item instanceof SoneRemovedEvent)) {
146                                         mismatchDescription.appendText("is not SoneRemovedEvent");
147                                         return false;
148                                 }
149                                 if (((SoneRemovedEvent) item).sone() != sone) {
150                                         mismatchDescription.appendText("sone is ").appendValue(((SoneRemovedEvent) item).sone());
151                                         return false;
152                                 }
153                                 return true;
154                         }
155
156                         @Override
157                         public void describeTo(Description description) {
158                                 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone);
159                         }
160                 };
161         }
162
163 }