Skip to content

Commit

Permalink
SONAR-10543 Add ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses authored and SonarTech committed Apr 26, 2018
1 parent 7dd669b commit ec13da4
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 6 deletions.
Expand Up @@ -143,6 +143,7 @@ public SearchResponse search(SearchRequest request) {
.setParam("facets", request.getFacets() == null ? null : request.getFacets().stream().collect(Collectors.joining(","))) .setParam("facets", request.getFacets() == null ? null : request.getFacets().stream().collect(Collectors.joining(",")))
.setParam("inheritance", request.getInheritance() == null ? null : request.getInheritance().stream().collect(Collectors.joining(","))) .setParam("inheritance", request.getInheritance() == null ? null : request.getInheritance().stream().collect(Collectors.joining(",")))
.setParam("is_template", request.getIsTemplate()) .setParam("is_template", request.getIsTemplate())
.setParam("is_external", request.getIsExternal())
.setParam("languages", request.getLanguages() == null ? null : request.getLanguages().stream().collect(Collectors.joining(","))) .setParam("languages", request.getLanguages() == null ? null : request.getLanguages().stream().collect(Collectors.joining(",")))
.setParam("organization", request.getOrganization()) .setParam("organization", request.getOrganization())
.setParam("p", request.getP()) .setParam("p", request.getP())
Expand Down
Expand Up @@ -39,6 +39,7 @@ public class SearchRequest {
private List<String> f; private List<String> f;
private List<String> facets; private List<String> facets;
private List<String> inheritance; private List<String> inheritance;
private String isExternal;
private String isTemplate; private String isTemplate;
private List<String> languages; private List<String> languages;
private String organization; private String organization;
Expand Down Expand Up @@ -237,6 +238,24 @@ public SearchRequest setIsTemplate(String isTemplate) {
public String getIsTemplate() { public String getIsTemplate() {
return isTemplate; return isTemplate;
} }

/**
* Possible values:
* <ul>
* <li>"true"</li>
* <li>"false"</li>
* <li>"yes"</li>
* <li>"no"</li>
* </ul>
*/
public SearchRequest setIsExternal(String isExternal) {
this.isExternal = isExternal;
return this;
}

public String getIsExternal() {
return isExternal;
}


/** /**
* Example value: "java,js" * Example value: "java,js"
Expand Down
@@ -0,0 +1,96 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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 org.sonarqube.tests.issue;

import com.sonar.orchestrator.build.SonarScanner;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonarqube.qa.util.Tester;
import org.sonarqube.ws.Common.RuleScope;
import org.sonarqube.ws.Common.RuleType;
import org.sonarqube.ws.Common.Severity;
import org.sonarqube.ws.Issues.Issue;
import org.sonarqube.ws.client.issues.SearchRequest;
import util.ItUtils;

import static org.assertj.core.api.Assertions.assertThat;

public class ExternalIssueTest extends AbstractIssueTest {
private static final String PROJECT_KEY = "project";

@Rule
public Tester tester = new Tester(ORCHESTRATOR);

@Before
public void setUp() {
ORCHESTRATOR.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);
ItUtils.restoreProfile(ORCHESTRATOR, getClass().getResource("/issue/ExternalIssueTest/no-rules.xml"));
ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "no-rules");
}

@Test
public void should_import_external_issues_and_create_external_rules() {
noExternalRuleAndNoIssues();

SonarScanner sonarScanner = ItUtils.runProjectAnalysis(ORCHESTRATOR, "shared/xoo-sample",
"sonar.oneExternalIssuePerLine.activate", "true");
List<Issue> issuesList = tester.wsClient().issues().search(new SearchRequest()).getIssuesList();
assertThat(issuesList).hasSize(17);

assertThat(issuesList).allMatch(issue -> "external_xoo:OneExternalIssuePerLine".equals(issue.getRule()));
assertThat(issuesList).allMatch(issue -> "This issue is generated on each line".equals(issue.getMessage()));
assertThat(issuesList).allMatch(issue -> "This issue is generated on each line".equals(issue.getMessage()));
assertThat(issuesList).allMatch(issue -> Severity.MAJOR.equals(issue.getSeverity()));
assertThat(issuesList).allMatch(issue -> RuleType.CODE_SMELL.equals(issue.getType()));
assertThat(issuesList).allMatch(issue -> "sample:src/main/xoo/sample/Sample.xoo".equals(issue.getComponent()));
assertThat(issuesList).allMatch(issue -> "OPEN".equals(issue.getStatus()));
assertThat(issuesList).allMatch(issue -> Boolean.TRUE.equals(issue.getFromExternalRule()));

List<org.sonarqube.ws.Rules.Rule> rulesList = tester.wsClient().rules()
.search(new org.sonarqube.ws.client.rules.SearchRequest().setIsExternal(Boolean.toString(true))).getRulesList();
List<org.sonarqube.ws.Rules.Rule> externalRules = rulesList.stream().filter(rule -> rule.getIsExternal()).collect(Collectors.toList());

assertThat(externalRules).hasSize(1);
assertThat(externalRules.get(0).getKey()).isEqualTo("external_xoo:OneExternalIssuePerLine");
assertThat(externalRules.get(0).getIsTemplate()).isFalse();
assertThat(externalRules.get(0).getIsExternal()).isTrue();
assertThat(externalRules.get(0).getTags().getTagsCount()).isEqualTo(0);
assertThat(externalRules.get(0).getScope()).isEqualTo(RuleScope.ALL);

// second analysis, issue tracking should work
sonarScanner = ItUtils.runProjectAnalysis(ORCHESTRATOR, "shared/xoo-sample",
"sonar.oneExternalIssuePerLine.activate", "true");
issuesList = tester.wsClient().issues().search(new SearchRequest()).getIssuesList();
assertThat(issuesList).hasSize(17);
}

private void noExternalRuleAndNoIssues() {
List<org.sonarqube.ws.Rules.Rule> rulesList = tester.wsClient().rules()
.search(new org.sonarqube.ws.client.rules.SearchRequest().setIsExternal(Boolean.toString(true))).getRulesList();
assertThat(rulesList).noneMatch(rule -> rule.getIsExternal());

List<Issue> issuesList = tester.wsClient().issues().search(new SearchRequest()).getIssuesList();
assertThat(issuesList).isEmpty();
}

}
Expand Up @@ -33,6 +33,7 @@
AutoAssignTest.class, AutoAssignTest.class,
CommonRulesTest.class, CommonRulesTest.class,
CustomRulesTest.class, CustomRulesTest.class,
ExternalIssueTest.class,
IssueActionTest.class, IssueActionTest.class,
IssueBulkChangeTest.class, IssueBulkChangeTest.class,
IssueChangelogTest.class, IssueChangelogTest.class,
Expand Down
12 changes: 6 additions & 6 deletions tests/src/test/java/util/ItUtils.java
Expand Up @@ -27,7 +27,7 @@
import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.OrchestratorBuilder; import com.sonar.orchestrator.OrchestratorBuilder;
import com.sonar.orchestrator.build.BuildResult; import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SonarRunner; import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.container.Server; import com.sonar.orchestrator.container.Server;
import com.sonar.orchestrator.locator.FileLocation; import com.sonar.orchestrator.locator.FileLocation;
import java.io.File; import java.io.File;
Expand Down Expand Up @@ -253,21 +253,21 @@ public static void assertIssuesInJsonReport(BuildResult result, int newIssues, i
assertThat(countExisting).isEqualTo(existingIssues); assertThat(countExisting).isEqualTo(existingIssues);
} }


public static SonarRunner runVerboseProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) { public static SonarScanner runVerboseProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) {
return runProjectAnalysis(orchestrator, projectRelativePath, true, properties); return runProjectAnalysis(orchestrator, projectRelativePath, true, properties);
} }


public static SonarRunner runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) { public static SonarScanner runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) {
return runProjectAnalysis(orchestrator, projectRelativePath, false, properties); return runProjectAnalysis(orchestrator, projectRelativePath, false, properties);
} }


private static SonarRunner runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, boolean enableDebugLogs, String... properties) { private static SonarScanner runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, boolean enableDebugLogs, String... properties) {
SonarRunner sonarRunner = SonarRunner.create(projectDir(projectRelativePath)); SonarScanner sonarScanner = SonarScanner.create(projectDir(projectRelativePath));
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (int i = 0; i < properties.length; i += 2) { for (int i = 0; i < properties.length; i += 2) {
builder.put(properties[i], properties[i + 1]); builder.put(properties[i], properties[i + 1]);
} }
SonarRunner scan = sonarRunner.setDebugLogs(enableDebugLogs).setProperties(builder.build()); SonarScanner scan = sonarScanner.setDebugLogs(enableDebugLogs).setProperties(builder.build());
orchestrator.executeBuild(scan); orchestrator.executeBuild(scan);
return scan; return scan;
} }
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/resources/issue/ExternalIssueTest/no-rules.xml
@@ -0,0 +1,6 @@
<profile>
<name>no-rules</name>
<language>xoo</language>
<rules>
</rules>
</profile>

0 comments on commit ec13da4

Please sign in to comment.