*/
package org.sonar.ce.task.projectanalysis.measure;
-import org.sonar.api.ce.ComputeEngineSide;
+import org.sonar.api.utils.System2;
+import org.sonar.ce.task.log.CeTaskMessages;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.ce.task.step.ComputationStep;
+import org.sonar.db.dismissmessage.MessageType;
+
+import static org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.PreMeasuresComputationCheckException;
/**
* Execute {@link PreMeasuresComputationCheck} instances in no specific order.
* If an extension fails (throws an exception), consecutive extensions
* won't be called.
*/
-@ComputeEngineSide
public class PreMeasuresComputationChecksStep implements ComputationStep {
private final AnalysisMetadataHolder analysisMetadataHolder;
private final PreMeasuresComputationCheck[] extensions;
+ private final CeTaskMessages ceTaskMessages;
+
- public PreMeasuresComputationChecksStep(AnalysisMetadataHolder analysisMetadataHolder, PreMeasuresComputationCheck... extensions) {
+ public PreMeasuresComputationChecksStep(AnalysisMetadataHolder analysisMetadataHolder, CeTaskMessages ceTaskMessages, PreMeasuresComputationCheck... extensions) {
this.analysisMetadataHolder = analysisMetadataHolder;
+ this.ceTaskMessages = ceTaskMessages;
this.extensions = extensions;
}
public void execute(Context context) {
PreMeasuresComputationCheck.Context extensionContext = new ContextImpl();
for (PreMeasuresComputationCheck extension : extensions) {
- extension.onCheck(extensionContext);
+ try {
+ extension.onCheck(extensionContext);
+ } catch (PreMeasuresComputationCheckException pmcce) {
+ ceTaskMessages.add(new CeTaskMessages.Message(pmcce.getMessage(), System2.INSTANCE.now(), MessageType.GENERIC));
+ }
}
}
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
+import org.sonar.ce.task.log.CeTaskMessages;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.Context;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.sonar.ce.task.projectanalysis.measure.PreMeasuresComputationCheck.PreMeasuresComputationCheckException;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
public class PreMeasuresComputationChecksStepTest {
- public AnalysisMetadataHolderRule analysisMetadataHolder = mock(AnalysisMetadataHolderRule.class);
+ public AnalysisMetadataHolderRule analysisMetadataHolder = mock();
+ public CeTaskMessages ceTaskMessages = mock();
@Test
- public void execute_extensions() {
+ public void execute_extensions() throws PreMeasuresComputationCheckException {
PreMeasuresComputationCheck check1 = mock(PreMeasuresComputationCheck.class);
PreMeasuresComputationCheck check2 = mock(PreMeasuresComputationCheck.class);
}
@Test
- public void context_contains_project_uuid_from_analysis_metadata_holder() {
+ public void context_contains_project_uuid_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
Project project = Project.from(newPrivateProjectDto());
when(analysisMetadataHolder.getProject()).thenReturn(project);
PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
}
@Test
- public void context_contains_pullRequest_key_from_analysis_metadata_holder() {
+ public void context_contains_pullRequest_key_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
mockPr("pr1");
PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
}
@Test
- public void context_contains_branch_from_analysis_metadata_holder() {
+ public void context_contains_branch_from_analysis_metadata_holder() throws PreMeasuresComputationCheckException {
mockBranch("branchName");
PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
assertThat(contextArgumentCaptor.getValue().getBranch().getName()).isEqualTo("branchName");
}
+ @Test
+ public void whenCheckThrows_thenLogCeMessage() throws PreMeasuresComputationCheckException {
+ PreMeasuresComputationCheck check = mock(PreMeasuresComputationCheck.class);
+ doThrow(new PreMeasuresComputationCheckException("error"))
+ .when(check).onCheck(any());
+
+ newStep(check).execute(new TestComputationStepContext());
+
+ var messageCaptor = ArgumentCaptor.forClass(CeTaskMessages.Message.class);
+ verify(ceTaskMessages).add(messageCaptor.capture());
+ assertThat(messageCaptor.getValue().getText()).isEqualTo("error");
+ }
+
@Test
public void test_getDescription() {
assertThat(newStep().getDescription()).isNotEmpty();
private PreMeasuresComputationChecksStep newStep(PreMeasuresComputationCheck... preMeasuresComputationChecks) {
if (preMeasuresComputationChecks.length == 0) {
- return new PreMeasuresComputationChecksStep(analysisMetadataHolder);
+ return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages);
}
- return new PreMeasuresComputationChecksStep(analysisMetadataHolder, preMeasuresComputationChecks);
+ return new PreMeasuresComputationChecksStep(analysisMetadataHolder, ceTaskMessages, preMeasuresComputationChecks);
}
private void mockBranch(String branchName) {