2 * Sone - FieldTest.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
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;
28 import java.util.List;
30 import net.pterodactylus.sone.data.Profile.Field;
32 import com.google.common.base.Optional;
33 import com.google.common.collect.Lists;
34 import org.junit.Test;
37 * Unit test for {@link Profile}.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class ProfileTest {
43 final Profile profile = new Profile((Sone) null);
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"));
54 public void testGettingAFieldByName() {
55 profile.addField("TestField");
56 Optional<Field> testField = profile.getFieldByName("TestField");
57 assertThat(testField.isPresent(), is(true));
61 public void testGettingANonExistingFieldByName() {
62 profile.addField("TestField");
63 Optional<Field> testField = profile.getFieldByName("TestField2");
64 assertThat(testField.isPresent(), is(false));
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));
76 public void testGettingANonExistingFieldById() {
77 Optional<Field> testField = profile.getFieldById("does not exist");
78 assertThat(testField.isPresent(), is(false));
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()));
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));
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"));
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));
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));
127 public void testDeletingANonExistingField() {
128 Field testField = profile.addField("TestField");
129 profile.removeField(testField);
130 profile.removeField(testField);
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));
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));
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));
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));
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));
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));
189 public void testSettingAnAvatar() {
190 profile.setAvatar(of("avatar1"));
191 assertThat(profile.getAvatar(), is("avatar1"));
195 public void testSettingNoAvatar() {
196 profile.setAvatar(Optional.<String>absent());
197 assertThat(profile.getAvatar(), is((String) null));
201 public void testDuplicatingAProfileAndGettingTheSameFingerprint() {
202 Profile secondProfile = new Profile(profile);
203 assertThat(profile.getFingerprint(), is(secondProfile.getFingerprint()));
207 public void testGettingAFingerprint() {
208 String fingerprint = profile.getFingerprint();
209 assertThat(fingerprint, notNullValue());
213 public void testThatSettingProfileFieldsChangesTheFingerprint() {
214 List<String> fingerprints = Lists.newArrayList();
215 String fingerprint = profile.getFingerprint();
216 assertThat(fingerprint, notNullValue());
217 fingerprints.add(fingerprint);
219 Field testField = profile.addField("TestField");
220 profile.setField(testField, "Value");
221 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
223 profile.modify().setFirstName("First").update();
224 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
226 profile.modify().setMiddleName("M.").update();
227 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
229 profile.modify().setLastName("Last").update();
230 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
232 profile.modify().setBirthYear(2013).update();
233 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
235 profile.modify().setBirthMonth(10).update();
236 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
238 profile.modify().setBirthDay(24).update();
239 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
241 profile.setAvatar(of("avatar1"));
242 verifyFingerprintAndAddItToListOfFingerprints(fingerprints);
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);