]> source.dussan.org Git - sonarqube.git/blob
a4648392c1378ddda45fe6113ffbb1d5583a70c2
[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.ce.log.CeLogging;
28 import org.sonar.ce.queue.CeTask;
29 import org.sonar.db.ce.CeActivityDto;
30 import org.sonar.db.ce.CeTaskTypes;
31 import org.sonar.server.computation.queue.InternalCeQueue;
32 import org.sonar.server.computation.taskprocessor.report.ReportTaskProcessor;
33
34 import static org.assertj.core.api.Assertions.assertThat;
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;
39
40 public class CeWorkerCallableImplTest {
41
42   @Rule
43   public CeTaskProcessorRepositoryRule taskProcessorRepository = new CeTaskProcessorRepositoryRule();
44
45   InternalCeQueue queue = mock(InternalCeQueue.class);
46   ReportTaskProcessor taskProcessor = mock(ReportTaskProcessor.class);
47   CeLogging ceLogging = mock(CeLogging.class);
48   CeWorkerCallable underTest = new CeWorkerCallableImpl(queue, ceLogging, taskProcessorRepository);
49   InOrder inOrder = Mockito.inOrder(ceLogging, taskProcessor, queue);
50
51   @Test
52   public void no_pending_tasks_in_queue() throws Exception {
53     when(queue.peek()).thenReturn(Optional.<CeTask>absent());
54
55     assertThat(underTest.call()).isFalse();
56
57     verifyZeroInteractions(taskProcessor, ceLogging);
58   }
59
60   @Test
61   public void fail_when_no_CeTaskProcessor_is_found_in_repository() throws Exception {
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));
65
66     assertThat(underTest.call()).isTrue();
67
68     inOrder.verify(ceLogging).initForTask(task);
69     inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null);
70     inOrder.verify(ceLogging).clearForTask();
71   }
72
73   @Test
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));
78
79     assertThat(underTest.call()).isTrue();
80
81     inOrder.verify(ceLogging).initForTask(task);
82     inOrder.verify(taskProcessor).process(task);
83     inOrder.verify(queue).remove(task, CeActivityDto.Status.SUCCESS, null);
84     inOrder.verify(ceLogging).clearForTask();
85   }
86
87   @Test
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);
93
94     assertThat(underTest.call()).isTrue();
95
96     inOrder.verify(ceLogging).initForTask(task);
97     inOrder.verify(taskProcessor).process(task);
98     inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null);
99     inOrder.verify(ceLogging).clearForTask();
100   }
101 }