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.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;
41 public class CeWorkerRunnableImplTest {
44 public CeTaskProcessorRepositoryRule taskProcessorRepository = new CeTaskProcessorRepositoryRule();
46 CeQueue queue = mock(CeQueueImpl.class);
47 ReportTaskProcessor taskProcessor = mock(ReportTaskProcessor.class);
48 CeLogging ceLogging = mock(CeLogging.class);
49 CeWorkerRunnable underTest = new CeWorkerRunnableImpl(queue, ceLogging, taskProcessorRepository);
50 InOrder inOrder = Mockito.inOrder(ceLogging, taskProcessor, queue);
53 public void no_pending_tasks_in_queue() throws Exception {
54 when(queue.peek()).thenReturn(Optional.<CeTask>absent());
56 assertThat(underTest.call()).isFalse();
58 verifyZeroInteractions(taskProcessor, ceLogging);
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));
67 assertThat(underTest.call()).isTrue();
69 inOrder.verify(ceLogging).initForTask(task);
70 inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED);
71 inOrder.verify(ceLogging).clearForTask();
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));
80 assertThat(underTest.call()).isTrue();
82 inOrder.verify(ceLogging).initForTask(task);
83 inOrder.verify(taskProcessor).process(task);
84 inOrder.verify(queue).remove(task, CeActivityDto.Status.SUCCESS);
85 inOrder.verify(ceLogging).clearForTask();
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);
95 assertThat(underTest.call()).isTrue();
97 inOrder.verify(ceLogging).initForTask(task);
98 inOrder.verify(taskProcessor).process(task);
99 inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED);
100 inOrder.verify(ceLogging).clearForTask();