Add unit test for ImageAccessor.
[Sone.git] / src / test / java / net / pterodactylus / sone / template / ImageAccessorTest.java
1 /*
2  * Sone - ImageAccessorTest.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import static java.util.Arrays.asList;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.nullValue;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.util.List;
28
29 import net.pterodactylus.sone.data.Album;
30 import net.pterodactylus.sone.data.Image;
31
32 import org.junit.Test;
33
34 /**
35  * Unit test for {@link ImageAccessor}.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class ImageAccessorTest {
40
41         private final ImageAccessor imageAccessor = new ImageAccessor();
42
43         @Test
44         public void previousImageIsFound() {
45                 Image image = createImage("Image3");
46                 List<Image> images = createAlbumWithImages(asList(
47                                 createImage("Image1"),
48                                 createImage("Image2"),
49                                 image
50                 ));
51                 Image previousImage = (Image) imageAccessor.get(null, image, "previous");
52                 assertThat(previousImage, is(images.get(1)));
53         }
54
55         private List<Image> createAlbumWithImages(List<Image> images) {
56                 Album album = mock(Album.class);
57                 when(album.getImages()).thenReturn(images);
58                 for (Image image : images) {
59                         when(image.getAlbum()).thenReturn(album);
60                 }
61                 return images;
62         }
63
64         @Test
65         public void noPreviousImageIsFound() {
66                 Image image = createImage("Image3");
67                 createAlbumWithImages(asList(
68                                 image,
69                                 createImage("Image1"),
70                                 createImage("Image2")
71                 ));
72                 Image previousImage = (Image) imageAccessor.get(null, image, "previous");
73                 assertThat(previousImage, nullValue());
74         }
75
76         @Test
77         public void noNextImageIsFound() {
78                 Image image = createImage("Image3");
79                 List<Image> images = createAlbumWithImages(asList(
80                                 createImage("Image1"),
81                                 createImage("Image2"),
82                                 image
83                 ));
84                 Image previousImage = (Image) imageAccessor.get(null, image, "next");
85                 assertThat(previousImage, nullValue());
86         }
87
88         @Test
89         public void nextImageIsFound() {
90                 Image image = createImage("Image3");
91                 List<Image> images = createAlbumWithImages(asList(
92                                 image,
93                                 createImage("Image1"),
94                                 createImage("Image2")
95                 ));
96                 Image previousImage = (Image) imageAccessor.get(null, image, "next");
97                 assertThat(previousImage, is(images.get(1)));
98         }
99
100         @Test
101         public void reflectionAccessorIsUsed() {
102                 Image image = createImage("imageId");
103                 String id = (String) imageAccessor.get(null, image, "id");
104                 assertThat(id, is(image.getId()));
105         }
106
107         private static Image createImage(String id) {
108                 Image image = mock(Image.class);
109                 when(image.getId()).thenReturn(id);
110                 return image;
111         }
112
113 }