]> source.dussan.org Git - sonarqube.git/blob
d8182ed2324cf5b5933425af2baba74e89cd05be
[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 java.util.List;
24 import javax.annotation.Nullable;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.mockito.InOrder;
28 import org.mockito.Mockito;
29 import org.sonar.api.utils.log.LogTester;
30 import org.sonar.api.utils.log.LoggerLevel;
31 import org.sonar.ce.log.CeLogging;
32 import org.sonar.ce.queue.CeTask;
33 import org.sonar.db.ce.CeActivityDto;
34 import org.sonar.db.ce.CeTaskTypes;
35 import org.sonar.server.computation.queue.InternalCeQueue;
36 import org.sonar.server.computation.task.projectanalysis.taskprocessor.ReportTaskProcessor;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.doThrow;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.spy;
42 import static org.mockito.Mockito.verifyZeroInteractions;
43 import static org.mockito.Mockito.when;
44
45 public class CeWorkerCallableImplTest {
46
47   @Rule
48   public CeTaskProcessorRepositoryRule taskProcessorRepository = new CeTaskProcessorRepositoryRule();
49   @Rule
50   public LogTester logTester = new LogTester();
51
52   InternalCeQueue queue = mock(InternalCeQueue.class);
53   ReportTaskProcessor taskProcessor = mock(ReportTaskProcessor.class);
54   CeLogging ceLogging = spy(CeLogging.class);
55   CeWorkerCallable underTest = new CeWorkerCallableImpl(queue, ceLogging, taskProcessorRepository);
56   InOrder inOrder = Mockito.inOrder(ceLogging, taskProcessor, queue);
57
58   @Test
59   public void no_pending_tasks_in_queue() throws Exception {
60     when(queue.peek()).thenReturn(Optional.<CeTask>absent());
61
62     assertThat(underTest.call()).isFalse();
63
64     verifyZeroInteractions(taskProcessor, ceLogging);
65   }
66
67   @Test
68   public void fail_when_no_CeTaskProcessor_is_found_in_repository() throws Exception {
69     CeTask task = createCeTask(null);
70     taskProcessorRepository.setNoProcessorForTask(CeTaskTypes.REPORT);
71     when(queue.peek()).thenReturn(Optional.of(task));
72
73     assertThat(underTest.call()).isTrue();
74
75     inOrder.verify(ceLogging).initForTask(task);
76     inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null);
77     inOrder.verify(ceLogging).clearForTask();
78   }
79
80   @Test
81   public void peek_and_process_task() throws Exception {
82     CeTask task = createCeTask(null);
83     taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
84     when(queue.peek()).thenReturn(Optional.of(task));
85
86     assertThat(underTest.call()).isTrue();
87
88     inOrder.verify(ceLogging).initForTask(task);
89     inOrder.verify(taskProcessor).process(task);
90     inOrder.verify(queue).remove(task, CeActivityDto.Status.SUCCESS, null);
91     inOrder.verify(ceLogging).clearForTask();
92   }
93
94   @Test
95   public void fail_to_process_task() throws Exception {
96     CeTask task = createCeTask(null);
97     when(queue.peek()).thenReturn(Optional.of(task));
98     taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
99     makeTaskProcessorFail(task);
100
101     assertThat(underTest.call()).isTrue();
102
103     inOrder.verify(ceLogging).initForTask(task);
104     inOrder.verify(taskProcessor).process(task);
105     inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null);
106     inOrder.verify(ceLogging).clearForTask();
107   }
108
109   @Test
110   public void do_not_display_submitter_param_in_log_when_submitterLogin_is_not_set_in_case_of_success() throws Exception {
111     when(queue.peek()).thenReturn(Optional.of(createCeTask(null)));
112     taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
113
114     underTest.call();
115
116     List<String> logs = logTester.logs(LoggerLevel.INFO);
117     assertThat(logs).hasSize(2);
118     for (int i = 0; i < 2; i++) {
119       assertThat(logs.get(i)).doesNotContain(" | submitter=");
120     }
121   }
122
123   @Test
124   public void do_not_display_submitter_param_in_log_when_submitterLogin_is_not_set_in_case_of_error() throws Exception {
125     CeTask ceTask = createCeTask(null);
126     when(queue.peek()).thenReturn(Optional.of(ceTask));
127     taskProcessorRepository.setProcessorForTask(ceTask.getType(), taskProcessor);
128     makeTaskProcessorFail(ceTask);
129
130     underTest.call();
131
132     List<String> logs = logTester.logs(LoggerLevel.INFO);
133     assertThat(logs).hasSize(1);
134     assertThat(logs.get(0)).doesNotContain(" | submitter=");
135     logs = logTester.logs(LoggerLevel.ERROR);
136     assertThat(logs).hasSize(2);
137     for (int i = 0; i < 2; i++) {
138       assertThat(logs.get(i)).doesNotContain(" | submitter=");
139     }
140     assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
141   }
142
143   @Test
144   public void display_submitterLogin_in_logs_when_set_in_case_of_success() throws Exception {
145     when(queue.peek()).thenReturn(Optional.of(createCeTask("FooBar")));
146     taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
147
148     underTest.call();
149
150     List<String> logs = logTester.logs(LoggerLevel.INFO);
151     assertThat(logs).hasSize(2);
152     assertThat(logs.get(0)).contains(" | submitter=FooBar");
153     assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
154     assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
155     assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
156   }
157
158   @Test
159   public void display_submitterLogin_in_logs_when_set_in_case_of_error() throws Exception {
160     CeTask ceTask = createCeTask("FooBar");
161     when(queue.peek()).thenReturn(Optional.of(ceTask));
162     taskProcessorRepository.setProcessorForTask(ceTask.getType(), taskProcessor);
163     makeTaskProcessorFail(ceTask);
164
165     underTest.call();
166
167     List<String> logs = logTester.logs(LoggerLevel.INFO);
168     assertThat(logs).hasSize(1);
169     assertThat(logs.iterator().next()).contains(" | submitter=FooBar");
170     logs = logTester.logs(LoggerLevel.ERROR);
171     assertThat(logs).hasSize(2);
172     assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid());
173     assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
174   }
175
176   @Test
177   public void display_start_stop_at_debug_level_for_console_if_DEBUG_is_enabled_and_task_successful() throws Exception {
178     logTester.setLevel(LoggerLevel.DEBUG);
179
180     when(queue.peek()).thenReturn(Optional.of(createCeTask("FooBar")));
181     taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
182
183     underTest.call();
184
185     List<String> logs = logTester.logs(LoggerLevel.INFO);
186     assertThat(logs).hasSize(2);
187     assertThat(logs.get(0)).contains(" | submitter=FooBar");
188     assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
189     assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
190     assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
191   }
192
193   @Test
194   public void display_start_at_debug_level_stop_at_error_level_for_console_if_DEBUG_is_enabled_and_task_failed() throws Exception {
195     logTester.setLevel(LoggerLevel.DEBUG);
196
197     CeTask ceTask = createCeTask("FooBar");
198     when(queue.peek()).thenReturn(Optional.of(ceTask));
199     taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
200     makeTaskProcessorFail(ceTask);
201
202     underTest.call();
203
204     List<String> logs = logTester.logs(LoggerLevel.INFO);
205     assertThat(logs).hasSize(1);
206     assertThat(logs.iterator().next()).contains(" | submitter=FooBar");
207     logs = logTester.logs(LoggerLevel.ERROR);
208     assertThat(logs).hasSize(2);
209     assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid());
210     assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
211     assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
212   }
213
214   private static CeTask createCeTask(@Nullable String submitterLogin) {
215     return new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(submitterLogin).build();
216   }
217
218   private void makeTaskProcessorFail(CeTask task) {
219     doThrow(new IllegalStateException("simulate exception thrown by TaskProcessor#process")).when(taskProcessor).process(task);
220   }
221 }