f6275fc442fd87aabb62e1217a534aea4a64535f
[Sone.git] / src / test / java / net / pterodactylus / sone / template / ParserFilterTest.java
1 package net.pterodactylus.sone.template;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.containsInAnyOrder;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.ArgumentMatchers.any;
7 import static org.mockito.ArgumentMatchers.anyString;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.when;
10
11 import java.util.Collections;
12
13 import net.pterodactylus.sone.core.Core;
14 import net.pterodactylus.sone.data.Profile;
15 import net.pterodactylus.sone.data.Sone;
16 import net.pterodactylus.sone.text.FreemailPart;
17 import net.pterodactylus.sone.text.Part;
18 import net.pterodactylus.sone.text.SoneTextParser;
19 import net.pterodactylus.sone.text.SoneTextParserContext;
20 import net.pterodactylus.util.template.HtmlFilter;
21 import net.pterodactylus.util.template.TemplateContext;
22 import net.pterodactylus.util.template.TemplateContextFactory;
23
24 import com.google.common.base.Optional;
25 import org.jsoup.Jsoup;
26 import org.jsoup.nodes.Attribute;
27 import org.jsoup.nodes.Document;
28 import org.jsoup.nodes.Element;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 /**
33  * Unit test for {@link ParserFilter}.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class ParserFilterTest {
38
39         private static final String FREEMAIL_ID = "t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra";
40         private static final String SONE_FREEMAIL = "sone@" + FREEMAIL_ID + ".freemail";
41         private static final String SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI";
42
43         private final Core core = mock(Core.class);
44         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
45         private final SoneTextParser soneTextParser = mock(SoneTextParser.class);
46         private final ParserFilter filter = new ParserFilter(core, templateContextFactory, soneTextParser);
47
48         @Before
49         public void setupTemplateContextFactory() {
50                 templateContextFactory.addFilter("html", new HtmlFilter());
51         }
52
53         @Test
54         public void freemailAddressIsDisplayedCorrectly() {
55                 Sone sone = mock(Sone.class);
56                 when(sone.getProfile()).thenReturn(new Profile(sone));
57                 sone.getProfile().setFirstName("Sone");
58                 when(core.getSone(SONE_IDENTITY)).thenReturn(Optional.of(sone));
59                 when(soneTextParser.parse(anyString(), any(SoneTextParserContext.class))).thenReturn(Collections.<Part>singletonList(new FreemailPart("sone", FREEMAIL_ID, SONE_IDENTITY)));
60                 TemplateContext templateContext = templateContextFactory.createTemplateContext();
61                 String output = String.valueOf(filter.format(templateContext, SONE_FREEMAIL, Collections.<String, Object>emptyMap()));
62                 Document document = Jsoup.parseBodyFragment(output);
63                 Element linkNode = document.body().child(0);
64                 assertThat(linkNode.nodeName(), is("a"));
65                 assertThat(linkNode.attributes().asList(), containsInAnyOrder(
66                                 new Attribute("href", "/Freemail/NewMessage?to=" + SONE_IDENTITY),
67                                 new Attribute("class", "in-sone"),
68                                 new Attribute("title", "Sone\n" + SONE_FREEMAIL)
69                 ));
70                 assertThat(linkNode.text(), is("sone@Sone.freemail"));
71         }
72
73 }