Use internal image IDs for temporary images
[Sone.git] / src / test / java / net / pterodactylus / sone / template / ImageLinkFilterTest.java
1 package net.pterodactylus.sone.template;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.containsString;
5 import static org.hamcrest.Matchers.is;
6 import static org.hamcrest.Matchers.nullValue;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
9
10 import net.pterodactylus.sone.core.Core;
11 import net.pterodactylus.sone.data.Image;
12 import net.pterodactylus.util.template.HtmlFilter;
13 import net.pterodactylus.util.template.TemplateContext;
14 import net.pterodactylus.util.template.TemplateContextFactory;
15
16 import com.google.common.collect.ImmutableMap;
17 import org.hamcrest.Matchers;
18 import org.jsoup.Jsoup;
19 import org.jsoup.nodes.Document;
20 import org.jsoup.nodes.Element;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 /**
25  * Unit test for {@link ImageLinkFilterTest}.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class ImageLinkFilterTest {
30
31         private final Core core = mock(Core.class);
32         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
33         private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(core, templateContextFactory);
34         private final TemplateContext templateContext = null;
35         private final Image image = mock(Image.class);
36
37         @Before
38         public void setupTemplateContextFactory() {
39                 templateContextFactory.addFilter("html", new HtmlFilter());
40         }
41
42         @Before
43         public void setupCore() {
44                 when(core.getImage("image-id", false)).thenReturn(image);
45         }
46
47         @Before
48         public void setupImage() {
49                 when(image.getId()).thenReturn("image-id");
50                 when(image.getInternalId()).thenReturn("internal-image-id");
51                 when(image.getKey()).thenReturn("image-key");
52                 when(image.isInserted()).thenReturn(true);
53                 when(image.getWidth()).thenReturn(640);
54                 when(image.getHeight()).thenReturn(270);
55                 when(image.getTitle()).thenReturn("image title");
56                 when(image.getDescription()).thenReturn("image description");
57         }
58
59         @Test
60         public void imageLinkIsGeneratedCorrectlyForNotInsertedImages() {
61                 when(image.isInserted()).thenReturn(false);
62                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
63                 Element imageElement = getSingleElement(result);
64                 assertThat(imageElement.attr("class"), is(""));
65                 assertThat(imageElement.attr("src"), is("getImage.html?image=internal-image-id"));
66                 assertThat(imageElement.attr("title"), is("image title"));
67                 assertThat(imageElement.attr("alt"), is("image description"));
68                 assertThat(imageElement.attr("width"), is("640"));
69                 assertThat(imageElement.attr("height"), is("270"));
70         }
71
72         @Test
73         public void imageLinkIsGeneratedCorrectlyForInsertedImages() {
74                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
75                 Element imageElement = getSingleElement(result);
76                 assertThat(imageElement.attr("class"), is(""));
77                 assertThat(imageElement.attr("src"), is("/image-key?forcedownload=true"));
78                 assertThat(imageElement.attr("title"), is("image title"));
79                 assertThat(imageElement.attr("alt"), is("image description"));
80                 assertThat(imageElement.attr("width"), is("640"));
81                 assertThat(imageElement.attr("height"), is("270"));
82         }
83
84         @Test
85         public void imageTitleAndDescriptionAreOverriddenCorrectly() {
86                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("title", "Test Title")));
87                 Element imageElement = getSingleElement(result);
88                 assertThat(imageElement.attr("title"), is("Test Title"));
89                 assertThat(imageElement.attr("alt"), is("Test Title"));
90         }
91
92         @Test
93         public void imageIsScaledByWidthCorrectly() {
94                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-width", "320")));
95                 Element imageElement = getSingleElement(result);
96                 assertThat(imageElement.attr("width"), is("320"));
97                 assertThat(imageElement.attr("height"), is("135"));
98         }
99
100         @Test
101         public void imageIsScaledByHeightCorrectly() {
102                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-height", "135")));
103                 Element imageElement = getSingleElement(result);
104                 assertThat(imageElement.attr("width"), is("320"));
105                 assertThat(imageElement.attr("height"), is("135"));
106         }
107
108         @Test
109         public void wideImageIsEnlargedCorrectly() {
110                 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
111                                 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
112                 Element imageElement = getSingleElement(result);
113                 assertThat(imageElement.attr("width"), is("237"));
114                 assertThat(imageElement.attr("height"), is("100"));
115                 assertThat(imageElement.attr("style"), containsString("left: -68px"));
116                 assertThat(imageElement.attr("style"), containsString("top: 0px"));
117         }
118
119         @Test
120         public void highImageIsEnlargedCorrectly() {
121                 when(image.getWidth()).thenReturn(270);
122                 when(image.getHeight()).thenReturn(640);
123                 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
124                                 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
125                 Element imageElement = getSingleElement(result);
126                 assertThat(imageElement.attr("width"), is("100"));
127                 assertThat(imageElement.attr("height"), is("237"));
128                 assertThat(imageElement.attr("style"), containsString("left: 0px"));
129                 assertThat(imageElement.attr("style"), containsString("top: -68px"));
130         }
131
132         @Test
133         public void nullImageIsReturnedAsNull() {
134                 assertThat(imageLinkFilter.format(templateContext, null, null), nullValue());
135         }
136
137         @Test
138         public void stringIsUsedToLoadImageFromCore() {
139                 String result = String.valueOf(imageLinkFilter.format(templateContext, "image-id", ImmutableMap.<String, Object>of()));
140                 Element imageElement = getSingleElement(result);
141                 assertThat(imageElement.attr("class"), is(""));
142                 assertThat(imageElement.attr("src"), is("/image-key?forcedownload=true"));
143                 assertThat(imageElement.attr("title"), is("image title"));
144                 assertThat(imageElement.attr("alt"), is("image description"));
145                 assertThat(imageElement.attr("width"), is("640"));
146                 assertThat(imageElement.attr("height"), is("270"));
147         }
148
149         private Element getSingleElement(String result) {
150                 Document document = Jsoup.parseBodyFragment(result);
151                 assertThatBodyHasASingleElement(document);
152                 return getSingleElement(document);
153         }
154
155         private void assertThatBodyHasASingleElement(Document document) {
156                 assertThat(document.body().select("> *"), Matchers.hasSize(1));
157         }
158
159         private Element getSingleElement(Document document) {
160                 return document.body().select("> *").get(0);
161         }
162
163 }