Don’t allow null for any attribute of a LinkPart
[Sone.git] / src / test / java / net / pterodactylus / sone / core / CoreTest.java
1 package net.pterodactylus.sone.core;
2
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;
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.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;
34
35 /**
36  * Unit test for {@link Core} and its subclasses.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class CoreTest {
41
42         @Test
43         public void markPostKnownMarksPostAsKnown() {
44                 Core core = mock(Core.class);
45                 Post post = mock(Post.class);
46                 MarkPostKnown markPostKnown = core.new MarkPostKnown(post);
47                 markPostKnown.run();
48                 verify(core).markPostKnown(eq(post));
49         }
50
51         @Test
52         public void markReplyKnownMarksReplyAsKnown() {
53                 Core core = mock(Core.class);
54                 PostReply postReply = mock(PostReply.class);
55                 MarkReplyKnown markReplyKnown = core.new MarkReplyKnown(postReply);
56                 markReplyKnown.run();
57                 verify(core).markReplyKnown(eq(postReply));
58         }
59
60         @Test
61         public void removingAnIdentitySendsRemovalEventsForAllSoneElements() {
62                 // given
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));
84
85                 // when
86                 core.identityRemoved(new IdentityRemovedEvent(ownIdentity, identity));
87
88                 // then
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);
96         }
97
98         private Matcher<Object> isPostRemoved(final Post post) {
99                 return new TypeSafeDiagnosingMatcher<Object>() {
100                         @Override
101                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
102                                 if (!(item instanceof PostRemovedEvent)) {
103                                         mismatchDescription.appendText("is not PostRemovedEvent");
104                                         return false;
105                                 }
106                                 if (((PostRemovedEvent) item).post() != post) {
107                                         mismatchDescription.appendText("post is ").appendValue(((PostRemovedEvent) item).post());
108                                         return false;
109                                 }
110                                 return true;
111                         }
112
113                         @Override
114                         public void describeTo(Description description) {
115                                 description.appendText("is PostRemovedEvent and post is ").appendValue(post);
116                         }
117                 };
118         }
119
120         private Matcher<Object> isPostReplyRemoved(final PostReply postReply) {
121                 return new TypeSafeDiagnosingMatcher<Object>() {
122                         @Override
123                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
124                                 if (!(item instanceof PostReplyRemovedEvent)) {
125                                         mismatchDescription.appendText("is not PostReplyRemovedEvent");
126                                         return false;
127                                 }
128                                 if (((PostReplyRemovedEvent) item).postReply() != postReply) {
129                                         mismatchDescription.appendText("post reply is ").appendValue(((PostReplyRemovedEvent) item).postReply());
130                                         return false;
131                                 }
132                                 return true;
133                         }
134
135                         @Override
136                         public void describeTo(Description description) {
137                                 description.appendText("is PostReplyRemovedEvent and post is ").appendValue(postReply);
138                         }
139                 };
140         }
141
142         private Matcher<Object> isSoneRemoved(final Sone sone) {
143                 return new TypeSafeDiagnosingMatcher<Object>() {
144                         @Override
145                         protected boolean matchesSafely(Object item, Description mismatchDescription) {
146                                 if (!(item instanceof SoneRemovedEvent)) {
147                                         mismatchDescription.appendText("is not SoneRemovedEvent");
148                                         return false;
149                                 }
150                                 if (((SoneRemovedEvent) item).sone() != sone) {
151                                         mismatchDescription.appendText("sone is ").appendValue(((SoneRemovedEvent) item).sone());
152                                         return false;
153                                 }
154                                 return true;
155                         }
156
157                         @Override
158                         public void describeTo(Description description) {
159                                 description.appendText("is SoneRemovedEvent and sone is ").appendValue(sone);
160                         }
161                 };
162         }
163
164 }