2 * Sone - AlbumAccessorTest.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.template;
20 import static com.google.common.base.Optional.of;
21 import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.containsString;
24 import static org.hamcrest.Matchers.hasSize;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.nullValue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.util.List;
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Mocks;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.template.AlbumAccessor.Link;
38 import com.google.common.base.Optional;
39 import org.junit.Test;
42 * Unit test for {@link AlbumAccessor}.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class AlbumAccessorTest {
48 private final Mocks mocks = new Mocks();
49 private final Sone sone = mocks.mockSone("Sone").withName("Sone").withProfileName("S.", "O.", "Ne").create();
50 private final AlbumAccessor albumAccessor = new AlbumAccessor();
53 public void generateSingleBacklinkForRootAlbum() {
54 Album rootAlbum = createRootAlbum("AlbumId");
55 List<Link> backlinks = (List<Link>) albumAccessor.get(null, rootAlbum, "backlinks");
56 verifyAlbums(backlinks, rootAlbum);
60 public void generateBacklinksForNestedAlbums() {
61 Album rootAlbum = createRootAlbum("RootAlbum");
62 Album firstLevelAlbum = createNestedAlbum(rootAlbum, "FirstLevel");
63 Album secondLevelAlbum = createNestedAlbum(firstLevelAlbum, "SecondLevel");
64 List<Link> backlinks = (List<Link>) albumAccessor.get(null, secondLevelAlbum, "backlinks");
65 verifyAlbums(backlinks, secondLevelAlbum);
69 public void returnAlbumImage() {
70 Album album = createRootAlbum("AlbumId");
71 Image albumImage = mock(Image.class);
72 when(album.getAlbumImage()).thenReturn(of(albumImage));
73 Image image = (Image) albumAccessor.get(null, album, "albumImage");
74 assertThat(image, is(albumImage));
78 public void returnNonExistingAlbumImage() {
79 Album album = createRootAlbum("AlbumId");
80 when(album.getAlbumImage()).thenReturn(Optional.<Image>absent());
81 Image image = (Image) albumAccessor.get(null, album, "albumImage");
82 assertThat(image, nullValue());
86 public void reflectionAccessorIsUsed() {
87 Album album = mock(Album.class);
88 when(album.getId()).thenReturn("Album");
89 assertThat(albumAccessor.get(null, album, "id"), is((Object) "Album"));
92 private void verifyAlbums(List<Link> backlinks, Album album) {
93 assertThat(backlinks, hasSize(findAlbumLevel(album)));
94 Album currentAlbum = album;
95 int currentIndex = backlinks.size() - 1;
96 while (!currentAlbum.isRoot()) {
97 assertThat(backlinks.get(currentIndex).getTarget(), containsString(currentAlbum.getId()));
98 assertThat(backlinks.get(currentIndex).getName(), is(currentAlbum.getTitle()));
100 currentAlbum = currentAlbum.getParent();
102 assertThat(backlinks.get(0).getTarget(), containsString(currentAlbum.getSone().getId()));
103 assertThat(backlinks.get(0).getName(), is(getNiceName(currentAlbum.getSone())));
106 private int findAlbumLevel(Album album) {
108 Album currentAlbum = album;
109 while (!currentAlbum.isRoot()) {
111 currentAlbum = currentAlbum.getParent();
116 private Album createRootAlbum(String id) {
117 Album album = mock(Album.class);
118 when(album.getId()).thenReturn(id);
119 when(album.isRoot()).thenReturn(true);
120 when(album.getSone()).thenReturn(sone);
124 private Album createNestedAlbum(Album parentAlbum, String id) {
125 Album album = mock(Album.class);
126 when(album.getId()).thenReturn(id);
127 when(album.isRoot()).thenReturn(false);
128 when(album.getSone()).thenReturn(sone);
129 when(album.getParent()).thenReturn(parentAlbum);