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

File RecordableEventDescriptorHelperTest.java

 

Code metrics

0
29
3
1
111
73
3
0.1
9.67
3
1

Classes

Class Line # Actions
RecordableEventDescriptorHelperTest 48 29 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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.notifications.sources.internal;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.context.Execution;
29    import org.xwiki.context.ExecutionContext;
30    import org.xwiki.eventstream.RecordableEventDescriptor;
31    import org.xwiki.eventstream.RecordableEventDescriptorManager;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.test.mockito.MockitoComponentMockingRule;
34    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
35   
36    import static org.junit.Assert.assertEquals;
37    import static org.junit.Assert.assertFalse;
38    import static org.junit.Assert.assertNotNull;
39    import static org.junit.Assert.assertTrue;
40    import static org.mockito.Mockito.mock;
41    import static org.mockito.Mockito.verifyZeroInteractions;
42    import static org.mockito.Mockito.when;
43   
44    /**
45    * @version $Id: 2784129854b9b254bab2ab9236d910c9f8e6877d $
46    * @since
47    */
 
48    public class RecordableEventDescriptorHelperTest
49    {
50    @Rule
51    public final MockitoComponentMockingRule<RecordableEventDescriptorHelper> mocker =
52    new MockitoComponentMockingRule<>(RecordableEventDescriptorHelper.class);
53   
54    private WikiDescriptorManager wikiDescriptorManager;
55    private RecordableEventDescriptorManager recordableEventDescriptorManager;
56    private Execution execution;
57   
 
58  2 toggle @Before
59    public void setUp() throws Exception
60    {
61  2 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
62  2 recordableEventDescriptorManager = mocker.getInstance(RecordableEventDescriptorManager.class);
63  2 execution = mocker.getInstance(Execution.class);
64    }
65   
 
66  1 toggle @Test
67    public void hasDescriptorTest() throws Exception
68    {
69  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "UserA");
70  1 when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
71  1 ExecutionContext executionContext = new ExecutionContext();
72  1 when(execution.getContext()).thenReturn(executionContext);
73   
74  1 RecordableEventDescriptor descriptor1 = mock(RecordableEventDescriptor.class);
75  1 RecordableEventDescriptor descriptor2 = mock(RecordableEventDescriptor.class);
76  1 when(descriptor1.getEventType()).thenReturn("eventType1");
77  1 when(descriptor2.getEventType()).thenReturn("eventType2");
78   
79  1 when(recordableEventDescriptorManager.getRecordableEventDescriptors(true)).thenReturn(
80    Arrays.asList(descriptor1, descriptor2));
81   
82  1 assertTrue(mocker.getComponentUnderTest().hasDescriptor("eventType1", user));
83  1 assertFalse(mocker.getComponentUnderTest().hasDescriptor("eventType3", user));
84   
85  1 List<RecordableEventDescriptor> descriptorList = (List<RecordableEventDescriptor>) executionContext
86    .getProperty("RecordableEventDescriptorHelperCache_xwiki:XWiki.UserA");
87  1 assertNotNull(descriptorList);
88  1 assertTrue(descriptorList.contains(descriptor1));
89  1 assertTrue(descriptorList.contains(descriptor2));
90  1 assertEquals(2, descriptorList.size());
91    }
92   
 
93  1 toggle @Test
94    public void hasDescriptorWhenInCacheTest() throws Exception
95    {
96  1 DocumentReference user = new DocumentReference("xwiki", "XWiki", "UserA");
97  1 when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
98  1 ExecutionContext executionContext = new ExecutionContext();
99  1 when(execution.getContext()).thenReturn(executionContext);
100   
101  1 RecordableEventDescriptor descriptor1 = mock(RecordableEventDescriptor.class);
102  1 RecordableEventDescriptor descriptor2 = mock(RecordableEventDescriptor.class);
103  1 when(descriptor2.getEventType()).thenReturn("eventType");
104   
105  1 executionContext.setProperty("RecordableEventDescriptorHelperCache_xwiki:XWiki.UserA",
106    Arrays.asList(descriptor1, descriptor2));
107   
108  1 assertTrue(mocker.getComponentUnderTest().hasDescriptor("eventType", user));
109  1 verifyZeroInteractions(recordableEventDescriptorManager);
110    }
111    }