Add more tests.
[Sone.git] / src / test / java / net / pterodactylus / sone / data / ProfileTest.java
1 /*
2  * Sone - FieldTest.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.data;
19
20 import static com.google.common.base.Optional.of;
21 import static org.hamcrest.CoreMatchers.not;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.contains;
24 import static org.hamcrest.Matchers.hasItem;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.notNullValue;
27
28 import java.util.List;
29
30 import net.pterodactylus.sone.data.Profile.Field;
31
32 import com.google.common.base.Optional;
33 import com.google.common.collect.Lists;
34 import org.junit.Test;
35
36 /**
37  * Unit test for {@link Profile}.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class ProfileTest {
42
43         final Profile profile = new Profile((Sone) null);
44
45         @Test
46         public void testAddingAField() {
47                 profile.addField("TestField");
48                 Optional<Field> testField = profile.getFieldByName("TestField");
49                 assertThat(testField.isPresent(), is(true));
50                 assertThat(testField.get().getName(), is("TestField"));
51         }
52
53         @Test
54         public void testGettingAFieldByName() {
55                 profile.addField("TestField");
56                 Optional<Field> testField = profile.getFieldByName("TestField");
57                 assertThat(testField.isPresent(), is(true));
58         }
59
60         @Test
61         public void testGettingANonExistingFieldByName() {
62                 profile.addField("TestField");
63                 Optional<Field> testField = profile.getFieldByName("TestField2");
64                 assertThat(testField.isPresent(), is(false));
65         }
66
67         @Test
68         public void testGettingAFieldById() {
69                 profile.addField("TestField");
70                 Optional<Field> testField = profile.getFieldByName("TestField");
71                 testField = profile.getFieldById(testField.get().getId());
72                 assertThat(testField.isPresent(), is(true));
73         }
74
75         @Test
76         public void testGettingANonExistingFieldById() {
77                 Optional<Field> testField = profile.getFieldById("does not exist");
78                 assertThat(testField.isPresent(), is(false));
79         }
80
81         @Test
82         public void testRenamingAField() {
83                 profile.addField("TestField");
84                 Optional<Field> testField = profile.getFieldByName("TestField");
85                 profile.renameField(testField.get(), "RenamedField");
86                 Optional<Field> renamedField = profile.getFieldByName("RenamedField");
87                 assertThat(testField.get().getId(), is(renamedField.get().getId()));
88         }
89
90         @Test
91         public void testRenamingANonExistingField() {
92                 Field testField = profile.addField("TestField");
93                 profile.removeField(testField);
94                 profile.renameField(testField, "TestField2");
95                 Optional<Field> testField2 = profile.getFieldByName("TestField2");
96                 assertThat(testField2.isPresent(), is(false));
97         }
98
99         @Test
100         public void testChangingTheValueOfAField() {
101                 profile.addField("TestField");
102                 Optional<Field> testField = profile.getFieldByName("TestField");
103                 profile.setField(testField.get(), "Test");
104                 testField = profile.getFieldByName("TestField");
105                 assertThat(testField.get().getValue(), is("Test"));
106         }
107
108         @Test
109         public void testChangingTheValueOfANonExistingField() {
110                 Field testField = profile.addField("TestField");
111                 profile.removeField(testField);
112                 profile.setField(testField, "Test");
113                 Optional<Field> testField2 = profile.getFieldByName("TestField");
114                 assertThat(testField2.isPresent(), is(false));
115         }
116
117         @Test
118         public void testDeletingAField() {
119                 profile.addField("TestField");
120                 Optional<Field> testField = profile.getFieldByName("TestField");
121                 profile.removeField(testField.get());
122                 testField = profile.getFieldByName("TestField");
123                 assertThat(testField.isPresent(), is(false));
124         }
125
126         @Test
127         public void testDeletingANonExistingField() {
128                 Field testField = profile.addField("TestField");
129                 profile.removeField(testField);
130                 profile.removeField(testField);
131         }
132
133         @Test
134         public void testGettingFieldList() {
135                 Field firstField = profile.addField("First");
136                 Field secondField = profile.addField("Second");
137                 List<Field> fields = profile.getFields();
138                 assertThat(fields, contains(firstField, secondField));
139         }
140
141         @Test
142         public void testMovingAFieldUp() {
143                 Field firstField = profile.addField("First");
144                 Field secondField = profile.addField("Second");
145                 profile.moveFieldUp(secondField);
146                 List<Field> fields = profile.getFields();
147                 assertThat(fields, contains(secondField, firstField));
148         }
149
150         @Test
151         public void testMovingTheFirstFieldUp() {
152                 Field firstField = profile.addField("First");
153                 Field secondField = profile.addField("Second");
154                 profile.moveFieldUp(firstField);
155                 List<Field> fields = profile.getFields();
156                 assertThat(fields, contains(firstField, secondField));
157         }
158
159         @Test
160         public void testMovingAFieldDown() {
161                 Field firstField = profile.addField("First");
162                 Field secondField = profile.addField("Second");
163                 profile.moveFieldDown(firstField);
164                 List<Field> fields = profile.getFields();
165                 assertThat(fields, contains(secondField, firstField));
166         }
167
168         @Test
169         public void testMovingTheLastFieldDown() {
170                 Field firstField = profile.addField("First");
171                 Field secondField = profile.addField("Second");
172                 profile.moveFieldDown(secondField);
173                 List<Field> fields = profile.getFields();
174                 assertThat(fields, contains(firstField, secondField));
175         }
176
177         @Test
178         public void testModifyingAProfile() {
179                 profile.modify().setFirstName("First").setMiddleName("M.").setLastName("Last").setBirthYear(2013).setBirthMonth(10).setBirthDay(24).update();
180                 assertThat(profile.getFirstName(), is("First"));
181                 assertThat(profile.getMiddleName(), is("M."));
182                 assertThat(profile.getLastName(), is("Last"));
183                 assertThat(profile.getBirthYear(), is(2013));
184                 assertThat(profile.getBirthMonth(), is(10));
185                 assertThat(profile.getBirthDay(), is(24));
186         }
187
188         @Test
189         public void testSettingAnAvatar() {
190                 profile.setAvatar(of("avatar1"));
191                 assertThat(profile.getAvatar(), is("avatar1"));
192         }
193
194         @Test
195         public void testSettingNoAvatar() {
196                 profile.setAvatar(Optional.<String>absent());
197                 assertThat(profile.getAvatar(), is((String) null));
198         }
199
200         @Test
201         public void testDuplicatingAProfileAndGettingTheSameFingerprint() {
202                 Profile secondProfile = new Profile(profile);
203                 assertThat(profile.getFingerprint(), is(secondProfile.getFingerprint()));
204         }
205
206         @Test
207         public void testGettingAFingerprint() {
208                 String fingerprint = profile.getFingerprint();
209                 assertThat(fingerprint, notNullValue());
210         }
211
212         @Test
213         public void testThatSettingProfileFieldsChangesTheFingerprint() {
214                 List<String> fingerprints = Lists.newArrayList();
215                 String fingerprint = profile.getFingerprint();
216                 assertThat(fingerprint, notNullValue());
217                 fingerprints.add(fingerprint);
218
219                 Field testField = profile.addField("TestField");
220                 profile.setField(testField, "Value");
221                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
222
223                 profile.modify().setFirstName("First").update();
224                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
225
226                 profile.modify().setMiddleName("M.").update();
227                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
228
229                 profile.modify().setLastName("Last").update();
230                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
231
232                 profile.modify().setBirthYear(2013).update();
233                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
234
235                 profile.modify().setBirthMonth(10).update();
236                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
237
238                 profile.modify().setBirthDay(24).update();
239                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
240
241                 profile.setAvatar(of("avatar1"));
242                 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
243         }
244
245         private void verifyFingerprintAndAddItToListOfFingerprints(List<String> fingerprints) {
246                 String fingerprint = profile.getFingerprint();
247                 assertThat(fingerprint, notNullValue());
248                 assertThat(fingerprints, not(hasItem(fingerprint)));
249                 fingerprints.add(fingerprint);
250         }
251
252 }