From dcd9c925cd2aff0e3a8ec5594723fe79cdfcfd26 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 2 Nov 2014 10:32:48 +0100 Subject: [PATCH] Add test for album accessor. --- .../sone/template/AlbumAccessorTest.java | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java diff --git a/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java new file mode 100644 index 0000000..afc2a54 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java @@ -0,0 +1,93 @@ +package net.pterodactylus.sone.template; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.List; + +import net.pterodactylus.sone.TestUtil; +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Sone; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link AlbumAccessor}. + * + * @author David ‘Bombe’ Roden + */ +public class AlbumAccessorTest { + + private final AlbumAccessor albumAccessor = new AlbumAccessor(); + private final Album album = mock(Album.class); + + @Before + public void setupAlbum() { + when(album.getId()).thenReturn("Album"); + when(album.getTitle()).thenReturn("Album Title"); + } + + @Test + public void backlinksAreGenerated() { + Sone sone = mock(Sone.class); + Profile profile = new Profile(sone); + when(sone.getId()).thenReturn("Sone"); + when(sone.getName()).thenReturn("Sone Name"); + when(sone.getProfile()).thenReturn(profile); + Album parentAlbum = mock(Album.class); + when(parentAlbum.isRoot()).thenReturn(true); + when(album.getSone()).thenReturn(sone); + when(album.getParent()).thenReturn(parentAlbum); + List backlinks = + (List) albumAccessor.get(null, album, "backlinks"); + assertThat(backlinks, contains(isLink("sone=Sone", "Sone Name"), + isLink("album=Album", "Album Title"))); + } + + @Test + public void nameIsGenerated() { + assertThat((String) albumAccessor.get(null, album, "id"), + is("Album")); + assertThat((String) albumAccessor.get(null, album, "title"), + is("Album Title")); + } + + private static Matcher isLink(final String target, + final String name) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Object item, + Description mismatchDescription) { + if (!TestUtil.callPrivateMethod(item, "getTarget") + .contains(target)) { + mismatchDescription.appendText("link does not contain ") + .appendValue(target); + return false; + } + if (!TestUtil.callPrivateMethod(item, "getName") + .equals(name)) { + mismatchDescription.appendText("is not named ") + .appendValue(name); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("link containing ") + .appendValue(target); + description.appendText(", named ").appendValue(name); + } + }; + } + +} -- 2.7.4