private void resolveAssignee(IssueDto dbIssue, DefaultIssue issue) { issue.setAssigneeUuid(dbIssue.getAssigneeUuid()); } }
public boolean assign(DefaultIssue issue, @Nullable UserDto user, IssueChangeContext context) { String assigneeUuid = user != null ? user.getUuid() : null; if (!Objects.equals(assigneeUuid, issue.assignee())) { String newAssigneeName = user == null ? null : user.getName(); issue.setFieldChange(context, ASSIGNEE, UNUSED, newAssigneeName); issue.setAssigneeUuid(user != null ? user.getUuid() : null); issue.setUpdateDate(context.date()); issue.setChanged(true); issue.setSendNotifications(true); return true; } return false; }
@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 unassign() { issue.setAssigneeUuid("user_uuid"); boolean updated = underTest.assign(issue, null, context); assertThat(updated).isTrue(); assertThat(issue.assignee()).isNull(); assertThat(issue.mustSendNotifications()).isTrue(); FieldDiffs.Diff diff = issue.currentChange().get(ASSIGNEE); assertThat(diff.oldValue()).isEqualTo(UNUSED); assertThat(diff.newValue()).isNull(); }
@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_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(); }
@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_does_not_count_ruleKey_if_null_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setRuleKey(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.RULE); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isEqualTo(0); assertThat(distribution.getForLabel(null).isPresent()).isFalse(); }); }
@Test public void change_assignee() { UserDto user = newUserDto().setLogin("emmerik").setName("Emmerik"); issue.setAssigneeUuid("user_uuid"); boolean updated = underTest.assign(issue, user, context); assertThat(updated).isTrue(); assertThat(issue.assignee()).isEqualTo(user.getUuid()); assertThat(issue.mustSendNotifications()).isTrue(); FieldDiffs.Diff diff = issue.currentChange().get(ASSIGNEE); assertThat(diff.oldValue()).isEqualTo(UNUSED); assertThat(diff.newValue()).isEqualTo(user.getName()); }
@Test public void add_counts_issue_per_ruleKey_off_leak_globally_and_per_assignee() { String repository = randomAlphanumeric(3); List<String> ruleKeys = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).collect(Collectors.toList()); String assignee = randomAlphanumeric(10); ruleKeys.stream() .map(ruleKey -> new DefaultIssue().setType(randomRuleTypeExceptHotspot).setRuleKey(RuleKey.of(repository, ruleKey)).setAssigneeUuid(assignee).setNew(false)) .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.RULE); DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> ruleKeys.forEach(ruleKey -> assertStats(distribution, RuleKey.of(repository, ruleKey).toString(), 0, 1, 1))); }
@Test public void not_change_assignee() { UserDto user = newUserDto().setLogin("morgan").setName("Morgan"); issue.setAssigneeUuid(user.getUuid()); boolean updated = underTest.assign(issue, user, context); assertThat(updated).isFalse(); assertThat(issue.currentChange()).isNull(); assertThat(issue.mustSendNotifications()).isFalse(); }
@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)); }
@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 fail_with_ISE_when_setting_new_assignee_on_already_assigned_issue() { issue.setAssigneeUuid("user_uuid"); thrown.expect(IllegalStateException.class); thrown.expectMessage("It's not possible to update the assignee with this method, please use assign()"); underTest.setNewAssignee(issue, "another_user_uuid", context); }
@Test public void do_not_assign_issue_if_unassigned_but_already_authored() { addScmUser("john", "u1234"); setSingleChangeset("john", 123456789L, "rev-1"); DefaultIssue issue = newIssueOnLines(1) .setAuthorLogin("john") .setAssigneeUuid(null); underTest.onIssue(FILE, issue); assertThat(issue.authorLogin()).isEqualTo("john"); assertThat(issue.assignee()).isNull(); }
private DefaultIssue prepareIssue(long issueCreatedAt, UserDto user, ComponentDto project, ComponentDto file, RuleDefinitionDto ruleDefinitionDto, RuleType type) { DefaultIssue issue = newIssue(ruleDefinitionDto, project, file).setType(type).toDefaultIssue() .setNew(false).setChanged(true).setSendNotifications(true).setCreationDate(new Date(issueCreatedAt)).setAssigneeUuid(user.getUuid()); ruleRepository.add(ruleDefinitionDto.getKey()).setName(ruleDefinitionDto.getName()); issueCache.newAppender().append(issue).close(); when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true); return issue; }