1 package net.pterodactylus.sone.core;
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;
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;
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;
35 * Unit test for {@link Core} and its subclasses.
37 public class CoreTest {
40 public void markPostKnownMarksPostAsKnown() {
41 Core core = mock(Core.class);
42 Post post = mock(Post.class);
43 MarkPostKnown markPostKnown = core.new MarkPostKnown(post);
45 verify(core).markPostKnown(eq(post));
49 public void markReplyKnownMarksReplyAsKnown() {
50 Core core = mock(Core.class);
51 PostReply postReply = mock(PostReply.class);
52 MarkReplyKnown markReplyKnown = core.new MarkReplyKnown(postReply);
54 verify(core).markReplyKnown(eq(postReply));
58 public void removingAnIdentitySendsRemovalEventsForAllSoneElements() {
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));
83 core.identityRemoved(new IdentityRemovedEvent(ownIdentity, identity));
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);
95 private Matcher<Object> isPostRemoved(final Post post) {
96 return new TypeSafeDiagnosingMatcher<Object>() {
98 protected boolean matchesSafely(Object item, Description mismatchDescription) {
99 if (!(item instanceof PostRemovedEvent)) {
100 mismatchDescription.appendText("is not PostRemovedEvent");
103 if (((PostRemovedEvent) item).post() != post) {
104 mismatchDescription.appendText("post is ").appendValue(((PostRemovedEvent) item).post());
111 public void describeTo(Description description) {
112 description.appendText("is PostRemovedEvent and post is ").appendValue(post);
117 private Matcher<Object> isPostReplyRemoved(final PostReply postReply) {
118 return new TypeSafeDiagnosingMatcher<Object>() {
120 protected boolean matchesSafely(Object item, Description mismatchDescription) {
121 if (!(item instanceof PostReplyRemovedEvent)) {
122 mismatchDescription.appendText("is not PostReplyRemovedEvent");
125 if (((PostReplyRemovedEvent) item).postReply() != postReply) {
126 mismatchDescription.appendText("post reply is ").appendValue(((PostReplyRemovedEvent) item).postReply());
133 public void describeTo(Description description) {
134 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply);
139 private Matcher<Object> isSoneRemoved(final Sone sone) {
140 return new TypeSafeDiagnosingMatcher<Object>() {
142 protected boolean matchesSafely(Object item, Description mismatchDescription) {
143 if (!(item instanceof SoneRemovedEvent)) {
144 mismatchDescription.appendText("is not SoneRemovedEvent");
147 if (((SoneRemovedEvent) item).sone() != sone) {
148 mismatchDescription.appendText("sone is ").appendValue(((SoneRemovedEvent) item).sone());
155 public void describeTo(Description description) {
156 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone);