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

File DefaultJobStatusTest.java

 

Code metrics

0
27
4
1
114
68
4
0.15
6.75
4
1

Classes

Class Line # Actions
DefaultJobStatusTest 44 27 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.job.internal;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Test;
25    import org.mockito.invocation.InvocationOnMock;
26    import org.mockito.stubbing.Answer;
27    import org.xwiki.job.DefaultRequest;
28    import org.xwiki.job.DefaultJobStatus;
29    import org.xwiki.job.event.status.JobStatus;
30    import org.xwiki.job.event.status.QuestionAnsweredEvent;
31    import org.xwiki.job.event.status.QuestionAskedEvent;
32    import org.xwiki.logging.LoggerManager;
33    import org.xwiki.observation.ObservationManager;
34    import org.xwiki.observation.event.Event;
35   
36    import static org.junit.Assert.*;
37    import static org.mockito.Mockito.*;
38   
39    /**
40    * Unit tests for {@link DefaultJobStatus}.
41    *
42    * @version $Id: 03ae4c92b0a3fdf4fcf41f2069da570b08070e7d $
43    */
 
44    public class DefaultJobStatusTest
45    {
46    private ObservationManager observationManager = mock(ObservationManager.class);
47   
48    private LoggerManager loggerManager = mock(LoggerManager.class);
49   
 
50  1 toggle @Test
51    public void subJobQuestionIsForwardedToParent() throws Exception
52    {
53  1 JobStatus parentJobStatus = mock(JobStatus.class);
54  1 org.xwiki.job.DefaultJobStatus<DefaultRequest> jobStatus = new org.xwiki.job.DefaultJobStatus<>(
55    new DefaultRequest(), parentJobStatus, this.observationManager, this.loggerManager);
56   
57  1 String question = "What's up?";
58  1 jobStatus.ask(question);
59   
60  1 assertSame(question, jobStatus.getQuestion());
61  1 verify(parentJobStatus).ask(question);
62   
63  1 jobStatus.answered();
64   
65  1 assertNull(jobStatus.getQuestion());
66  1 verify(parentJobStatus).answered();
67   
68    // Only the parent job status should fire QuestionAsked/Answered events.
69  1 verify(this.observationManager, never()).notify(any(Event.class), anyObject());
70  1 verify(this.observationManager, never()).notify(any(Event.class), anyObject(), anyObject());
71    }
72   
 
73  1 toggle @Test
74    public void fireQuestionAnsweredOK()
75    {
76  1 DefaultRequest request = new DefaultRequest();
77  1 request.setId(Arrays.asList("test", "answered"));
78   
79  1 DefaultJobStatus<DefaultRequest> jobStatus =
80    new DefaultJobStatus<>(request, null, this.observationManager, this.loggerManager);
81   
82  1 jobStatus.answered();
83   
84    // The question type is null because we didn't asked any question.
85  1 verify(this.observationManager).notify(new QuestionAnsweredEvent(null, request.getId()), jobStatus);
86    }
87   
 
88  1 toggle @Test
89    public void fireQuestionAskedOK() throws Exception
90    {
91  1 DefaultRequest request = new DefaultRequest();
92  1 request.setId(Arrays.asList("test", "asked"));
93   
94  1 DefaultJobStatus<DefaultRequest> jobStatus =
95    new DefaultJobStatus<>(request, null, this.observationManager, this.loggerManager);
96   
97  1 QuestionAskedEvent questionAsked = new QuestionAskedEvent(String.class.getName(), request.getId());
98  1 doAnswer(new Answer<Void>()
99    {
 
100  1 toggle @Override
101    public Void answer(InvocationOnMock invocation) throws Throwable
102    {
103  1 QuestionAskedEvent event = (QuestionAskedEvent) invocation.getArguments()[0];
104  1 event.answered();
105  1 return null;
106    }
107    }).when(this.observationManager).notify(questionAsked, jobStatus);
108   
109  1 jobStatus.ask("What's up?");
110   
111  1 QuestionAnsweredEvent questionAnswered = new QuestionAnsweredEvent(String.class.getName(), request.getId());
112  1 verify(this.observationManager).notify(questionAnswered, jobStatus);
113    }
114    }