Use event bus to activate and deactivate the FCP interface.
[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 org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.instanceOf;
6 import static org.hamcrest.Matchers.is;
7 import static org.mockito.Matchers.any;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.verifyNoMoreInteractions;
11 import static org.mockito.Mockito.when;
12
13 import net.pterodactylus.sone.core.Options.Option;
14 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
15 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
16 import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent;
17 import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
18
19 import com.google.common.eventbus.EventBus;
20 import org.junit.Before;
21 import org.junit.Test;
22
23 /**
24  * Unit test for {@link Preferences}.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class PreferencesTest {
29
30         private static final int INTEGER_VALUE = 1;
31         private static final String STRING_VALUE = "string-value";
32         private final Options options = mock(Options.class);
33         private final EventBus eventBus = mock(EventBus.class);
34         private final Preferences preferences = new Preferences(eventBus, options);
35         private final Option<Integer> integerOption = when(mock(Option.class).get()).thenReturn(INTEGER_VALUE).getMock();
36         private final Option<Boolean> booleanOption = when(mock(Option.class).get()).thenReturn(true).getMock();
37         private final Option<String> stringOption = when(mock(Option.class).get()).thenReturn(STRING_VALUE).getMock();
38
39         @Before
40         public void setupOptions() {
41                 when(integerOption.validate(INTEGER_VALUE)).thenReturn(true);
42                 when(options.getIntegerOption("InsertionDelay")).thenReturn(integerOption);
43                 when(options.getIntegerOption("PostsPerPage")).thenReturn(integerOption);
44                 when(options.getIntegerOption("ImagesPerPage")).thenReturn(integerOption);
45                 when(options.getIntegerOption("CharactersPerPost")).thenReturn(integerOption);
46                 when(options.getIntegerOption("PostCutOffLength")).thenReturn(integerOption);
47                 when(options.getBooleanOption("RequireFullAccess")).thenReturn(booleanOption);
48                 when(options.getIntegerOption("PositiveTrust")).thenReturn(integerOption);
49                 when(options.getIntegerOption("NegativeTrust")).thenReturn(integerOption);
50                 when(options.getStringOption("TrustComment")).thenReturn(stringOption);
51                 when(options.getBooleanOption("ActivateFcpInterface")).thenReturn(booleanOption);
52                 when(options.getIntegerOption("FcpFullAccessRequired")).thenReturn(integerOption);
53         }
54
55         @Test
56         public void testGettingInsertionDelay() {
57                 assertThat(preferences.getInsertionDelay(), is(INTEGER_VALUE));
58                 verify(integerOption).get();
59         }
60
61         @Test
62         public void validationOfInsertionDelayIsForwardedToOptions() {
63                 preferences.validateInsertionDelay(INTEGER_VALUE);
64                 verify(integerOption).validate(INTEGER_VALUE);
65         }
66
67         @Test
68         public void settingInsertionDelayIsForwardedToOptions() {
69                 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
70                 verify(integerOption).set(INTEGER_VALUE);
71         }
72
73         @Test
74         public void settingInsertionDelayIsForwardedToEventBus() {
75                 assertThat(preferences.setInsertionDelay(INTEGER_VALUE), instanceOf(Preferences.class));
76                 verify(eventBus).post(any(InsertionDelayChangedEvent.class));
77         }
78
79         @Test
80         public void testGettingPostsPerPage() {
81                 assertThat(preferences.getPostsPerPage(), is(INTEGER_VALUE));
82                 verify(integerOption).get();
83         }
84
85         @Test
86         public void validationOfPostsPerPageIsForwardedToOptions() {
87                 preferences.validatePostsPerPage(INTEGER_VALUE);
88                 verify(integerOption).validate(INTEGER_VALUE);
89         }
90
91         @Test
92         public void settingPostsPerPageIsForwardedToOptions() {
93                 assertThat(preferences.setPostsPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
94                 verify(integerOption).set(INTEGER_VALUE);
95         }
96
97         @Test
98         public void testGettingImagesPerPage() {
99                 assertThat(preferences.getImagesPerPage(), is(INTEGER_VALUE));
100                 verify(integerOption).get();
101         }
102
103         @Test
104         public void validationOfImagesPerPageIsForwardedToOptions() {
105                 preferences.validateImagesPerPage(INTEGER_VALUE);
106                 verify(integerOption).validate(INTEGER_VALUE);
107         }
108
109         @Test
110         public void settingImagesPerPageIsForwardedToOptions() {
111                 assertThat(preferences.setImagesPerPage(INTEGER_VALUE), instanceOf(Preferences.class));
112                 verify(integerOption).set(INTEGER_VALUE);
113         }
114
115         @Test
116         public void testGettingCharactersPerPost() {
117                 assertThat(preferences.getCharactersPerPost(), is(INTEGER_VALUE));
118                 verify(integerOption).get();
119         }
120
121         @Test
122         public void validationOfCharactersPerPostIsForwardedToOptions() {
123                 preferences.validateCharactersPerPost(INTEGER_VALUE);
124                 verify(integerOption).validate(INTEGER_VALUE);
125         }
126
127         @Test
128         public void settingCharactersPerPostIsForwardedToOptions() {
129                 assertThat(preferences.setCharactersPerPost(INTEGER_VALUE), instanceOf(Preferences.class));
130                 verify(integerOption).set(INTEGER_VALUE);
131         }
132
133         @Test
134         public void testGettingPostCutOffLength() {
135                 assertThat(preferences.getPostCutOffLength(), is(INTEGER_VALUE));
136                 verify(integerOption).get();
137         }
138
139         @Test
140         public void validationOfPostCutOffLengthIsForwardedToOptions() {
141                 preferences.validatePostCutOffLength(INTEGER_VALUE);
142                 verify(integerOption).validate(INTEGER_VALUE);
143         }
144
145         @Test
146         public void settingPostCutOffLengthIsForwardedToOptions() {
147                 assertThat(preferences.setPostCutOffLength(INTEGER_VALUE), instanceOf(Preferences.class));
148                 verify(integerOption).set(INTEGER_VALUE);
149         }
150
151         @Test
152         public void testGettingRequireFullAccess() {
153                 assertThat(preferences.isRequireFullAccess(), is(true));
154                 verify(booleanOption).get();
155         }
156
157         @Test
158         public void settingRequireFullAccessIsForwardedToOption() {
159                 preferences.setRequireFullAccess(true);
160                 verify(booleanOption).set(true);
161         }
162
163         @Test
164         public void testGettingPositiveTrust() {
165                 assertThat(preferences.getPositiveTrust(), is(INTEGER_VALUE));
166                 verify(integerOption).get();
167         }
168
169         @Test
170         public void validationOfPositiveTrustIsForwardedToOptions() {
171                 preferences.validatePositiveTrust(INTEGER_VALUE);
172                 verify(integerOption).validate(INTEGER_VALUE);
173         }
174
175         @Test
176         public void settingPositiveTrustIsForwardedToOptions() {
177                 assertThat(preferences.setPositiveTrust(INTEGER_VALUE), instanceOf(Preferences.class));
178                 verify(integerOption).set(INTEGER_VALUE);
179         }
180
181         @Test
182         public void testGettingNegativeTrust() {
183                 assertThat(preferences.getNegativeTrust(), is(INTEGER_VALUE));
184                 verify(integerOption).get();
185         }
186
187         @Test
188         public void validationOfNegativeTrustIsForwardedToOptions() {
189                 preferences.validateNegativeTrust(INTEGER_VALUE);
190                 verify(integerOption).validate(INTEGER_VALUE);
191         }
192
193         @Test
194         public void settingNegativeTrustIsForwardedToOptions() {
195                 assertThat(preferences.setNegativeTrust(INTEGER_VALUE), instanceOf(Preferences.class));
196                 verify(integerOption).set(INTEGER_VALUE);
197         }
198
199         @Test
200         public void gettingTrustCommentIsForwardedToOption() {
201                 assertThat(preferences.getTrustComment(), is(STRING_VALUE));
202                 verify(stringOption).get();
203         }
204
205         @Test
206         public void settingTrustCommentIsForwardedToOption() {
207                 preferences.setTrustComment(STRING_VALUE);
208                 verify(stringOption).set(STRING_VALUE);
209         }
210
211         @Test
212         public void gettingFcpInterfaceActiveIsForwardedToOption() {
213                 assertThat(preferences.isFcpInterfaceActive(), is(true));
214                 verify(booleanOption).get();
215         }
216
217         @Test
218         public void settingFcpInterfaceActiveIsForwardedToEventBus() {
219             preferences.setFcpInterfaceActive(true);
220                 verify(eventBus).post(any(FcpInterfaceActivatedEvent.class));
221                 verifyNoMoreInteractions(eventBus);
222         }
223
224         @Test
225         public void settingFcpInterfaceInactiveIsForwardedToEventBus() {
226             preferences.setFcpInterfaceActive(false);
227                 verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class));
228                 verifyNoMoreInteractions(eventBus);
229         }
230
231         @Test
232         public void settingFcpInterfaceActiveIsForwardedToOption() {
233                 preferences.setFcpInterfaceActive(true);
234                 verify(booleanOption).set(true);
235         }
236
237         @Test
238         public void gettingFcpFullAccessRequired() {
239                 assertThat(preferences.getFcpFullAccessRequired(), is(FullAccessRequired.values()[INTEGER_VALUE]));
240                 verify(integerOption).get();
241         }
242
243         @Test
244         public void settingFcpFullAccessRequiredIsForwardedToOption() {
245                 preferences.setFcpFullAccessRequired(ALWAYS);
246                 verify(integerOption).set(ALWAYS.ordinal());
247         }
248
249         @Test
250         public void settingFcpFullAccessRequiredToNullIsForwardedToOption() {
251                 preferences.setFcpFullAccessRequired(null);
252                 verify(integerOption).set(null);
253         }
254
255 }