🔀 Merge changes from other next branch
[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 public class CoreTest {
38
39         @Test
40         public void markPostKnownMarksPostAsKnown() {
41                 Core core = mock(Core.class);
42                 Post post = mock(Post.class);
43                 MarkPostKnown markPostKnown = core.new MarkPostKnown(post);
44                 markPostKnown.run();
45                 verify(core).markPostKnown(eq(post));
46         }
47
48         @Test
49         public void markReplyKnownMarksReplyAsKnown() {
50                 Core core = mock(Core.class);
51                 PostReply postReply = mock(PostReply.class);
52                 MarkReplyKnown markReplyKnown = core.new MarkReplyKnown(postReply);
53                 markReplyKnown.run();
54                 verify(core).markReplyKnown(eq(postReply));
55         }
56
57         @Test
58         public void removingAnIdentitySendsRemovalEventsForAllSoneElements() {
59                 // given
60                 Configuration configuration = mock(Configuration.class);
61                 FreenetInterface freenetInterface = mock(FreenetInterface.class);
62                 IdentityManager identityManager = mock(IdentityManager.class);
63                 SoneDownloader soneDownloader = mock(SoneDownloader.class);
64                 ImageInserter imageInserter = mock(ImageInserter.class);
65                 UpdateChecker updateChecker = mock(UpdateChecker.class);
66                 WebOfTrustUpdater webOfTrustUpdater = mock(WebOfTrustUpdater.class);
67                 EventBus eventBus = mock(EventBus.class);
68                 Database database = mock(Database.class);
69                 Core core = new Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database);
70                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
71                 Identity identity = mock(Identity.class);
72                 when(identity.getId()).thenReturn("sone-id");
73                 Sone sone = mock(Sone.class);
74                 when(database.getSone("sone-id")).thenReturn(sone);
75                 PostReply postReply1 = mock(PostReply.class);
76                 PostReply postReply2 = mock(PostReply.class);
77                 when(sone.getReplies()).thenReturn(ImmutableSet.of(postReply1, postReply2));
78                 Post post1 = mock(Post.class);
79                 Post post2 = mock(Post.class);
80                 when(sone.getPosts()).thenReturn(ImmutableList.of(post1, post2));
81
82                 // when
83                 core.identityRemoved(new IdentityRemovedEvent(ownIdentity, identity));
84
85                 // then
86                 InOrder inOrder = inOrder(eventBus, database);
87                 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)));
88                 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)));
89                 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)));
90                 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)));
91                 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)));
92                 inOrder.verify(database).removeSone(sone);
93         }
94
95         private Matcher<Object> isPostRemoved(final Post post) {
96                 return new TypeSafeDiagnosingMatcher<Object>() {
97                         @Override
98                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
99                                 if (!(item instanceof PostRemovedEvent)) {
100                                         mismatchDescription.appendText("is not PostRemovedEvent");
101                                         return false;
102                                 }
103                                 if (((PostRemovedEvent) item).getPost() != post) {
104                                         mismatchDescription.appendText("post is ").appendValue(((PostRemovedEvent) item).getPost());
105                                         return false;
106                                 }
107                                 return true;
108                         }
109
110                         @Override
111                         public void describeTo(Description description) {
112                                 description.appendText("is PostRemovedEvent and post is ").appendValue(post);
113                         }
114                 };
115         }
116
117         private Matcher<Object> isPostReplyRemoved(final PostReply postReply) {
118                 return new TypeSafeDiagnosingMatcher<Object>() {
119                         @Override
120                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
121                                 if (!(item instanceof PostReplyRemovedEvent)) {
122                                         mismatchDescription.appendText("is not PostReplyRemovedEvent");
123                                         return false;
124                                 }
125                                 if (((PostReplyRemovedEvent) item).postReply() != postReply) {
126                                         mismatchDescription.appendText("post reply is ").appendValue(((PostReplyRemovedEvent) item).postReply());
127                                         return false;
128                                 }
129                                 return true;
130                         }
131
132                         @Override
133                         public void describeTo(Description description) {
134                                 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply);
135                         }
136                 };
137         }
138
139         private Matcher<Object> isSoneRemoved(final Sone sone) {
140                 return new TypeSafeDiagnosingMatcher<Object>() {
141                         @Override
142                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
143                                 if (!(item instanceof SoneRemovedEvent)) {
144                                         mismatchDescription.appendText("is not SoneRemovedEvent");
145                                         return false;
146                                 }
147                                 if (((SoneRemovedEvent) item).getSone() != sone) {
148                                         mismatchDescription.appendText("sone is ").appendValue(((SoneRemovedEvent) item).getSone());
149                                         return false;
150                                 }
151                                 return true;
152                         }
153
154                         @Override
155                         public void describeTo(Description description) {
156                                 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone);
157                         }
158                 };
159         }
160
161 }