/** * Returns the enum constant of the specified DB column value. */ public static RuleType valueOf(int dbConstant) { // iterating the array is fast-enough as size is small. No need for a map. for (RuleType type : values()) { if (type.getDbConstant() == dbConstant) { return type; } } throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant)); }
@CheckForNull public static RuleType valueOfNullable(int dbConstant) { // iterating the array is fast-enough as size is small. No need for a map. for (RuleType type : values()) { if (type.getDbConstant() == dbConstant) { return type; } } if (dbConstant == 0) { return null; } throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant)); }
private void setRuleTypeStatistics(NewIssuesStatistics.Stats stats) { DistributedMetricStatsInt distributedMetricStats = stats.getDistributedMetricStats(RULE_TYPE); setFieldValue(RULE_TYPE + COUNT, String.valueOf(distributedMetricStats.getOnLeak())); Arrays.stream(RuleType.values()) .forEach(ruleType -> setFieldValue( RULE_TYPE + DOT + ruleType + COUNT, String.valueOf(distributedMetricStats.getForLabel(ruleType.name()).map(MetricStatsInt::getOnLeak).orElse(0)))); }
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))); }
private void sendIssueChangeNotification(long issueCreatedAt) { UserDto user = db.users().insertUser(); ComponentDto project = newPrivateProjectDto(newOrganizationDto()).setDbKey(PROJECT.getDbKey()).setLongName(PROJECT.getName()); ComponentDto file = newFileDto(project).setDbKey(FILE.getDbKey()).setLongName(FILE.getName()); RuleDefinitionDto ruleDefinitionDto = newRule(); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = prepareIssue(issueCreatedAt, user, project, file, ruleDefinitionDto, randomTypeExceptHotspot); underTest.execute(new TestComputationStepContext()); ArgumentCaptor<IssueChangeNotification> issueChangeNotificationCaptor = forClass(IssueChangeNotification.class); verify(notificationService).deliver(issueChangeNotificationCaptor.capture()); IssueChangeNotification issueChangeNotification = issueChangeNotificationCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("message")).isEqualTo(issue.message()); assertThat(issueChangeNotification.getFieldValue("ruleName")).isEqualTo(ruleDefinitionDto.getName()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.longName()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getKey()); assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getKey()); assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName()); assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(user.getLogin()); }
@Test public void verify_notification_on_branch() { RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), branch, file) .setType(randomTypeExceptHotspot)).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, "increase severity"); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getDbKey()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.name()); assertThat(issueChangeNotification.getFieldValue("branch")).isEqualTo(branch.getBranch()); }
@Test public void verify_notification() { UserDto assignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)) .setSeverity(MAJOR) .setAssigneeUuid(assignee.getUuid()) .toDefaultIssue(); UserDto changeAuthor = db.users().insertUser(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid()); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, "increase severity"); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("old.severity")).isEqualTo(MAJOR); assertThat(issueChangeNotification.getFieldValue("new.severity")).isEqualTo(BLOCKER); assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getDbKey()); assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getDbKey()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.name()); assertThat(issueChangeNotification.getFieldValue("ruleName")).isEqualTo(rule.getName()); assertThat(issueChangeNotification.getFieldValue("changeAuthor")).isEqualTo(changeAuthor.getLogin()); assertThat(issueChangeNotification.getFieldValue("comment")).isEqualTo("increase severity"); assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(assignee.getLogin()); }
@Test public void verify_notification_when_issue_is_linked_on_removed_rule() { RuleDto rule = db.rules().insertRule(r -> r.setStatus(RuleStatus.REMOVED)); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, null); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); assertThat(notificationArgumentCaptor.getValue().getFieldValue("ruleName")).isNull(); }
@Test public void verify_notification_when_assignee_has_changed() { UserDto oldAssignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)) .setAssigneeUuid(oldAssignee.getUuid()) .toDefaultIssue(); UserDto changeAuthor = db.users().insertUser(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid()); UserDto newAssignee = db.users().insertUser(); issueFieldsSetter.assign(issue, newAssignee, context); underTest.saveIssue(db.getSession(), issue, context, null); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("new.assignee")).isEqualTo(newAssignee.getName()); assertThat(issueChangeNotification.getFieldValue("old.assignee")).isNull(); assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(newAssignee.getLogin()); }
private void sendIssueChangeNotificationOnBranch(long issueCreatedAt) { ComponentDto project = newPrivateProjectDto(newOrganizationDto()); ComponentDto branch = newProjectBranch(project, newBranchDto(project).setKey(BRANCH_NAME)); ComponentDto file = newFileDto(branch); treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getDbKey()).setPublicKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren( builder(Type.FILE, 11).setKey(file.getDbKey()).setPublicKey(file.getKey()).setName(file.longName()).build()).build()); RuleDefinitionDto ruleDefinitionDto = newRule(); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = newIssue(ruleDefinitionDto, branch, file).setType(randomTypeExceptHotspot).toDefaultIssue() .setNew(false) .setChanged(true) .setSendNotifications(true) .setCreationDate(new Date(issueCreatedAt)); ruleRepository.add(ruleDefinitionDto.getKey()).setName(ruleDefinitionDto.getName()); issueCache.newAppender().append(issue).close(); when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true); analysisMetadataHolder.setBranch(newBranch()); underTest.execute(new TestComputationStepContext()); ArgumentCaptor<IssueChangeNotification> issueChangeNotificationCaptor = forClass(IssueChangeNotification.class); verify(notificationService).deliver(issueChangeNotificationCaptor.capture()); IssueChangeNotification issueChangeNotification = issueChangeNotificationCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(branch.longName()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(branch.getKey()); assertThat(issueChangeNotification.getFieldValue("branch")).isEqualTo(BRANCH_NAME); assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getKey()); assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName()); }
.setDescription("Comma-separated list of types.") .setSince("5.5") .setPossibleValues((Object[]) RuleType.values()) .setDefaultValue(format("%s,%s,%s", RuleType.BUG, RuleType.VULNERABILITY, RuleType.CODE_SMELL)) .setExampleValue(format("%s,%s", RuleType.CODE_SMELL, RuleType.BUG));
.setSince("5.5") .setDescription("Comma-separated list of types. Returned rules match any of the tags (OR operator)") .setPossibleValues(RuleType.values()) .setExampleValue(RuleType.BUG);
/** * Returns the enum constant of the specified DB column value. */ public static RuleType valueOf(int dbConstant) { // iterating the array is fast-enough as size is small. No need for a map. for (RuleType type : values()) { if (type.getDbConstant() == dbConstant) { return type; } } throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant)); }
@CheckForNull public static RuleType valueOfNullable(int dbConstant) { // iterating the array is fast-enough as size is small. No need for a map. for (RuleType type : values()) { if (type.getDbConstant() == dbConstant) { return type; } } if (dbConstant == 0) { return null; } throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant)); }
private void setRuleTypeStatistics(NewIssuesStatistics.Stats stats) { DistributedMetricStatsInt distributedMetricStats = stats.getDistributedMetricStats(RULE_TYPE); setFieldValue(RULE_TYPE + COUNT, String.valueOf(distributedMetricStats.getOnLeak())); Arrays.stream(RuleType.values()) .forEach(ruleType -> setFieldValue( RULE_TYPE + DOT + ruleType + COUNT, String.valueOf(distributedMetricStats.getForLabel(ruleType.name()).map(MetricStatsInt::getOnLeak).orElse(0)))); }
.setDescription("Comma-separated list of types.") .setSince("5.5") .setPossibleValues((Object[]) RuleType.values()) .setExampleValue(format("%s,%s", RuleType.CODE_SMELL, RuleType.BUG)); action.createParam(PARAM_AUTHORS)
.setSince("5.5") .setDescription("Comma-separated list of types. Returned rules match any of the tags (OR operator)") .setPossibleValues(RuleType.values()) .setExampleValue(RuleType.BUG);