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

File DefaultJobStatusTest.java

 

Code metrics

0
27
4
1
120
74
4
0.15
6.75
4
1

Classes

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