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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class CoreTest {
42 public void markPostKnownMarksPostAsKnown() {
43 Core core = mock(Core.class);
44 Post post = mock(Post.class);
45 MarkPostKnown markPostKnown = core.new MarkPostKnown(post);
47 verify(core).markPostKnown(eq(post));
51 public void markReplyKnownMarksReplyAsKnown() {
52 Core core = mock(Core.class);
53 PostReply postReply = mock(PostReply.class);
54 MarkReplyKnown markReplyKnown = core.new MarkReplyKnown(postReply);
56 verify(core).markReplyKnown(eq(postReply));
60 public void removingAnIdentitySendsRemovalEventsForAllSoneElements() {
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));
85 core.identityRemoved(new IdentityRemovedEvent(ownIdentity, identity));
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);
97 private Matcher<Object> isPostRemoved(final Post post) {
98 return new TypeSafeDiagnosingMatcher<Object>() {
100 protected boolean matchesSafely(Object item, Description mismatchDescription) {
101 if (!(item instanceof PostRemovedEvent)) {
102 mismatchDescription.appendText("is not PostRemovedEvent");
105 if (((PostRemovedEvent) item).post() != post) {
106 mismatchDescription.appendText("post is ").appendValue(((PostRemovedEvent) item).post());
113 public void describeTo(Description description) {
114 description.appendText("is PostRemovedEvent and post is ").appendValue(post);
119 private Matcher<Object> isPostReplyRemoved(final PostReply postReply) {
120 return new TypeSafeDiagnosingMatcher<Object>() {
122 protected boolean matchesSafely(Object item, Description mismatchDescription) {
123 if (!(item instanceof PostReplyRemovedEvent)) {
124 mismatchDescription.appendText("is not PostReplyRemovedEvent");
127 if (((PostReplyRemovedEvent) item).postReply() != postReply) {
128 mismatchDescription.appendText("post reply is ").appendValue(((PostReplyRemovedEvent) item).postReply());
135 public void describeTo(Description description) {
136 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply);
141 private Matcher<Object> isSoneRemoved(final Sone sone) {
142 return new TypeSafeDiagnosingMatcher<Object>() {
144 protected boolean matchesSafely(Object item, Description mismatchDescription) {
145 if (!(item instanceof SoneRemovedEvent)) {
146 mismatchDescription.appendText("is not SoneRemovedEvent");
149 if (((SoneRemovedEvent) item).sone() != sone) {
150 mismatchDescription.appendText("sone is ").appendValue(((SoneRemovedEvent) item).sone());
157 public void describeTo(Description description) {
158 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone);