1 package net.pterodactylus.sone.template;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.contains;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
11 import net.pterodactylus.sone.TestUtil;
12 import net.pterodactylus.sone.data.Album;
13 import net.pterodactylus.sone.data.Profile;
14 import net.pterodactylus.sone.data.Sone;
16 import org.hamcrest.Description;
17 import org.hamcrest.Matcher;
18 import org.hamcrest.TypeSafeDiagnosingMatcher;
19 import org.junit.Before;
20 import org.junit.Test;
23 * Unit test for {@link AlbumAccessor}.
25 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27 public class AlbumAccessorTest {
29 private final AlbumAccessor albumAccessor = new AlbumAccessor();
30 private final Album album = mock(Album.class);
33 public void setupAlbum() {
34 when(album.getId()).thenReturn("Album");
35 when(album.getTitle()).thenReturn("Album Title");
39 public void backlinksAreGenerated() {
40 Sone sone = mock(Sone.class);
41 Profile profile = new Profile(sone);
42 when(sone.getId()).thenReturn("Sone");
43 when(sone.getName()).thenReturn("Sone Name");
44 when(sone.getProfile()).thenReturn(profile);
45 Album parentAlbum = mock(Album.class);
46 when(parentAlbum.isRoot()).thenReturn(true);
47 when(album.getSone()).thenReturn(sone);
48 when(album.getParent()).thenReturn(parentAlbum);
49 List<Object> backlinks =
50 (List<Object>) albumAccessor.get(null, album, "backlinks");
51 assertThat(backlinks, contains(isLink("sone=Sone", "Sone Name"),
52 isLink("album=Album", "Album Title")));
56 public void nameIsGenerated() {
57 assertThat((String) albumAccessor.get(null, album, "id"),
59 assertThat((String) albumAccessor.get(null, album, "title"),
63 private static Matcher<Object> isLink(final String target,
65 return new TypeSafeDiagnosingMatcher<Object>() {
67 protected boolean matchesSafely(Object item,
68 Description mismatchDescription) {
69 if (!TestUtil.<String>callPrivateMethod(item, "getTarget")
71 mismatchDescription.appendText("link does not contain ")
75 if (!TestUtil.<String>callPrivateMethod(item, "getName")
77 mismatchDescription.appendText("is not named ")
85 public void describeTo(Description description) {
86 description.appendText("link containing ")
88 description.appendText(", named ").appendValue(name);