1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.messagestream.script

File MessageStreamScriptServiceTest.java

 

Code metrics

0
45
12
1
183
124
12
0.27
3.75
12
1

Classes

Class Line # Actions
MessageStreamScriptServiceTest 45 45 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 11 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.messagestream.script;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.bridge.DocumentAccessBridge;
26    import org.xwiki.context.Execution;
27    import org.xwiki.context.ExecutionContext;
28    import org.xwiki.messagestream.MessageStream;
29    import org.xwiki.messagestream.MessageStreamConfiguration;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.test.mockito.MockitoComponentMockingRule;
32   
33    import static org.junit.Assert.assertEquals;
34    import static org.junit.Assert.assertFalse;
35    import static org.junit.Assert.assertTrue;
36    import static org.mockito.Mockito.doThrow;
37    import static org.mockito.Mockito.verify;
38    import static org.mockito.Mockito.when;
39   
40    /**
41    * Unit tests for {@link MessageStreamScriptService}.
42    *
43    * @version $Id: c60f4a8a01825a2f2077b2a86bbbd911b3dd7702 $
44    */
 
45    public class MessageStreamScriptServiceTest
46    {
47    private final DocumentReference targetUser = new DocumentReference("wiki", "XWiki", "JaneBuck");
48   
49    private final DocumentReference targetGroup = new DocumentReference("wiki", "XWiki", "MyFriends");
50   
51    @Rule
52    public MockitoComponentMockingRule<MessageStreamScriptService> mocker =
53    new MockitoComponentMockingRule<>(MessageStreamScriptService.class);
54   
55    private MessageStreamScriptService streamService;
56   
57    private DocumentAccessBridge documentAccessBridge;
58   
59    private MessageStreamConfiguration messageStreamConfiguration;
60   
61    private ExecutionContext executionContext;
62   
 
63  11 toggle @Before
64    public void configure() throws Exception
65    {
66  11 this.streamService = this.mocker.getComponentUnderTest();
67   
68  11 Execution execution = this.mocker.getInstance(Execution.class);
69  11 this.executionContext = new ExecutionContext();
70  11 when(execution.getContext()).thenReturn(this.executionContext);
71   
72  11 documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
73  11 messageStreamConfiguration = mocker.getInstance(MessageStreamConfiguration.class);
74    }
75   
 
76  1 toggle @Test
77    public void postPublicMessage() throws Exception
78    {
79  1 assertTrue(this.streamService.postPublicMessage("Hello World!"));
80   
81  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
82  1 verify(stream).postPublicMessage("Hello World!");
83    }
84   
 
85  1 toggle @Test
86    public void postPublicMessageWithFailure() throws Exception
87    {
88  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
89  1 doThrow(new RuntimeException("error")).when(stream).postPublicMessage("Hello World!");
90   
91  1 assertFalse(this.streamService.postPublicMessage("Hello World!"));
92    }
93   
 
94  1 toggle @Test
95    public void postPersonalMessage() throws Exception
96    {
97  1 assertTrue(this.streamService.postPersonalMessage("Hello World!"));
98   
99  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
100  1 verify(stream).postPersonalMessage("Hello World!");
101    }
102   
 
103  1 toggle @Test
104    public void postPersonalMessageWithFailure() throws Exception
105    {
106  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
107  1 doThrow(new RuntimeException("error")).when(stream).postPersonalMessage("Hello World!");
108   
109  1 assertFalse(this.streamService.postPersonalMessage("Hello World!"));
110  1 assertEquals("error", this.streamService.getLastError().getMessage());
111    }
112   
 
113  1 toggle @Test
114    public void postDirectMessage() throws Exception
115    {
116  1 assertTrue(this.streamService.postDirectMessageToUser("Hello World!", this.targetUser));
117   
118  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
119  1 verify(stream).postDirectMessageToUser("Hello World!", this.targetUser);
120    }
121   
 
122  1 toggle @Test
123    public void postDirectMessageWithFailure() throws Exception
124    {
125  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
126  1 doThrow(new RuntimeException("error")).when(stream).postDirectMessageToUser("Hello World!", this.targetUser);
127   
128  1 assertFalse(this.streamService.postDirectMessageToUser("Hello World!", this.targetUser));
129  1 assertEquals("error", this.streamService.getLastError().getMessage());
130    }
131   
 
132  1 toggle @Test
133    public void postGroupMessage() throws Exception
134    {
135  1 assertTrue(this.streamService.postMessageToGroup("Hello World!", this.targetGroup));
136   
137  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
138  1 verify(stream).postMessageToGroup("Hello World!", this.targetGroup);
139    }
140   
 
141  1 toggle @Test
142    public void postGroupMessageWithFailure() throws Exception
143    {
144  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
145  1 doThrow(new RuntimeException("error")).when(stream).postMessageToGroup("Hello World!", this.targetGroup);
146   
147  1 assertFalse(this.streamService.postMessageToGroup("Hello World!", this.targetGroup));
148  1 assertEquals("error", this.streamService.getLastError().getMessage());
149    }
150   
 
151  1 toggle @Test
152    public void deleteMessage() throws Exception
153    {
154  1 assertTrue(this.streamService.deleteMessage("abc123"));
155   
156  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
157  1 verify(stream).deleteMessage("abc123");
158    }
159   
 
160  1 toggle @Test
161    public void deleteMessageWithFailure() throws Exception
162    {
163  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
164  1 doThrow(new IllegalArgumentException("error")).when(stream).deleteMessage("abc123");
165   
166  1 assertFalse(this.streamService.deleteMessage("abc123"));
167  1 assertEquals("error", this.streamService.getLastError().getMessage());
168    }
169   
 
170  1 toggle @Test
171    public void isActive() throws Exception
172    {
173  1 when(documentAccessBridge.getCurrentDocumentReference()).thenReturn(
174    new DocumentReference("wikiA", "Space", "Page"),
175    new DocumentReference("wikiB", "Space", "Page"));
176  1 when(messageStreamConfiguration.isActive("wikiA")).thenReturn(true);
177  1 when(messageStreamConfiguration.isActive("wikiB")).thenReturn(false);
178   
179    // Test
180  1 assertTrue(mocker.getComponentUnderTest().isActive());
181  1 assertFalse(mocker.getComponentUnderTest().isActive());
182    }
183    }