use of org.sonarqube.ws.Hotspots in project sonarqube by SonarSource.
the class SearchActionTest method returns_hotspots_ordered_by_vulnerabilityProbability_score_then_rule_uuid.
@Test
public void returns_hotspots_ordered_by_vulnerabilityProbability_score_then_rule_uuid() {
ComponentDto project = dbTester.components().insertPublicProject();
userSessionRule.registerComponents(project);
indexPermissions();
ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
List<IssueDto> hotspots = Arrays.stream(SQCategory.values()).sorted(Ordering.from(Comparator.<SQCategory>comparingInt(t1 -> t1.getVulnerability().getScore()).reversed()).thenComparing(SQCategory::getKey)).flatMap(sqCategory -> {
Set<String> cwes = SecurityStandards.CWES_BY_SQ_CATEGORY.get(sqCategory);
Set<String> securityStandards = singleton("cwe:" + (cwes == null ? "unknown" : cwes.iterator().next()));
RuleDefinitionDto rule1 = newRule(SECURITY_HOTSPOT, t -> t.setUuid(sqCategory.name() + "_a").setName("rule_" + sqCategory.name() + "_a").setSecurityStandards(securityStandards));
RuleDefinitionDto rule2 = newRule(SECURITY_HOTSPOT, t -> t.setUuid(sqCategory.name() + "_b").setName("rule_" + sqCategory.name() + "_b").setSecurityStandards(securityStandards));
return Stream.of(newHotspot(rule1, project, file).setKee(sqCategory + "_a"), newHotspot(rule2, project, file).setKee(sqCategory + "_b"));
}).collect(toList());
String[] expectedHotspotKeys = hotspots.stream().map(IssueDto::getKey).toArray(String[]::new);
// insert hotspots in random order
Collections.shuffle(hotspots);
hotspots.forEach(dbTester.issues()::insertHotspot);
indexIssues();
SearchWsResponse response = newRequest(project).executeProtobuf(SearchWsResponse.class);
assertThat(response.getHotspotsList()).extracting(SearchWsResponse.Hotspot::getKey).containsExactly(expectedHotspotKeys);
}
use of org.sonarqube.ws.Hotspots in project sonarqube by SonarSource.
the class SearchActionTest method returns_hotspot_components_when_project_has_hotspots.
@Test
public void returns_hotspot_components_when_project_has_hotspots() {
ComponentDto project = dbTester.components().insertPublicProject();
userSessionRule.registerComponents(project);
indexPermissions();
ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
ComponentDto fileWithHotspot = dbTester.components().insertComponent(newFileDto(project));
Arrays.stream(RuleType.values()).filter(t -> t != SECURITY_HOTSPOT).forEach(ruleType -> {
RuleDefinitionDto rule = newRule(ruleType);
dbTester.issues().insert(rule, project, file, t -> t.setType(ruleType));
});
IssueDto[] hotspots = IntStream.range(0, 1 + RANDOM.nextInt(10)).mapToObj(i -> {
RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
return insertHotspot(project, fileWithHotspot, rule);
}).toArray(IssueDto[]::new);
indexIssues();
SearchWsResponse response = newRequest(project).executeProtobuf(SearchWsResponse.class);
assertThat(response.getHotspotsList()).extracting(SearchWsResponse.Hotspot::getKey).containsOnly(Arrays.stream(hotspots).map(IssueDto::getKey).toArray(String[]::new));
assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly(project.getKey(), fileWithHotspot.getKey());
}
use of org.sonarqube.ws.Hotspots in project sonarqube by SonarSource.
the class SearchActionTest method does_not_fail_if_rule_of_hotspot_does_not_exist_in_DB.
@Test
public void does_not_fail_if_rule_of_hotspot_does_not_exist_in_DB() {
ComponentDto project = dbTester.components().insertPublicProject();
userSessionRule.registerComponents(project);
ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
indexPermissions();
IssueDto[] hotspots = IntStream.range(0, 1 + RANDOM.nextInt(10)).mapToObj(i -> {
RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
return insertHotspot(project, file, rule);
}).toArray(IssueDto[]::new);
indexIssues();
IssueDto hotspotWithoutRule = hotspots[RANDOM.nextInt(hotspots.length)];
dbTester.executeUpdateSql("delete from rules where uuid=?", hotspotWithoutRule.getRuleUuid());
SearchWsResponse response = newRequest(project).executeProtobuf(SearchWsResponse.class);
assertThat(response.getHotspotsList()).extracting(Hotspots.SearchWsResponse.Hotspot::getKey).containsOnly(Arrays.stream(hotspots).filter(t -> !t.getKey().equals(hotspotWithoutRule.getKey())).map(IssueDto::getKey).toArray(String[]::new));
}
use of org.sonarqube.ws.Hotspots in project sonarqube by SonarSource.
the class SearchActionTest method verify_response_example.
@Test
public void verify_response_example() {
ComponentDto project = dbTester.components().insertPublicProject(componentDto -> componentDto.setName("test-project").setLongName("test-project").setDbKey("com.sonarsource:test-project"));
userSessionRule.registerComponents(project);
indexPermissions();
ComponentDto fileWithHotspot = dbTester.components().insertComponent(newFileDto(project).setDbKey("com.sonarsource:test-project:src/main/java/com/sonarsource/FourthClass.java").setName("FourthClass.java").setLongName("src/main/java/com/sonarsource/FourthClass.java").setPath("src/main/java/com/sonarsource/FourthClass.java"));
long time = 1577976190000L;
IssueDto[] hotspots = IntStream.range(0, 3).mapToObj(i -> {
RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT).setSecurityStandards(Sets.newHashSet(SQCategory.WEAK_CRYPTOGRAPHY.getKey()));
return insertHotspot(rule, project, fileWithHotspot, issueDto -> issueDto.setKee("hotspot-" + i).setAssigneeUuid("assignee-uuid").setAuthorLogin("joe").setMessage("message-" + i).setLine(10 + i).setIssueCreationTime(time).setIssueUpdateTime(time));
}).toArray(IssueDto[]::new);
indexIssues();
assertThat(actionTester.getDef().responseExampleAsString()).isNotNull();
newRequest(project).execute().assertJson(actionTester.getDef().responseExampleAsString());
}
use of org.sonarqube.ws.Hotspots in project sonarqube by SonarSource.
the class SearchActionTest method returns_first_page_with_100_results_by_default.
@Test
public void returns_first_page_with_100_results_by_default() {
ComponentDto project = dbTester.components().insertPublicProject();
userSessionRule.registerComponents(project);
indexPermissions();
ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
int total = 436;
List<IssueDto> hotspots = IntStream.range(0, total).mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i))).collect(toList());
indexIssues();
TestRequest request = newRequest(project);
SearchWsResponse response = request.executeProtobuf(SearchWsResponse.class);
assertThat(response.getHotspotsList()).extracting(SearchWsResponse.Hotspot::getKey).containsExactly(hotspots.stream().limit(100).map(IssueDto::getKey).toArray(String[]::new));
assertThat(response.getPaging().getTotal()).isEqualTo(hotspots.size());
assertThat(response.getPaging().getPageIndex()).isOne();
assertThat(response.getPaging().getPageSize()).isEqualTo(100);
}
Aggregations