@Test public void indexOnStartup_indexes_all_projects() { OrganizationDto organization = db.organizations().insert(); SnapshotDto project1 = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); SnapshotDto project2 = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); SnapshotDto project3 = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); underTest.indexOnStartup(emptySet()); assertThatIndexContainsOnly(project1, project2, project3); }
@Test public void count_provisioned() { OrganizationDto organization = db.organizations().insert(); db.components().insertPrivateProject(organization); db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization)); Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setOnProvisionedOnly(true); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT).build())).isEqualTo(1); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.VIEW).build())).isEqualTo(0); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, Qualifiers.VIEW).build())).isEqualTo(1); }
@Test public void selectByQuery_name_with_special_characters() { db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization()).setName("project-\\_%/-name")); ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("-\\_%/-").setQualifiers(PROJECT).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10); assertThat(result).hasSize(1); assertThat(result.get(0).name()).isEqualTo("project-\\_%/-name"); }
@Test public void selectByQuery_key_with_special_characters() { db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.organizations().insert()).setDbKey("project-_%-key")); db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.organizations().insert()).setDbKey("project-key-that-does-not-match")); ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("project-_%-key").setQualifiers(PROJECT).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10); assertThat(result).hasSize(1); assertThat(result.get(0).getDbKey()).isEqualTo("project-_%-key"); }
@Test public void update_name_and_description() { SnapshotDto analysis = dbTester.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert())); dbTester.events().insertEvent(newEvent(analysis).setUuid("E1")); underTest.update(dbSession, "E1", "New Name", "New Description"); EventDto result = dbClient.eventDao().selectByUuid(dbSession, "E1").get(); assertThat(result.getName()).isEqualTo("New Name"); assertThat(result.getDescription()).isEqualTo("New Description"); }
@Test public void create_event_on_application() { ComponentDto application = ComponentTesting.newApplication(db.getDefaultOrganization()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(application); logInAsProjectAdministrator(application); CreateEventResponse result = call(OTHER.name(), "Application Event", analysis.getUuid()); ProjectAnalyses.Event event = result.getEvent(); assertThat(event.getName()).isEqualTo("Application Event"); }
@Test public void show_with_browse_permission() { ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid"); db.components().insertProjectAndSnapshot(project); userSession.logIn().addProjectPermission(USER, project); ShowWsResponse response = newRequest("project-uuid", null); assertThat(response.getComponent().getId()).isEqualTo("project-uuid"); }
@Test public void create_version_event() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); logInAsProjectAdministrator(project); call(VERSION.name(), "5.6.3", analysis.getUuid()); Optional<SnapshotDto> newAnalysis = dbClient.snapshotDao().selectByUuid(dbSession, analysis.getUuid()); assertThat(newAnalysis.get().getVersion()).isEqualTo("5.6.3"); }
@Test public void select_projects() { OrganizationDto organization = db.organizations().insert(); ComponentDto provisionedProject = db.components().insertPrivateProject(); ComponentDto provisionedView = db.components().insertView(organization); String projectUuid = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)).getComponentUuid(); String disabledProjectUuid = db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization).setEnabled(false)).getComponentUuid(); String viewUuid = db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization)).getComponentUuid(); assertThat(underTest.selectProjects(dbSession)) .extracting(ComponentDto::uuid) .containsOnly(provisionedProject.uuid(), projectUuid); }
@Test public void create_event_as_project_admin() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "P1"); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); logInAsProjectAdministrator(project); CreateEventResponse result = call(VERSION.name(), "5.6.3", analysis.getUuid()); assertThat(result.getEvent().getKey()).isNotEmpty(); }
@Test public void create_2_version_events_on_same_project() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); SnapshotDto firstAnalysis = db.components().insertProjectAndSnapshot(project); SnapshotDto secondAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)); db.commit(); logInAsProjectAdministrator(project); call(VERSION.name(), "5.6.3", firstAnalysis.getUuid()); call(VERSION.name(), "6.3", secondAnalysis.getUuid()); List<EventDto> events = dbClient.eventDao().selectByComponentUuid(dbSession, project.uuid()); assertThat(events).hasSize(2); }
@Test public void throw_ForbiddenException_if_not_project_administrator() { SnapshotDto analysis = db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.organizations().insert(), "P1")); userSession.logIn(); expectedException.expect(ForbiddenException.class); expectedException.expectMessage("Insufficient privileges"); call(VERSION.name(), "5.6.3", analysis.getUuid()); }
@Test public void fail_if_category_other_than_authorized() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); logInAsProjectAdministrator(project); expectedException.expect(IllegalArgumentException.class); ws.newRequest() .setParam(PARAM_ANALYSIS, analysis.getUuid()) .setParam(PARAM_NAME, "Project Import") .setParam(PARAM_CATEGORY, "QP") .execute(); }
@Test public void fail_if_not_enough_permission() { SnapshotDto analysis = db.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto(db.organizations().insert())); db.events().insertEvent(newEvent(analysis).setUuid("E1")); userSession.logIn(); expectedException.expect(ForbiddenException.class); call("E1"); }
@Test public void throw_ForbiddenException_if_not_project_administrator() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); db.events().insertEvent(newEvent(analysis).setUuid("E1")); userSession.logIn().addProjectPermission(UserRole.USER, project); expectedException.expect(ForbiddenException.class); call("E1", "name"); }
@Test public void select_descendants_with_leaves_strategy() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), PROJECT_UUID); db.components().insertProjectAndSnapshot(project); db.components().insertComponent(newModuleDto("module-1-uuid", project)); db.components().insertComponent(newFileDto(project, null, "file-1-uuid")); db.components().insertComponent(newFileDto(project, null, "file-2-uuid")); db.commit(); ComponentTreeQuery query = newTreeQuery(PROJECT_UUID).setStrategy(LEAVES).build(); List<ComponentDto> result = underTest.selectDescendants(dbSession, query); assertThat(result).extracting("uuid").containsOnly("file-1-uuid", "file-2-uuid", "module-1-uuid"); }
@Test public void fail_if_2_version_events_on_the_same_analysis() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); logInAsProjectAdministrator(project); call(VERSION.name(), "5.6.3", analysis.getUuid()); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("A version event already exists on analysis '" + analysis.getUuid() + "'"); call(VERSION.name(), "6.3", analysis.getUuid()); }
@Test public void fail_if_2_other_events_on_same_analysis_with_same_name() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); logInAsProjectAdministrator(project); call(OTHER.name(), "Project Import", analysis.getUuid()); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("An 'Other' event with the same name already exists on analysis '" + analysis.getUuid() + "'"); call(OTHER.name(), "Project Import", analysis.getUuid()); }
@Test public void delete_event() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); SnapshotDto analysis = db.components().insertProjectAndSnapshot(project); db.events().insertEvent(newEvent(analysis).setUuid("E1")); db.events().insertEvent(newEvent(analysis).setUuid("E2")); logInAsProjectAdministrator(project); call("E2"); List<EventDto> events = db.getDbClient().eventDao().selectByAnalysisUuid(db.getSession(), analysis.getUuid()); assertThat(events).extracting(EventDto::getUuid).containsExactly("E1"); }
@Test public void selectLastSnapshotsByRootComponentUuids_returns_snapshots_flagged_as_last() { ComponentDto firstProject = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "PROJECT_UUID_1")); dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(false)); SnapshotDto lastSnapshotOfFirstProject = dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(true)); ComponentDto secondProject = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "PROJECT_UUID_2")); SnapshotDto lastSnapshotOfSecondProject = dbClient.snapshotDao().insert(dbSession, newAnalysis(secondProject).setLast(true)); db.components().insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization())); List<SnapshotDto> result = underTest.selectLastAnalysesByRootComponentUuids(dbSession, newArrayList(firstProject.uuid(), secondProject.uuid())); assertThat(result).extracting(SnapshotDto::getId).containsOnly(lastSnapshotOfFirstProject.getId(), lastSnapshotOfSecondProject.getId()); }