1 package net.pterodactylus.sone.notify;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.Matchers.sameInstance;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
9 import net.pterodactylus.sone.data.Post;
10 import net.pterodactylus.sone.data.Sone;
11 import net.pterodactylus.sone.freenet.wot.Identity;
12 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
13 import net.pterodactylus.sone.freenet.wot.Trust;
15 import com.google.common.base.Optional;
16 import com.google.inject.Guice;
17 import com.google.inject.Injector;
18 import org.junit.Test;
21 * Unit test for {@link PostVisibilityFilterTest}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 public class PostVisibilityFilterTest {
27 private static final String LOCAL_ID = "local-id";
28 private static final String REMOTE_ID = "remote-id";
30 private final PostVisibilityFilter postVisibilityFilter = new PostVisibilityFilter();
32 private final Sone localSone = mock(Sone.class);
33 private final OwnIdentity localIdentity = mock(OwnIdentity.class);
34 private final Post post = mock(Post.class);
35 private final Sone remoteSone = mock(Sone.class);
36 private final Identity remoteIdentity = mock(Identity.class);
38 public PostVisibilityFilterTest() {
39 when(localSone.getId()).thenReturn(LOCAL_ID);
40 when(localSone.isLocal()).thenReturn(true);
41 when(localSone.getIdentity()).thenReturn(localIdentity);
42 when(localIdentity.getId()).thenReturn(LOCAL_ID);
43 when(remoteSone.getId()).thenReturn(REMOTE_ID);
44 when(remoteSone.getIdentity()).thenReturn(remoteIdentity);
45 when(remoteIdentity.getId()).thenReturn(REMOTE_ID);
46 when(post.getRecipientId()).thenReturn(Optional.<String>absent());
50 public void postVisibilityFilterIsOnlyCreatedOnce() {
51 Injector injector = Guice.createInjector();
52 PostVisibilityFilter firstFilter = injector.getInstance(PostVisibilityFilter.class);
53 PostVisibilityFilter secondFilter = injector.getInstance(PostVisibilityFilter.class);
54 assertThat(firstFilter, sameInstance(secondFilter));
58 public void postIsNotVisibleIfItIsNotLoaded() {
59 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
62 private static void makePostLoaded(Post post) {
63 when(post.isLoaded()).thenReturn(true);
67 public void loadedPostIsVisibleWithoutSone() {
69 assertThat(postVisibilityFilter.isPostVisible(null, post), is(true));
72 private void makePostComeFromTheFuture() {
73 when(post.getTime()).thenReturn(System.currentTimeMillis() + 1000);
77 public void loadedPostFromTheFutureIsNotVisible() {
79 makePostComeFromTheFuture();
80 assertThat(postVisibilityFilter.isPostVisible(null, post), is(false));
83 private void makePostFromRemoteSone() {
84 when(post.getSone()).thenReturn(remoteSone);
87 private void giveRemoteIdentityNegativeExplicitTrust() {
88 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(-1, null, null));
92 public void loadedPostFromExplicitelyNotTrustedSoneIsNotVisible() {
94 makePostFromRemoteSone();
95 giveRemoteIdentityNegativeExplicitTrust();
96 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
99 private void giveRemoteIdentityNegativeImplicitTrust() {
100 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, -1, null));
104 public void loadedPostFromImplicitelyUntrustedSoneIsNotVisible() {
105 makePostLoaded(post);
106 makePostFromRemoteSone();
107 giveRemoteIdentityNegativeImplicitTrust();
108 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
111 private void makeLocalSoneFollowRemoteSone() {
112 when(localSone.hasFriend(REMOTE_ID)).thenReturn(true);
115 private void giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() {
116 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(1, -1, null));
120 public void loadedPostFromExplicitelyTrustedButImplicitelyUntrustedSoneIsVisible() {
121 makePostLoaded(post);
122 makePostFromRemoteSone();
123 makeLocalSoneFollowRemoteSone();
124 giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust();
125 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
128 private void giveTheRemoteIdentityPositiveImplicitTrust() {
129 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, 1, null));
133 public void loadedPostFromImplicitelyTrustedSoneIsVisible() {
134 makePostLoaded(post);
135 makePostFromRemoteSone();
136 makeLocalSoneFollowRemoteSone();
137 giveTheRemoteIdentityPositiveImplicitTrust();
138 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
141 private void giveTheRemoteIdentityUnknownTrust() {
142 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, null, null));
146 public void loadedPostFromSoneWithUnknownTrustIsVisible() {
147 makePostLoaded(post);
148 makePostFromRemoteSone();
149 makeLocalSoneFollowRemoteSone();
150 giveTheRemoteIdentityUnknownTrust();
151 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
155 public void loadedPostFromUnfollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsNotVisible() {
156 makePostLoaded(post);
157 makePostFromRemoteSone();
158 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
161 private void makePostFromLocalSone() {
162 makePostLoaded(post);
163 when(post.getSone()).thenReturn(localSone);
167 public void loadedPostFromLocalSoneIsVisible() {
168 makePostFromLocalSone();
169 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
173 public void loadedPostFromFollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsVisible() {
174 makePostLoaded(post);
175 makePostFromRemoteSone();
176 makeLocalSoneFollowRemoteSone();
177 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
180 private void makePostDirectedAtLocalId() {
181 when(post.getRecipientId()).thenReturn(Optional.of(LOCAL_ID));
185 public void loadedPostFromRemoteSoneThatIsDirectedAtLocalSoneIsVisible() {
186 makePostLoaded(post);
187 makePostFromRemoteSone();
188 makePostDirectedAtLocalId();
189 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
193 public void predicateWillCorrectlyRecognizeVisiblePost() {
194 makePostFromLocalSone();
195 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(true));
199 public void predicateWillCorrectlyRecognizeNotVisiblePost() {
200 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(false));