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