1 package net.pterodactylus.sone.template;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.empty;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.verify;
9 import static org.mockito.Mockito.when;
11 import java.io.IOException;
12 import java.io.StringReader;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
18 import net.pterodactylus.sone.core.Core;
19 import net.pterodactylus.sone.data.Sone;
20 import net.pterodactylus.sone.text.Part;
21 import net.pterodactylus.sone.text.PlainTextPart;
22 import net.pterodactylus.sone.text.SoneTextParser;
23 import net.pterodactylus.sone.text.SoneTextParserContext;
24 import net.pterodactylus.util.template.TemplateContext;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.ImmutableMap;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
32 * Unit test for {@link ParserFilter}.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class ParserFilterTest {
38 private final Core core = mock(Core.class);
39 private final SoneTextParser soneTextParser = mock(SoneTextParser.class);
40 private final ParserFilter parserFilter = new ParserFilter(core, soneTextParser);
41 private final TemplateContext templateContext = new TemplateContext();
42 private final Sone sone = mock(Sone.class);
45 public void filterReturnsPartsReturnedByParser() throws IOException {
46 List<Part> parts = setupSoneTextParser();
47 assertThat(parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), is((Object) parts));
50 private List<Part> setupSoneTextParser() throws IOException {
51 List<Part> parts = Arrays.<Part>asList(new PlainTextPart("Text"));
52 when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenReturn(parts);
57 public void filterUsesGivenSone() throws IOException {
58 List<Part> parts = setupSoneTextParser();
59 assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", sone)), is((Object) parts));
60 verifyThatContextContainsCorrectSone();
64 public void filterGetsCorrectSoneFromCore() throws IOException {
65 when(core.getSone("sone-id")).thenReturn(Optional.of(sone));
66 List<Part> parts = setupSoneTextParser();
67 assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", "sone-id")), is((Object) parts));
68 verifyThatContextContainsCorrectSone();
71 private void verifyThatContextContainsCorrectSone() throws IOException {
72 ArgumentCaptor<SoneTextParserContext> contextArgumentCaptor = ArgumentCaptor.forClass(SoneTextParserContext.class);
73 verify(soneTextParser).parse(contextArgumentCaptor.capture(), any(StringReader.class));
74 assertThat(contextArgumentCaptor.getValue().getPostingSone(), is(sone));
78 public void filterReturnsEmptyCollectionOnExceptionInParser() throws IOException {
79 when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenThrow(IOException.class);
80 assertThat((Collection<Part>) parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), empty());