From 886674d75083f2e067460b8113f1153c0bab404b Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 11 Aug 2016 11:34:56 +0200 Subject: [PATCH] SONAR-7928 SONAR-7929 Update and bulk update of component key does not update disabled component --- .../ComponentServiceUpdateKeyTest.java | 6 +- .../component/ws/BulkUpdateKeyActionTest.java | 8 +++ .../component/ComponentKeyUpdaterMapper.xml | 5 +- .../component/ComponentKeyUpdaterDaoTest.java | 61 ++++++++++++++++++- 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java index c4b35a88ec0..808ab9f8eae 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java @@ -70,8 +70,8 @@ public class ComponentServiceUpdateKeyTest { @Test public void update_project_key() { ComponentDto project = insertSampleRootProject(); - ComponentDto file = ComponentTesting.newFileDto(project, null).setKey("sample:root:src/File.xoo"); - dbClient.componentDao().insert(dbSession, file); + ComponentDto file = componentDb.insertComponent(ComponentTesting.newFileDto(project, null).setKey("sample:root:src/File.xoo")); + ComponentDto inactiveFile = componentDb.insertComponent(ComponentTesting.newFileDto(project, null).setKey("sample:root:src/InactiveFile.xoo").setEnabled(false)); dbSession.commit(); @@ -86,6 +86,8 @@ public class ComponentServiceUpdateKeyTest { // Check file key has been updated assertThat(underTest.getNullableByKey(file.key())).isNull(); assertThat(underTest.getNullableByKey("sample2:root:src/File.xoo")).isNotNull(); + + assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getKey())).isPresent(); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/BulkUpdateKeyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/BulkUpdateKeyActionTest.java index 722e8cc6fb2..5260f86e683 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/BulkUpdateKeyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/BulkUpdateKeyActionTest.java @@ -117,7 +117,9 @@ public class BulkUpdateKeyActionTest { public void bulk_update_project_key() { ComponentDto project = insertMyProject(); ComponentDto module = componentDb.insertComponent(newModuleDto(project).setKey("my_project:root:module")); + ComponentDto inactiveModule = componentDb.insertComponent(newModuleDto(project).setKey("my_project:root:inactive_module").setEnabled(false)); ComponentDto file = componentDb.insertComponent(newFileDto(module, null).setKey("my_project:root:module:src/File.xoo")); + ComponentDto inactiveFile = componentDb.insertComponent(newFileDto(module, null).setKey("my_project:root:module:src/InactiveFile.xoo").setEnabled(false)); BulkUpdateKeyWsResponse result = callByUuid(project.uuid(), FROM, TO); @@ -130,6 +132,8 @@ public class BulkUpdateKeyActionTest { assertComponentKeyUpdated(project.key(), "your_project"); assertComponentKeyUpdated(module.key(), "your_project:root:module"); assertComponentKeyUpdated(file.key(), "your_project:root:module:src/File.xoo"); + assertComponentKeyNotUpdated(inactiveModule.key()); + assertComponentKeyNotUpdated(inactiveFile.key()); } @Test @@ -244,6 +248,10 @@ public class BulkUpdateKeyActionTest { assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent(); } + private void assertComponentKeyNotUpdated(String key) { + assertThat(dbClient.componentDao().selectByKey(dbSession, key)).isPresent(); + } + private ComponentDto insertMyProject() { return componentDb.insertComponent(newProjectDto().setKey(MY_PROJECT_KEY)); } diff --git a/sonar-db/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml index c1adecc5f45..2a98c2cdad2 100644 --- a/sonar-db/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml @@ -10,6 +10,7 @@ + diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java index ede24716973..25b86210b58 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java @@ -20,18 +20,22 @@ package org.sonar.db.component; import com.google.common.base.Strings; +import java.util.List; import java.util.Map; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; +import static org.sonar.db.component.ComponentTesting.newDirectory; import static org.sonar.db.component.ComponentTesting.newFileDto; +import static org.sonar.db.component.ComponentTesting.newModuleDto; import static org.sonar.db.component.ComponentTesting.newProjectDto; public class ComponentKeyUpdaterDaoTest { @@ -41,7 +45,8 @@ public class ComponentKeyUpdaterDaoTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - private DbSession dbSession = db.getSession(); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); ComponentDbTester componentDb = new ComponentDbTester(db); ComponentKeyUpdaterDao underTest = db.getDbClient().componentKeyUpdaterDao(); @@ -55,6 +60,22 @@ public class ComponentKeyUpdaterDaoTest { db.assertDbUnit(getClass(), "shouldUpdateKey-result.xml", "projects"); } + @Test + public void update_key_does_not_updated_inactive_components() { + ComponentDto project = componentDb.insertComponent(newProjectDto("A").setKey("my_project")); + ComponentDto directory = componentDb.insertComponent(newDirectory(project, "/directory").setKey("my_project:directory")); + componentDb.insertComponent(newFileDto(project, directory).setKey("my_project:directory/file")); + ComponentDto inactiveDirectory = componentDb.insertComponent(newDirectory(project, "/inactive_directory").setKey("my_project:inactive_directory").setEnabled(false)); + componentDb.insertComponent(newFileDto(project, inactiveDirectory).setKey("my_project:inactive_directory/file").setEnabled(false)); + + underTest.updateKey("A", "your_project"); + db.commit(); + + List result = dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, "your_project"); + assertThat(result).hasSize(5).extracting(ComponentDto::getKey) + .containsOnlyOnce("your_project", "your_project:directory", "your_project:directory/file", "my_project:inactive_directory", "my_project:inactive_directory/file"); + } + @Test public void shouldNotUpdateKey() { db.prepareDbUnit(getClass(), "shared.xml"); @@ -65,6 +86,19 @@ public class ComponentKeyUpdaterDaoTest { underTest.updateKey("B", "org.struts:struts-ui"); } + @Test + public void bulk_update_key_does_not_update_inactive_components() { + ComponentDto project = componentDb.insertComponent(newProjectDto("A").setKey("my_project")); + componentDb.insertComponent(newModuleDto(project).setKey("my_project:module")); + componentDb.insertComponent(newModuleDto(project).setKey("my_project:inactive_module").setEnabled(false)); + + underTest.bulkUpdateKey(dbSession, "A", "my_", "your_"); + + List result = dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, "your_project"); + assertThat(result).hasSize(3).extracting(ComponentDto::getKey) + .containsOnlyOnce("your_project", "your_project:module", "my_project:inactive_module"); + } + @Test public void shouldBulkUpdateKey() { db.prepareDbUnit(getClass(), "shared.xml"); @@ -150,6 +184,18 @@ public class ComponentKeyUpdaterDaoTest { .containsOnly(entry("foo:struts", false), entry("foo:struts-core", true), entry("foo:struts-ui", false)); } + @Test + public void check_component_keys_checks_inactive_components() { + componentDb.insertComponent(newProjectDto().setKey("my-project")); + componentDb.insertComponent(newProjectDto().setKey("your-project").setEnabled(false)); + + Map result = underTest.checkComponentKeys(dbSession, newArrayList("my-project", "your-project", "new-project")); + + assertThat(result) + .hasSize(3) + .containsOnly(entry("my-project", true), entry("your-project", true), entry("new-project", false)); + } + @Test public void simulate_bulk_update_key() { db.prepareDbUnit(getClass(), "shared.xml"); @@ -160,4 +206,17 @@ public class ComponentKeyUpdaterDaoTest { .hasSize(3) .containsOnly(entry("org.struts:struts", "foo:struts"), entry("org.struts:struts-core", "foo:struts-core"), entry("org.struts:struts-ui", "foo:struts-ui")); } + + @Test + public void simulate_bulk_update_key_do_not_return_disable_components() { + ComponentDto project = componentDb.insertComponent(newProjectDto("A").setKey("project")); + componentDb.insertComponent(newModuleDto(project).setKey("project:enabled-module")); + componentDb.insertComponent(newModuleDto(project).setKey("project:disabled-module").setEnabled(false)); + + Map result = underTest.simulateBulkUpdateKey(dbSession, "A", "project", "new-project"); + + assertThat(result) + .hasSize(2) + .containsOnly(entry("project", "new-project"), entry("project:enabled-module", "new-project:enabled-module")); + } } -- 2.39.5