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