1 package net.pterodactylus.sone.core;
3 import static org.mockito.Matchers.argThat;
4 import static org.mockito.Matchers.eq;
5 import static org.mockito.Mockito.inOrder;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.when;
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.base.Optional;
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.eventbus.EventBus;
29 import org.hamcrest.Description;
30 import org.hamcrest.Matcher;
31 import org.hamcrest.TypeSafeDiagnosingMatcher;
32 import org.junit.Test;
33 import org.mockito.InOrder;
36 * Unit test for {@link Core} and its subclasses.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class CoreTest {
43 public void markPostKnownMarksPostAsKnown() {
44 Core core = mock(Core.class);
45 Post post = mock(Post.class);
46 MarkPostKnown markPostKnown = core.new MarkPostKnown(post);
48 verify(core).markPostKnown(eq(post));
52 public void markReplyKnownMarksReplyAsKnown() {
53 Core core = mock(Core.class);
54 PostReply postReply = mock(PostReply.class);
55 MarkReplyKnown markReplyKnown = core.new MarkReplyKnown(postReply);
57 verify(core).markReplyKnown(eq(postReply));
61 public void removingAnIdentitySendsRemovalEventsForAllSoneElements() {
63 Configuration configuration = mock(Configuration.class);
64 FreenetInterface freenetInterface = mock(FreenetInterface.class);
65 IdentityManager identityManager = mock(IdentityManager.class);
66 SoneDownloader soneDownloader = mock(SoneDownloader.class);
67 ImageInserter imageInserter = mock(ImageInserter.class);
68 UpdateChecker updateChecker = mock(UpdateChecker.class);
69 WebOfTrustUpdater webOfTrustUpdater = mock(WebOfTrustUpdater.class);
70 EventBus eventBus = mock(EventBus.class);
71 Database database = mock(Database.class);
72 Core core = new Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database);
73 OwnIdentity ownIdentity = mock(OwnIdentity.class);
74 Identity identity = mock(Identity.class);
75 when(identity.getId()).thenReturn("sone-id");
76 Sone sone = mock(Sone.class);
77 when(database.getSone("sone-id")).thenReturn(Optional.of(sone));
78 PostReply postReply1 = mock(PostReply.class);
79 PostReply postReply2 = mock(PostReply.class);
80 when(sone.getReplies()).thenReturn(ImmutableSet.of(postReply1, postReply2));
81 Post post1 = mock(Post.class);
82 Post post2 = mock(Post.class);
83 when(sone.getPosts()).thenReturn(ImmutableList.of(post1, post2));
86 core.identityRemoved(new IdentityRemovedEvent(ownIdentity, identity));
89 InOrder inOrder = inOrder(eventBus, database);
90 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply1)));
91 inOrder.verify(eventBus).post(argThat(isPostReplyRemoved(postReply2)));
92 inOrder.verify(eventBus).post(argThat(isPostRemoved(post1)));
93 inOrder.verify(eventBus).post(argThat(isPostRemoved(post2)));
94 inOrder.verify(eventBus).post(argThat(isSoneRemoved(sone)));
95 inOrder.verify(database).removeSone(sone);
98 private Matcher<Object> isPostRemoved(final Post post) {
99 return new TypeSafeDiagnosingMatcher<Object>() {
101 protected boolean matchesSafely(Object item, Description mismatchDescription) {
102 if (!(item instanceof PostRemovedEvent)) {
103 mismatchDescription.appendText("is not PostRemovedEvent");
106 if (((PostRemovedEvent) item).post() != post) {
107 mismatchDescription.appendText("post is ").appendValue(((PostRemovedEvent) item).post());
114 public void describeTo(Description description) {
115 description.appendText("is PostRemovedEvent and post is ").appendValue(post);
120 private Matcher<Object> isPostReplyRemoved(final PostReply postReply) {
121 return new TypeSafeDiagnosingMatcher<Object>() {
123 protected boolean matchesSafely(Object item, Description mismatchDescription) {
124 if (!(item instanceof PostReplyRemovedEvent)) {
125 mismatchDescription.appendText("is not PostReplyRemovedEvent");
128 if (((PostReplyRemovedEvent) item).postReply() != postReply) {
129 mismatchDescription.appendText("post reply is ").appendValue(((PostReplyRemovedEvent) item).postReply());
136 public void describeTo(Description description) {
137 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply);
142 private Matcher<Object> isSoneRemoved(final Sone sone) {
143 return new TypeSafeDiagnosingMatcher<Object>() {
145 protected boolean matchesSafely(Object item, Description mismatchDescription) {
146 if (!(item instanceof SoneRemovedEvent)) {
147 mismatchDescription.appendText("is not SoneRemovedEvent");
150 if (((SoneRemovedEvent) item).sone() != sone) {
151 mismatchDescription.appendText("sone is ").appendValue(((SoneRemovedEvent) item).sone());
158 public void describeTo(Description description) {
159 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone);