Remove @author tags
[Sone.git] / src / test / java / net / pterodactylus / sone / template / AlbumAccessorTest.java
1 package net.pterodactylus.sone.template;
2
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;
12
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.List;
16
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;
22
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;
28
29 /**
30  * Unit test for {@link AlbumAccessor}.
31  */
32 public class AlbumAccessorTest {
33
34         private final AlbumAccessor albumAccessor = new AlbumAccessor();
35         private final Album album = mock(Album.class);
36
37         @Before
38         public void setupAlbum() {
39                 when(album.getId()).thenReturn("Album");
40                 when(album.getTitle()).thenReturn("Album Title");
41         }
42
43         @Test
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")));
58         }
59
60         @Test
61         public void nameIsGenerated() {
62                 assertThat((String) albumAccessor.get(null, album, "id"),
63                                 is("Album"));
64                 assertThat((String) albumAccessor.get(null, album, "title"),
65                                 is("Album Title"));
66         }
67
68         private static Matcher<Object> isLink(final String target,
69                         final String name) {
70                 return new TypeSafeDiagnosingMatcher<Object>() {
71                         @Override
72                         protected boolean matchesSafely(Object item,
73                                         Description mismatchDescription) {
74                                 if (!TestUtil.<String>callPrivateMethod(item, "getTarget")
75                                                 .contains(target)) {
76                                         mismatchDescription.appendText("link does not contain ")
77                                                         .appendValue(target);
78                                         return false;
79                                 }
80                                 if (!TestUtil.<String>callPrivateMethod(item, "getName")
81                                                 .equals(name)) {
82                                         mismatchDescription.appendText("is not named ")
83                                                         .appendValue(name);
84                                         return false;
85                                 }
86                                 return true;
87                         }
88
89                         @Override
90                         public void describeTo(Description description) {
91                                 description.appendText("link containing ")
92                                                 .appendValue(target);
93                                 description.appendText(", named ").appendValue(name);
94                         }
95                 };
96         }
97
98         @Test
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) {
107                                 matchedImage++;
108                         }
109                 }
110                 assertThat(matchedImage, allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(750)));
111         }
112
113         @Test
114         public void albumImageIsNullIfThereAreNoImagesInAnAlbum() {
115                 when(album.getImages()).thenReturn(Collections.<Image>emptyList());
116                 assertThat(albumAccessor.get(null, album, "albumImage"), nullValue());
117         }
118
119 }