--- /dev/null
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+/**
+ * Unit test for {@link ParserFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ParserFilterTest {
+
+ private final Core core = mock(Core.class);
+ private final SoneTextParser soneTextParser = mock(SoneTextParser.class);
+ private final ParserFilter parserFilter = new ParserFilter(core, soneTextParser);
+ private final TemplateContext templateContext = new TemplateContext();
+ private final Sone sone = mock(Sone.class);
+
+ @Test
+ public void filterReturnsPartsReturnedByParser() throws IOException {
+ List<Part> parts = setupSoneTextParser();
+ assertThat(parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), is((Object) parts));
+ }
+
+ private List<Part> setupSoneTextParser() throws IOException {
+ List<Part> parts = Arrays.<Part>asList(new PlainTextPart("Text"));
+ when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenReturn(parts);
+ return parts;
+ }
+
+ @Test
+ public void filterUsesGivenSone() throws IOException {
+ List<Part> parts = setupSoneTextParser();
+ assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", sone)), is((Object) parts));
+ verifyThatContextContainsCorrectSone();
+ }
+
+ @Test
+ public void filterGetsCorrectSoneFromCore() throws IOException {
+ when(core.getSone("sone-id")).thenReturn(Optional.of(sone));
+ List<Part> parts = setupSoneTextParser();
+ assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", "sone-id")), is((Object) parts));
+ verifyThatContextContainsCorrectSone();
+ }
+
+ private void verifyThatContextContainsCorrectSone() throws IOException {
+ ArgumentCaptor<SoneTextParserContext> contextArgumentCaptor = ArgumentCaptor.forClass(SoneTextParserContext.class);
+ verify(soneTextParser).parse(contextArgumentCaptor.capture(), any(StringReader.class));
+ assertThat(contextArgumentCaptor.getValue().getPostingSone(), is(sone));
+ }
+
+ @Test
+ public void filterReturnsEmptyCollectionOnExceptionInParser() throws IOException {
+ when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenThrow(IOException.class);
+ assertThat((Collection<Part>) parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), empty());
+ }
+
+}