@CheckForNull String getFileLongName(@Nullable String fileUuid) { if (fileUuid == null) { return null; } ComponentDto file = files.get(fileUuid); return file == null ? null : file.longName(); }
private static List<Project> toProjects(Map<String, ComponentDto> projectsByUuids) { return projectsByUuids.values().stream() .map(p -> Project.newBuilder() .setKey(p.getDbKey()) .setName(p.longName()) .build()) .collect(Collectors.toList()); } }
public IssueChangeNotification setComponent(ComponentDto component) { return setComponent(component.getKey(), component.longName()); }
private static Duplications.File toWsFile(ComponentDto file, @Nullable ComponentDto project, @Nullable ComponentDto subProject, @Nullable String branch, @Nullable String pullRequest) { Duplications.File.Builder wsFile = Duplications.File.newBuilder(); wsFile.setKey(file.getKey()); wsFile.setUuid(file.uuid()); wsFile.setName(file.longName()); // Do not return sub project if sub project and project are the same ofNullable(project).ifPresent(p -> { wsFile.setProject(p.getKey()); wsFile.setProjectUuid(p.uuid()); wsFile.setProjectName(p.longName()); // Do not return sub project if sub project and project are the same boolean displaySubProject = subProject != null && !subProject.uuid().equals(project.uuid()); if (displaySubProject) { wsFile.setSubProject(subProject.getKey()); wsFile.setSubProjectUuid(subProject.uuid()); wsFile.setSubProjectName(subProject.longName()); } ofNullable(branch).ifPresent(wsFile::setBranch); ofNullable(pullRequest).ifPresent(wsFile::setPullRequest); }); return wsFile.build(); }
private static Optional<ComponentUpdateDto> compareForUpdate(ComponentDto existing, ComponentDto target) { boolean hasDifferences = !StringUtils.equals(existing.getCopyResourceUuid(), target.getCopyResourceUuid()) || !StringUtils.equals(existing.description(), target.description()) || !StringUtils.equals(existing.getDbKey(), target.getDbKey()) || !existing.isEnabled() || !StringUtils.equals(existing.getUuidPath(), target.getUuidPath()) || !StringUtils.equals(existing.language(), target.language()) || !StringUtils.equals(existing.longName(), target.longName()) || !StringUtils.equals(existing.moduleUuid(), target.moduleUuid()) || !StringUtils.equals(existing.moduleUuidPath(), target.moduleUuidPath()) || !StringUtils.equals(existing.name(), target.name()) || !StringUtils.equals(existing.path(), target.path()) || !StringUtils.equals(existing.scope(), target.scope()) || !StringUtils.equals(existing.qualifier(), target.qualifier()); ComponentUpdateDto update = null; if (hasDifferences) { update = ComponentUpdateDto .copyFrom(target) .setBChanged(true); } return ofNullable(update); }
private static List<Issues.Component> formatComponents(SearchResponseData data) { Collection<ComponentDto> components = data.getComponents(); List<Issues.Component> result = new ArrayList<>(); for (ComponentDto dto : components) { String uuid = dto.uuid(); Component.Builder builder = Component.newBuilder() .setOrganization(data.getOrganizationKey(dto.getOrganizationUuid())) .setKey(dto.getKey()) .setUuid(uuid) .setQualifier(dto.qualifier()) .setName(nullToEmpty(dto.name())) .setLongName(nullToEmpty(dto.longName())) .setEnabled(dto.isEnabled()); ofNullable(dto.getBranch()).ifPresent(builder::setBranch); ofNullable(dto.getPullRequest()).ifPresent(builder::setPullRequest); ofNullable(emptyToNull(dto.path())).ifPresent(builder::setPath); result.add(builder.build()); } return result; }
/** * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()} */ private void assertDtoIsView(ComponentDto dto) { assertThat(dto.getOrganizationUuid()).isEqualTo(ORGANIZATION_UUID); assertThat(dto.name()).isEqualTo(VIEW_NAME); assertThat(dto.longName()).isEqualTo(VIEW_NAME); assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION); assertThat(dto.path()).isNull(); assertThat(dto.uuid()).isEqualTo(VIEW_UUID); assertThat(dto.projectUuid()).isEqualTo(VIEW_UUID); assertThat(dto.getRootUuid()).isEqualTo(VIEW_UUID); assertThat(dto.moduleUuid()).isNull(); assertThat(dto.moduleUuidPath()).isEqualTo("." + dto.uuid() + "."); assertThat(dto.qualifier()).isEqualTo(Qualifiers.VIEW); assertThat(dto.scope()).isEqualTo(Scopes.PROJECT); assertThat(dto.getCopyResourceUuid()).isNull(); assertThat(dto.getCreatedAt()).isEqualTo(now); }
private void assertDtoIsProjectView1(ComponentDto pv1Dto, ComponentDto viewDto, ComponentDto parentViewDto, ComponentDto project) { assertThat(pv1Dto.getOrganizationUuid()).isEqualTo(ORGANIZATION_UUID); assertThat(pv1Dto.name()).isEqualTo(PROJECT_VIEW_1_NAME); assertThat(pv1Dto.longName()).isEqualTo(PROJECT_VIEW_1_NAME); assertThat(pv1Dto.description()).isNull(); assertThat(pv1Dto.path()).isNull(); assertThat(pv1Dto.uuid()).isEqualTo(PROJECT_VIEW_1_UUID); assertThat(pv1Dto.projectUuid()).isEqualTo(viewDto.uuid()); assertThat(pv1Dto.getRootUuid()).isEqualTo(viewDto.uuid()); assertThat(pv1Dto.moduleUuid()).isEqualTo(parentViewDto.uuid()); assertThat(pv1Dto.moduleUuidPath()).isEqualTo(parentViewDto.moduleUuidPath() + pv1Dto.uuid() + "."); assertThat(pv1Dto.qualifier()).isEqualTo(Qualifiers.PROJECT); assertThat(pv1Dto.scope()).isEqualTo(Scopes.FILE); assertThat(pv1Dto.getCopyResourceUuid()).isEqualTo(project.uuid()); assertThat(pv1Dto.getCreatedAt()).isEqualTo(now); }
/** * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()} */ private void assertDtoIsApplication(ComponentDto dto) { assertThat(dto.getOrganizationUuid()).isEqualTo(ORGANIZATION_UUID); assertThat(dto.name()).isEqualTo(VIEW_NAME); assertThat(dto.longName()).isEqualTo(VIEW_NAME); assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION); assertThat(dto.path()).isNull(); assertThat(dto.uuid()).isEqualTo(VIEW_UUID); assertThat(dto.projectUuid()).isEqualTo(VIEW_UUID); assertThat(dto.getRootUuid()).isEqualTo(VIEW_UUID); assertThat(dto.moduleUuid()).isNull(); assertThat(dto.moduleUuidPath()).isEqualTo("." + dto.uuid() + "."); assertThat(dto.qualifier()).isEqualTo(Qualifiers.APP); assertThat(dto.scope()).isEqualTo(Scopes.PROJECT); assertThat(dto.getCopyResourceUuid()).isNull(); assertThat(dto.getCreatedAt()).isEqualTo(now); }
/** * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto, Long)} */ private void assertDtoIsSubView1(ComponentDto viewDto, ComponentDto sv1Dto) { assertThat(sv1Dto.getOrganizationUuid()).isEqualTo(ORGANIZATION_UUID); assertThat(sv1Dto.name()).isEqualTo(SUBVIEW_1_NAME); assertThat(sv1Dto.longName()).isEqualTo(SUBVIEW_1_NAME); assertThat(sv1Dto.description()).isEqualTo(SUBVIEW_1_DESCRIPTION); assertThat(sv1Dto.path()).isNull(); assertThat(sv1Dto.uuid()).isEqualTo(SUBVIEW_1_UUID); assertThat(sv1Dto.projectUuid()).isEqualTo(viewDto.uuid()); assertThat(sv1Dto.getRootUuid()).isEqualTo(viewDto.uuid()); assertThat(sv1Dto.moduleUuid()).isEqualTo(viewDto.uuid()); assertThat(sv1Dto.moduleUuidPath()).isEqualTo(viewDto.moduleUuidPath() + sv1Dto.uuid() + "."); assertThat(sv1Dto.qualifier()).isEqualTo(Qualifiers.SUBVIEW); assertThat(sv1Dto.scope()).isEqualTo(Scopes.PROJECT); assertThat(sv1Dto.getCopyResourceUuid()).isNull(); assertThat(sv1Dto.getCreatedAt()).isEqualTo(now); }
@Test public void send_global_new_issues_notification_on_branch() { ComponentDto branch = setUpProjectWithBranch(); issueCache.newAppender().append( new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close(); when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true); analysisMetadataHolder.setBranch(newBranch()); TestComputationStepContext context = new TestComputationStepContext(); underTest.execute(context); verify(notificationService).deliver(newIssuesNotificationMock); verify(newIssuesNotificationMock).setProject(branch.getKey(), branch.longName(), BRANCH_NAME, null); verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE)); verify(newIssuesNotificationMock).setStatistics(eq(branch.longName()), any(NewIssuesStatistics.Stats.class)); verify(newIssuesNotificationMock).setDebt(ISSUE_DURATION); verifyStatistics(context, 1, 0, 0); }
@Test public void send_global_new_issues_notification_on_pull_request() { ComponentDto branch = setUpProjectWithBranch(); issueCache.newAppender().append( new DefaultIssue().setType(randomRuleType).setEffort(ISSUE_DURATION).setCreationDate(new Date(ANALYSE_DATE))).close(); when(notificationService.hasProjectSubscribersForTypes(branch.uuid(), NOTIF_TYPES)).thenReturn(true); analysisMetadataHolder.setBranch(newPullRequest()); analysisMetadataHolder.setPullRequestKey(PULL_REQUEST_ID); TestComputationStepContext context = new TestComputationStepContext(); underTest.execute(context); verify(notificationService).deliver(newIssuesNotificationMock); verify(newIssuesNotificationMock).setProject(branch.getKey(), branch.longName(), null, PULL_REQUEST_ID); verify(newIssuesNotificationMock).setAnalysisDate(new Date(ANALYSE_DATE)); verify(newIssuesNotificationMock).setStatistics(eq(branch.longName()), any(NewIssuesStatistics.Stats.class)); verify(newIssuesNotificationMock).setDebt(ISSUE_DURATION); verifyStatistics(context, 1, 0, 0); }
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 selectByKey() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto directory = db.components().insertComponent(newDirectory(project, "src")); ComponentDto file = db.components().insertComponent(newFileDto(project, directory) .setDbKey("org.struts:struts-core:src/org/struts/RequestContext.java") .setName("RequestContext.java") .setLongName("org.struts.RequestContext") .setLanguage("java") .setPath("src/RequestContext.java")); Optional<ComponentDto> optional = underTest.selectByKey(dbSession, file.getDbKey()); ComponentDto result = optional.get(); assertThat(result.getOrganizationUuid()).isEqualTo(organization.getUuid()); assertThat(result.uuid()).isEqualTo(file.uuid()); assertThat(result.getDbKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java"); assertThat(result.path()).isEqualTo("src/RequestContext.java"); assertThat(result.name()).isEqualTo("RequestContext.java"); assertThat(result.longName()).isEqualTo("org.struts.RequestContext"); assertThat(result.qualifier()).isEqualTo("FIL"); assertThat(result.scope()).isEqualTo("FIL"); assertThat(result.language()).isEqualTo("java"); assertThat(result.projectUuid()).isEqualTo(project.uuid()); assertThat(underTest.selectByKey(dbSession, "unknown")).isEmpty(); }
@Test public void set_assignee() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleDefinitionDto rule = db.rules().insert(); UserDto user = db.users().insertUser(); IssueDto issue = db.issues().insert(rule, project, file, i -> i.setAssigneeUuid(user.getUuid())); NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats(i -> true); IntStream.rangeClosed(1, 5).forEach(i -> stats.add(issue.toDefaultIssue())); underTest.setStatistics(project.longName(), stats); assertThat(underTest.getFieldValue(ASSIGNEE + ".1.label")).isEqualTo(user.getName()); assertThat(underTest.getFieldValue(ASSIGNEE + ".1.count")).isEqualTo("5"); }
@Test public void persist_unit_test() { ComponentDto projectDto = prepareProject(); treeRootHolder.setRoot( asTreeRoot(projectDto) .addChildren( builder(DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/test/java/dir") .setName("src/test/java/dir") .addChildren( builder(FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/test/java/dir/FooTest.java") .setName("src/test/java/dir/FooTest.java") .setShortName("FooTest.java") .setFileAttributes(new FileAttributes(true, null, 1)) .build()) .build()) .build()); underTest.execute(new TestComputationStepContext()); ComponentDto file = dbClient.componentDao().selectByKey(db.getSession(), PROJECT_KEY + ":src/test/java/dir/FooTest.java").get(); assertThat(file.name()).isEqualTo("FooTest.java"); assertThat(file.longName()).isEqualTo("src/test/java/dir/FooTest.java"); assertThat(file.path()).isEqualTo("src/test/java/dir/FooTest.java"); assertThat(file.qualifier()).isEqualTo("UTS"); assertThat(file.scope()).isEqualTo("FIL"); }
@Test public void changelog_of_file_move_contains_file_names() { RuleDto rule = db.rules().insertRule(newRuleDto()); OrganizationDto org = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(org); ComponentDto file1 = db.components().insertComponent(newFileDto(project)); ComponentDto file2 = db.components().insertComponent(newFileDto(project)); IssueDto issueDto = db.issues().insertIssue(newDto(rule, file2, project)); userSession.logIn("john") .addMembership(org) .addProjectPermission(USER, project, file); db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", file1.uuid(), file2.uuid()).setCreationDate(new Date())); ChangelogWsResponse result = call(issueDto.getKey()); assertThat(result.getChangelogList()).hasSize(1); assertThat(result.getChangelogList().get(0).hasUser()).isFalse(); assertThat(result.getChangelogList().get(0).getCreationDate()).isNotEmpty(); assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue) .containsOnly(tuple("file", file1.longName(), file2.longName())); }
public static ComponentDto newProjectCopy(String uuid, ComponentDto project, ComponentDto view) { checkNotNull(project.getId(), "The project need to be persisted before creating this technical project."); return newChildComponent(uuid, view, view) .setDbKey(view.getDbKey() + project.getDbKey()) .setName(project.name()) .setLongName(project.longName()) .setCopyComponentUuid(project.uuid()) .setScope(Scopes.FILE) .setQualifier(Qualifiers.PROJECT) .setPath(null) .setLanguage(null); }
private ComponentDto setUpProjectWithBranch() { 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()); return branch; }
/** * Copy the A-fields to B-fields. The field bChanged is kept to false. */ public static ComponentUpdateDto copyFrom(ComponentDto from) { return new ComponentUpdateDto() .setUuid(from.uuid()) .setBChanged(false) .setBKey(from.getDbKey()) .setBCopyComponentUuid(from.getCopyResourceUuid()) .setBDescription(from.description()) .setBEnabled(from.isEnabled()) .setBUuidPath(from.getUuidPath()) .setBLanguage(from.language()) .setBLongName(from.longName()) .setBModuleUuid(from.moduleUuid()) .setBModuleUuidPath(from.moduleUuidPath()) .setBName(from.name()) .setBPath(from.path()) // We don't have a b_scope. The applyBChangesForRootComponentUuid query is using a case ... when to infer scope from the qualifier .setBQualifier(from.qualifier()); } }