aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-03 17:45:37 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-05 14:46:58 +0200
commit000f6b88a4cb60410a2bad5ef976bb783b3c7ba9 (patch)
tree6aa0f8f94bf11d82beed50ac7d38a833455a35f0 /sonar-db
parent645d874eda971f79a0927aa6370adb9489f148b7 (diff)
downloadsonarqube-000f6b88a4cb60410a2bad5ef976bb783b3c7ba9.tar.gz
sonarqube-000f6b88a4cb60410a2bad5ef976bb783b3c7ba9.zip
SONAR-7929 Create WS api/components/bulk_update_key
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ResourceKeyUpdaterDao.java42
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/ResourceKeyUpdaterDaoTest.java37
2 files changed, 74 insertions, 5 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ResourceKeyUpdaterDao.java b/sonar-db/src/main/java/org/sonar/db/component/ResourceKeyUpdaterDao.java
index 4c0fb971c34..24fd831d68a 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/ResourceKeyUpdaterDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/ResourceKeyUpdaterDao.java
@@ -19,6 +19,7 @@
*/
package org.sonar.db.component;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
@@ -26,18 +27,26 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.core.component.ComponentKeys.isValidModuleKey;
+
/**
* Class used to rename the key of a project and its resources.
*
* @since 3.2
*/
public class ResourceKeyUpdaterDao implements Dao {
+ private static final Set<String> PROJECT_OR_MODULE_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.MODULE);
+
private MyBatis mybatis;
public ResourceKeyUpdaterDao(MyBatis mybatis) {
@@ -87,6 +96,29 @@ public class ResourceKeyUpdaterDao implements Dao {
return result;
}
+ public static void checkIsProjectOrModule(ComponentDto component) {
+ checkArgument(PROJECT_OR_MODULE_QUALIFIERS.contains(component.qualifier()), "Component updated must be a module or a key");
+ }
+
+ /**
+ *
+ * @return a map with currentKey/newKey is a bulk update was executed
+ */
+ public Map<String, String> simulateBulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) {
+ return collectAllModules(projectUuid, stringToReplace, mapper(dbSession))
+ .stream()
+ .collect(Collectors.toMap(
+ ResourceDto::getKey,
+ component -> computeNewKey(component, stringToReplace, replacementString)));
+ }
+
+ /**
+ * @return a map with the component key as key, and boolean as true if key already exists in db
+ */
+ public Map<String, Boolean> checkComponentKeys(DbSession dbSession, List<String> newComponentKeys) {
+ return newComponentKeys.stream().collect(Collectors.toMap(Function.identity(), key -> mapper(dbSession).countResourceByKey(key) > 0));
+ }
+
public void bulkUpdateKey(DbSession session, String projectUuid, String stringToReplace, String replacementString) {
ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
// must SELECT first everything
@@ -139,11 +171,15 @@ public class ResourceKeyUpdaterDao implements Dao {
private static void checkNewNameOfAllModules(Set<ResourceDto> modules, String stringToReplace, String replacementString, ResourceKeyUpdaterMapper mapper) {
for (ResourceDto module : modules) {
- String newName = computeNewKey(module, stringToReplace, replacementString);
- if (mapper.countResourceByKey(newName) > 0) {
- throw new IllegalStateException("Impossible to update key: a resource with \"" + newName + "\" key already exists.");
+ String newKey = computeNewKey(module, stringToReplace, replacementString);
+ checkArgument(isValidModuleKey(newKey), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", newKey);
+ if (mapper.countResourceByKey(newKey) > 0) {
+ throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists.");
}
}
}
+ private ResourceKeyUpdaterMapper mapper(DbSession dbSession) {
+ return dbSession.getMapper(ResourceKeyUpdaterMapper.class);
+ }
}
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ResourceKeyUpdaterDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ResourceKeyUpdaterDaoTest.java
index df673897e46..fc6fe33e731 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/ResourceKeyUpdaterDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/ResourceKeyUpdaterDaoTest.java
@@ -28,7 +28,9 @@ import org.sonar.api.utils.System2;
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.newFileDto;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
@@ -87,8 +89,8 @@ public class ResourceKeyUpdaterDaoTest {
public void shouldFailBulkUpdateKeyIfKeyAlreadyExist() {
db.prepareDbUnit(getClass(), "shared.xml");
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Impossible to update key: a resource with \"foo:struts-core\" key already exists.");
+ 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");
dbSession.commit();
@@ -117,6 +119,16 @@ public class ResourceKeyUpdaterDaoTest {
}
@Test
+ public void fail_when_new_key_is_invalid() {
+ ComponentDto project = componentDb.insertProject();
+
+ 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.key(), "my?project?key");
+ }
+
+ @Test
public void shouldCheckModuleKeysBeforeRenaming() {
db.prepareDbUnit(getClass(), "shared.xml");
@@ -127,4 +139,25 @@ public class ResourceKeyUpdaterDaoTest {
assertThat(checkResults.get("org.struts:struts-ui")).isEqualTo("foo:struts-ui");
}
+ @Test
+ public void check_component_keys() {
+ db.prepareDbUnit(getClass(), "shared.xml");
+
+ Map<String, Boolean> result = underTest.checkComponentKeys(dbSession, newArrayList("foo:struts", "foo:struts-core", "foo:struts-ui"));
+
+ assertThat(result)
+ .hasSize(3)
+ .containsOnly(entry("foo:struts", false), entry("foo:struts-core", true), entry("foo:struts-ui", false));
+ }
+
+ @Test
+ public void simulate_bulk_update_key() {
+ db.prepareDbUnit(getClass(), "shared.xml");
+
+ Map<String, String> result = underTest.simulateBulkUpdateKey(dbSession, "A", "org.struts", "foo");
+
+ assertThat(result)
+ .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"));
+ }
}