d49e59b5d07f9766edc3110c22e687c181c14aa3
[Sone.git] / src / test / java / net / pterodactylus / sone / notify / PostVisibilityFilterTest.java
1 package net.pterodactylus.sone.notify;
2
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;
8
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;
14
15 import com.google.common.base.Optional;
16 import com.google.inject.Guice;
17 import com.google.inject.Injector;
18 import org.junit.Test;
19
20 /**
21  * Unit test for {@link PostVisibilityFilterTest}.
22  *
23  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24  */
25 public class PostVisibilityFilterTest {
26
27         private static final String LOCAL_ID = "local-id";
28         private static final String REMOTE_ID = "remote-id";
29
30         private final PostVisibilityFilter postVisibilityFilter = new PostVisibilityFilter();
31
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);
37
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());
47         }
48
49         @Test
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));
55         }
56
57         @Test
58         public void postIsNotVisibleIfItIsNotLoaded() {
59                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
60         }
61
62         private static void makePostLoaded(Post post) {
63                 when(post.isLoaded()).thenReturn(true);
64         }
65
66         @Test
67         public void loadedPostIsVisibleWithoutSone() {
68                 makePostLoaded(post);
69                 assertThat(postVisibilityFilter.isPostVisible(null, post), is(true));
70         }
71
72         private void makePostComeFromTheFuture() {
73                 when(post.getTime()).thenReturn(System.currentTimeMillis() + 1000);
74         }
75
76         @Test
77         public void loadedPostFromTheFutureIsNotVisible() {
78                 makePostLoaded(post);
79                 makePostComeFromTheFuture();
80                 assertThat(postVisibilityFilter.isPostVisible(null, post), is(false));
81         }
82
83         private void makePostFromRemoteSone() {
84                 when(post.getSone()).thenReturn(remoteSone);
85         }
86
87         private void giveRemoteIdentityNegativeExplicitTrust() {
88                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(-1, null, null));
89         }
90
91         @Test
92         public void loadedPostFromExplicitelyNotTrustedSoneIsNotVisible() {
93                 makePostLoaded(post);
94                 makePostFromRemoteSone();
95                 giveRemoteIdentityNegativeExplicitTrust();
96                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
97         }
98
99         private void giveRemoteIdentityNegativeImplicitTrust() {
100                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, -1, null));
101         }
102
103         @Test
104         public void loadedPostFromImplicitelyUntrustedSoneIsNotVisible() {
105                 makePostLoaded(post);
106                 makePostFromRemoteSone();
107                 giveRemoteIdentityNegativeImplicitTrust();
108                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
109         }
110
111         private void makeLocalSoneFollowRemoteSone() {
112                 when(localSone.hasFriend(REMOTE_ID)).thenReturn(true);
113         }
114
115         private void giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() {
116                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(1, -1, null));
117         }
118
119         @Test
120         public void loadedPostFromExplicitelyTrustedButImplicitelyUntrustedSoneIsVisible() {
121                 makePostLoaded(post);
122                 makePostFromRemoteSone();
123                 makeLocalSoneFollowRemoteSone();
124                 giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust();
125                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
126         }
127
128         private void giveTheRemoteIdentityPositiveImplicitTrust() {
129                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, 1, null));
130         }
131
132         @Test
133         public void loadedPostFromImplicitelyTrustedSoneIsVisible() {
134                 makePostLoaded(post);
135                 makePostFromRemoteSone();
136                 makeLocalSoneFollowRemoteSone();
137                 giveTheRemoteIdentityPositiveImplicitTrust();
138                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
139         }
140
141         private void giveTheRemoteIdentityUnknownTrust() {
142                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, null, null));
143         }
144
145         @Test
146         public void loadedPostFromSoneWithUnknownTrustIsVisible() {
147                 makePostLoaded(post);
148                 makePostFromRemoteSone();
149                 makeLocalSoneFollowRemoteSone();
150                 giveTheRemoteIdentityUnknownTrust();
151                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
152         }
153
154         @Test
155         public void loadedPostFromUnfollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsNotVisible() {
156                 makePostLoaded(post);
157                 makePostFromRemoteSone();
158                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
159         }
160
161         private void makePostFromLocalSone() {
162                 makePostLoaded(post);
163                 when(post.getSone()).thenReturn(localSone);
164         }
165
166         @Test
167         public void loadedPostFromLocalSoneIsVisible() {
168                 makePostFromLocalSone();
169                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
170         }
171
172         @Test
173         public void loadedPostFromFollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsVisible() {
174                 makePostLoaded(post);
175                 makePostFromRemoteSone();
176                 makeLocalSoneFollowRemoteSone();
177                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
178         }
179
180         private void makePostDirectedAtLocalId() {
181                 when(post.getRecipientId()).thenReturn(Optional.of(LOCAL_ID));
182         }
183
184         @Test
185         public void loadedPostFromRemoteSoneThatIsDirectedAtLocalSoneIsVisible() {
186                 makePostLoaded(post);
187                 makePostFromRemoteSone();
188                 makePostDirectedAtLocalId();
189                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
190         }
191
192         @Test
193         public void predicateWillCorrectlyRecognizeVisiblePost() {
194                 makePostFromLocalSone();
195                 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(true));
196         }
197
198         @Test
199         public void predicateWillCorrectlyRecognizeNotVisiblePost() {
200                 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(false));
201         }
202
203 }