Skip to content

Commit

Permalink
add ProjectAnalysisRule + use it in issues package
Browse files Browse the repository at this point in the history
rule avoid code factoring by static code + remove code duplication + add auto reset features
also fixed licence headers in issues package
  • Loading branch information
sns-seb authored and julienlancelot committed Oct 23, 2015
1 parent 48f46a2 commit b9fef5c
Show file tree
Hide file tree
Showing 15 changed files with 788 additions and 154 deletions.
22 changes: 18 additions & 4 deletions it/it-tests/src/test/java/issue/suite/CommonRulesTest.java
@@ -1,7 +1,21 @@
/* /*
* Copyright (C) 2009-2014 SonarSource SA * SonarQube, open source software quality management tool.
* All rights reserved * Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com * mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
package issue.suite; package issue.suite;


Expand Down Expand Up @@ -33,8 +47,8 @@ public static void setUp() {
orchestrator.getServer().provisionProject("common-rules-project", "Sample"); orchestrator.getServer().provisionProject("common-rules-project", "Sample");
orchestrator.getServer().associateProjectToQualityProfile("common-rules-project", "xoo", "xoo-common-rules"); orchestrator.getServer().associateProjectToQualityProfile("common-rules-project", "xoo", "xoo-common-rules");
runProjectAnalysis(orchestrator, "issue/common-rules", runProjectAnalysis(orchestrator, "issue/common-rules",
"sonar.cpd.xoo.minimumTokens", "2", "sonar.cpd.xoo.minimumTokens", "2",
"sonar.cpd.xoo.minimumLines", "2"); "sonar.cpd.xoo.minimumLines", "2");
} }


@Test @Test
Expand Down
42 changes: 29 additions & 13 deletions it/it-tests/src/test/java/issue/suite/CustomRulesTest.java
@@ -1,35 +1,56 @@
/* /*
* Copyright (C) 2009-2014 SonarSource SA * SonarQube, open source software quality management tool.
* All rights reserved * Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com * mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
package issue.suite; package issue.suite;


import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.locator.FileLocation;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.sonar.wsclient.issue.Issue; import org.sonar.wsclient.issue.Issue;
import util.ProjectAnalysis;
import util.ProjectAnalysisRule;


import static issue.suite.IssueTestSuite.searchIssues; import static issue.suite.IssueTestSuite.searchIssues;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.runProjectAnalysis;


public class CustomRulesTest { public class CustomRulesTest {


@ClassRule @ClassRule
public static Orchestrator orchestrator = IssueTestSuite.ORCHESTRATOR; public static Orchestrator orchestrator = IssueTestSuite.ORCHESTRATOR;
@Rule
public final ProjectAnalysisRule projectAnalysisRule = ProjectAnalysisRule.from(orchestrator);

private ProjectAnalysis xooSampleAnalysis;


@Before @Before
public void deleteData() { public void setup() {
orchestrator.resetData(); String profileKey = projectAnalysisRule.registerProfile("/issue/suite/CustomRulesTest/custom.xml");
String projectKey = projectAnalysisRule.registerProject("shared/xoo-sample");
this.xooSampleAnalysis = projectAnalysisRule.newProjectAnalysis(projectKey)
.withQualityProfile(profileKey);
} }


@Test @Test
public void analyzeProjectWithCustomRules() throws Exception { public void analyzeProjectWithCustomRules() throws Exception {

orchestrator.getServer().adminWsClient().post("api/rules/create", orchestrator.getServer().adminWsClient().post("api/rules/create",
"template_key", "xoo:TemplateRule", "template_key", "xoo:TemplateRule",
"custom_key", "MyCustomRule", "custom_key", "MyCustomRule",
Expand All @@ -38,12 +59,7 @@ public void analyzeProjectWithCustomRules() throws Exception {
"severity", "BLOCKER", "severity", "BLOCKER",
"params", "line=2"); "params", "line=2");


orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/CustomRulesTest/custom.xml")); xooSampleAnalysis.run();

orchestrator.getServer().provisionProject("sample", "Sample");
orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "Custom");

runProjectAnalysis(orchestrator, "shared/xoo-sample");


List<Issue> issues = searchIssues(); List<Issue> issues = searchIssues();
assertThat(issues).hasSize(1); assertThat(issues).hasSize(1);
Expand Down
51 changes: 26 additions & 25 deletions it/it-tests/src/test/java/issue/suite/IssueActionTest.java
Expand Up @@ -20,11 +20,10 @@
package issue.suite; package issue.suite;


import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarRunner;
import com.sonar.orchestrator.locator.FileLocation;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.sonar.wsclient.base.HttpException; import org.sonar.wsclient.base.HttpException;
import org.sonar.wsclient.issue.ActionPlan; import org.sonar.wsclient.issue.ActionPlan;
Expand All @@ -34,6 +33,8 @@
import org.sonar.wsclient.issue.IssueQuery; import org.sonar.wsclient.issue.IssueQuery;
import org.sonar.wsclient.issue.Issues; import org.sonar.wsclient.issue.Issues;
import org.sonar.wsclient.issue.NewActionPlan; import org.sonar.wsclient.issue.NewActionPlan;
import util.ProjectAnalysis;
import util.ProjectAnalysisRule;


import static issue.suite.IssueTestSuite.ORCHESTRATOR; import static issue.suite.IssueTestSuite.ORCHESTRATOR;
import static issue.suite.IssueTestSuite.adminIssueClient; import static issue.suite.IssueTestSuite.adminIssueClient;
Expand All @@ -43,35 +44,27 @@
import static issue.suite.IssueTestSuite.searchRandomIssue; import static issue.suite.IssueTestSuite.searchRandomIssue;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static util.ItUtils.runProjectAnalysis;
import static util.ItUtils.toDate; import static util.ItUtils.toDate;
import static util.ItUtils.verifyHttpException; import static util.ItUtils.verifyHttpException;


public class IssueActionTest { public class IssueActionTest {


@ClassRule @ClassRule
public static Orchestrator orchestrator = ORCHESTRATOR; public static Orchestrator orchestrator = ORCHESTRATOR;
@Rule
public final ProjectAnalysisRule projectAnalysisRule = ProjectAnalysisRule.from(orchestrator);


Issue issue; Issue issue;
SonarRunner scan; ProjectAnalysis projectAnalysis;

private static List<Issue> searchIssuesBySeverities(String componentKey, String... severities) {
return searchIssues(IssueQuery.create().componentRoots(componentKey).severities(severities));
}

private static ActionPlanClient adminActionPlanClient() {
return orchestrator.getServer().adminWsClient().actionPlanClient();
}


@Before @Before
public void resetData() { public void setup() {
orchestrator.resetData(); String qualityProfileKey = projectAnalysisRule.registerProfile("/issue/suite/IssueActionTest/xoo-one-issue-per-line-profile.xml");
orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/IssueActionTest/xoo-one-issue-per-line-profile.xml")); String projectKey = projectAnalysisRule.registerProject("shared/xoo-sample");
orchestrator.getServer().provisionProject("sample", "Sample");
orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "xoo-one-issue-per-line-profile"); this.projectAnalysis = projectAnalysisRule.newProjectAnalysis(projectKey).withQualityProfile(qualityProfileKey);

this.projectAnalysis.run();
scan = runProjectAnalysis(orchestrator, "shared/xoo-sample"); this.issue = searchRandomIssue();
issue = searchRandomIssue();
} }


@Test @Test
Expand Down Expand Up @@ -127,7 +120,7 @@ public void change_severity() {


assertThat(searchIssuesBySeverities(componentKey, "BLOCKER")).hasSize(1); assertThat(searchIssuesBySeverities(componentKey, "BLOCKER")).hasSize(1);


orchestrator.executeBuild(scan); projectAnalysis.run();
Issue reloaded = searchIssueByKey(issue.key()); Issue reloaded = searchIssueByKey(issue.key());
assertThat(reloaded.severity()).isEqualTo("BLOCKER"); assertThat(reloaded.severity()).isEqualTo("BLOCKER");
assertThat(reloaded.status()).isEqualTo("OPEN"); assertThat(reloaded.status()).isEqualTo("OPEN");
Expand All @@ -148,7 +141,7 @@ public void assign() {
adminIssueClient().assign(issue.key(), "admin"); adminIssueClient().assign(issue.key(), "admin");
assertThat(searchIssues(IssueQuery.create().assignees("admin"))).hasSize(1); assertThat(searchIssues(IssueQuery.create().assignees("admin"))).hasSize(1);


orchestrator.executeBuild(scan); projectAnalysis.run();
Issue reloaded = searchIssueByKey(issue.key()); Issue reloaded = searchIssueByKey(issue.key());
assertThat(reloaded.assignee()).isEqualTo("admin"); assertThat(reloaded.assignee()).isEqualTo("admin");
assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate()); assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate());
Expand Down Expand Up @@ -192,7 +185,7 @@ public void plan() {
adminIssueClient().plan(issue.key(), newActionPlan.key()); adminIssueClient().plan(issue.key(), newActionPlan.key());
assertThat(search(IssueQuery.create().actionPlans(newActionPlan.key())).list()).hasSize(1); assertThat(search(IssueQuery.create().actionPlans(newActionPlan.key())).list()).hasSize(1);


orchestrator.executeBuild(scan); projectAnalysis.run();
Issue reloaded = searchIssueByKey(issue.key()); Issue reloaded = searchIssueByKey(issue.key());
assertThat(reloaded.actionPlan()).isEqualTo(newActionPlan.key()); assertThat(reloaded.actionPlan()).isEqualTo(newActionPlan.key());
assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate()); assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate());
Expand Down Expand Up @@ -227,7 +220,7 @@ public void unplan() {
adminIssueClient().plan(issue.key(), null); adminIssueClient().plan(issue.key(), null);
assertThat(search(IssueQuery.create().actionPlans(newActionPlan.key())).list()).hasSize(0); assertThat(search(IssueQuery.create().actionPlans(newActionPlan.key())).list()).hasSize(0);


orchestrator.executeBuild(scan); projectAnalysis.run();
Issue reloaded = searchIssueByKey(issue.key()); Issue reloaded = searchIssueByKey(issue.key());
assertThat(reloaded.actionPlan()).isNull(); assertThat(reloaded.actionPlan()).isNull();
assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate()); assertThat(reloaded.creationDate()).isEqualTo(issue.creationDate());
Expand Down Expand Up @@ -264,10 +257,18 @@ public void issue_attribute_are_kept_on_new_analysis() {
adminIssueClient().doAction(issue.key(), "fake"); adminIssueClient().doAction(issue.key(), "fake");
assertThat(adminIssueClient().actions(issue.key())).doesNotContain("fake"); assertThat(adminIssueClient().actions(issue.key())).doesNotContain("fake");


orchestrator.executeBuild(scan); projectAnalysis.run();


// Fake action is no more available if the issue attribute is still there // Fake action is no more available if the issue attribute is still there
assertThat(adminIssueClient().actions(issue.key())).doesNotContain("fake"); assertThat(adminIssueClient().actions(issue.key())).doesNotContain("fake");
} }


private static List<Issue> searchIssuesBySeverities(String componentKey, String... severities) {
return searchIssues(IssueQuery.create().componentRoots(componentKey).severities(severities));
}

private static ActionPlanClient adminActionPlanClient() {
return orchestrator.getServer().adminWsClient().actionPlanClient();
}

} }
43 changes: 22 additions & 21 deletions it/it-tests/src/test/java/issue/suite/IssueBulkChangeTest.java
Expand Up @@ -22,10 +22,10 @@
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.locator.FileLocation;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.sonar.wsclient.base.HttpException; import org.sonar.wsclient.base.HttpException;
import org.sonar.wsclient.issue.ActionPlan; import org.sonar.wsclient.issue.ActionPlan;
Expand All @@ -35,12 +35,13 @@
import org.sonar.wsclient.issue.Issue; import org.sonar.wsclient.issue.Issue;
import org.sonar.wsclient.issue.NewActionPlan; import org.sonar.wsclient.issue.NewActionPlan;
import util.ItUtils; import util.ItUtils;
import util.ProjectAnalysis;
import util.ProjectAnalysisRule;


import static issue.suite.IssueTestSuite.ORCHESTRATOR; import static issue.suite.IssueTestSuite.ORCHESTRATOR;
import static issue.suite.IssueTestSuite.adminIssueClient; import static issue.suite.IssueTestSuite.adminIssueClient;
import static issue.suite.IssueTestSuite.issueClient; import static issue.suite.IssueTestSuite.issueClient;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.runProjectAnalysis;


/** /**
* SONAR-4421 * SONAR-4421
Expand All @@ -53,15 +54,22 @@ public class IssueBulkChangeTest {


@ClassRule @ClassRule
public static Orchestrator orchestrator = ORCHESTRATOR; public static Orchestrator orchestrator = ORCHESTRATOR;
@Rule
public final ProjectAnalysisRule projectAnalysisRule = ProjectAnalysisRule.from(orchestrator);

private ProjectAnalysis xooSampleLittleIssuesAnalysis;


@Before @Before
public void resetData() { public void setUp() throws Exception {
orchestrator.resetData(); String qualityProfileKey = projectAnalysisRule.registerProfile("/issue/suite/IssueBulkChangeTest/one-issue-per-line-profile.xml");
String projectKey = projectAnalysisRule.registerProject("shared/xoo-sample");
this.xooSampleLittleIssuesAnalysis = projectAnalysisRule.newProjectAnalysis(projectKey)
.withQualityProfile(qualityProfileKey);
} }


@Test @Test
public void should_change_severity() { public void should_change_severity() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


String newSeverity = "BLOCKER"; String newSeverity = "BLOCKER";
String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT); String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT);
Expand All @@ -73,7 +81,7 @@ public void should_change_severity() {


@Test @Test
public void should_do_transition() { public void should_do_transition() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();
String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT); String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT);
BulkChange bulkChange = bulkTransitionStatusOfIssues(issueKeys, "confirm"); BulkChange bulkChange = bulkTransitionStatusOfIssues(issueKeys, "confirm");


Expand All @@ -83,7 +91,7 @@ public void should_do_transition() {


@Test @Test
public void should_assign() { public void should_assign() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT); String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT);
BulkChange bulkChange = buldChangeAssigneeOfIssues(issueKeys, "admin"); BulkChange bulkChange = buldChangeAssigneeOfIssues(issueKeys, "admin");
Expand All @@ -96,7 +104,7 @@ public void should_assign() {


@Test @Test
public void should_plan() { public void should_plan() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


// Create action plan // Create action plan
ActionPlan newActionPlan = adminActionPlanClient().create( ActionPlan newActionPlan = adminActionPlanClient().create(
Expand All @@ -118,7 +126,7 @@ public void should_plan() {


@Test @Test
public void should_setSeverity_add_comment_in_single_WS_call() { public void should_setSeverity_add_comment_in_single_WS_call() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


String newSeverity = "BLOCKER"; String newSeverity = "BLOCKER";
String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT); String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT);
Expand All @@ -140,7 +148,7 @@ public void should_setSeverity_add_comment_in_single_WS_call() {


@Test @Test
public void should_apply_bulk_change_on_many_actions() { public void should_apply_bulk_change_on_many_actions() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


String newSeverity = "BLOCKER"; String newSeverity = "BLOCKER";
String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT); String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT);
Expand All @@ -167,7 +175,7 @@ public void should_apply_bulk_change_on_many_actions() {


@Test @Test
public void should_not_apply_bulk_change_if_not_logged() { public void should_not_apply_bulk_change_if_not_logged() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


String newSeverity = "BLOCKER"; String newSeverity = "BLOCKER";
String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT); String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT);
Expand All @@ -181,7 +189,7 @@ public void should_not_apply_bulk_change_if_not_logged() {


@Test @Test
public void should_not_apply_bulk_change_if_no_change_to_do() { public void should_not_apply_bulk_change_if_no_change_to_do() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


String newSeverity = "BLOCKER"; String newSeverity = "BLOCKER";
String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT); String[] issueKeys = searchIssueKeys(BULK_EDITED_ISSUE_COUNT);
Expand All @@ -207,7 +215,7 @@ public void should_not_apply_bulk_change_if_no_issue_selected() {


@Test @Test
public void should_not_apply_bulk_change_if_action_is_invalid() { public void should_not_apply_bulk_change_if_action_is_invalid() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();


int limit = BULK_EDITED_ISSUE_COUNT; int limit = BULK_EDITED_ISSUE_COUNT;
String[] issueKeys = searchIssueKeys(limit); String[] issueKeys = searchIssueKeys(limit);
Expand All @@ -222,7 +230,7 @@ public void should_not_apply_bulk_change_if_action_is_invalid() {


@Test @Test
public void should_add_comment_only_on_issues_that_will_be_changed() { public void should_add_comment_only_on_issues_that_will_be_changed() {
analyzeSampleProjectWillSmallNumberOfIssues(); xooSampleLittleIssuesAnalysis.run();
int nbIssues = BULK_EDITED_ISSUE_COUNT; int nbIssues = BULK_EDITED_ISSUE_COUNT;
String[] issueKeys = searchIssueKeys(nbIssues); String[] issueKeys = searchIssueKeys(nbIssues);


Expand All @@ -249,13 +257,6 @@ public void should_add_comment_only_on_issues_that_will_be_changed() {
assertThat(nbIssuesWithComment).isEqualTo(1); assertThat(nbIssuesWithComment).isEqualTo(1);
} }


private void analyzeSampleProjectWillSmallNumberOfIssues() {
orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/IssueBulkChangeTest/one-issue-per-line-profile.xml"));
orchestrator.getServer().provisionProject("sample", "Sample");
orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
runProjectAnalysis(orchestrator, "shared/xoo-sample");
}

private static void assertIssueSeverity(String[] issueKeys, String expectedSeverity) { private static void assertIssueSeverity(String[] issueKeys, String expectedSeverity) {
for (Issue issue : IssueTestSuite.searchIssues(issueKeys)) { for (Issue issue : IssueTestSuite.searchIssues(issueKeys)) {
assertThat(issue.severity()).isEqualTo(expectedSeverity); assertThat(issue.severity()).isEqualTo(expectedSeverity);
Expand Down

0 comments on commit b9fef5c

Please sign in to comment.