@SafeVarargs public final ComponentDto insertPublicApplication(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) { return insertComponentImpl(newApplication(organization).setPrivate(false), false, dtoPopulators); }
@SafeVarargs public final ComponentDto insertPublicPortfolio(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) { return insertComponentImpl(newView(organization).setPrivate(false), false, dtoPopulators); }
@SafeVarargs public final ComponentDto insertPrivatePortfolio(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) { return insertComponentImpl(newView(organization).setPrivate(true), true, dtoPopulators); }
@SafeVarargs public final ComponentDto insertPrivateApplication(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) { return insertComponentImpl(newApplication(organization).setPrivate(true), true, dtoPopulators); }
public static ComponentDto newView(OrganizationDto organizationDto, String uuid) { return newPrivateProjectDto(organizationDto, uuid) .setUuid(uuid) .setScope(Scopes.PROJECT) .setQualifier(Qualifiers.VIEW) .setPrivate(false); }
@Test public void execute_has_no_effect_if_specified_project_already_has_specified_visibility() { ComponentDto project = randomPublicOrPrivateProject(); boolean initiallyPrivate = project.isPrivate(); BranchDto branchDto = ComponentTesting.newBranchDto(project); dbClient.branchDao().insert(dbSession, branchDto); ComponentDto branch = ComponentTesting.newProjectBranch(project, branchDto) .setPrivate(initiallyPrivate); ComponentDto module = ComponentTesting.newModuleDto(project) .setPrivate(initiallyPrivate); ComponentDto dir = ComponentTesting.newDirectory(project, "path") // child is inconsistent with root (should not occur) and won't be fixed .setPrivate(!initiallyPrivate); ComponentDto file = ComponentTesting.newFileDto(project) .setPrivate(initiallyPrivate); dbTester.components().insertComponents(branch, module, dir, file); userSessionRule.addProjectPermission(UserRole.ADMIN, project); request.setParam(PARAM_PROJECT, project.getDbKey()) .setParam(PARAM_VISIBILITY, initiallyPrivate ? PRIVATE : PUBLIC) .execute(); assertThat(isPrivateInDb(project)).isEqualTo(initiallyPrivate); assertThat(isPrivateInDb(branch)).isEqualTo(initiallyPrivate); assertThat(isPrivateInDb(module)).isEqualTo(initiallyPrivate); assertThat(isPrivateInDb(dir)).isEqualTo(!initiallyPrivate); assertThat(isPrivateInDb(file)).isEqualTo(initiallyPrivate); }
@Test public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() { ComponentDto project = prepareProject(p -> p.setPrivate(true)); ComponentDto module = newModuleDto(project).setPrivate(false); db.components().insertComponent(module); ComponentDto dir = db.components().insertComponent(newDirectory(module, "DEFG", "Directory").setDbKey("DIR").setPrivate(true)); treeRootHolder.setRoot(createSampleProjectComponentTree(project)); underTest.execute(new TestComputationStepContext()); Stream.of(project.uuid(), module.uuid(), dir.uuid()) .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(db.getSession(), uuid).get().isPrivate()) .describedAs("for uuid " + uuid) .isEqualTo(true)); }
@Test public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() { boolean isRootPrivate = new Random().nextBoolean(); ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert())); OrganizationDto organization = dbTester.organizations().insert(); ComponentDto view = newViewDto(organization).setUuid(VIEW_UUID).setDbKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate); dbTester.components().insertComponent(view); ComponentDto subView = newSubView(view).setUuid("BCDE").setDbKey("MODULE").setPrivate(!isRootPrivate); dbTester.components().insertComponent(subView); dbTester.components().insertComponent(newProjectCopy("DEFG", project, view).setDbKey("DIR").setPrivate(isRootPrivate)); treeRootHolder.setRoot( createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null) .addChildren( createProjectView1Builder(project, null).build()) .build()) .build()); underTest.execute(new TestComputationStepContext()); Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID, subView.uuid(), "DEFG") .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate()) .describedAs("for uuid " + uuid) .isEqualTo(isRootPrivate)); }
@Test public void persists_new_components_with_visibility_of_root_in_db_out_of_functional_transaction() { boolean isRootPrivate = new Random().nextBoolean(); ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert())); OrganizationDto organization = dbTester.organizations().insert(); ComponentDto view = newViewDto(organization).setUuid(VIEW_UUID).setDbKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate); dbTester.components().insertComponent(view); treeRootHolder.setRoot( createViewBuilder(PORTFOLIO) .addChildren( createSubView1Builder(null) .addChildren( createProjectView1Builder(project, null).build()) .build()) .build()); underTest.execute(new TestComputationStepContext()); Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID) .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate()) .describedAs("for uuid " + uuid) .isEqualTo(isRootPrivate)); }
private ComponentDto createRootComponent(DbSession session, NewComponent newComponent) { checkBranchFormat(newComponent.qualifier(), newComponent.deprecatedBranch()); String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.deprecatedBranch()); checkRequest(!dbClient.componentDao().selectByKey(session, keyWithBranch).isPresent(), "Could not create %s, key already exists: %s", getQualifierToDisplay(newComponent.qualifier()), keyWithBranch); String uuid = Uuids.create(); ComponentDto component = new ComponentDto() .setOrganizationUuid(newComponent.getOrganizationUuid()) .setUuid(uuid) .setUuidPath(ComponentDto.UUID_PATH_OF_ROOT) .setRootUuid(uuid) .setModuleUuid(null) .setModuleUuidPath(ComponentDto.UUID_PATH_SEPARATOR + uuid + ComponentDto.UUID_PATH_SEPARATOR) .setProjectUuid(uuid) .setDbKey(keyWithBranch) .setName(newComponent.name()) .setLongName(newComponent.name()) .setScope(Scopes.PROJECT) .setQualifier(newComponent.qualifier()) .setPrivate(newComponent.isPrivate()) .setCreatedAt(new Date(system2.now())); dbClient.componentDao().insert(session, component); return component; }
@Test public void return_error_on_not_existing_branch() throws ParseException { ComponentDto project = db.components().insertMainBranch(p -> p.setPrivate(false)); userSession.registerComponents(project); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); TestResponse response = ws.newRequest() .setParam("project", branch.getKey()) .setParam("branch", "unknown") .execute(); checkError(response, "Project has not been found"); }
@Test public void return_error_on_short_living_branch() throws ParseException { ComponentDto project = db.components().insertMainBranch(p -> p.setPrivate(false)); userSession.registerComponents(project); ComponentDto shortBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT)); TestResponse response = ws.newRequest() .setParam("project", shortBranch.getKey()) .setParam("branch", shortBranch.getBranch()) .execute(); checkError(response, "Project is invalid"); }
public static ComponentDto newChildComponent(String uuid, ComponentDto moduleOrProject, ComponentDto parent) { checkArgument(moduleOrProject.isPrivate() == parent.isPrivate(), "private flag inconsistent between moduleOrProject (%s) and parent (%s)", moduleOrProject.isPrivate(), parent.isPrivate()); return new ComponentDto() .setOrganizationUuid(parent.getOrganizationUuid()) .setUuid(uuid) .setUuidPath(formatUuidPathFromParent(parent)) .setProjectUuid(moduleOrProject.projectUuid()) .setRootUuid(moduleOrProject.uuid()) .setModuleUuid(moduleOrProject.uuid()) .setModuleUuidPath(moduleOrProject.moduleUuidPath()) .setMainBranchProjectUuid(moduleOrProject.getMainBranchProjectUuid()) .setCreatedAt(new Date()) .setEnabled(true) .setPrivate(moduleOrProject.isPrivate()); }
public static ComponentDto newProjectBranch(ComponentDto project, BranchDto branchDto) { checkArgument(project.qualifier().equals(Qualifiers.PROJECT) || project.qualifier().equals(Qualifiers.APP)); checkArgument(project.getMainBranchProjectUuid() == null); String branchName = branchDto.getKey(); String branchSeparator = branchDto.getBranchType() == PULL_REQUEST ? PULL_REQUEST_SEPARATOR : BRANCH_KEY_SEPARATOR; String uuid = branchDto.getUuid(); return new ComponentDto() .setUuid(uuid) .setOrganizationUuid(project.getOrganizationUuid()) .setUuidPath(UUID_PATH_OF_ROOT) .setProjectUuid(uuid) .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR) .setRootUuid(uuid) // name of the branch is not mandatory on the main branch .setDbKey(branchName != null ? project.getDbKey() + branchSeparator + branchName : project.getKey()) .setMainBranchProjectUuid(project.uuid()) .setName(project.name()) .setLongName(project.longName()) .setDescription(project.description()) .setScope(project.scope()) .setQualifier(project.qualifier()) .setPath(null) .setLanguage(null) .setEnabled(true) .setPrivate(project.isPrivate()); } }
private static ComponentDto newProjectDto(String organizationUuid, String uuid, boolean isPrivate) { return new ComponentDto() .setOrganizationUuid(organizationUuid) .setUuid(uuid) .setUuidPath(UUID_PATH_OF_ROOT) .setProjectUuid(uuid) .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR) .setRootUuid(uuid) .setDbKey("KEY_" + uuid) .setName("NAME_" + uuid) .setLongName("LONG_NAME_" + uuid) .setDescription("DESCRIPTION_" + uuid) .setScope(Scopes.PROJECT) .setQualifier(Qualifiers.PROJECT) .setPath(null) .setLanguage(null) .setEnabled(true) .setPrivate(isPrivate); }
@Test public void selectPrivateProjectsWithNcloc() { MetricDto metric = db.measures().insertMetric(m -> m.setKey("ncloc")); OrganizationDto organizationDto = db.organizations().insert(); // project1, not the biggest branch - not returned final ComponentDto project1 = db.components().insertMainBranch(organizationDto, b -> b.setName("foo")); insertMeasure(20d, project1, metric); // long branch of project1 - returned insertMeasure(30d, db.components().insertProjectBranch(project1, b -> b.setBranchType(BranchType.LONG)), metric); // project2 - returned insertMeasure(10d, db.components().insertMainBranch(organizationDto, b -> b.setName("bar")), metric); // public project - not returned insertMeasure(11d, db.components().insertMainBranch(organizationDto, b -> b.setPrivate(false)), metric); // different org - not returned insertMeasure(12d, db.components().insertMainBranch(db.organizations().insert()), metric); List<ProjectNclocDistributionDto> result = underTest.selectPrivateProjectsWithNcloc(db.getSession(), organizationDto.getUuid()); assertThat(result).extracting(ProjectNclocDistributionDto::getName).containsExactly("foo", "bar"); assertThat(result).extracting(ProjectNclocDistributionDto::getNcloc).containsExactly(30L, 10L); }
@Test public void quality_gate_on_long_living_branch() { ComponentDto project = db.components().insertMainBranch(p -> p.setPrivate(false)); userSession.registerComponents(project); MetricDto metric = createQualityGateMetric(); db.measures().insertLiveMeasure(project, metric, m -> m.setData(OK.name())); ComponentDto longBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); db.measures().insertLiveMeasure(longBranch, metric, m -> m.setData(ERROR.name())); TestResponse response = ws.newRequest() .setParam("project", longBranch.getKey()) .setParam("branch", longBranch.getBranch()) .execute(); checkResponse(response, ERROR); }
@Test public void json_example() { OrganizationDto organization = db.organizations().insertForKey("my-org-1"); userSession.addPermission(ADMINISTER, organization); ComponentDto publicProject = newPrivateProjectDto(organization, "project-uuid-1").setName("Project Name 1").setDbKey("project-key-1").setPrivate(false); ComponentDto privateProject = newPrivateProjectDto(organization, "project-uuid-2").setName("Project Name 1").setDbKey("project-key-2"); db.components().insertComponents( publicProject, privateProject); db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(publicProject).setCreatedAt(parseDateTime("2017-03-01T11:39:03+0300").getTime())); db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(privateProject).setCreatedAt(parseDateTime("2017-03-02T15:21:47+0300").getTime())); db.commit(); String response = ws.newRequest() .setMediaType(MediaTypes.JSON) .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute().getInput(); assertJson(response).isSimilarTo(ws.getDef().responseExampleAsString()); assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(response); }
@Test public void provisioned_projects_as_defined_in_the_example() { OrganizationDto org = db.organizations().insert(); ComponentDto hBaseProject = ComponentTesting.newPrivateProjectDto(org, "ce4c03d6-430f-40a9-b777-ad877c00aa4d") .setDbKey("org.apache.hbas:hbase") .setName("HBase") .setCreatedAt(DateUtils.parseDateTime("2015-03-04T23:03:44+0100")) .setPrivate(false); ComponentDto roslynProject = ComponentTesting.newPrivateProjectDto(org, "c526ef20-131b-4486-9357-063fa64b5079") .setDbKey("com.microsoft.roslyn:roslyn") .setName("Roslyn") .setCreatedAt(DateUtils.parseDateTime("2013-03-04T23:03:44+0100")); db.components().insertComponents(hBaseProject, roslynProject); userSessionRule.logIn().addPermission(PROVISION_PROJECTS, org); TestResponse result = ws.newRequest() .setParam(PARAM_ORGANIZATION, org.getKey()) .execute(); assertJson(result.getInput()).isSimilarTo(ws.getDef().responseExampleAsString()); }
@Test public void ghost_projects_base_on_json_example() { OrganizationDto organization = db.organizations().insert(); ComponentDto hBaseProject = ComponentTesting.newPrivateProjectDto(organization, "ce4c03d6-430f-40a9-b777-ad877c00aa4d") .setDbKey("org.apache.hbas:hbase") .setName("HBase") .setCreatedAt(DateUtils.parseDateTime("2015-03-04T23:03:44+0100")) .setPrivate(false); dbClient.componentDao().insert(db.getSession(), hBaseProject); dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(hBaseProject) .setStatus(STATUS_UNPROCESSED)); ComponentDto roslynProject = ComponentTesting.newPrivateProjectDto(organization, "c526ef20-131b-4486-9357-063fa64b5079") .setDbKey("com.microsoft.roslyn:roslyn") .setName("Roslyn") .setCreatedAt(DateUtils.parseDateTime("2013-03-04T23:03:44+0100")); dbClient.componentDao().insert(db.getSession(), roslynProject); dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(roslynProject) .setStatus(STATUS_UNPROCESSED)); db.getSession().commit(); userSessionRule.logIn().addPermission(ADMINISTER, organization); TestResponse result = ws.newRequest() .setParam("organization", organization.getKey()) .execute(); assertJson(result.getInput()).isSimilarTo(ws.getDef().responseExampleAsString()); }