Remove @author tags
[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 public class PostVisibilityFilterTest {
24
25         private static final String LOCAL_ID = "local-id";
26         private static final String REMOTE_ID = "remote-id";
27
28         private final PostVisibilityFilter postVisibilityFilter = new PostVisibilityFilter();
29
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);
35
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());
45         }
46
47         @Test
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));
53         }
54
55         @Test
56         public void postIsNotVisibleIfItIsNotLoaded() {
57                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
58         }
59
60         private static void makePostLoaded(Post post) {
61                 when(post.isLoaded()).thenReturn(true);
62         }
63
64         @Test
65         public void loadedPostIsVisibleWithoutSone() {
66                 makePostLoaded(post);
67                 assertThat(postVisibilityFilter.isPostVisible(null, post), is(true));
68         }
69
70         private void makePostComeFromTheFuture() {
71                 when(post.getTime()).thenReturn(System.currentTimeMillis() + 1000);
72         }
73
74         @Test
75         public void loadedPostFromTheFutureIsNotVisible() {
76                 makePostLoaded(post);
77                 makePostComeFromTheFuture();
78                 assertThat(postVisibilityFilter.isPostVisible(null, post), is(false));
79         }
80
81         private void makePostFromRemoteSone() {
82                 when(post.getSone()).thenReturn(remoteSone);
83         }
84
85         private void giveRemoteIdentityNegativeExplicitTrust() {
86                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(-1, null, null));
87         }
88
89         @Test
90         public void loadedPostFromExplicitelyNotTrustedSoneIsNotVisible() {
91                 makePostLoaded(post);
92                 makePostFromRemoteSone();
93                 giveRemoteIdentityNegativeExplicitTrust();
94                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
95         }
96
97         private void giveRemoteIdentityNegativeImplicitTrust() {
98                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, -1, null));
99         }
100
101         @Test
102         public void loadedPostFromImplicitelyUntrustedSoneIsNotVisible() {
103                 makePostLoaded(post);
104                 makePostFromRemoteSone();
105                 giveRemoteIdentityNegativeImplicitTrust();
106                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
107         }
108
109         private void makeLocalSoneFollowRemoteSone() {
110                 when(localSone.hasFriend(REMOTE_ID)).thenReturn(true);
111         }
112
113         private void giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust() {
114                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(1, -1, null));
115         }
116
117         @Test
118         public void loadedPostFromExplicitelyTrustedButImplicitelyUntrustedSoneIsVisible() {
119                 makePostLoaded(post);
120                 makePostFromRemoteSone();
121                 makeLocalSoneFollowRemoteSone();
122                 giveRemoteIdentityPositiveExplicitTrustButNegativeImplicitTrust();
123                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
124         }
125
126         private void giveTheRemoteIdentityPositiveImplicitTrust() {
127                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, 1, null));
128         }
129
130         @Test
131         public void loadedPostFromImplicitelyTrustedSoneIsVisible() {
132                 makePostLoaded(post);
133                 makePostFromRemoteSone();
134                 makeLocalSoneFollowRemoteSone();
135                 giveTheRemoteIdentityPositiveImplicitTrust();
136                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
137         }
138
139         private void giveTheRemoteIdentityUnknownTrust() {
140                 when(remoteIdentity.getTrust(localIdentity)).thenReturn(new Trust(null, null, null));
141         }
142
143         @Test
144         public void loadedPostFromSoneWithUnknownTrustIsVisible() {
145                 makePostLoaded(post);
146                 makePostFromRemoteSone();
147                 makeLocalSoneFollowRemoteSone();
148                 giveTheRemoteIdentityUnknownTrust();
149                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
150         }
151
152         @Test
153         public void loadedPostFromUnfollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsNotVisible() {
154                 makePostLoaded(post);
155                 makePostFromRemoteSone();
156                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(false));
157         }
158
159         private void makePostFromLocalSone() {
160                 makePostLoaded(post);
161                 when(post.getSone()).thenReturn(localSone);
162         }
163
164         @Test
165         public void loadedPostFromLocalSoneIsVisible() {
166                 makePostFromLocalSone();
167                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
168         }
169
170         @Test
171         public void loadedPostFromFollowedRemoteSoneThatIsNotDirectedAtLocalSoneIsVisible() {
172                 makePostLoaded(post);
173                 makePostFromRemoteSone();
174                 makeLocalSoneFollowRemoteSone();
175                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
176         }
177
178         private void makePostDirectedAtLocalId() {
179                 when(post.getRecipientId()).thenReturn(Optional.of(LOCAL_ID));
180         }
181
182         @Test
183         public void loadedPostFromRemoteSoneThatIsDirectedAtLocalSoneIsVisible() {
184                 makePostLoaded(post);
185                 makePostFromRemoteSone();
186                 makePostDirectedAtLocalId();
187                 assertThat(postVisibilityFilter.isPostVisible(localSone, post), is(true));
188         }
189
190         @Test
191         public void predicateWillCorrectlyRecognizeVisiblePost() {
192                 makePostFromLocalSone();
193                 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(true));
194         }
195
196         @Test
197         public void predicateWillCorrectlyRecognizeNotVisiblePost() {
198                 assertThat(postVisibilityFilter.isVisible(null).apply(post), is(false));
199         }
200
201 }