1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.macro.rss

File RssMacroTest.java

 

Code metrics

0
72
4
1
230
159
4
0.06
18
4
1

Classes

Class Line # Actions
RssMacroTest 78 72 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.macro.rss;
21   
22    import java.io.File;
23    import java.io.FileInputStream;
24    import java.util.Collections;
25    import java.util.List;
26   
27    import javax.inject.Named;
28   
29    import org.junit.jupiter.api.BeforeEach;
30    import org.junit.jupiter.api.Test;
31    import org.mockito.Mock;
32    import org.xwiki.context.Execution;
33    import org.xwiki.context.ExecutionContext;
34    import org.xwiki.properties.internal.DefaultBeanManager;
35    import org.xwiki.rendering.block.Block;
36    import org.xwiki.rendering.block.GroupBlock;
37    import org.xwiki.rendering.block.ImageBlock;
38    import org.xwiki.rendering.block.LinkBlock;
39    import org.xwiki.rendering.block.NewLineBlock;
40    import org.xwiki.rendering.block.ParagraphBlock;
41    import org.xwiki.rendering.block.RawBlock;
42    import org.xwiki.rendering.block.WordBlock;
43    import org.xwiki.rendering.block.XDOM;
44    import org.xwiki.rendering.listener.reference.ResourceType;
45    import org.xwiki.rendering.macro.MacroContentParser;
46    import org.xwiki.rendering.macro.MacroExecutionException;
47    import org.xwiki.rendering.macro.rss.RssMacroParameters;
48    import org.xwiki.rendering.parser.Parser;
49    import org.xwiki.rendering.syntax.Syntax;
50    import org.xwiki.rendering.transformation.MacroTransformationContext;
51    import org.xwiki.test.annotation.ComponentList;
52    import org.xwiki.test.junit5.mockito.ComponentTest;
53    import org.xwiki.test.junit5.mockito.InjectMockComponents;
54    import org.xwiki.test.junit5.mockito.MockComponent;
55   
56    import com.sun.syndication.feed.synd.SyndFeed;
57    import com.sun.syndication.io.SyndFeedInput;
58    import com.sun.syndication.io.XmlReader;
59   
60    import static org.junit.jupiter.api.Assertions.assertEquals;
61    import static org.junit.jupiter.api.Assertions.assertSame;
62    import static org.junit.jupiter.api.Assertions.assertThrows;
63    import static org.junit.jupiter.api.Assertions.assertTrue;
64    import static org.mockito.ArgumentMatchers.any;
65    import static org.mockito.Mockito.mock;
66    import static org.mockito.Mockito.when;
67   
68    /**
69    * Unit tests for {@link RssMacro}.
70    *
71    * @version $Id: 30a4e6e58f4ab1bb061eca821ba27a422d3e3d3f $
72    * @since 1.9
73    */
74    @ComponentTest
75    @ComponentList({
76    DefaultBeanManager.class
77    })
 
78    public class RssMacroTest
79    {
80    @InjectMockComponents
81    private RssMacro macro;
82   
83    @MockComponent
84    private Execution execution;
85   
86    @MockComponent
87    private ExecutionContext context;
88   
89    @MockComponent
90    @Named("plain/1.0")
91    private Parser plainTextParser;
92   
93    @MockComponent
94    private MacroContentParser contentParser;
95   
96    @Mock
97    private RomeFeedFactory romeFeedFactory;
98   
 
99  3 toggle @BeforeEach
100    public void setup()
101    {
102  3 when(execution.getContext()).thenReturn(context);
103    }
104   
105    /**
106    * Tests whether the macro throws the appropriate exception
107    * in cases where the required 'feed' parameter is missing.
108    */
 
109  1 toggle @Test
110    public void testRequiredParameterMissing() throws Exception
111    {
112  1 Throwable exception = assertThrows(MacroExecutionException.class, () -> {
113  1 this.macro.execute(new RssMacroParameters(), null, null);
114    });
115  1 assertEquals("The required 'feed' parameter is missing", exception.getMessage());
116    }
117   
118    /**
119    * Tests the macro's behavior when the server hosting the feeds doesn't respond.
120    */
 
121  1 toggle @Test
122    public void testInvalidDocument() throws Exception
123    {
124  1 final RssMacroParameters parameters = new RssMacroParameters();
125  1 MacroExecutionException expectedException = new MacroExecutionException("Error");
126  1 when(romeFeedFactory.createFeed(parameters)).thenThrow(expectedException);
127   
128  1 this.macro.setFeedFactory(romeFeedFactory);
129    // Dummy URL since a feed URL is mandatory
130  1 parameters.setFeed("http://www.xwiki.org");
131   
132  1 Throwable exception = assertThrows(MacroExecutionException.class, () -> {
133  1 this.macro.execute(parameters, null, null);
134    });
135  1 assertSame(expectedException, exception);
136    }
137   
 
138  1 toggle @Test
139    public void execute() throws Exception
140    {
141  1 RssMacroParameters rssMacroParameters = new RssMacroParameters();
142  1 rssMacroParameters.setContent(true);
143  1 rssMacroParameters.setCount(4);
144  1 rssMacroParameters.setImage(true);
145  1 rssMacroParameters.setWidth("100");
146  1 rssMacroParameters.setEncoding("UTF-8");
147  1 rssMacroParameters.setDecoration(true);
148  1 rssMacroParameters.setFeed("http://www.xwiki.org");
149   
150  1 MacroTransformationContext macroTransformationContext = mock(MacroTransformationContext.class);
151  1 File feedFile = new File("src/test/resources/feed1.xml");
152   
153  1 SyndFeed syndFeed = new SyndFeedInput().build(new XmlReader(new FileInputStream(feedFile), true,
154    rssMacroParameters.getEncoding()));
155   
156  1 when(romeFeedFactory.createFeed(rssMacroParameters)).thenReturn(syndFeed);
157  1 when(context.getProperty("RssMacro.feed")).thenReturn(syndFeed);
158  1 this.macro.setFeedFactory(romeFeedFactory);
159   
160  1 List<Block> expectedBlockList = Collections.singletonList(
161    new GroupBlock(Collections.singletonList(new WordBlock("foo")))
162    );
163  1 when(plainTextParser.parse(any())).thenReturn(new XDOM(expectedBlockList));
164  1 when(contentParser.parse("Some content", macroTransformationContext, false, false))
165    .thenReturn(new XDOM(expectedBlockList));
166   
167  1 List<Block> obtainedContent = this.macro.execute(rssMacroParameters, "Some content", macroTransformationContext);
168  1 assertEquals(1, obtainedContent.size());
169   
170  1 GroupBlock groupBlock = (GroupBlock) obtainedContent.get(0);
171    // checking main parameters
172  1 assertEquals("box rssfeed", groupBlock.getParameter("class"));
173  1 assertEquals("width:100", groupBlock.getParameter("style"));
174   
175    // start checking children
176  1 List<Block> children = groupBlock.getChildren();
177  1 assertEquals(12, children.size());
178   
179    // checking created image
180  1 assertTrue(children.get(0) instanceof ImageBlock);
181  1 ImageBlock imageBlock = (ImageBlock) children.get(0);
182  1 assertEquals("http://www.w3schools.com/images/logo.gif", imageBlock.getReference().getReference());
183   
184  1 assertTrue(children.get(1) instanceof NewLineBlock);
185   
186    // checking first paragraph and first group presenting channel description
187  1 assertTrue(children.get(2) instanceof ParagraphBlock);
188  1 ParagraphBlock paragraphBlock = (ParagraphBlock) children.get(2);
189  1 assertEquals("rsschanneltitle", paragraphBlock.getParameter("class"));
190   
191  1 assertEquals(2, paragraphBlock.getChildren().size());
192  1 assertTrue(paragraphBlock.getChildren().get(0) instanceof LinkBlock);
193  1 LinkBlock firstLink = (LinkBlock) paragraphBlock.getChildren().get(0);
194  1 assertEquals("http://liftoff.msfc.nasa.gov/", firstLink.getReference().getReference());
195  1 assertEquals(ResourceType.URL, firstLink.getReference().getType());
196  1 assertEquals(1, firstLink.getChildren().size());
197  1 assertEquals(new WordBlock("foo"), firstLink.getChildren().get(0));
198   
199  1 assertTrue(paragraphBlock.getChildren().get(1) instanceof LinkBlock);
200  1 LinkBlock secondLink = (LinkBlock) paragraphBlock.getChildren().get(1);
201  1 assertEquals("http://liftoff.msfc.nasa.gov/", secondLink.getReference().getReference());
202  1 assertEquals(ResourceType.URL, secondLink.getReference().getType());
203  1 assertEquals(1, secondLink.getChildren().size());
204  1 assertTrue(secondLink.getChildren().get(0) instanceof ImageBlock);
205   
206  1 assertEquals(expectedBlockList.get(0), children.get(3));
207   
208    // checking second paragraph presenting first item link
209  1 assertTrue(children.get(4) instanceof ParagraphBlock);
210  1 paragraphBlock = (ParagraphBlock) children.get(4);
211  1 assertEquals("rssitemtitle", paragraphBlock.getParameter("class"));
212  1 assertEquals(1, paragraphBlock.getChildren().size());
213  1 assertTrue(paragraphBlock.getChildren().get(0) instanceof LinkBlock);
214  1 firstLink = (LinkBlock) paragraphBlock.getChildren().get(0);
215  1 assertEquals("http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp",
216    firstLink.getReference().getReference());
217  1 assertEquals(ResourceType.URL, firstLink.getReference().getType());
218  1 assertEquals(1, firstLink.getChildren().size());
219  1 assertEquals(new WordBlock("foo"), firstLink.getChildren().get(0));
220   
221    // checking second group block presenting first item description
222  1 assertTrue(children.get(5) instanceof GroupBlock);
223  1 groupBlock = (GroupBlock) children.get(5);
224  1 assertEquals("rssitemdescription", groupBlock.getParameter("class"));
225  1 assertEquals(1, groupBlock.getChildren().size());
226  1 assertEquals(
227    new RawBlock("How do Americans get ready to work with Russians aboard the International Space Station?",
228    Syntax.XHTML_1_0), groupBlock.getChildren().get(0));
229    }
230    }