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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class AlbumAccessorTest {
36 private final AlbumAccessor albumAccessor = new AlbumAccessor();
37 private final Album album = mock(Album.class);
40 public void setupAlbum() {
41 when(album.getId()).thenReturn("Album");
42 when(album.getTitle()).thenReturn("Album Title");
46 public void backlinksAreGenerated() {
47 Sone sone = mock(Sone.class);
48 Profile profile = new Profile(sone);
49 when(sone.getId()).thenReturn("Sone");
50 when(sone.getName()).thenReturn("Sone Name");
51 when(sone.getProfile()).thenReturn(profile);
52 Album parentAlbum = mock(Album.class);
53 when(parentAlbum.isRoot()).thenReturn(true);
54 when(album.getSone()).thenReturn(sone);
55 when(album.getParent()).thenReturn(parentAlbum);
56 List<Object> backlinks =
57 (List<Object>) albumAccessor.get(null, album, "backlinks");
58 assertThat(backlinks, contains(isLink("sone=Sone", "Sone Name"),
59 isLink("album=Album", "Album Title")));
63 public void nameIsGenerated() {
64 assertThat((String) albumAccessor.get(null, album, "id"),
66 assertThat((String) albumAccessor.get(null, album, "title"),
70 private static Matcher<Object> isLink(final String target,
72 return new TypeSafeDiagnosingMatcher<Object>() {
74 protected boolean matchesSafely(Object item,
75 Description mismatchDescription) {
76 if (!TestUtil.<String>callPrivateMethod(item, "getTarget")
78 mismatchDescription.appendText("link does not contain ")
82 if (!TestUtil.<String>callPrivateMethod(item, "getName")
84 mismatchDescription.appendText("is not named ")
92 public void describeTo(Description description) {
93 description.appendText("link containing ")
95 description.appendText(", named ").appendValue(name);
101 public void albumImageIsGeneratedRandomly() {
102 Image image = mock(Image.class);
103 List<Image> albumImages = Arrays.asList(mock(Image.class), image);
104 when(album.getImages()).thenReturn(albumImages);
105 int matchedImage = 0;
106 for (int i = 0; i < 1000; i++) {
107 Image randomImage = (Image) albumAccessor.get(null, album, "albumImage");
108 if (randomImage == image) {
112 assertThat(matchedImage, allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(750)));
116 public void albumImageIsNullIfThereAreNoImagesInAnAlbum() {
117 when(album.getImages()).thenReturn(Collections.<Image>emptyList());
118 assertThat(albumAccessor.get(null, album, "albumImage"), nullValue());