X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FProfileTest.java;h=66aa568b44feaf25b9a2b7a443f5399ec5acd8fe;hb=68cf128b60ff1e9a27964de5bc07cebe666c41c6;hp=4f81966c249221a87a65a1b4f472f3a5f1224422;hpb=d77d71a2dc86a03078ab1c8b0ec10321c147492c;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/data/ProfileTest.java b/src/test/java/net/pterodactylus/sone/data/ProfileTest.java index 4f81966..66aa568 100644 --- a/src/test/java/net/pterodactylus/sone/data/ProfileTest.java +++ b/src/test/java/net/pterodactylus/sone/data/ProfileTest.java @@ -17,15 +17,24 @@ package net.pterodactylus.sone.data; +import static com.google.common.base.Optional.of; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import java.util.List; import net.pterodactylus.sone.data.Profile.Field; +import com.google.common.base.Optional; +import com.google.common.collect.Lists; import org.junit.Test; /** - * Unit test for {@link Field}. + * Unit test for {@link Profile}. * * @author David ‘Bombe’ Roden */ @@ -34,12 +43,210 @@ public class ProfileTest { final Profile profile = new Profile((Sone) null); @Test + public void testAddingAField() { + profile.addField("TestField"); + Optional testField = profile.getFieldByName("TestField"); + assertThat(testField.isPresent(), is(true)); + assertThat(testField.get().getName(), is("TestField")); + } + + @Test + public void testGettingAFieldByName() { + profile.addField("TestField"); + Optional testField = profile.getFieldByName("TestField"); + assertThat(testField.isPresent(), is(true)); + } + + @Test + public void testGettingANonExistingFieldByName() { + profile.addField("TestField"); + Optional testField = profile.getFieldByName("TestField2"); + assertThat(testField.isPresent(), is(false)); + } + + @Test + public void testGettingAFieldById() { + profile.addField("TestField"); + Optional testField = profile.getFieldByName("TestField"); + testField = profile.getFieldById(testField.get().getId()); + assertThat(testField.isPresent(), is(true)); + } + + @Test + public void testGettingANonExistingFieldById() { + Optional testField = profile.getFieldById("does not exist"); + assertThat(testField.isPresent(), is(false)); + } + + @Test public void testRenamingAField() { profile.addField("TestField"); - Field testField = profile.getFieldByName("TestField"); - profile.renameField(testField, "RenamedField"); - Field renamedField = profile.getFieldByName("RenamedField"); - assertThat(testField.getId(), is(renamedField.getId())); + Optional testField = profile.getFieldByName("TestField"); + profile.renameField(testField.get(), "RenamedField"); + Optional renamedField = profile.getFieldByName("RenamedField"); + assertThat(testField.get().getId(), is(renamedField.get().getId())); + } + + @Test + public void testRenamingANonExistingField() { + Field testField = profile.addField("TestField"); + profile.removeField(testField); + profile.renameField(testField, "TestField2"); + Optional testField2 = profile.getFieldByName("TestField2"); + assertThat(testField2.isPresent(), is(false)); + } + + @Test + public void testChangingTheValueOfAField() { + profile.addField("TestField"); + Optional testField = profile.getFieldByName("TestField"); + profile.setField(testField.get(), "Test"); + testField = profile.getFieldByName("TestField"); + assertThat(testField.get().getValue(), is("Test")); + } + + @Test + public void testChangingTheValueOfANonExistingField() { + Field testField = profile.addField("TestField"); + profile.removeField(testField); + profile.setField(testField, "Test"); + Optional testField2 = profile.getFieldByName("TestField"); + assertThat(testField2.isPresent(), is(false)); + } + + @Test + public void testDeletingAField() { + profile.addField("TestField"); + Optional testField = profile.getFieldByName("TestField"); + profile.removeField(testField.get()); + testField = profile.getFieldByName("TestField"); + assertThat(testField.isPresent(), is(false)); + } + + @Test + public void testDeletingANonExistingField() { + Field testField = profile.addField("TestField"); + profile.removeField(testField); + profile.removeField(testField); + } + + @Test + public void testGettingFieldList() { + Field firstField = profile.addField("First"); + Field secondField = profile.addField("Second"); + List fields = profile.getFields(); + assertThat(fields, contains(firstField, secondField)); + } + + @Test + public void testMovingAFieldUp() { + Field firstField = profile.addField("First"); + Field secondField = profile.addField("Second"); + profile.moveFieldUp(secondField); + List fields = profile.getFields(); + assertThat(fields, contains(secondField, firstField)); + } + + @Test + public void testMovingTheFirstFieldUp() { + Field firstField = profile.addField("First"); + Field secondField = profile.addField("Second"); + profile.moveFieldUp(firstField); + List fields = profile.getFields(); + assertThat(fields, contains(firstField, secondField)); + } + + @Test + public void testMovingAFieldDown() { + Field firstField = profile.addField("First"); + Field secondField = profile.addField("Second"); + profile.moveFieldDown(firstField); + List fields = profile.getFields(); + assertThat(fields, contains(secondField, firstField)); + } + + @Test + public void testMovingTheLastFieldDown() { + Field firstField = profile.addField("First"); + Field secondField = profile.addField("Second"); + profile.moveFieldDown(secondField); + List fields = profile.getFields(); + assertThat(fields, contains(firstField, secondField)); + } + + @Test + public void testModifyingAProfile() { + profile.modify().setFirstName("First").setMiddleName("M.").setLastName("Last").setBirthYear(2013).setBirthMonth(10).setBirthDay(24).update(); + assertThat(profile.getFirstName(), is("First")); + assertThat(profile.getMiddleName(), is("M.")); + assertThat(profile.getLastName(), is("Last")); + assertThat(profile.getBirthYear(), is(2013)); + assertThat(profile.getBirthMonth(), is(10)); + assertThat(profile.getBirthDay(), is(24)); + } + + @Test + public void testSettingAnAvatar() { + profile.setAvatar(of("avatar1")); + assertThat(profile.getAvatar(), is("avatar1")); + } + + @Test + public void testSettingNoAvatar() { + profile.setAvatar(Optional.absent()); + assertThat(profile.getAvatar(), is((String) null)); + } + + @Test + public void testDuplicatingAProfileAndGettingTheSameFingerprint() { + Profile secondProfile = new Profile(profile); + assertThat(profile.getFingerprint(), is(secondProfile.getFingerprint())); + } + + @Test + public void testGettingAFingerprint() { + String fingerprint = profile.getFingerprint(); + assertThat(fingerprint, notNullValue()); + } + + @Test + public void testThatSettingProfileFieldsChangesTheFingerprint() { + List fingerprints = Lists.newArrayList(); + String fingerprint = profile.getFingerprint(); + assertThat(fingerprint, notNullValue()); + fingerprints.add(fingerprint); + + Field testField = profile.addField("TestField"); + profile.setField(testField, "Value"); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + + profile.modify().setFirstName("First").update(); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + + profile.modify().setMiddleName("M.").update(); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + + profile.modify().setLastName("Last").update(); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + + profile.modify().setBirthYear(2013).update(); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + + profile.modify().setBirthMonth(10).update(); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + + profile.modify().setBirthDay(24).update(); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + + profile.setAvatar(of("avatar1")); + verifyFingerprintAndAddItToListOfFingerprints(fingerprints); + } + + private void verifyFingerprintAndAddItToListOfFingerprints(List fingerprints) { + String fingerprint = profile.getFingerprint(); + assertThat(fingerprint, notNullValue()); + assertThat(fingerprints, not(hasItem(fingerprint))); + fingerprints.add(fingerprint); } }