Add unit test for ImageLinkFilter.
[Sone.git] / src / test / java / net / pterodactylus / sone / template / ImageLinkFilterTest.java
1 /*
2  * Sone - ImageLinkFilterTest.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 com.google.common.base.Optional.of;
21 import static net.pterodactylus.util.template.TemplateContextFactory.getInstance;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.is;
24 import static org.hamcrest.Matchers.nullValue;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Collections;
30
31 import net.pterodactylus.sone.data.Image;
32 import net.pterodactylus.sone.data.Mocks;
33 import net.pterodactylus.util.template.HtmlFilter;
34 import net.pterodactylus.util.template.TemplateContextFactory;
35
36 import com.google.common.collect.ImmutableMap;
37 import org.junit.Before;
38 import org.junit.Test;
39
40 /**
41  * Unit test for {@link ImageLinkFilter}.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class ImageLinkFilterTest {
46
47         private final Mocks mocks = new Mocks();
48         private final TemplateContextFactory templateContextFactory = getInstance();
49         private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(mocks.core, templateContextFactory);
50
51         @Before
52         public void setup() {
53                 templateContextFactory.addFilter("css", new CssClassNameFilter());
54                 templateContextFactory.addFilter("html", new HtmlFilter());
55         }
56
57         @Test
58         public void nullImageReturnsANullLink() {
59                 assertThat(imageLinkFilter.format(null, null, Collections.<String, Object>emptyMap()), nullValue());
60         }
61
62         @Test
63         public void linkForAnImageWithoutAdditionalParametersIsGenerated() {
64                 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
65                 String link = (String) imageLinkFilter.format(null, image, Collections.<String, Object>emptyMap());
66                 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
67         }
68
69         @Test
70         public void overridingTheTitleChangesBothAltAndTitleAttributes() {
71                 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
72                 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("title", "Other Title"));
73                 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Other Title\" title=\"Other Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
74         }
75
76         @Test
77         public void classAttributeIsFilteredAndSet() {
78                 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
79                 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("class", "some class"));
80                 assertThat(link, is("<img class=\"some_class\" src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
81         }
82
83         @Test
84         public void resizingImageToHalfItsSize() {
85                 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
86                 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("max-width", "320", "max-height", "320"));
87                 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"320\" height=\"240\" style=\"position: relative;\"/>"));
88         }
89
90         @Test
91         public void enlargingAnImage() {
92                 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
93                 String link = (String) imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "960", "max-height", "960"));
94                 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"1280\" height=\"960\" style=\"position: relative;top: 0px;left: -160px;\"/>"));
95         }
96
97         @Test
98         public void notInsertedImage() {
99                 Image image = createNotInsertedImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
100                 String link = (String) imageLinkFilter.format(null, image, Collections.<String, Object>emptyMap());
101                 assertThat(link, is("<img src=\"getImage.html?image=ImageId\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
102         }
103
104         @Test
105         public void creatingALinkFromAnImageId() {
106                 Image image = createImage("ImageId", "KSK@foo.png", "Title", "Description", 640, 480);
107                 when(mocks.core.getImage(eq("ImageId"))).thenReturn(of(image));
108                 String link = (String) imageLinkFilter.format(null, image.getId(), Collections.<String, Object>emptyMap());
109                 assertThat(link, is("<img src=\"/KSK@foo.png?forcedownload=true\" alt=\"Description\" title=\"Title\" width=\"640\" height=\"480\" style=\"position: relative;\"/>"));
110         }
111
112         private Image createNotInsertedImage(String id, String key, String title, String description, int width, int height) {
113                 Image image = createBasicImage(id, key, title, description, width, height);
114                 when(image.isInserted()).thenReturn(false);
115                 return image;
116         }
117
118         private Image createImage(String id, String key, String title, String description, int width, int height) {
119                 Image image = createBasicImage(id, key, title, description, width, height);
120                 when(image.isInserted()).thenReturn(true);
121                 return image;
122         }
123
124         private Image createBasicImage(String id, String key, String title, String description, int width, int height) {
125                 Image image = mock(Image.class);
126                 when(image.getId()).thenReturn(id);
127                 when(image.getKey()).thenReturn(key);
128                 when(image.getTitle()).thenReturn(title);
129                 when(image.getDescription()).thenReturn(description);
130                 when(image.getWidth()).thenReturn(width);
131                 when(image.getHeight()).thenReturn(height);
132                 return image;
133         }
134
135 }