From e3d3e9943d309e602da7475dc830956e8b5b976c Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 9 May 2016 14:40:24 +0200 Subject: [PATCH] SONAR-7532 display submitter login in logs when available --- .../taskprocessor/CeWorkerCallableImpl.java | 17 +++++++-- .../CeWorkerCallableImplTest.java | 35 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java index 19f19cc2e1d..9d76669a7e7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImpl.java @@ -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(); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java index a4648392c13..520f4fc60d4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/taskprocessor/CeWorkerCallableImplTest.java @@ -20,10 +20,13 @@ 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 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 logs = logTester.logs(LoggerLevel.INFO); + assertThat(logs).hasSize(4); + for (int i = 0; i < 4; i++) { + assertThat(logs.get(i)).contains(" | submitter='FooBar'"); + } + } } -- 2.39.5