Skip to content

Commit

Permalink
SONAR-7166 add explicit error when CeTask.componentKey is null
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Feb 4, 2016
1 parent d231654 commit b9ec130
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
Expand Up @@ -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.
Expand Down Expand Up @@ -55,11 +56,19 @@ public void execute() {

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) {
Expand Down
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -134,13 +135,27 @@ public void set_cross_project_duplication_to_false_when_nothing_in_the_report()
}

@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();
Expand Down

0 comments on commit b9ec130

Please sign in to comment.