Remove @author tags
[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 public class ImageLinkFilterTest {
28
29         private final Core core = mock(Core.class);
30         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
31         private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(core, templateContextFactory);
32         private final TemplateContext templateContext = null;
33         private final Image image = mock(Image.class);
34
35         @Before
36         public void setupTemplateContextFactory() {
37                 templateContextFactory.addFilter("html", new HtmlFilter());
38         }
39
40         @Before
41         public void setupCore() {
42                 when(core.getImage("image-id", false)).thenReturn(image);
43         }
44
45         @Before
46         public void setupImage() {
47                 when(image.getId()).thenReturn("image-id");
48                 when(image.getKey()).thenReturn("image-key");
49                 when(image.isInserted()).thenReturn(true);
50                 when(image.getWidth()).thenReturn(640);
51                 when(image.getHeight()).thenReturn(270);
52                 when(image.getTitle()).thenReturn("image title");
53                 when(image.getDescription()).thenReturn("image description");
54         }
55
56         @Test
57         public void imageLinkIsGeneratedCorrectlyForNotInsertedImages() {
58                 when(image.isInserted()).thenReturn(false);
59                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
60                 Element imageElement = getSingleElement(result);
61                 assertThat(imageElement.attr("class"), is(""));
62                 assertThat(imageElement.attr("src"), is("getImage.html?image=image-id"));
63                 assertThat(imageElement.attr("title"), is("image title"));
64                 assertThat(imageElement.attr("alt"), is("image description"));
65                 assertThat(imageElement.attr("width"), is("640"));
66                 assertThat(imageElement.attr("height"), is("270"));
67         }
68
69         @Test
70         public void imageLinkIsGeneratedCorrectlyForInsertedImages() {
71                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
72                 Element imageElement = getSingleElement(result);
73                 assertThat(imageElement.attr("class"), is(""));
74                 assertThat(imageElement.attr("src"), is("/image-key"));
75                 assertThat(imageElement.attr("title"), is("image title"));
76                 assertThat(imageElement.attr("alt"), is("image description"));
77                 assertThat(imageElement.attr("width"), is("640"));
78                 assertThat(imageElement.attr("height"), is("270"));
79         }
80
81         @Test
82         public void imageTitleAndDescriptionAreOverriddenCorrectly() {
83                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("title", "Test Title")));
84                 Element imageElement = getSingleElement(result);
85                 assertThat(imageElement.attr("title"), is("Test Title"));
86                 assertThat(imageElement.attr("alt"), is("Test Title"));
87         }
88
89         @Test
90         public void imageIsScaledByWidthCorrectly() {
91                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-width", "320")));
92                 Element imageElement = getSingleElement(result);
93                 assertThat(imageElement.attr("width"), is("320"));
94                 assertThat(imageElement.attr("height"), is("135"));
95         }
96
97         @Test
98         public void imageIsScaledByHeightCorrectly() {
99                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-height", "135")));
100                 Element imageElement = getSingleElement(result);
101                 assertThat(imageElement.attr("width"), is("320"));
102                 assertThat(imageElement.attr("height"), is("135"));
103         }
104
105         @Test
106         public void wideImageIsEnlargedCorrectly() {
107                 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
108                                 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
109                 Element imageElement = getSingleElement(result);
110                 assertThat(imageElement.attr("width"), is("237"));
111                 assertThat(imageElement.attr("height"), is("100"));
112                 assertThat(imageElement.attr("style"), containsString("left: -68px"));
113                 assertThat(imageElement.attr("style"), containsString("top: 0px"));
114         }
115
116         @Test
117         public void highImageIsEnlargedCorrectly() {
118                 when(image.getWidth()).thenReturn(270);
119                 when(image.getHeight()).thenReturn(640);
120                 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
121                                 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
122                 Element imageElement = getSingleElement(result);
123                 assertThat(imageElement.attr("width"), is("100"));
124                 assertThat(imageElement.attr("height"), is("237"));
125                 assertThat(imageElement.attr("style"), containsString("left: 0px"));
126                 assertThat(imageElement.attr("style"), containsString("top: -68px"));
127         }
128
129         @Test
130         public void nullImageIsReturnedAsNull() {
131                 assertThat(imageLinkFilter.format(templateContext, null, null), nullValue());
132         }
133
134         @Test
135         public void stringIsUsedToLoadImageFromCore() {
136                 String result = String.valueOf(imageLinkFilter.format(templateContext, "image-id", ImmutableMap.<String, Object>of()));
137                 Element imageElement = getSingleElement(result);
138                 assertThat(imageElement.attr("class"), is(""));
139                 assertThat(imageElement.attr("src"), is("/image-key"));
140                 assertThat(imageElement.attr("title"), is("image title"));
141                 assertThat(imageElement.attr("alt"), is("image description"));
142                 assertThat(imageElement.attr("width"), is("640"));
143                 assertThat(imageElement.attr("height"), is("270"));
144         }
145
146         private Element getSingleElement(String result) {
147                 Document document = Jsoup.parseBodyFragment(result);
148                 assertThatBodyHasASingleElement(document);
149                 return getSingleElement(document);
150         }
151
152         private void assertThatBodyHasASingleElement(Document document) {
153                 assertThat(document.body().select("> *"), Matchers.hasSize(1));
154         }
155
156         private Element getSingleElement(Document document) {
157                 return document.body().select("> *").get(0);
158         }
159
160 }