]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7532 display submitter login in logs when available 937/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 9 May 2016 12:40:24 +0000 (14:40 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 10 May 2016 08:11:42 +0000 (10:11 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java

index 19f19cc2e1d5dc6b0a1de1afda482d21a3bfe958..9d76669a7e7ba684e2b964382188a45826b446ab 100644 (file)
@@ -96,14 +96,25 @@ public class CeWorkerCallableImpl implements CeWorkerCallable {
   }
 
   private static Profiler startProfiler(CeTask task) {
-    return Profiler.create(LOG).startInfo("Execute task | project={} | type={} | id={}", task.getComponentKey(), task.getType(), task.getUuid());
+    Profiler profiler = Profiler.create(LOG);
+    return profiler.startInfo("Execute task | project={} | type={} | id={} | submitter='{}'",
+      task.getComponentKey(), task.getType(), task.getUuid(), getSubmitterLoginForLog(task));
   }
 
   private static void stopProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) {
     if (status == CeActivityDto.Status.FAILED) {
-      profiler.stopError("Executed task | project={} | type={} | id={}", task.getComponentKey(), task.getType(), task.getUuid());
+      profiler.stopError("Executed task | project={} | type={} | id={} | submitter='{}'",
+          task.getComponentKey(), task.getType(), task.getUuid(), getSubmitterLoginForLog(task));
     } else {
-      profiler.stopInfo("Executed task | project={} | type={} | id={}", task.getComponentKey(), task.getType(), task.getUuid());
+      profiler.stopInfo("Executed task | project={} | type={} | id={} | submitter='{}'",
+          task.getComponentKey(), task.getType(), task.getUuid(), getSubmitterLoginForLog(task));
     }
   }
+
+  private static String getSubmitterLoginForLog(CeTask task) {
+    if (task.getSubmitterLogin() == null) {
+      return "";
+    }
+    return task.getSubmitterLogin();
+  }
 }
index a4648392c1378ddda45fe6113ffbb1d5583a70c2..520f4fc60d432bc7dc3768bab9fb0ef51d8a43ac 100644 (file)
 package org.sonar.server.computation.taskprocessor;
 
 import com.google.common.base.Optional;
+import java.util.List;
 import org.junit.Rule;
 import org.junit.Test;
 import org.mockito.InOrder;
 import org.mockito.Mockito;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.ce.log.CeLogging;
 import org.sonar.ce.queue.CeTask;
 import org.sonar.db.ce.CeActivityDto;
@@ -41,6 +44,8 @@ public class CeWorkerCallableImplTest {
 
   @Rule
   public CeTaskProcessorRepositoryRule taskProcessorRepository = new CeTaskProcessorRepositoryRule();
+  @Rule
+  public LogTester logTester = new LogTester();
 
   InternalCeQueue queue = mock(InternalCeQueue.class);
   ReportTaskProcessor taskProcessor = mock(ReportTaskProcessor.class);
@@ -98,4 +103,34 @@ public class CeWorkerCallableImplTest {
     inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null);
     inOrder.verify(ceLogging).clearForTask();
   }
+
+  @Test
+  public void do_not_display_null_in_log_when_submitterLogin_is_not_set() throws Exception {
+    CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin(null).build();
+    when(queue.peek()).thenReturn(Optional.of(task));
+    taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
+
+    underTest.call();
+
+    List<String> logs = logTester.logs(LoggerLevel.INFO);
+    assertThat(logs).hasSize(4);
+    for (int i = 0; i < 4; i++) {
+      assertThat(logs.get(i)).contains(" | submitter=''");
+    }
+  }
+
+  @Test
+  public void display_submitterLogin_in_logs_when_set() throws Exception {
+    CeTask task = new CeTask.Builder().setUuid("TASK_1").setType(CeTaskTypes.REPORT).setComponentUuid("PROJECT_1").setSubmitterLogin("FooBar").build();
+    when(queue.peek()).thenReturn(Optional.of(task));
+    taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
+
+    underTest.call();
+
+    List<String> logs = logTester.logs(LoggerLevel.INFO);
+    assertThat(logs).hasSize(4);
+    for (int i = 0; i < 4; i++) {
+      assertThat(logs.get(i)).contains(" | submitter='FooBar'");
+    }
+  }
 }