From e60ab7dcb0502fdbfaa4405b76714b2cded89318 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 9 Nov 2013 15:49:01 +0100 Subject: [PATCH] Add unit test for AlbumAccessor. --- .../sone/template/AlbumAccessorTest.java | 133 +++++++++++++++++++++ 1 file changed, 133 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..0bcda36 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/AlbumAccessorTest.java @@ -0,0 +1,133 @@ +/* + * Sone - AlbumAccessorTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import static com.google.common.base.Optional.of; +import static net.pterodactylus.sone.template.SoneAccessor.getNiceName; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.List; + +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.Mocks; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.template.AlbumAccessor.Link; + +import com.google.common.base.Optional; +import org.junit.Test; + +/** + * Unit test for {@link AlbumAccessor}. + * + * @author David ‘Bombe’ Roden + */ +public class AlbumAccessorTest { + + private final Mocks mocks = new Mocks(); + private final Sone sone = mocks.mockSone("Sone").withName("Sone").withProfileName("S.", "O.", "Ne").create(); + private final AlbumAccessor albumAccessor = new AlbumAccessor(); + + @Test + public void generateSingleBacklinkForRootAlbum() { + Album rootAlbum = createRootAlbum("AlbumId"); + List backlinks = (List) albumAccessor.get(null, rootAlbum, "backlinks"); + verifyAlbums(backlinks, rootAlbum); + } + + @Test + public void generateBacklinksForNestedAlbums() { + Album rootAlbum = createRootAlbum("RootAlbum"); + Album firstLevelAlbum = createNestedAlbum(rootAlbum, "FirstLevel"); + Album secondLevelAlbum = createNestedAlbum(firstLevelAlbum, "SecondLevel"); + List backlinks = (List) albumAccessor.get(null, secondLevelAlbum, "backlinks"); + verifyAlbums(backlinks, secondLevelAlbum); + } + + @Test + public void returnAlbumImage() { + Album album = createRootAlbum("AlbumId"); + Image albumImage = mock(Image.class); + when(album.getAlbumImage()).thenReturn(of(albumImage)); + Image image = (Image) albumAccessor.get(null, album, "albumImage"); + assertThat(image, is(albumImage)); + } + + @Test + public void returnNonExistingAlbumImage() { + Album album = createRootAlbum("AlbumId"); + when(album.getAlbumImage()).thenReturn(Optional.absent()); + Image image = (Image) albumAccessor.get(null, album, "albumImage"); + assertThat(image, nullValue()); + } + + @Test + public void reflectionAccessorIsUsed() { + Album album = mock(Album.class); + when(album.getId()).thenReturn("Album"); + assertThat(albumAccessor.get(null, album, "id"), is((Object) "Album")); + } + + private void verifyAlbums(List backlinks, Album album) { + assertThat(backlinks, hasSize(findAlbumLevel(album))); + Album currentAlbum = album; + int currentIndex = backlinks.size() - 1; + while (!currentAlbum.isRoot()) { + assertThat(backlinks.get(currentIndex).getTarget(), containsString(currentAlbum.getId())); + assertThat(backlinks.get(currentIndex).getName(), is(currentAlbum.getTitle())); + currentIndex--; + currentAlbum = currentAlbum.getParent(); + } + assertThat(backlinks.get(0).getTarget(), containsString(currentAlbum.getSone().getId())); + assertThat(backlinks.get(0).getName(), is(getNiceName(currentAlbum.getSone()))); + } + + private int findAlbumLevel(Album album) { + int albumLevel = 1; + Album currentAlbum = album; + while (!currentAlbum.isRoot()) { + albumLevel++; + currentAlbum = currentAlbum.getParent(); + } + return albumLevel; + } + + private Album createRootAlbum(String id) { + Album album = mock(Album.class); + when(album.getId()).thenReturn(id); + when(album.isRoot()).thenReturn(true); + when(album.getSone()).thenReturn(sone); + return album; + } + + private Album createNestedAlbum(Album parentAlbum, String id) { + Album album = mock(Album.class); + when(album.getId()).thenReturn(id); + when(album.isRoot()).thenReturn(false); + when(album.getSone()).thenReturn(sone); + when(album.getParent()).thenReturn(parentAlbum); + return album; + } + +} -- 2.7.4