1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal.email.live

File LiveNotificationEmailListenerTest.java

 

Code metrics

0
18
3
1
109
62
3
0.17
6
3
1

Classes

Class Line # Actions
LiveNotificationEmailListenerTest 49 18 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.notifications.notifiers.internal.email.live;
21   
22    import java.io.ByteArrayOutputStream;
23    import java.io.PrintStream;
24    import java.util.Arrays;
25   
26    import org.junit.After;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.eventstream.Event;
32    import org.xwiki.eventstream.RecordableEventDescriptor;
33    import org.xwiki.eventstream.RecordableEventDescriptorManager;
34    import org.xwiki.eventstream.events.EventStreamAddedEvent;
35    import org.xwiki.notifications.NotificationConfiguration;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import static org.mockito.Mockito.mock;
39    import static org.mockito.Mockito.times;
40    import static org.mockito.Mockito.verify;
41    import static org.mockito.Mockito.when;
42   
43    /**
44    * Unit tests for {@link LiveNotificationEmailListener}.
45    *
46    * @since 9.6RC1
47    * @version $Id: e750ead1c06bfc7399efd2a1ec01de8e36854382 $
48    */
 
49    public class LiveNotificationEmailListenerTest
50    {
51    @Rule
52    public final MockitoComponentMockingRule<LiveNotificationEmailListener> mocker =
53    new MockitoComponentMockingRule<>(LiveNotificationEmailListener.class);
54   
55    private RecordableEventDescriptorManager recordableEventDescriptorManager;
56   
57    private LiveNotificationEmailManager liveNotificationEmailManager;
58   
59    private NotificationConfiguration notificationConfiguration;
60   
61    private ComponentManager componentManager;
62   
63    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
64    private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
65   
 
66  1 toggle @Before
67    public void setUp() throws Exception
68    {
69  1 this.recordableEventDescriptorManager =
70    this.mocker.registerMockComponent(RecordableEventDescriptorManager.class);
71   
72  1 this.liveNotificationEmailManager = this.mocker.registerMockComponent(LiveNotificationEmailManager.class);
73   
74  1 this.notificationConfiguration = this.mocker.registerMockComponent(NotificationConfiguration.class);
75   
76  1 this.componentManager = this.mocker.registerMockComponent(ComponentManager.class, "context");
77   
78    // Override the default output streams in order for the tests to pass even if the thread raised by the test
79    // crashes and log its crash to console.
80  1 System.setOut(new PrintStream(outContent));
81  1 System.setErr(new PrintStream(errContent));
82    }
83   
 
84  1 toggle @After
85    public void cleanUpStreams() {
86  1 System.setOut(null);
87  1 System.setErr(null);
88    }
89   
 
90  1 toggle @Test
91    public void testOnEvent() throws Exception
92    {
93  1 Event eventStreamEvent = mock(Event.class);
94  1 EventStreamAddedEvent event = mock(EventStreamAddedEvent.class);
95   
96  1 RecordableEventDescriptor eventDescriptor = mock(RecordableEventDescriptor.class);
97  1 when(eventDescriptor.getEventType()).thenReturn("eventType");
98  1 when(this.recordableEventDescriptorManager.getRecordableEventDescriptors(true))
99    .thenReturn(Arrays.asList(eventDescriptor));
100  1 when(eventStreamEvent.getType()).thenReturn("eventType");
101   
102  1 when(this.notificationConfiguration.areEmailsEnabled()).thenReturn(true);
103  1 when(this.notificationConfiguration.isEnabled()).thenReturn(true);
104   
105  1 this.mocker.getComponentUnderTest().onEvent(event, eventStreamEvent, null);
106   
107  1 verify(this.liveNotificationEmailManager, times(1)).addEvent(eventStreamEvent);
108    }
109    }