Add unit test for AlbumAccessor.
[Sone.git] / src / test / java / net / pterodactylus / sone / template / AlbumAccessorTest.java
1 /*
2  * Sone - AlbumAccessorTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
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;
29
30 import java.util.List;
31
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;
37
38 import com.google.common.base.Optional;
39 import org.junit.Test;
40
41 /**
42  * Unit test for {@link AlbumAccessor}.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class AlbumAccessorTest {
47
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();
51
52         @Test
53         public void generateSingleBacklinkForRootAlbum() {
54                 Album rootAlbum = createRootAlbum("AlbumId");
55                 List<Link> backlinks = (List<Link>) albumAccessor.get(null, rootAlbum, "backlinks");
56                 verifyAlbums(backlinks, rootAlbum);
57         }
58
59         @Test
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);
66         }
67
68         @Test
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));
75         }
76
77         @Test
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());
83         }
84
85         @Test
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"));
90         }
91
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()));
99                         currentIndex--;
100                         currentAlbum = currentAlbum.getParent();
101                 }
102                 assertThat(backlinks.get(0).getTarget(), containsString(currentAlbum.getSone().getId()));
103                 assertThat(backlinks.get(0).getName(), is(getNiceName(currentAlbum.getSone())));
104         }
105
106         private int findAlbumLevel(Album album) {
107                 int albumLevel = 1;
108                 Album currentAlbum = album;
109                 while (!currentAlbum.isRoot()) {
110                         albumLevel++;
111                         currentAlbum = currentAlbum.getParent();
112                 }
113                 return albumLevel;
114         }
115
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);
121                 return album;
122         }
123
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);
130                 return album;
131         }
132
133 }