From b9ec130f779be67f33316fd47fb6987f02c48d2a Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 2 Feb 2016 12:18:16 +0100 Subject: [PATCH] SONAR-7166 add explicit error when CeTask.componentKey is null --- .../LoadReportAnalysisMetadataHolderStep.java | 21 +++++++++++----- ...dReportAnalysisMetadataHolderStepTest.java | 25 +++++++++++++++---- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java index f602a4e027b..200e540a400 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java @@ -19,12 +19,13 @@ */ package org.sonar.server.computation.step; +import org.sonar.api.utils.MessageException; import org.sonar.batch.protocol.output.BatchReport; import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolder; import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.computation.queue.CeTask; -import static com.google.common.base.Preconditions.checkState; +import static java.lang.String.format; /** * Feed analysis metadata holder with metadata from the analysis report. @@ -55,11 +56,19 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep { private void checkProjectKeyConsistency(BatchReport.Metadata reportMetadata) { String reportProjectKey = projectKeyFromReport(reportMetadata); - checkState( - ceTask.getComponentKey().equals(reportProjectKey), - "ProjectKey in report (%s) is not consistent with projectKey under which the report as been submitted (%s)", - reportProjectKey, - ceTask.getComponentKey()); + if (ceTask.getComponentKey() == null) { + throw MessageException.of(format( + "Compute Engine task component key is null. Project with UUID %s must have been deleted since report was uploaded. Can not proceed.", + ceTask.getComponentUuid() + )); + } + if (!ceTask.getComponentKey().equals(reportProjectKey)) { + throw MessageException.of(format( + "ProjectKey in report (%s) is not consistent with projectKey under which the report as been submitted (%s)", + reportProjectKey, + ceTask.getComponentKey() + )); + } } private static String projectKeyFromReport(BatchReport.Metadata reportMetadata) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStepTest.java index 762c507f09f..2ae20cd8289 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStepTest.java @@ -22,6 +22,7 @@ package org.sonar.server.computation.step; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonar.api.utils.MessageException; import org.sonar.batch.protocol.output.BatchReport; import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule; import org.sonar.server.computation.batch.BatchReportReaderRule; @@ -134,13 +135,27 @@ public class LoadReportAnalysisMetadataHolderStepTest { } @Test - public void execute_fails_with_ISE_when_projectKey_in_report_is_different_from_componentKey_in_CE_task() { + public void execute_fails_with_MessageException_if_projectKey_is_null_in_CE_task() { + CeTask res = mock(CeTask.class); + when(res.getComponentUuid()).thenReturn("prj_uuid"); + reportReader.setMetadata(BatchReport.Metadata.newBuilder().build()); + + ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(res, reportReader, analysisMetadataHolder); + + expectedException.expect(MessageException.class); + expectedException.expectMessage("Compute Engine task component key is null. Project with UUID prj_uuid must have been deleted since report was uploaded. Can not proceed."); + + underTest.execute(); + } + + @Test + public void execute_fails_with_MessageException_when_projectKey_in_report_is_different_from_componentKey_in_CE_task() { reportReader.setMetadata( - BatchReport.Metadata.newBuilder() - .setProjectKey("some other key") - .build()); + BatchReport.Metadata.newBuilder() + .setProjectKey("some other key") + .build()); - expectedException.expect(IllegalStateException.class); + expectedException.expect(MessageException.class); expectedException.expectMessage("ProjectKey in report (some other key) is not consistent with projectKey under which the report as been submitted (" + PROJECT_KEY + ")"); underTest.execute(); -- 2.39.5