]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7928 SONAR-7929 Update and bulk update of component key does not update disable...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 11 Aug 2016 09:34:56 +0000 (11:34 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 11 Aug 2016 09:35:09 +0000 (11:35 +0200)
server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/BulkUpdateKeyActionTest.java
sonar-db/src/main/resources/org/sonar/db/component/ComponentKeyUpdaterMapper.xml
sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java

index c4b35a88ec0734a12927484ff84ecff18569db41..808ab9f8eae9d400d396437df79d07ecdf6559b8 100644 (file)
@@ -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
index 722e8cc6fb25a22d7afb2e891e8ba8f15a6d414e..5260f86e683e54180523d1b5d859d1277d131ce4 100644 (file)
@@ -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));
   }
index c1adecc5f45a3bbffadb9dd4e4567187476f4b34..2a98c2cdad2bbbd26d9a74601f4197c839da921e 100644 (file)
@@ -10,6 +10,7 @@
     <result property="deprecatedKey" column="deprecated_kee"/>
     <result property="rootUuid" column="root_uuid"/>
     <result property="scope" column="scope"/>
+    <result property="enabled" column="enabled"/>
   </resultMap>
 
   <select id="countResourceByKey" parameterType="String" resultType="int">
   </select>
 
   <select id="selectProjectResources" parameterType="String" resultMap="resourceResultMap">
-    select * from projects where root_uuid=#{rootUuid} AND scope!='PRJ'
+    select * from projects where root_uuid=#{rootUuid} AND scope!='PRJ' and enabled=${_true}
   </select>
 
   <select id="selectDescendantProjects" parameterType="String" resultMap="resourceResultMap">
-    select * from projects where scope='PRJ' and root_uuid=#{rootUuid} and uuid!=#{rootUuid}
+    select * from projects where scope='PRJ' and root_uuid=#{rootUuid} and uuid!=#{rootUuid} and enabled=${_true}
   </select>
 
   <update id="update" parameterType="Resource">
index ede24716973f2958c5a82b398e4e278e3b4baf96..25b86210b589c56a4818625644d161ef29a55694 100644 (file)
 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<ComponentDto> 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<ComponentDto> 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<String, Boolean> 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<String, String> 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"));
+  }
 }