♻️ Create custom context factory in image link filter
[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
13 import com.google.common.collect.ImmutableMap;
14 import org.hamcrest.Matchers;
15 import org.jsoup.Jsoup;
16 import org.jsoup.nodes.Document;
17 import org.jsoup.nodes.Element;
18 import org.junit.Before;
19 import org.junit.Test;
20
21 /**
22  * Unit test for {@link ImageLinkFilterTest}.
23  */
24 public class ImageLinkFilterTest {
25
26         private final Core core = mock(Core.class);
27         private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(core);
28         private final Image image = mock(Image.class);
29
30         @Before
31         public void setupCore() {
32                 when(core.getImage("image-id", false)).thenReturn(image);
33         }
34
35         @Before
36         public void setupImage() {
37                 when(image.getId()).thenReturn("image-id");
38                 when(image.getKey()).thenReturn("image-key");
39                 when(image.isInserted()).thenReturn(true);
40                 when(image.getWidth()).thenReturn(640);
41                 when(image.getHeight()).thenReturn(270);
42                 when(image.getTitle()).thenReturn("image title");
43                 when(image.getDescription()).thenReturn("image description");
44         }
45
46         @Test
47         public void imageLinkIsGeneratedCorrectlyForNotInsertedImages() {
48                 when(image.isInserted()).thenReturn(false);
49                 String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of()));
50                 Element imageElement = getSingleElement(result);
51                 assertThat(imageElement.attr("class"), is(""));
52                 assertThat(imageElement.attr("src"), is("getImage.html?image=image-id"));
53                 assertThat(imageElement.attr("title"), is("image title"));
54                 assertThat(imageElement.attr("alt"), is("image description"));
55                 assertThat(imageElement.attr("width"), is("640"));
56                 assertThat(imageElement.attr("height"), is("270"));
57         }
58
59         @Test
60         public void imageLinkIsGeneratedCorrectlyForInsertedImages() {
61                 String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of()));
62                 Element imageElement = getSingleElement(result);
63                 assertThat(imageElement.attr("class"), is(""));
64                 assertThat(imageElement.attr("src"), is("/image-key"));
65                 assertThat(imageElement.attr("title"), is("image title"));
66                 assertThat(imageElement.attr("alt"), is("image description"));
67                 assertThat(imageElement.attr("width"), is("640"));
68                 assertThat(imageElement.attr("height"), is("270"));
69         }
70
71         @Test
72         public void imageTitleAndDescriptionAreOverriddenCorrectly() {
73                 String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("title", "Test Title")));
74                 Element imageElement = getSingleElement(result);
75                 assertThat(imageElement.attr("title"), is("Test Title"));
76                 assertThat(imageElement.attr("alt"), is("Test Title"));
77         }
78
79         @Test
80         public void imageIsScaledByWidthCorrectly() {
81                 String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("max-width", "320")));
82                 Element imageElement = getSingleElement(result);
83                 assertThat(imageElement.attr("width"), is("320"));
84                 assertThat(imageElement.attr("height"), is("135"));
85         }
86
87         @Test
88         public void imageIsScaledByHeightCorrectly() {
89                 String result = String.valueOf(imageLinkFilter.format(null, image, ImmutableMap.<String, Object>of("max-height", "135")));
90                 Element imageElement = getSingleElement(result);
91                 assertThat(imageElement.attr("width"), is("320"));
92                 assertThat(imageElement.attr("height"), is("135"));
93         }
94
95         @Test
96         public void wideImageIsEnlargedCorrectly() {
97                 String result = String.valueOf(imageLinkFilter.format(null, image,
98                                 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
99                 Element imageElement = getSingleElement(result);
100                 assertThat(imageElement.attr("width"), is("237"));
101                 assertThat(imageElement.attr("height"), is("100"));
102                 assertThat(imageElement.attr("style"), containsString("left: -68px"));
103                 assertThat(imageElement.attr("style"), containsString("top: 0px"));
104         }
105
106         @Test
107         public void highImageIsEnlargedCorrectly() {
108                 when(image.getWidth()).thenReturn(270);
109                 when(image.getHeight()).thenReturn(640);
110                 String result = String.valueOf(imageLinkFilter.format(null, 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("100"));
114                 assertThat(imageElement.attr("height"), is("237"));
115                 assertThat(imageElement.attr("style"), containsString("left: 0px"));
116                 assertThat(imageElement.attr("style"), containsString("top: -68px"));
117         }
118
119         @Test
120         public void nullImageIsReturnedAsNull() {
121                 assertThat(imageLinkFilter.format(null, null, null), nullValue());
122         }
123
124         @Test
125         public void stringIsUsedToLoadImageFromCore() {
126                 String result = String.valueOf(imageLinkFilter.format(null, "image-id", ImmutableMap.<String, Object>of()));
127                 Element imageElement = getSingleElement(result);
128                 assertThat(imageElement.attr("class"), is(""));
129                 assertThat(imageElement.attr("src"), is("/image-key"));
130                 assertThat(imageElement.attr("title"), is("image title"));
131                 assertThat(imageElement.attr("alt"), is("image description"));
132                 assertThat(imageElement.attr("width"), is("640"));
133                 assertThat(imageElement.attr("height"), is("270"));
134         }
135
136         private Element getSingleElement(String result) {
137                 Document document = Jsoup.parseBodyFragment(result);
138                 assertThatBodyHasASingleElement(document);
139                 return getSingleElement(document);
140         }
141
142         private void assertThatBodyHasASingleElement(Document document) {
143                 assertThat(document.body().select("> *"), Matchers.hasSize(1));
144         }
145
146         private Element getSingleElement(Document document) {
147                 return document.body().select("> *").get(0);
148         }
149
150 }