fdae222e90eb296a84db1dc116c3b52d9cda23fc
[Sone.git] / PreferencesTest.java
1 package net.pterodactylus.sone.core;
2
3 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS;
4 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO;
5 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING;
6 import static org.hamcrest.MatcherAssert.assertThat;
7 import static org.hamcrest.Matchers.hasItem;
8 import static org.hamcrest.Matchers.is;
9 import static org.mockito.ArgumentCaptor.forClass;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.atLeastOnce;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
14
15 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
16 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
17 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
18 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
19 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
20
21 import com.google.common.eventbus.EventBus;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24
25 /**
26  * Unit test for {@link Preferences}.
27  */
28 public class PreferencesTest {
29
30         private final EventBus eventBus = mock(EventBus.class);
31         private final Preferences preferences = new Preferences(eventBus);
32
33         @Test
34         public void preferencesRetainInsertionDelay() {
35                 preferences.setInsertionDelay(15);
36                 assertThat(preferences.getInsertionDelay(), is(15));
37         }
38
39         @Test
40         public void preferencesSendsEventOnSettingInsertionDelay() {
41                 preferences.setInsertionDelay(15);
42                 ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
43                 verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
44                 assertThat(eventsCaptor.getAllValues(), hasItem(new InsertionDelayChangedEvent(15)));
45         }
46
47         @Test(expected = IllegalArgumentException.class)
48         public void invalidInsertionDelayIsRejected() {
49                 preferences.setInsertionDelay(-15);
50         }
51
52         @Test
53         public void preferencesReturnDefaultValueWhenInsertionDelayIsSetToNull() {
54                 preferences.setInsertionDelay(null);
55                 assertThat(preferences.getInsertionDelay(), is(60));
56         }
57
58         @Test
59         public void preferencesStartWithInsertionDelayDefaultValue() {
60                 assertThat(preferences.getInsertionDelay(), is(60));
61         }
62
63         @Test
64         public void preferencesRetainPostsPerPage() {
65                 preferences.setPostsPerPage(15);
66                 assertThat(preferences.getPostsPerPage(), is(15));
67         }
68
69         @Test(expected = IllegalArgumentException.class)
70         public void invalidPostsPerPageIsRejected() {
71                 preferences.setPostsPerPage(-15);
72         }
73
74         @Test
75         public void preferencesReturnDefaultValueWhenPostsPerPageIsSetToNull() {
76                 preferences.setPostsPerPage(null);
77                 assertThat(preferences.getPostsPerPage(), is(10));
78         }
79
80         @Test
81         public void preferencesStartWithPostsPerPageDefaultValue() {
82                 assertThat(preferences.getPostsPerPage(), is(10));
83         }
84
85         @Test
86         public void preferencesRetainImagesPerPage() {
87                 preferences.setImagesPerPage(15);
88                 assertThat(preferences.getImagesPerPage(), is(15));
89         }
90
91         @Test(expected = IllegalArgumentException.class)
92         public void invalidImagesPerPageIsRejected() {
93                 preferences.setImagesPerPage(-15);
94         }
95
96         @Test
97         public void preferencesReturnDefaultValueWhenImagesPerPageIsSetToNull() {
98                 preferences.setImagesPerPage(null);
99                 assertThat(preferences.getImagesPerPage(), is(9));
100         }
101
102         @Test
103         public void preferencesStartWithImagesPerPageDefaultValue() {
104                 assertThat(preferences.getImagesPerPage(), is(9));
105         }
106
107         @Test
108         public void preferencesRetainCharactersPerPost() {
109                 preferences.setCharactersPerPost(150);
110                 assertThat(preferences.getCharactersPerPost(), is(150));
111         }
112
113         @Test(expected = IllegalArgumentException.class)
114         public void invalidCharactersPerPostIsRejected() {
115                 preferences.setCharactersPerPost(-15);
116         }
117
118         @Test
119         public void preferencesReturnDefaultValueWhenCharactersPerPostIsSetToNull() {
120                 preferences.setCharactersPerPost(null);
121                 assertThat(preferences.getCharactersPerPost(), is(400));
122         }
123
124         @Test
125         public void preferencesStartWithCharactersPerPostDefaultValue() {
126                 assertThat(preferences.getCharactersPerPost(), is(400));
127         }
128
129         @Test
130         public void preferencesRetainPostCutOffLength() {
131                 preferences.setPostCutOffLength(150);
132                 assertThat(preferences.getPostCutOffLength(), is(150));
133         }
134
135         @Test(expected = IllegalArgumentException.class)
136         public void invalidPostCutOffLengthIsRejected() {
137                 preferences.setPostCutOffLength(-15);
138         }
139
140         @Test(expected = IllegalArgumentException.class)
141         public void cutOffLengthOfMinusOneIsNotAllowed() {
142                 preferences.setPostCutOffLength(-1);
143         }
144
145         @Test
146         public void preferencesReturnDefaultValueWhenPostCutOffLengthIsSetToNull() {
147                 preferences.setPostCutOffLength(null);
148                 assertThat(preferences.getPostCutOffLength(), is(200));
149         }
150
151         @Test
152         public void preferencesStartWithPostCutOffLengthDefaultValue() {
153                 assertThat(preferences.getPostCutOffLength(), is(200));
154         }
155
156         @Test
157         public void preferencesRetainRequireFullAccessOfTrue() {
158                 preferences.setRequireFullAccess(true);
159                 assertThat(preferences.isRequireFullAccess(), is(true));
160         }
161
162         @Test
163         public void preferencesRetainRequireFullAccessOfFalse() {
164                 preferences.setRequireFullAccess(false);
165                 assertThat(preferences.isRequireFullAccess(), is(false));
166         }
167
168         @Test
169         public void preferencesReturnDefaultValueWhenRequireFullAccessIsSetToNull() {
170                 preferences.setRequireFullAccess(null);
171                 assertThat(preferences.isRequireFullAccess(), is(false));
172         }
173
174         @Test
175         public void preferencesStartWithRequireFullAccessDefaultValue() {
176                 assertThat(preferences.isRequireFullAccess(), is(false));
177         }
178
179         @Test
180         public void preferencesRetainPositiveTrust() {
181                 preferences.setPositiveTrust(15);
182                 assertThat(preferences.getPositiveTrust(), is(15));
183         }
184
185         @Test(expected = IllegalArgumentException.class)
186         public void invalidPositiveTrustIsRejected() {
187                 preferences.setPositiveTrust(-15);
188         }
189
190         @Test
191         public void preferencesReturnDefaultValueWhenPositiveTrustIsSetToNull() {
192                 preferences.setPositiveTrust(null);
193                 assertThat(preferences.getPositiveTrust(), is(75));
194         }
195
196         @Test
197         public void preferencesStartWithPositiveTrustDefaultValue() {
198                 assertThat(preferences.getPositiveTrust(), is(75));
199         }
200
201         @Test
202         public void preferencesRetainNegativeTrust() {
203                 preferences.setNegativeTrust(-15);
204                 assertThat(preferences.getNegativeTrust(), is(-15));
205         }
206
207         @Test(expected = IllegalArgumentException.class)
208         public void invalidNegativeTrustIsRejected() {
209                 preferences.setNegativeTrust(150);
210         }
211
212         @Test
213         public void preferencesReturnDefaultValueWhenNegativeTrustIsSetToNull() {
214                 preferences.setNegativeTrust(null);
215                 assertThat(preferences.getNegativeTrust(), is(-25));
216         }
217
218         @Test
219         public void preferencesStartWithNegativeTrustDefaultValue() {
220                 assertThat(preferences.getNegativeTrust(), is(-25));
221         }
222
223         @Test
224         public void preferencesRetainTrustComment() {
225                 preferences.setTrustComment("Trust");
226                 assertThat(preferences.getTrustComment(), is("Trust"));
227         }
228
229         @Test
230         public void preferencesReturnDefaultValueWhenTrustCommentIsSetToNull() {
231                 preferences.setTrustComment(null);
232                 assertThat(preferences.getTrustComment(),
233                                 is("Set from Sone Web Interface"));
234         }
235
236         @Test
237         public void preferencesStartWithTrustCommentDefaultValue() {
238                 assertThat(preferences.getTrustComment(),
239                                 is("Set from Sone Web Interface"));
240         }
241
242         @Test
243         public void preferencesRetainFcpInterfaceActiveOfTrue() {
244                 preferences.setFcpInterfaceActive(true);
245                 assertThat(preferences.isFcpInterfaceActive(), is(true));
246                 verify(eventBus).post(any(FcpInterfaceActivatedEvent.class));
247         }
248
249         @Test
250         public void preferencesRetainFcpInterfaceActiveOfFalse() {
251                 preferences.setFcpInterfaceActive(false);
252                 assertThat(preferences.isFcpInterfaceActive(), is(false));
253                 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
254         }
255
256         @Test
257         public void preferencesReturnDefaultValueWhenFcpInterfaceActiveIsSetToNull() {
258                 preferences.setFcpInterfaceActive(null);
259                 assertThat(preferences.isFcpInterfaceActive(), is(false));
260                 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
261         }
262
263         @Test
264         public void preferencesStartWithFcpInterfaceActiveDefaultValue() {
265                 assertThat(preferences.isFcpInterfaceActive(), is(false));
266         }
267
268         @Test
269         public void preferencesRetainFcpFullAccessRequiredOfNo() {
270                 preferences.setFcpFullAccessRequired(NO);
271                 assertThat(preferences.getFcpFullAccessRequired(), is(NO));
272                 verifyFullAccessRequiredChangedEvent(NO);
273         }
274
275         private void verifyFullAccessRequiredChangedEvent(
276                         FullAccessRequired fullAccessRequired) {
277                 ArgumentCaptor<FullAccessRequiredChanged> fullAccessRequiredCaptor =
278                                 forClass(FullAccessRequiredChanged.class);
279                 verify(eventBus).post(fullAccessRequiredCaptor.capture());
280                 assertThat(
281                                 fullAccessRequiredCaptor.getValue().getFullAccessRequired(),
282                                 is(fullAccessRequired));
283         }
284
285         @Test
286         public void preferencesRetainFcpFullAccessRequiredOfWriting() {
287                 preferences.setFcpFullAccessRequired(WRITING);
288                 assertThat(preferences.getFcpFullAccessRequired(), is(WRITING));
289                 verifyFullAccessRequiredChangedEvent(WRITING);
290         }
291
292         @Test
293         public void preferencesRetainFcpFullAccessRequiredOfAlways() {
294                 preferences.setFcpFullAccessRequired(ALWAYS);
295                 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
296                 verifyFullAccessRequiredChangedEvent(ALWAYS);
297         }
298
299         @Test
300         public void preferencesReturnDefaultValueWhenFcpFullAccessRequiredIsSetToNull() {
301                 preferences.setFcpFullAccessRequired(null);
302                 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
303                 verifyFullAccessRequiredChangedEvent(ALWAYS);
304         }
305
306         @Test
307         public void preferencesStartWithFcpFullAccessRequiredDefaultValue() {
308                 assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
309         }
310
311         @Test
312         public void settingInsertionDelayToValidValueSendsChangeEvent() {
313                 preferences.setInsertionDelay(30);
314                 ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
315                 verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
316                 assertThat(eventsCaptor.getAllValues(), hasItem(new PreferenceChangedEvent("InsertionDelay", 30)));
317         }
318
319 }