Skip to content

Commit

Permalink
SONAR-6729 Analysis should fail when no quality profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Lancelot committed Jul 28, 2015
1 parent 70c24eb commit e781c00
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.Map;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.MessageException;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.PathAwareVisitor;
import org.sonar.server.computation.component.TreeRootHolder;
Expand Down Expand Up @@ -75,6 +76,11 @@ public QProfiles createForAny(Component component) {
@Override
protected void visitProject(Component project, Path<QProfiles> path) {
addMeasure(project, path.current());
Optional<Measure> qProfileMeasure = measureRepository.getRawMeasure(project, qProfilesMetric);
if (!qProfileMeasure.isPresent() || QPMeasureData.fromJson(qProfileMeasure.get().getData()).getProfiles().isEmpty()) {
throw MessageException.of(String.format("No quality profiles has been found on project '%s', you probably don't have any language plugin suitable for this analysis.",
project.getKey()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.MessageException;
import org.sonar.server.computation.batch.TreeRootHolderRule;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.DumbComponent;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureRepositoryRule;
Expand All @@ -49,6 +52,23 @@ public class ComputeQProfileMeasureStepTest {
private static final String LANGUAGE_KEY_1 = "language_key1";
private static final String LANGUAGE_KEY_2 = "language_key2";

private static final String PROJECT_KEY = "PROJECT KEY";
private static final int PROJECT_REF = 1;
private static final int MODULE_REF = 11;
private static final int SUB_MODULE_REF = 111;

private static final Component MULTI_MODULE_PROJECT = DumbComponent.builder(PROJECT, PROJECT_REF).setKey(PROJECT_KEY)
.addChildren(
DumbComponent.builder(MODULE, MODULE_REF)
.addChildren(
DumbComponent.builder(MODULE, SUB_MODULE_REF).build()
)
.build()
).build();

@Rule
public ExpectedException thrown = ExpectedException.none();

@Rule
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();

Expand All @@ -67,32 +87,23 @@ public void setUp() throws Exception {

@Test
public void add_quality_profile_measure_on_project() throws Exception {
DumbComponent project = DumbComponent.builder(PROJECT, 1)
.addChildren(
DumbComponent.builder(MODULE, 11)
.addChildren(
DumbComponent.builder(MODULE, 111).build()
)
.build()
).build();

treeRootHolder.setRoot(project);
treeRootHolder.setRoot(MULTI_MODULE_PROJECT);

QualityProfile qp = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
addMeasure(111, qp);
addMeasure(SUB_MODULE_REF, qp);

underTest.execute();

assertThat(measureRepository.getAddedRawMeasures(1).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp));
assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp));
}

@Test
public void add_quality_profile_measure_from_multiple_modules() throws Exception {
DumbComponent project = DumbComponent.builder(PROJECT, 1)
DumbComponent project = DumbComponent.builder(PROJECT, PROJECT_REF)
.addChildren(
DumbComponent.builder(MODULE, 11)
DumbComponent.builder(MODULE, MODULE_REF)
.addChildren(
DumbComponent.builder(MODULE, 111).build()
DumbComponent.builder(MODULE, SUB_MODULE_REF).build()
)
.build(),
DumbComponent.builder(MODULE, 12).build()
Expand All @@ -101,45 +112,52 @@ public void add_quality_profile_measure_from_multiple_modules() throws Exception
treeRootHolder.setRoot(project);

QualityProfile qp1 = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
addMeasure(111, qp1);
addMeasure(SUB_MODULE_REF, qp1);
QualityProfile qp2 = createQProfile(QP_NAME_2, LANGUAGE_KEY_2);
addMeasure(12, qp2);

underTest.execute();

assertThat(measureRepository.getAddedRawMeasures(1).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp1, qp2));
assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp1, qp2));
}

@Test
public void nothing_to_add_when_no_measure() throws Exception {
DumbComponent project = DumbComponent.builder(PROJECT, 1)
.addChildren(
DumbComponent.builder(MODULE, 11)
.addChildren(
DumbComponent.builder(MODULE, 111).build()
)
.build()
).build();
public void nothing_to_add_when_measure_already_exists_on_project() throws Exception {
DumbComponent project = DumbComponent.builder(PROJECT, PROJECT_REF).build();

treeRootHolder.setRoot(project);

QualityProfile qp = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
addMeasure(PROJECT_REF, qp);

underTest.execute();

assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty();
assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
}

@Test
public void nothing_to_add_when_measure_already_exists_on_project() throws Exception {
DumbComponent project = DumbComponent.builder(PROJECT, 1).build();
public void fail_with_message_exception_when_no_qprofile_computed_on_project() throws Exception {
thrown.expect(MessageException.class);
thrown.expectMessage("No quality profiles has been found on project 'PROJECT KEY'");

treeRootHolder.setRoot(project);
treeRootHolder.setRoot(MULTI_MODULE_PROJECT);

QualityProfile qp = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
addMeasure(1, qp);
underTest.execute();

assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
}

@Test
public void fail_with_message_exception_when_qprofiles_computed_on_project_are_empty() throws Exception {
thrown.expect(MessageException.class);
thrown.expectMessage("No quality profiles has been found on project 'PROJECT KEY', you probably don't have any language plugin suitable for this analysis.");

treeRootHolder.setRoot(MULTI_MODULE_PROJECT);
measureRepository.addRawMeasure(PROJECT_REF, QUALITY_PROFILES_KEY, newMeasureBuilder().create(toJson()));

underTest.execute();

assertThat(measureRepository.getAddedRawMeasures(1)).isEmpty();
assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
}

private static QualityProfile createQProfile(String qpName, String languageKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
*/
package org.sonar.batch.repository;

import org.sonar.batch.bootstrap.GlobalMode;

import org.sonar.batch.util.BatchUtils;
import org.sonar.batch.bootstrap.WSLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.AnalysisProperties;
import org.sonar.batch.bootstrap.GlobalMode;
import org.sonar.batch.bootstrap.WSLoader;
import org.sonar.batch.protocol.input.ProjectRepositories;
import org.sonar.batch.rule.ModuleQProfiles;
import org.sonar.batch.util.BatchUtils;

public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {

Expand All @@ -54,7 +54,15 @@ public ProjectRepositories load(ProjectReactor reactor, AnalysisProperties taskP
url += "&profile=" + BatchUtils.encodeForUrl(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP));
}
url += "&preview=" + globalMode.isPreview();
return ProjectRepositories.fromJson(wsLoader.loadString(url));
ProjectRepositories projectRepositories = ProjectRepositories.fromJson(wsLoader.loadString(url));
validateProjectRepositories(projectRepositories, reactor.getRoot().getKey());
return projectRepositories;
}

private static void validateProjectRepositories(ProjectRepositories projectRepositories, String projectKey) {
if (projectRepositories.qProfiles().isEmpty()) {
throw MessageException.of("No quality profiles has been found this project, you probably don't have any language plugin suitable for this analysis.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
*/
package org.sonar.batch.repository;

import org.sonar.batch.bootstrap.GlobalMode;

import org.sonar.batch.bootstrap.WSLoader;
import com.google.common.collect.Maps;
import java.util.Date;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.AnalysisProperties;
import org.sonar.batch.bootstrap.GlobalMode;
import org.sonar.batch.bootstrap.WSLoader;
import org.sonar.batch.protocol.input.ProjectRepositories;
import org.sonar.batch.protocol.input.QProfile;
import org.sonar.batch.rule.ModuleQProfiles;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand All @@ -37,6 +43,9 @@

public class DefaultProjectRepositoriesLoaderTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

private DefaultProjectRepositoriesLoader loader;
private WSLoader wsLoader;
private GlobalMode globalMode;
Expand All @@ -55,6 +64,7 @@ public void prepare() {

@Test
public void passPreviewParameter() {
addQualityProfile();
reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo"));
when(globalMode.isPreview()).thenReturn(false);
loader.load(reactor, taskProperties);
Expand All @@ -67,17 +77,36 @@ public void passPreviewParameter() {

@Test
public void passAndEncodeProjectKeyParameter() {
addQualityProfile();
reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo bàr"));
loader.load(reactor, taskProperties);
verify(wsLoader).loadString("/batch/project?key=foo+b%C3%A0r&preview=false");
}

@Test
public void passAndEncodeProfileParameter() {
addQualityProfile();
reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo"));
taskProperties.properties().put(ModuleQProfiles.SONAR_PROFILE_PROP, "my-profile#2");
loader.load(reactor, taskProperties);
verify(wsLoader).loadString("/batch/project?key=foo&profile=my-profile%232&preview=false");
}

@Test
public void fail_with_message_exception_when_no_quality_profile() throws Exception {
thrown.expect(MessageException.class);
thrown.expectMessage("No quality profiles has been found this project, you probably don't have any language plugin suitable for this analysis.");

reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo"));
when(wsLoader.loadString(anyString())).thenReturn(new ProjectRepositories().toJson());

loader.load(reactor, taskProperties);
}

private void addQualityProfile(){
ProjectRepositories projectRepositories = new ProjectRepositories();
projectRepositories.addQProfile(new QProfile("key", "name", "language", new Date()));
when(wsLoader.loadString(anyString())).thenReturn(projectRepositories.toJson());
}

}

0 comments on commit e781c00

Please sign in to comment.