public boolean setType(DefaultIssue issue, RuleType type, IssueChangeContext context) { if (!Objects.equals(type, issue.type())) { issue.setFieldChange(context, TYPE, issue.type(), type); issue.setType(type); issue.setUpdateDate(context.date()); issue.setChanged(true); return true; } return false; }
private void setType(DefaultIssue issue) { if (!issue.isFromExternalRuleEngine()) { Rule rule = ruleRepository.getByKey(issue.ruleKey()); issue.setType(rule.getType()); } issue.setIsFromHotspot(issue.type() == RuleType.SECURITY_HOTSPOT); }
@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))); }
private DefaultIssue oldBugIssue(long effort) { return newCodeSmellIssueWithoutEffort() .setEffort(Duration.create(effort)) .setType(RuleType.BUG) .setCreationDate(new Date(OLD_ISSUES_DATES[random.nextInt(OLD_ISSUES_DATES.length)])); }
private DefaultIssue oldVulnerabilityIssue(long effort) { return newCodeSmellIssueWithoutEffort() .setEffort(Duration.create(effort)) .setType(RuleType.VULNERABILITY) .setCreationDate(new Date(OLD_ISSUES_DATES[random.nextInt(OLD_ISSUES_DATES.length)])); }
@Test public void add_does_not_count_tags_if_empty_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(Collections.emptyList()).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.TAG); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.TAG); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isEqualTo(0); assertThat(distribution.getForLabel(null).isPresent()).isFalse(); }); }
@Test public void add_counts_issue_per_tags_on_leak_globally_and_per_assignee() { List<String> tags = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(tags).setAssigneeUuid(assignee).setNew(true)); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.TAG); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.TAG); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> tags.forEach(tag -> assertStats(distribution, tag, 1, 0, 1))); }
@Test public void add_counts_issue_per_tags_off_leak_globally_and_per_assignee() { List<String> tags = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(tags).setAssigneeUuid(assignee).setNew(false)); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.TAG); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.TAG); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> tags.forEach(tag -> assertStats(distribution, tag, 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 add_does_not_assignee_if_empty_neither_globally_nor_per_assignee() { underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setAssigneeUuid(null).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.ASSIGNEE); assertThat(globalDistribution.getTotal()).isEqualTo(0); assertThat(globalDistribution.getForLabel(null).isPresent()).isFalse(); assertThat(underTest.getAssigneesStatistics()).isEmpty(); }
private static DefaultIssue newDefaultIssue(String projectUuid) { DefaultIssue defaultIssue = new DefaultIssue(); defaultIssue.setKey("issue_" + issueIdCounter++); defaultIssue.setProjectUuid(projectUuid); defaultIssue.setType(RuleType.values()[new Random().nextInt(RuleType.values().length)]); defaultIssue.setStatus(POSSIBLE_STATUSES[new Random().nextInt(POSSIBLE_STATUSES.length)]); String[] possibleResolutions = possibleResolutions(defaultIssue.getStatus()); if (possibleResolutions.length > 0) { defaultIssue.setResolution(possibleResolutions[new Random().nextInt(possibleResolutions.length)]); } return defaultIssue; }
@Test public void add_counts_issue_per_RuleType_on_leak_globally_and_per_assignee() { String assignee = randomAlphanumeric(10); Arrays.stream(RuleType.values()) .map(ruleType -> new DefaultIssue().setType(ruleType).setAssigneeUuid(assignee).setNew(true)) .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.RULE_TYPE); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE_TYPE); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> Arrays.stream(RuleType.values()).forEach(ruleType -> assertStats(distribution, ruleType.name(), 1, 0, 1))); }
@Test public void add_counts_issue_per_RuleType_off_leak_globally_and_per_assignee() { String assignee = randomAlphanumeric(10); Arrays.stream(RuleType.values()) .map(ruleType -> new DefaultIssue().setType(ruleType).setAssigneeUuid(assignee).setNew(false)) .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.RULE_TYPE); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE_TYPE); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> Arrays.stream(RuleType.values()).forEach(ruleType -> assertStats(distribution, ruleType.name(), 0, 1, 1))); }
@Test public void add_does_not_sum_effort_if_null_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setEffort(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); MetricStatsLong globalDistribution = underTest.globalStatistics().effort(); MetricStatsLong assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).effort(); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> assertThat(distribution.getTotal()).isEqualTo(0)); }
private static DefaultIssue createIssue(@Nullable String resolution, String status, String severity, RuleType ruleType, long creationDate) { return new DefaultIssue() .setResolution(resolution).setStatus(status) .setSeverity(severity).setRuleKey(RuleTesting.XOO_X1) .setType(ruleType) .setCreationDate(new Date(creationDate)); }
private static DefaultIssue newIssue(String severity, RuleType type) { return new DefaultIssue() .setKey(Uuids.create()) .setSeverity(severity) .setType(type) .setCreationDate(new Date(1000l)); }
@Test public void add_fails_with_NPE_if_RuleType_is_null() { String assignee = randomAlphanumeric(10); DefaultIssue issue = new DefaultIssue().setType(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean()); expectedException.expect(NullPointerException.class); underTest.add(issue); }
@Test public void do_not_send_global_new_issues_notification_if_issue_has_been_backdated() { issueCache.newAppender().append( new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION) .setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))) .close(); when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true); TestComputationStepContext context = new TestComputationStepContext(); underTest.execute(context); verify(notificationService, never()).deliver(any()); verifyStatistics(context, 0, 0, 0); }
@Test public void do_not_send_global_new_issues_notification_on_branch_if_issue_has_been_backdated() { ComponentDto branch = setUpProjectWithBranch(); issueCache.newAppender().append( new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE - FIVE_MINUTES_IN_MS))).close(); when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true); analysisMetadataHolder.setBranch(newBranch()); TestComputationStepContext context = new TestComputationStepContext(); underTest.execute(context); verify(notificationService, never()).deliver(any()); verifyStatistics(context, 0, 0, 0); }
private static DefaultIssue newIssue(String severity, RuleType type) { return new DefaultIssue() .setKey(Uuids.create()) .setSeverity(severity) .setType(type) .setCreationDate(DEFAULT_ISSUE_CREATION_DATE); }