1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.job.history.internal

File ExtensionJobHistoryRecorderTest.java

 

Code metrics

0
27
2
1
113
73
2
0.07
13.5
2
1

Classes

Class Line # Actions
ExtensionJobHistoryRecorderTest 59 27 0% 2 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.extension.job.history.internal;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.Date;
26   
27    import javax.inject.Provider;
28   
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.mockito.ArgumentCaptor;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.util.DefaultParameterizedType;
35    import org.xwiki.extension.job.ExtensionRequest;
36    import org.xwiki.extension.job.history.ExtensionJobHistory;
37    import org.xwiki.extension.job.history.ExtensionJobHistoryRecord;
38    import org.xwiki.extension.job.history.QuestionRecorder;
39    import org.xwiki.job.DefaultJobStatus;
40    import org.xwiki.job.Job;
41    import org.xwiki.job.event.JobFinishedEvent;
42    import org.xwiki.job.event.JobStartedEvent;
43    import org.xwiki.job.event.status.QuestionAnsweredEvent;
44    import org.xwiki.observation.EventListener;
45    import org.xwiki.test.mockito.MockitoComponentMockingRule;
46   
47    import static org.junit.Assert.assertEquals;
48    import static org.junit.Assert.assertSame;
49    import static org.mockito.Mockito.mock;
50    import static org.mockito.Mockito.verify;
51    import static org.mockito.Mockito.when;
52   
53    /**
54    * Unit tests for {@link ExtensionJobHistoryRecorder}.
55    *
56    * @version $Id: e40ac3cca4f4c3a6395929eac82c4e8087385803 $
57    * @since 7.1RC1
58    */
 
59    public class ExtensionJobHistoryRecorderTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<EventListener> mocker = new MockitoComponentMockingRule<EventListener>(
63    ExtensionJobHistoryRecorder.class);
64   
 
65  1 toggle @Before
66    public void configure() throws Exception
67    {
68  1 Provider<ComponentManager> componentManagerProvider =
69    this.mocker.registerMockComponent(
70    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
71  1 when(componentManagerProvider.get()).thenReturn(this.mocker);
72    }
73   
 
74  1 toggle @Test
75    public void record() throws Exception
76    {
77  1 ExtensionJobHistory history = this.mocker.getInstance(ExtensionJobHistory.class);
78   
79  1 ExtensionRequest request = mock(ExtensionRequest.class);
80  1 when(request.getId()).thenReturn(Arrays.asList("job", "id"));
81   
82  1 DefaultJobStatus<ExtensionRequest> status = mock(DefaultJobStatus.class);
83  1 when(status.getRequest()).thenReturn(request);
84  1 when(status.getStartDate()).thenReturn(new Date());
85  1 when(status.getQuestion()).thenReturn("What's up?");
86   
87  1 Job job = mock(Job.class);
88  1 when(job.getType()).thenReturn("jobType");
89  1 when(job.getRequest()).thenReturn(request);
90  1 when(job.getStatus()).thenReturn(status);
91   
92  1 ParameterizedType questionRecorderType =
93    new DefaultParameterizedType(null, QuestionRecorder.class, String.class);
94  1 QuestionRecorder<String> questionRecorder = this.mocker.registerMockComponent(questionRecorderType);
95   
96  1 EventListener recorder = this.mocker.getComponentUnderTest();
97  1 recorder.onEvent(new JobStartedEvent(), job, null);
98  1 recorder.onEvent(new QuestionAnsweredEvent(), job.getStatus(), null);
99  1 recorder.onEvent(new JobFinishedEvent(), job, null);
100   
101  1 verify(questionRecorder).record((String) status.getQuestion());
102   
103  1 ArgumentCaptor<ExtensionJobHistoryRecord> recordCaptor =
104    ArgumentCaptor.forClass(ExtensionJobHistoryRecord.class);
105  1 verify(history).addRecord(recordCaptor.capture());
106   
107  1 ExtensionJobHistoryRecord record = recordCaptor.getValue();
108  1 assertEquals(job.getType(), record.getJobType());
109  1 assertSame(job.getRequest(), record.getRequest());
110  1 assertEquals(Collections.singletonMap(String.class.getName(), questionRecorder), record.getAnswers());
111  1 assertEquals(job.getStatus().getStartDate(), record.getStartDate());
112    }
113    }