1 package net.pterodactylus.sone.template;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.allOf;
5 import static org.hamcrest.Matchers.contains;
6 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
7 import static org.hamcrest.Matchers.is;
8 import static org.hamcrest.Matchers.lessThanOrEqualTo;
9 import static org.hamcrest.Matchers.nullValue;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.List;
17 import net.pterodactylus.sone.data.Album;
18 import net.pterodactylus.sone.data.Image;
19 import net.pterodactylus.sone.data.Profile;
20 import net.pterodactylus.sone.data.Sone;
21 import net.pterodactylus.sone.test.TestUtil;
23 import org.hamcrest.Description;
24 import org.hamcrest.Matcher;
25 import org.hamcrest.TypeSafeDiagnosingMatcher;
26 import org.junit.Before;
27 import org.junit.Test;
30 * Unit test for {@link AlbumAccessor}.
32 public class AlbumAccessorTest {
34 private final AlbumAccessor albumAccessor = new AlbumAccessor();
35 private final Album album = mock(Album.class);
38 public void setupAlbum() {
39 when(album.getId()).thenReturn("Album");
40 when(album.getTitle()).thenReturn("Album Title");
44 public void backlinksAreGenerated() {
45 Sone sone = mock(Sone.class);
46 Profile profile = new Profile(sone);
47 when(sone.getId()).thenReturn("Sone");
48 when(sone.getName()).thenReturn("Sone Name");
49 when(sone.getProfile()).thenReturn(profile);
50 Album parentAlbum = mock(Album.class);
51 when(parentAlbum.isRoot()).thenReturn(true);
52 when(album.getSone()).thenReturn(sone);
53 when(album.getParent()).thenReturn(parentAlbum);
54 List<Object> backlinks =
55 (List<Object>) albumAccessor.get(null, album, "backlinks");
56 assertThat(backlinks, contains(isLink("sone=Sone", "Sone Name"),
57 isLink("album=Album", "Album Title")));
61 public void nameIsGenerated() {
62 assertThat((String) albumAccessor.get(null, album, "id"),
64 assertThat((String) albumAccessor.get(null, album, "title"),
68 private static Matcher<Object> isLink(final String target,
70 return new TypeSafeDiagnosingMatcher<Object>() {
72 protected boolean matchesSafely(Object item,
73 Description mismatchDescription) {
74 if (!TestUtil.<String>callPrivateMethod(item, "getTarget")
76 mismatchDescription.appendText("link does not contain ")
80 if (!TestUtil.<String>callPrivateMethod(item, "getName")
82 mismatchDescription.appendText("is not named ")
90 public void describeTo(Description description) {
91 description.appendText("link containing ")
93 description.appendText(", named ").appendValue(name);
99 public void albumImageIsGeneratedRandomly() {
100 Image image = mock(Image.class);
101 List<Image> albumImages = Arrays.asList(mock(Image.class), image);
102 when(album.getImages()).thenReturn(albumImages);
103 int matchedImage = 0;
104 for (int i = 0; i < 1000; i++) {
105 Image randomImage = (Image) albumAccessor.get(null, album, "albumImage");
106 if (randomImage == image) {
110 assertThat(matchedImage, allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(750)));
114 public void albumImageIsNullIfThereAreNoImagesInAnAlbum() {
115 when(album.getImages()).thenReturn(Collections.<Image>emptyList());
116 assertThat(albumAccessor.get(null, album, "albumImage"), nullValue());