Split text parsing and rendering into two filters
[Sone.git] / src / test / java / net / pterodactylus / sone / template / RenderFilterTest.java
1 package net.pterodactylus.sone.template;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.Map;
12
13 import net.pterodactylus.sone.core.Core;
14 import net.pterodactylus.sone.data.Album;
15 import net.pterodactylus.sone.data.Post;
16 import net.pterodactylus.sone.data.Profile;
17 import net.pterodactylus.sone.data.Sone;
18 import net.pterodactylus.sone.text.AlbumPart;
19 import net.pterodactylus.sone.text.FreenetLinkPart;
20 import net.pterodactylus.sone.text.LinkPart;
21 import net.pterodactylus.sone.text.Part;
22 import net.pterodactylus.sone.text.PlainTextPart;
23 import net.pterodactylus.sone.text.PostPart;
24 import net.pterodactylus.sone.text.SonePart;
25 import net.pterodactylus.util.template.HtmlFilter;
26 import net.pterodactylus.util.template.TemplateContext;
27 import net.pterodactylus.util.template.TemplateContextFactory;
28
29 import com.google.common.collect.ImmutableMap;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 /**
34  * Unit test for {@link RenderFilter}.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class RenderFilterTest {
39
40         private static final Map<String, Object> EMPTY_MAP = Collections.emptyMap();
41         private final Core core = mock(Core.class);
42         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
43         private final RenderFilter renderFilter = new RenderFilter(core, templateContextFactory);
44         private final TemplateContext templateContext = new TemplateContext();
45         private final Sone sone = mock(Sone.class);
46         private final Post post = mock(Post.class);
47
48         @Before
49         public void setupTemplateContextFactory() {
50                 templateContextFactory.addFilter("html", new HtmlFilter());
51         }
52
53         @Before
54         public void setupSone() {
55                 when(sone.getId()).thenReturn("sone-id");
56                 when(sone.getName()).thenReturn("SoneName");
57                 when(sone.getProfile()).thenReturn(new Profile(sone));
58         }
59
60         @Before
61         public void setupPost() {
62                 when(post.getId()).thenReturn("post-id");
63                 when(post.getSone()).thenReturn(sone);
64         }
65
66         @Test
67         public void filterCanRenderPlainText() {
68                 List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"));
69                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "&lt;Text&gt;"));
70         }
71
72         @Test
73         public void filterCanRenderMultiplePlainTextParts() {
74                 List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"), new PlainTextPart("<Foo>"));
75                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "&lt;Text&gt;&lt;Foo&gt;"));
76         }
77
78         @Test
79         public void filterCanRenderUntrustedFreenetLinks() {
80                 List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", false));
81                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
82                                 is((Object) "<a class=\"freenet\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
83         }
84
85         @Test
86         public void filterCanRenderTrustedFreenetLinks() {
87                 List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", true));
88                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
89                                 is((Object) "<a class=\"freenet-trusted\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
90         }
91
92         @Test
93         public void filterCanRenderInternetLinks() {
94                 List<Part> parts = Arrays.<Part>asList(new LinkPart("http://link.sone", "link.sone"));
95                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
96                                 is((Object) "<a class=\"internet\" href=\"/external-link/?_CHECKED_HTTP_=http%3A%2F%2Flink.sone\" title=\"link.sone\">link.sone</a>"));
97         }
98
99         @Test
100         public void filterCanRenderSonePartsForUnknownSones() {
101                 Sone unknownSone = mock(Sone.class);
102                 when(unknownSone.getId()).thenReturn("sone-id");
103                 List<Part> parts = Arrays.<Part>asList(new SonePart(unknownSone));
104                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
105                                 is((Object) "<a class=\"in-sone\" href=\"/WebOfTrust/ShowIdentity?id=sone-id\" title=\"sone-id\">sone-id</a>"));
106         }
107
108         @Test
109         public void filterCanRenderSonePartsForKnownSones() {
110                 List<Part> parts = Arrays.<Part>asList(new SonePart(sone));
111                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
112                                 is((Object) "<a class=\"in-sone\" href=\"viewSone.html?sone=sone-id\" title=\"SoneName\">SoneName</a>"));
113         }
114
115         @Test
116         public void filterCanRenderPostParts() {
117                 when(post.getText()).thenReturn("123456789012345678901234567890");
118                 List<Part> parts = Arrays.<Part>asList(new PostPart(post));
119                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
120                                 is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345678901234567890&hellip;</a>"));
121         }
122
123         @Test
124         public void filterCanRenderPostPartsWithoutBreakingWords() {
125                 when(post.getText()).thenReturn("12345 12345 12345 12345 12345 12345");
126                 List<Part> parts = Arrays.<Part>asList(new PostPart(post));
127                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
128                                 is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345 12345&hellip;</a>"));
129         }
130
131         @Test
132         public void filterCanRenderPostPartsWithShortText() {
133                 when(post.getText()).thenReturn("12345 12345");
134                 List<Part> parts = Arrays.<Part>asList(new PostPart(post));
135                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
136                                 is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
137         }
138
139         @Test
140         public void filterCanRenderPostPartsWithOldPostIds() {
141                 when(post.getText()).thenReturn("12345 12345");
142                 List<Part> parts = Arrays.<Part>asList(new PostPart(post, true));
143                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
144                                 is((Object) "<a class=\"internet\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
145         }
146
147         @Test
148         public void filterCanRenderAlbumParts() {
149                 Album album = mock(Album.class);
150                 when(album.getId()).thenReturn("album-id");
151                 when(album.getTitle()).thenReturn("Title");
152                 when(album.getDescription()).thenReturn("Description");
153                 List<Part> parts = Arrays.<Part>asList(new AlbumPart(album));
154                 assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
155                                 is((Object) "<a class=\"in-sone\" href=\"imageBrowser.html?album=album-id\" title=\"Description\">Title</a>"));
156         }
157
158         @Test
159         public void filterHonorsLength() {
160                 List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
161                 assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10")),
162                                 is((Object) "1234567890&hellip;"));
163         }
164
165         @Test
166         public void filterHonorsCutOffLength() {
167                 List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
168                 assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10", "cut-off-length", "5")),
169                                 is((Object) "12345&hellip;"));
170         }
171
172 }