Merge branch 'release/0.9-rc1'
[Sone.git] / src / test / java / net / pterodactylus / sone / template / AlbumAccessorTest.java
1 package net.pterodactylus.sone.template;
2
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;
8
9 import java.util.List;
10
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;
15
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;
21
22 /**
23  * Unit test for {@link AlbumAccessor}.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class AlbumAccessorTest {
28
29         private final AlbumAccessor albumAccessor = new AlbumAccessor();
30         private final Album album = mock(Album.class);
31
32         @Before
33         public void setupAlbum() {
34                 when(album.getId()).thenReturn("Album");
35                 when(album.getTitle()).thenReturn("Album Title");
36         }
37
38         @Test
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")));
53         }
54
55         @Test
56         public void nameIsGenerated() {
57                 assertThat((String) albumAccessor.get(null, album, "id"),
58                                 is("Album"));
59                 assertThat((String) albumAccessor.get(null, album, "title"),
60                                 is("Album Title"));
61         }
62
63         private static Matcher<Object> isLink(final String target,
64                         final String name) {
65                 return new TypeSafeDiagnosingMatcher<Object>() {
66                         @Override
67                         protected boolean matchesSafely(Object item,
68                                         Description mismatchDescription) {
69                                 if (!TestUtil.<String>callPrivateMethod(item, "getTarget")
70                                                 .contains(target)) {
71                                         mismatchDescription.appendText("link does not contain ")
72                                                         .appendValue(target);
73                                         return false;
74                                 }
75                                 if (!TestUtil.<String>callPrivateMethod(item, "getName")
76                                                 .equals(name)) {
77                                         mismatchDescription.appendText("is not named ")
78                                                         .appendValue(name);
79                                         return false;
80                                 }
81                                 return true;
82                         }
83
84                         @Override
85                         public void describeTo(Description description) {
86                                 description.appendText("link containing ")
87                                                 .appendValue(target);
88                                 description.appendText(", named ").appendValue(name);
89                         }
90                 };
91         }
92
93 }