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 public class PostVisibilityFilterTest {
25 private static final String LOCAL_ID = "local-id";
26 private static final String REMOTE_ID = "remote-id";
28 private final PostVisibilityFilter postVisibilityFilter = new PostVisibilityFilter();
30 private final Sone localSone = mock(Sone.class);
31 private final OwnIdentity localIdentity = mock(OwnIdentity.class);
32 private final Post post = mock(Post.class);
33 private final Sone remoteSone = mock(Sone.class);
34 private final Identity remoteIdentity = mock(Identity.class);
36 public PostVisibilityFilterTest() {
37 when(localSone.getId()).thenReturn(LOCAL_ID);
38 when(localSone.isLocal()).thenReturn(true);
39 when(localSone.getIdentity()).thenReturn(localIdentity);
40 when(localIdentity.getId()).thenReturn(LOCAL_ID);
41 when(remoteSone.getId()).thenReturn(REMOTE_ID);
42 when(remoteSone.getIdentity()).thenReturn(remoteIdentity);
43 when(remoteIdentity.getId()).thenReturn(REMOTE_ID);
44 when(post.getRecipientId()).thenReturn(Optional.<String>absent());
48 public void postVisibilityFilterIsOnlyCreatedOnce() {
49 Injector injector = Guice.createInjector();
50 PostVisibilityFilter firstFilter = injector.getInstance(PostVisibilityFilter.class);
51 PostVisibilityFilter secondFilter = injector.getInstance(PostVisibilityFilter.class);
52 assertThat(firstFilter, sameInstance(secondFilter));
56 public void postIsNotVisibleIfItIsNotLoaded() {
57 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
60 private static void makePostLoaded(Post post) {
61 when(post.isLoaded()).thenReturn(true);
65 public void loadedPostIsVisibleWithoutSone() {
67 assertThat(postVisibilityFilter.isPostVisible(null, post), is(true));
70 private void makePostComeFromTheFuture() {
71 when(post.getTime()).thenReturn(System.currentTimeMillis() + 1000);
75 public void loadedPostFromTheFutureIsNotVisible() {
77 makePostComeFromTheFuture();
78 assertThat(postVisibilityFilter.isPostVisible(null, post), is(false));
81 private void makePostFromRemoteSone() {
82 when(post.getSone()).thenReturn(remoteSone);
85 private void giveRemoteIdentityNegativeExplicitTrust() {
86 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(-1, null, null));
90 public void loadedPostFromExplicitelyNotTrustedSoneIsNotVisible() {
92 makePostFromRemoteSone();
93 giveRemoteIdentityNegativeExplicitTrust();
94 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
97 private void giveRemoteIdentityNegativeImplicitTrust() {
98 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, -1, null));
102 public void loadedPostFromImplicitelyUntrustedSoneIsNotVisible() {
103 makePostLoaded(post);
104 makePostFromRemoteSone();
105 giveRemoteIdentityNegativeImplicitTrust();
106 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
109 private void makeLocalSoneFollowRemoteSone() {
110 when(localSone.hasFriend(REMOTE_ID)).thenReturn(true);
113 private void giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() {
114 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(1, -1, null));
118 public void loadedPostFromExplicitelyTrustedButImplicitelyUntrustedSoneIsVisible() {
119 makePostLoaded(post);
120 makePostFromRemoteSone();
121 makeLocalSoneFollowRemoteSone();
122 giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust();
123 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
126 private void giveTheRemoteIdentityPositiveImplicitTrust() {
127 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, 1, null));
131 public void loadedPostFromImplicitelyTrustedSoneIsVisible() {
132 makePostLoaded(post);
133 makePostFromRemoteSone();
134 makeLocalSoneFollowRemoteSone();
135 giveTheRemoteIdentityPositiveImplicitTrust();
136 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
139 private void giveTheRemoteIdentityUnknownTrust() {
140 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, null, null));
144 public void loadedPostFromSoneWithUnknownTrustIsVisible() {
145 makePostLoaded(post);
146 makePostFromRemoteSone();
147 makeLocalSoneFollowRemoteSone();
148 giveTheRemoteIdentityUnknownTrust();
149 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
153 public void loadedPostFromUnfollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsNotVisible() {
154 makePostLoaded(post);
155 makePostFromRemoteSone();
156 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
159 private void makePostFromLocalSone() {
160 makePostLoaded(post);
161 when(post.getSone()).thenReturn(localSone);
165 public void loadedPostFromLocalSoneIsVisible() {
166 makePostFromLocalSone();
167 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
171 public void loadedPostFromFollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsVisible() {
172 makePostLoaded(post);
173 makePostFromRemoteSone();
174 makeLocalSoneFollowRemoteSone();
175 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
178 private void makePostDirectedAtLocalId() {
179 when(post.getRecipientId()).thenReturn(Optional.of(LOCAL_ID));
183 public void loadedPostFromRemoteSoneThatIsDirectedAtLocalSoneIsVisible() {
184 makePostLoaded(post);
185 makePostFromRemoteSone();
186 makePostDirectedAtLocalId();
187 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
191 public void predicateWillCorrectlyRecognizeVisiblePost() {
192 makePostFromLocalSone();
193 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(true));
197 public void predicateWillCorrectlyRecognizeNotVisiblePost() {
198 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(false));