public boolean setIssueMoved(DefaultIssue issue, String newComponentUuid, IssueChangeContext context) { if (!Objects.equals(newComponentUuid, issue.componentUuid())) { issue.setFieldChange(context, FILE, issue.componentUuid(), newComponentUuid); issue.setComponentUuid(newComponentUuid); issue.setUpdateDate(context.date()); issue.setChanged(true); return true; } return false; }
@Test public void setIssueMoved_has_no_effect_if_component_uuid_is_not_changed() { String componentUuid = "a"; issue.setComponentUuid(componentUuid); underTest.setIssueMoved(issue, componentUuid, context); assertThat(issue.changes()).isEmpty(); assertThat(issue.componentUuid()).isEqualTo(componentUuid); assertThat(issue.isChanged()).isFalse(); assertThat(issue.updateDate()).isNull(); assertThat(issue.mustSendNotifications()).isFalse(); }
@Test public void add_counts_issue_per_component_on_leak_globally_and_per_assignee() { List<String> componentUuids = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); String assignee = randomAlphanumeric(10); componentUuids.stream() .map(componentUuid -> new DefaultIssue().setType(randomRuleTypeExceptHotspot).setComponentUuid(componentUuid).setAssigneeUuid(assignee).setNew(true)) .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.COMPONENT); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.COMPONENT); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> componentUuids.forEach(componentUuid -> assertStats(distribution, componentUuid, 1, 0, 1))); }
@Test public void add_counts_issue_per_component_off_leak_globally_and_per_assignee() { List<String> componentUuids = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); String assignee = randomAlphanumeric(10); componentUuids.stream() .map(componentUuid -> new DefaultIssue().setType(randomRuleTypeExceptHotspot).setComponentUuid(componentUuid).setAssigneeUuid(assignee).setNew(false)) .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.COMPONENT); NewIssuesStatistics.Stats stats = underTest.getAssigneesStatistics().get(assignee); DistributedMetricStatsInt assigneeDistribution = stats.getDistributedMetricStats(Metric.COMPONENT); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> componentUuids.forEach(componentUuid -> assertStats(distribution, componentUuid, 0, 1, 1))); }
@Test public void add_does_not_count_component_if_null_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setComponentUuid(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.COMPONENT); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.COMPONENT); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isEqualTo(0); assertThat(distribution.getForLabel(null).isPresent()).isFalse(); }); }
@Test public void onIssue_update_component_and_module_fields_to_component_and_flag_issue_has_changed() { MovedFilesRepository.OriginalFile originalFile = new MovedFilesRepository.OriginalFile(6451, "original uuid", "original key"); DefaultIssue issue = mockIssue(originalFile.getUuid()); when(movedFilesRepository.getOriginalFile(FILE)) .thenReturn(Optional.of(originalFile)); underTest.onIssue(FILE, issue); verify(issue).setComponentUuid(FILE.getUuid()); verify(issue).setComponentKey(FILE.getKey()); verify(issue).setModuleUuid(null); verify(issue).setModuleUuidPath(null); verify(issue).setChanged(true); ArgumentCaptor<IssueChangeContext> issueChangeContextCaptor = ArgumentCaptor.forClass(IssueChangeContext.class); verify(issue).setFieldChange(issueChangeContextCaptor.capture(), eq("file"), eq(originalFile.getUuid()), eq(FILE.getUuid())); assertThat(issueChangeContextCaptor.getValue().date()).isEqualTo(new Date(ANALYSIS_DATE)); assertThat(issueChangeContextCaptor.getValue().userUuid()).isNull(); assertThat(issueChangeContextCaptor.getValue().scan()).isFalse(); }
@Test public void setIssueMoved_changes_componentUuid_adds_a_change() { String oldComponentUuid = "a"; String newComponentUuid = "b"; issue.setComponentUuid(oldComponentUuid); underTest.setIssueMoved(issue, newComponentUuid, context); assertThat(issue.changes()).hasSize(1); FieldDiffs fieldDiffs = issue.changes().get(0); assertThat(fieldDiffs.creationDate()).isEqualTo(context.date()); assertThat(fieldDiffs.diffs()).hasSize(1); Map.Entry<String, FieldDiffs.Diff> entry = fieldDiffs.diffs().entrySet().iterator().next(); assertThat(entry.getKey()).isEqualTo("file"); assertThat(entry.getValue().oldValue()).isEqualTo(oldComponentUuid); assertThat(entry.getValue().newValue()).isEqualTo(newComponentUuid); assertThat(issue.componentUuid()).isEqualTo(newComponentUuid); assertThat(issue.isChanged()).isTrue(); assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(context.date(), Calendar.SECOND)); } }
private DefaultIssue init(DefaultIssue issue) { issue.setResolution(null); issue.setStatus(Issue.STATUS_OPEN); issue.setComponentUuid(component.getUuid()); issue.setComponentKey(component.getKey()); issue.setProjectUuid(treeRootHolder.getRoot().getUuid()); issue.setProjectKey(treeRootHolder.getRoot().getKey()); return issue; }
private static DefaultIssue newIssueOnLines(int... lines) { DefaultIssue issue = new DefaultIssue(); issue.setComponentUuid(FILE.getUuid()); DbIssues.Locations.Builder locations = DbIssues.Locations.newBuilder(); DbIssues.Flow.Builder flow = DbIssues.Flow.newBuilder(); Arrays.stream(lines).forEach(line -> flow.addLocation(newLocation(line))); locations.addFlow(flow.build()); issue.setLocations(locations.build()); return issue; }
@Test public void add_issues_with_correct_global_statistics() { DefaultIssue issue = new DefaultIssue() .setAssigneeUuid("maynard") .setComponentUuid("file-uuid") .setNew(true) .setType(RuleType.BUG) .setRuleKey(RuleKey.of("SonarQube", "rule-the-world")) .setTags(Lists.newArrayList("bug", "owasp")) .setEffort(Duration.create(5L)); underTest.add(issue); underTest.add(issue.setAssigneeUuid("james")); underTest.add(issue.setAssigneeUuid("keenan")); assertThat(countDistributionTotal(Metric.ASSIGNEE, "maynard")).isEqualTo(1); assertThat(countDistributionTotal(Metric.ASSIGNEE, "james")).isEqualTo(1); assertThat(countDistributionTotal(Metric.ASSIGNEE, "keenan")).isEqualTo(1); assertThat(countDistributionTotal(Metric.ASSIGNEE, "wrong.login")).isNull(); assertThat(countDistributionTotal(Metric.COMPONENT, "file-uuid")).isEqualTo(3); assertThat(countDistributionTotal(Metric.COMPONENT, "wrong-uuid")).isNull(); assertThat(countDistributionTotal(Metric.RULE_TYPE, RuleType.BUG.name())).isEqualTo(3); assertThat(countDistributionTotal(Metric.RULE_TYPE, RuleType.CODE_SMELL.name())).isNull(); assertThat(countDistributionTotal(Metric.TAG, "owasp")).isEqualTo(3); assertThat(countDistributionTotal(Metric.TAG, "wrong-tag")).isNull(); assertThat(countDistributionTotal(Metric.RULE, "SonarQube:rule-the-world")).isEqualTo(3); assertThat(countDistributionTotal(Metric.RULE, "SonarQube:has-a-fake-rule")).isNull(); assertThat(underTest.globalStatistics().effort().getTotal()).isEqualTo(15L); assertThat(underTest.globalStatistics().hasIssues()).isTrue(); assertThat(underTest.hasIssues()).isTrue(); assertThat(underTest.getAssigneesStatistics().get("maynard").hasIssues()).isTrue(); }
@Test public void load_component_id_from_db() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); ComponentDto file = db.components().insertComponent(newFileDto(project)); long componentId = underTest.component(db.getSession(), new DefaultIssue().setComponentUuid(file.uuid())).getId(); assertThat(componentId).isEqualTo(file.getId()); }
@Test public void allLinesFor_default_component_of_location_is_the_issue_component() { DbIssues.Locations.Builder locations = DbIssues.Locations.newBuilder(); locations.addFlowBuilder() .addLocation(newLocation("", 5, 5)) .addLocation(newLocation(null, 7, 7)) .addLocation(newLocation("file2", 9, 9)) .build(); DefaultIssue issue = new DefaultIssue() .setComponentUuid("file1") .setLocations(locations.build()); assertThat(IssueLocations.allLinesFor(issue, "file1")).containsExactlyInAnyOrder(5, 7); assertThat(IssueLocations.allLinesFor(issue, "file2")).containsExactlyInAnyOrder(9); assertThat(IssueLocations.allLinesFor(issue, "file3")).isEmpty(); }
@Test public void should_reload_issue_and_resolve_conflict() { DefaultIssue issue = new DefaultIssue() .setKey("ABCDE") .setType(CODE_SMELL) .setRuleKey(RuleKey.of("squid", "AvoidCycles")) .setProjectUuid("U1") .setComponentUuid("U2") .setNew(false) .setStatus(STATUS_OPEN); // Issue as seen and changed by end-user IssueMapper mapper = mock(IssueMapper.class); IssueDto issueDto = new IssueDto() .setKee("ABCDE") .setType(CODE_SMELL) .setRuleId(10) .setRuleKey("squid", "AvoidCycles") .setProjectUuid("U1") .setComponentUuid("U2") .setLine(10) .setStatus(STATUS_OPEN) // field changed by user .setAssigneeUuid("arthur-uuid"); new UpdateConflictResolver().resolve(issue, issueDto, mapper); ArgumentCaptor<IssueDto> argument = ArgumentCaptor.forClass(IssueDto.class); verify(mapper).update(argument.capture()); IssueDto updatedIssue = argument.getValue(); assertThat(updatedIssue.getKee()).isEqualTo("ABCDE"); assertThat(updatedIssue.getAssigneeUuid()).isEqualTo("arthur-uuid"); }
.setType(RuleType.CODE_SMELL) .setRuleKey(rule.getKey()) .setComponentUuid(file.uuid()) .setProjectUuid(project.uuid()) .setSeverity(BLOCKER)
underTest.add(new DefaultIssue() .setType(randomRuleTypeExceptHotspot) .setComponentUuid(componentUuid) .setTags(ImmutableSet.of(tag)) .setAssigneeUuid(assignee)
.setType(RuleType.CODE_SMELL) .setRuleKey(rule.getKey()) .setComponentUuid(file.uuid()) .setProjectUuid(project.uuid()) .setSeverity(BLOCKER)
.setType(RuleType.CODE_SMELL) .setRuleKey(rule.getKey()) .setComponentUuid(file.uuid()) .setProjectUuid(project.uuid()) .setSeverity(BLOCKER)
raw.setComponentUuid(base.componentUuid()); raw.setComponentKey(base.componentKey()); raw.setModuleUuid(base.moduleUuid());
issue.setAttributes(KeyValueFormat.parse(MoreObjects.firstNonNull(issueAttributes, ""))); issue.setComponentKey(componentKey); issue.setComponentUuid(componentUuid); issue.setModuleUuid(moduleUuid); issue.setModuleUuidPath(moduleUuidPath);
.setRuleKey(rule.getKey()) .setProjectUuid(project.uuid()) .setComponentUuid(file.uuid()) .setLine(5000) .setEffort(Duration.create(10L))