1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
55 |
|
|
56 |
|
@version |
57 |
|
@since |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 2 |
Complexity Density: 0.07 |
|
59 |
|
public class ExtensionJobHistoryRecorderTest |
60 |
|
{ |
61 |
|
@Rule |
62 |
|
public MockitoComponentMockingRule<EventListener> mocker = new MockitoComponentMockingRule<EventListener>( |
63 |
|
ExtensionJobHistoryRecorder.class); |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
65 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
74 |
1 |
@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 |
|
} |