Split text parsing and rendering into two filters
[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.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;
10
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;
17
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;
25
26 import com.google.common.base.Optional;
27 import com.google.common.collect.ImmutableMap;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
30
31 /**
32  * Unit test for {@link ParserFilter}.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class ParserFilterTest {
37
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);
43
44         @Test
45         public void filterReturnsPartsReturnedByParser() throws IOException {
46                 List<Part> parts = setupSoneTextParser();
47                 assertThat(parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), is((Object) parts));
48         }
49
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);
53                 return parts;
54         }
55
56         @Test
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();
61         }
62
63         @Test
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();
69         }
70
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));
75         }
76
77         @Test
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());
81         }
82
83 }