Add dependency injection configuration to identity accessor
[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.TestUtil;
18 import net.pterodactylus.sone.data.Album;
19 import net.pterodactylus.sone.data.Image;
20 import net.pterodactylus.sone.data.Profile;
21 import net.pterodactylus.sone.data.Sone;
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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class AlbumAccessorTest {
35
36         private final AlbumAccessor albumAccessor = new AlbumAccessor();
37         private final Album album = mock(Album.class);
38
39         @Before
40         public void setupAlbum() {
41                 when(album.getId()).thenReturn("Album");
42                 when(album.getTitle()).thenReturn("Album Title");
43         }
44
45         @Test
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")));
60         }
61
62         @Test
63         public void nameIsGenerated() {
64                 assertThat((String) albumAccessor.get(null, album, "id"),
65                                 is("Album"));
66                 assertThat((String) albumAccessor.get(null, album, "title"),
67                                 is("Album Title"));
68         }
69
70         private static Matcher<Object> isLink(final String target,
71                         final String name) {
72                 return new TypeSafeDiagnosingMatcher<Object>() {
73                         @Override
74                         protected boolean matchesSafely(Object item,
75                                         Description mismatchDescription) {
76                                 if (!TestUtil.<String>callPrivateMethod(item, "getTarget")
77                                                 .contains(target)) {
78                                         mismatchDescription.appendText("link does not contain ")
79                                                         .appendValue(target);
80                                         return false;
81                                 }
82                                 if (!TestUtil.<String>callPrivateMethod(item, "getName")
83                                                 .equals(name)) {
84                                         mismatchDescription.appendText("is not named ")
85                                                         .appendValue(name);
86                                         return false;
87                                 }
88                                 return true;
89                         }
90
91                         @Override
92                         public void describeTo(Description description) {
93                                 description.appendText("link containing ")
94                                                 .appendValue(target);
95                                 description.appendText(", named ").appendValue(name);
96                         }
97                 };
98         }
99
100         @Test
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) {
109                                 matchedImage++;
110                         }
111                 }
112                 assertThat(matchedImage, allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(750)));
113         }
114
115         @Test
116         public void albumImageIsNullIfThereAreNoImagesInAnAlbum() {
117                 when(album.getImages()).thenReturn(Collections.<Image>emptyList());
118                 assertThat(albumAccessor.get(null, album, "albumImage"), nullValue());
119         }
120
121 }