From 9bc9aa0ee4737d2f2ddcdaf8e404e1db31a34af3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 29 Oct 2016 10:21:32 +0200 Subject: [PATCH] Render freemail links as links --- .../pterodactylus/sone/template/ParserFilter.java | 24 +++++++ .../sone/template/ParserFilterTest.java | 73 ++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index 7cf8c70..c12b36d 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -29,8 +29,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import javax.annotation.Nonnull; + import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.text.FreemailPart; import net.pterodactylus.sone.text.FreenetLinkPart; import net.pterodactylus.sone.text.LinkPart; import net.pterodactylus.sone.text.Part; @@ -45,6 +48,9 @@ import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateContextFactory; import net.pterodactylus.util.template.TemplateParser; +import com.google.common.base.Function; +import com.google.common.base.Optional; + /** * Filter that filters a given text through a {@link SoneTextParser}. * @@ -170,6 +176,8 @@ public class ParserFilter implements Filter { render(writer, (SonePart) part); } else if (part instanceof PostPart) { render(writer, (PostPart) part); + } else if (part instanceof FreemailPart) { + render(writer, (FreemailPart) part); } else if (part instanceof Iterable) { render(writer, (Iterable) part); } @@ -263,6 +271,22 @@ public class ParserFilter implements Filter { renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone"); } + private void render(@Nonnull Writer writer, @Nonnull FreemailPart freemailPart) { + Optional sone = core.getSone(freemailPart.getIdentityId()); + String soneName = sone.transform(new Function() { + @Nonnull + @Override + public String apply(Sone input) { + return SoneAccessor.getNiceName(input); + } + }).or(freemailPart.getIdentityId()); + renderLink(writer, + "/Freemail/NewMessage?to=" + freemailPart.getIdentityId(), + String.format("%s@%s.freemail", freemailPart.getEmailLocalPart(), soneName), + String.format("%s", soneName), + "in-sone"); + } + /** * Renders the given link. * diff --git a/src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java b/src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java new file mode 100644 index 0000000..a689ab8 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java @@ -0,0 +1,73 @@ +package net.pterodactylus.sone.template; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.text.FreemailPart; +import net.pterodactylus.sone.text.Part; +import net.pterodactylus.sone.text.SoneTextParser; +import net.pterodactylus.sone.text.SoneTextParserContext; +import net.pterodactylus.util.template.HtmlFilter; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateContextFactory; + +import com.google.common.base.Optional; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Attribute; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link ParserFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class ParserFilterTest { + + private static final String FREEMAIL_ID = "t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra"; + private static final String SONE_FREEMAIL = "sone@" + FREEMAIL_ID + ".freemail"; + private static final String SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"; + + private final Core core = mock(Core.class); + private final TemplateContextFactory templateContextFactory = new TemplateContextFactory(); + private final SoneTextParser soneTextParser = mock(SoneTextParser.class); + private final ParserFilter filter = new ParserFilter(core, templateContextFactory, soneTextParser); + + @Before + public void setupTemplateContextFactory() { + templateContextFactory.addFilter("html", new HtmlFilter()); + } + + @Test + public void freemailAddressIsDisplayedCorrectly() { + Sone sone = mock(Sone.class); + when(sone.getProfile()).thenReturn(new Profile(sone)); + sone.getProfile().setFirstName("Sone"); + when(core.getSone(SONE_IDENTITY)).thenReturn(Optional.of(sone)); + when(soneTextParser.parse(anyString(), any(SoneTextParserContext.class))).thenReturn(Collections.singletonList(new FreemailPart("sone", FREEMAIL_ID, SONE_IDENTITY))); + TemplateContext templateContext = templateContextFactory.createTemplateContext(); + String output = String.valueOf(filter.format(templateContext, SONE_FREEMAIL, Collections.emptyMap())); + Document document = Jsoup.parseBodyFragment(output); + Element linkNode = document.body().child(0); + assertThat(linkNode.nodeName(), is("a")); + assertThat(linkNode.attributes().asList(), containsInAnyOrder( + new Attribute("href", "/Freemail/NewMessage?to=" + SONE_IDENTITY), + new Attribute("class", "in-sone"), + new Attribute("title", "Sone") + )); + assertThat(linkNode.text(), is("sone@Sone.freemail")); + } + +} -- 2.7.4