public void bulkUpdateKey(DbSession dbSession, ComponentDto projectOrModule, String stringToReplace, String replacementString) { Set<ComponentKeyUpdaterDao.RekeyedResource> rekeyedProjects = dbClient.componentKeyUpdaterDao().bulkUpdateKey( dbSession, projectOrModule.uuid(), stringToReplace, replacementString, ComponentService::isMainProject); projectIndexers.commitAndIndex(dbSession, singletonList(projectOrModule), ProjectIndexer.Cause.PROJECT_KEY_UPDATE); if (!rekeyedProjects.isEmpty()) { projectLifeCycleListeners.onProjectsRekeyed(rekeyedProjects.stream() .map(ComponentService::toRekeyedProject) .collect(MoreCollectors.toSet(rekeyedProjects.size()))); } }
@Test public void shouldBulkUpdateKey() { db.prepareDbUnit(getClass(), "shared.xml"); underTest.bulkUpdateKey(dbSession, "A", "org.struts", "org.apache.struts", doNotReturnAnyRekeyedResource()); dbSession.commit(); db.assertDbUnit(getClass(), "shouldBulkUpdateKey-result.xml", "projects"); }
@Test public void shouldBulkUpdateKeyOnOnlyOneSubmodule() { db.prepareDbUnit(getClass(), "shared.xml"); underTest.bulkUpdateKey(dbSession, "A", "struts-ui", "struts-web", doNotReturnAnyRekeyedResource()); dbSession.commit(); db.assertDbUnit(getClass(), "shouldBulkUpdateKeyOnOnlyOneSubmodule-result.xml", "projects"); }
@Test public void shouldNotUpdateAllSubmodules() { db.prepareDbUnit(getClass(), "shouldNotUpdateAllSubmodules.xml"); underTest.bulkUpdateKey(dbSession, "A", "org.struts", "org.apache.struts", doNotReturnAnyRekeyedResource()); dbSession.commit(); db.assertDbUnit(getClass(), "shouldNotUpdateAllSubmodules-result.xml", "projects"); }
@Test public void shouldFailBulkUpdateKeyIfKeyAlreadyExist() { db.prepareDbUnit(getClass(), "shared.xml"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Impossible to update key: a component with key \"foo:struts-core\" already exists."); underTest.bulkUpdateKey(dbSession, "A", "org.struts", "foo", doNotReturnAnyRekeyedResource()); dbSession.commit(); }
@Test public void bulk_updateKey_on_branch_containing_slash() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch/with/slash")); String newKey = "newKey"; underTest.bulkUpdateKey(dbSession, project.uuid(), project.getKey(), newKey, t -> true); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, newKey)).hasSize(1); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, ComponentDto.generateBranchKey(newKey, branch.getBranch()))).hasSize(1); }
@Test public void fail_when_new_key_is_invalid() { ComponentDto project = db.components().insertPrivateProject(); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Malformed key for 'my?project?key'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit."); underTest.bulkUpdateKey(dbSession, project.uuid(), project.getDbKey(), "my?project?key", doNotReturnAnyRekeyedResource()); }
Set<RekeyedResource> rekeyedResources = underTest.bulkUpdateKey(dbSession, project.uuid(), oldProjectKey, newProjectKey, a -> true);
Set<RekeyedResource> rekeyedResources = underTest.bulkUpdateKey(dbSession, project.uuid(), oldProjectKey, newProjectKey, t -> true);
@Test public void bulk_update_key_updates_disabled_components() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "A").setDbKey("my_project")); db.components().insertComponent(newModuleDto(project).setDbKey("my_project:module")); db.components().insertComponent(newModuleDto(project).setDbKey("my_project:inactive_module").setEnabled(false)); Set<RekeyedResource> rekeyedResources = underTest.bulkUpdateKey(dbSession, "A", "my_", "your_", doNotReturnAnyRekeyedResource()); List<ComponentDto> result = dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, "your_project"); assertThat(result) .hasSize(3) .extracting(ComponentDto::getDbKey) .containsOnlyOnce("your_project", "your_project:module", "your_project:inactive_module"); }
public void bulkUpdateKey(DbSession dbSession, ComponentDto projectOrModule, String stringToReplace, String replacementString) { Set<ComponentKeyUpdaterDao.RekeyedResource> rekeyedProjects = dbClient.componentKeyUpdaterDao().bulkUpdateKey( dbSession, projectOrModule.uuid(), stringToReplace, replacementString, ComponentService::isMainProject); projectIndexers.commitAndIndex(dbSession, singletonList(projectOrModule), ProjectIndexer.Cause.PROJECT_KEY_UPDATE); if (!rekeyedProjects.isEmpty()) { projectLifeCycleListeners.onProjectsRekeyed(rekeyedProjects.stream() .map(ComponentService::toRekeyedProject) .collect(MoreCollectors.toSet(rekeyedProjects.size()))); } }