1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.messagestream.script

File MessageStreamScriptServiceTest.java

 

Code metrics

0
48
12
1
182
122
12
0.25
4
12
1

Classes

Class Line # Actions
MessageStreamScriptServiceTest 45 48 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.context.Execution;
26    import org.xwiki.context.ExecutionContext;
27    import org.xwiki.messagestream.MessageStream;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.EntityReference;
30    import org.xwiki.test.mockito.MockitoComponentMockingRule;
31   
32    import com.xpn.xwiki.XWiki;
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.objects.BaseObject;
36   
37    import static org.mockito.Mockito.*;
38    import static org.junit.Assert.*;
39   
40    /**
41    * Unit tests for {@link MessageStreamScriptService}.
42    *
43    * @version $Id: 0499d9d71a14a95efd47462b36c295edbf4e7b78 $
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 ExecutionContext executionContext;
58   
 
59  11 toggle @Before
60    public void configure() throws Exception
61    {
62  11 this.streamService = this.mocker.getComponentUnderTest();
63   
64  11 Execution execution = this.mocker.getInstance(Execution.class);
65  11 this.executionContext = new ExecutionContext();
66  11 when(execution.getContext()).thenReturn(this.executionContext);
67    }
68   
 
69  1 toggle @Test
70    public void postPublicMessage() throws Exception
71    {
72  1 assertTrue(this.streamService.postPublicMessage("Hello World!"));
73   
74  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
75  1 verify(stream).postPublicMessage("Hello World!");
76    }
77   
 
78  1 toggle @Test
79    public void postPublicMessageWithFailure() throws Exception
80    {
81  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
82  1 doThrow(new RuntimeException("error")).when(stream).postPublicMessage("Hello World!");
83   
84  1 assertFalse(this.streamService.postPublicMessage("Hello World!"));
85    }
86   
 
87  1 toggle @Test
88    public void postPersonalMessage() throws Exception
89    {
90  1 assertTrue(this.streamService.postPersonalMessage("Hello World!"));
91   
92  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
93  1 verify(stream).postPersonalMessage("Hello World!");
94    }
95   
 
96  1 toggle @Test
97    public void postPersonalMessageWithFailure() throws Exception
98    {
99  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
100  1 doThrow(new RuntimeException("error")).when(stream).postPersonalMessage("Hello World!");
101   
102  1 assertFalse(this.streamService.postPersonalMessage("Hello World!"));
103  1 assertEquals("error", this.streamService.getLastError().getMessage());
104    }
105   
 
106  1 toggle @Test
107    public void postDirectMessage() throws Exception
108    {
109  1 assertTrue(this.streamService.postDirectMessageToUser("Hello World!", this.targetUser));
110   
111  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
112  1 verify(stream).postDirectMessageToUser("Hello World!", this.targetUser);
113    }
114   
 
115  1 toggle @Test
116    public void postDirectMessageWithFailure() throws Exception
117    {
118  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
119  1 doThrow(new RuntimeException("error")).when(stream).postDirectMessageToUser("Hello World!", this.targetUser);
120   
121  1 assertFalse(this.streamService.postDirectMessageToUser("Hello World!", this.targetUser));
122  1 assertEquals("error", this.streamService.getLastError().getMessage());
123    }
124   
 
125  1 toggle @Test
126    public void postGroupMessage() throws Exception
127    {
128  1 assertTrue(this.streamService.postMessageToGroup("Hello World!", this.targetGroup));
129   
130  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
131  1 verify(stream).postMessageToGroup("Hello World!", this.targetGroup);
132    }
133   
 
134  1 toggle @Test
135    public void postGroupMessageWithFailure() throws Exception
136    {
137  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
138  1 doThrow(new RuntimeException("error")).when(stream).postMessageToGroup("Hello World!", this.targetGroup);
139   
140  1 assertFalse(this.streamService.postMessageToGroup("Hello World!", this.targetGroup));
141  1 assertEquals("error", this.streamService.getLastError().getMessage());
142    }
143   
 
144  1 toggle @Test
145    public void deleteMessage() throws Exception
146    {
147  1 assertTrue(this.streamService.deleteMessage("abc123"));
148   
149  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
150  1 verify(stream).deleteMessage("abc123");
151    }
152   
 
153  1 toggle @Test
154    public void deleteMessageWithFailure() throws Exception
155    {
156  1 MessageStream stream = this.mocker.getInstance(MessageStream.class);
157  1 doThrow(new IllegalArgumentException("error")).when(stream).deleteMessage("abc123");
158   
159  1 assertFalse(this.streamService.deleteMessage("abc123"));
160  1 assertEquals("error", this.streamService.getLastError().getMessage());
161    }
162   
 
163  1 toggle @Test
164    public void isActive() throws Exception
165    {
166  1 XWikiContext xcontext = mock(XWikiContext.class);
167  1 this.executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xcontext);
168   
169  1 XWiki xwiki = mock(XWiki.class);
170  1 when(xcontext.getWiki()).thenReturn(xwiki);
171   
172  1 XWikiDocument document = mock(XWikiDocument.class);
173  1 when(xwiki.getDocument(any(EntityReference.class), any(XWikiContext.class))).thenReturn(document);
174   
175  1 BaseObject object = mock(BaseObject.class);
176  1 when(document.getXObject(any(EntityReference.class))).thenReturn(object);
177   
178  1 when(object.getIntValue("active")).thenReturn(1);
179   
180  1 assertTrue(this.streamService.isActive());
181    }
182    }