2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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.
11 * SonarQube 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.
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.
20 package org.sonar.server.computation.taskprocessor;
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;
35 import static org.mockito.Mockito.doThrow;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.verifyZeroInteractions;
38 import static org.mockito.Mockito.when;
40 public class CeWorkerRunnableImplTest {
43 public CeTaskProcessorRepositoryRule taskProcessorRepository = new CeTaskProcessorRepositoryRule();
45 CeQueue queue = mock(CeQueueImpl.class);
46 ReportTaskProcessor taskProcessor = mock(ReportTaskProcessor.class);
47 CeLogging ceLogging = mock(CeLogging.class);
48 CeWorkerRunnable underTest = new CeWorkerRunnableImpl(queue, ceLogging, taskProcessorRepository);
49 InOrder inOrder = Mockito.inOrder(ceLogging, taskProcessor, queue);
52 public void no_pending_tasks_in_queue() throws Exception {
53 when(queue.peek()).thenReturn(Optional.<CeTask>absent());
57 verifyZeroInteractions(taskProcessor, ceLogging);
61 public void fail_when_no_CeTaskProcessor_is_found_in_repository() {
62 CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(null).build();
63 taskProcessorRepository.setNoProcessorForTask(CeTaskTypes.REPORT);
64 when(queue.peek()).thenReturn(Optional.of(task));
68 inOrder.verify(ceLogging).initForTask(task);
69 inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED);
70 inOrder.verify(ceLogging).clearForTask();
74 public void peek_and_process_task() throws Exception {
75 CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(null).build();
76 taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
77 when(queue.peek()).thenReturn(Optional.of(task));
81 inOrder.verify(ceLogging).initForTask(task);
82 inOrder.verify(taskProcessor).process(task);
83 inOrder.verify(queue).remove(task, CeActivityDto.Status.SUCCESS);
84 inOrder.verify(ceLogging).clearForTask();
88 public void fail_to_process_task() throws Exception {
89 CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(null).build();
90 when(queue.peek()).thenReturn(Optional.of(task));
91 taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
92 doThrow(new IllegalStateException("simulate exception thrown by TaskProcessor#process")).when(taskProcessor).process(task);
96 inOrder.verify(ceLogging).initForTask(task);
97 inOrder.verify(taskProcessor).process(task);
98 inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED);
99 inOrder.verify(ceLogging).clearForTask();