]> source.dussan.org Git - sonarqube.git/blob
f9d708ee8384382059e994ea95995c323cb82de5
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.computation.taskprocessor;
21
22 import com.google.common.base.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.mockito.InOrder;
26 import org.mockito.Mockito;
27 import org.sonar.db.ce.CeActivityDto;
28 import org.sonar.db.ce.CeTaskTypes;
29 import org.sonar.server.computation.log.CeLogging;
30 import org.sonar.server.computation.queue.CeQueue;
31 import org.sonar.server.computation.queue.CeQueueImpl;
32 import org.sonar.server.computation.queue.CeTask;
33 import org.sonar.server.computation.taskprocessor.report.ReportTaskProcessor;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.doThrow;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verifyZeroInteractions;
39 import static org.mockito.Mockito.when;
40
41 public class CeWorkerCallableImplTest {
42
43   @Rule
44   public CeTaskProcessorRepositoryRule taskProcessorRepository = new CeTaskProcessorRepositoryRule();
45
46   CeQueue queue = mock(CeQueueImpl.class);
47   ReportTaskProcessor taskProcessor = mock(ReportTaskProcessor.class);
48   CeLogging ceLogging = mock(CeLogging.class);
49   CeWorkerCallable underTest = new CeWorkerCallableImpl(queue, ceLogging, taskProcessorRepository);
50   InOrder inOrder = Mockito.inOrder(ceLogging, taskProcessor, queue);
51
52   @Test
53   public void no_pending_tasks_in_queue() throws Exception {
54     when(queue.peek()).thenReturn(Optional.<CeTask>absent());
55
56     assertThat(underTest.call()).isFalse();
57
58     verifyZeroInteractions(taskProcessor, ceLogging);
59   }
60
61   @Test
62   public void fail_when_no_CeTaskProcessor_is_found_in_repository() throws Exception {
63     CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(null).build();
64     taskProcessorRepository.setNoProcessorForTask(CeTaskTypes.REPORT);
65     when(queue.peek()).thenReturn(Optional.of(task));
66
67     assertThat(underTest.call()).isTrue();
68
69     inOrder.verify(ceLogging).initForTask(task);
70     inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null);
71     inOrder.verify(ceLogging).clearForTask();
72   }
73
74   @Test
75   public void peek_and_process_task() throws Exception {
76     CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(null).build();
77     taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
78     when(queue.peek()).thenReturn(Optional.of(task));
79
80     assertThat(underTest.call()).isTrue();
81
82     inOrder.verify(ceLogging).initForTask(task);
83     inOrder.verify(taskProcessor).process(task);
84     inOrder.verify(queue).remove(task, CeActivityDto.Status.SUCCESS, null);
85     inOrder.verify(ceLogging).clearForTask();
86   }
87
88   @Test
89   public void fail_to_process_task() throws Exception {
90     CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(null).build();
91     when(queue.peek()).thenReturn(Optional.of(task));
92     taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
93     doThrow(new IllegalStateException("simulate exception thrown by TaskProcessor#process")).when(taskProcessor).process(task);
94
95     assertThat(underTest.call()).isTrue();
96
97     inOrder.verify(ceLogging).initForTask(task);
98     inOrder.verify(taskProcessor).process(task);
99     inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null);
100     inOrder.verify(ceLogging).clearForTask();
101   }
102 }