diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-08-17 18:00:26 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-08-25 10:03:31 +0200 |
commit | 1e9159e3b8dad9afa097718259866122a866b101 (patch) | |
tree | 2364babc426843659e81af1c4754cd3ded0bdcb2 /sonar-db | |
parent | 8aa3feee31f00009dbed4d9c78d74003480b754d (diff) | |
download | sonarqube-1e9159e3b8dad9afa097718259866122a866b101.tar.gz sonarqube-1e9159e3b8dad9afa097718259866122a866b101.zip |
SONAR-7969 Create /api/settings/values WS
Diffstat (limited to 'sonar-db')
6 files changed, 205 insertions, 40 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java b/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java index b7325cee8a3..140d7edf142 100644 --- a/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java +++ b/sonar-db/src/main/java/org/sonar/db/property/PropertiesDao.java @@ -28,6 +28,7 @@ import java.sql.SQLException; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.dbutils.DbUtils; @@ -38,6 +39,8 @@ import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + public class PropertiesDao implements Dao { private static final String NOTIFICATION_PREFIX = "notification."; @@ -161,6 +164,18 @@ public class PropertiesDao implements Dao { return session.getMapper(PropertiesMapper.class).selectByQuery(query); } + public List<PropertyDto> selectGlobalPropertiesByKeys(DbSession session, Set<String> keys) { + return selectByKeys(session, keys, null); + } + + public List<PropertyDto> selectComponentPropertiesByKeys(DbSession session, Set<String> keys, long componentId) { + return selectByKeys(session, keys, componentId); + } + + private List<PropertyDto> selectByKeys(DbSession session, Set<String> keys, @Nullable Long componentId) { + return executeLargeInputs(keys, propertyKeys -> session.getMapper(PropertiesMapper.class).selectByKeys(propertyKeys, componentId)); + } + public void insertProperty(DbSession session, PropertyDto property) { PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); PropertyDto persistedProperty = mapper.selectByKey(property); diff --git a/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java b/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java index a90fed439af..e8ef34a84da 100644 --- a/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/property/PropertiesMapper.java @@ -39,6 +39,8 @@ public interface PropertiesMapper { PropertyDto selectByKey(PropertyDto key); + List<PropertyDto> selectByKeys(@Param("keys") List<String> keys, @Nullable @Param("componentId") Long componentId); + List<PropertyDto> selectByQuery(@Param("query") PropertyQuery query); List<PropertyDto> selectDescendantModuleProperties(@Param("moduleUuid") String moduleUuid, @Param(value = "scope") String scope, diff --git a/sonar-db/src/main/java/org/sonar/db/property/PropertyTesting.java b/sonar-db/src/main/java/org/sonar/db/property/PropertyTesting.java new file mode 100644 index 00000000000..88ab57acd12 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/property/PropertyTesting.java @@ -0,0 +1,98 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.property; + +import javax.annotation.Nullable; +import org.apache.commons.lang.math.RandomUtils; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.user.UserDto; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class PropertyTesting { + + private static int cursor = RandomUtils.nextInt(100); + + private PropertyTesting() { + // static methods only + } + + public static PropertyDto newGlobalPropertyDto(String key, String value) { + return newPropertyDto(key, value, (Long) null, null); + } + + public static PropertyDto newGlobalPropertyDto() { + return newPropertyDto((Long) null, null); + } + + public static PropertyDto newComponentPropertyDto(String key, String value, ComponentDto component) { + checkNotNull(component.getId()); + return newPropertyDto(key, value, component.getId(), null); + } + + public static PropertyDto newComponentPropertyDto(ComponentDto component) { + checkNotNull(component.getId()); + return newPropertyDto(component.getId(), null); + } + + public static PropertyDto newUserPropertyDto(String key, String value, UserDto user) { + checkNotNull(user.getId()); + return newPropertyDto(key, value, null, user.getId()); + } + + public static PropertyDto newUserPropertyDto(UserDto user) { + checkNotNull(user.getId()); + return newPropertyDto(null, user.getId()); + } + + public static PropertyDto newPropertyDto(String key, String value, ComponentDto component, UserDto user) { + checkNotNull(component.getId()); + checkNotNull(user.getId()); + return newPropertyDto(key, value, component.getId(), user.getId()); + } + + public static PropertyDto newPropertyDto(ComponentDto component, UserDto user) { + checkNotNull(component.getId()); + checkNotNull(user.getId()); + return newPropertyDto(component.getId(), user.getId()); + } + + private static PropertyDto newPropertyDto(@Nullable Long componentId, @Nullable Long userId) { + String key = String.valueOf(cursor); + cursor++; + String value = String.valueOf(cursor); + cursor++; + return newPropertyDto(key, value, componentId, userId); + } + + private static PropertyDto newPropertyDto(String key, String value, @Nullable Long componentId, @Nullable Long userId) { + PropertyDto propertyDto = new PropertyDto() + .setKey(key) + .setValue(value); + if (componentId != null) { + propertyDto.setResourceId(componentId); + } + if (userId != null) { + propertyDto.setUserId(userId); + } + return propertyDto; + } + +} diff --git a/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml b/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml index afb6f9d5c1d..a64c3756236 100644 --- a/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/property/PropertiesMapper.xml @@ -81,6 +81,24 @@ </if> </select> + <select id="selectByKeys" parameterType="map" resultType="Property"> + SELECT p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId + FROM properties p + <where> + AND p.prop_key in + <foreach collection="keys" open="(" close=")" item="key" separator=","> + #{key} + </foreach> + <if test="componentId == null"> + AND p.resource_id is null + </if> + <if test="componentId != null"> + AND p.resource_id=#{componentId} + </if> + AND p.user_id is null + </where> + </select> + <select id="selectByQuery" parameterType="map" resultType="Property"> select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId from properties p diff --git a/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java b/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java index a31013ac096..60fcf5edcb1 100644 --- a/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/property/PropertiesDaoTest.java @@ -20,18 +20,27 @@ package org.sonar.db.property; import com.google.common.collect.ImmutableMap; -import java.util.Arrays; import java.util.List; 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 org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentTesting; +import org.sonar.db.user.UserDto; +import org.sonar.db.user.UserTesting; +import static com.google.common.collect.Sets.newHashSet; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; - +import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto; +import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto; +import static org.sonar.db.property.PropertyTesting.newUserPropertyDto; public class PropertiesDaoTest { @@ -41,6 +50,9 @@ public class PropertiesDaoTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); + DbClient dbClient = dbTester.getDbClient(); + DbSession session = dbTester.getSession(); + PropertiesDao dao = dbTester.getDbClient().propertiesDao(); @Test @@ -98,18 +110,18 @@ public class PropertiesDaoTest { dbTester.prepareDbUnit(getClass(), "findNotificationSubscribers.xml"); // Nobody is subscribed - assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", Arrays.asList("NotSexyDispatcher"))).isFalse(); + assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", asList("NotSexyDispatcher"))).isFalse(); // Global subscribers - assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", Arrays.asList("DispatcherWithGlobalSubscribers"))).isTrue(); + assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", asList("DispatcherWithGlobalSubscribers"))).isTrue(); // Project subscribers - assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", Arrays.asList("DispatcherWithProjectSubscribers"))).isTrue(); - assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", Arrays.asList("DispatcherWithProjectSubscribers"))).isFalse(); + assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", asList("DispatcherWithProjectSubscribers"))).isTrue(); + assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", asList("DispatcherWithProjectSubscribers"))).isFalse(); // Global + Project subscribers - assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", Arrays.asList("DispatcherWithGlobalAndProjectSubscribers"))).isTrue(); - assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", Arrays.asList("DispatcherWithGlobalAndProjectSubscribers"))).isTrue(); + assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", asList("DispatcherWithGlobalAndProjectSubscribers"))).isTrue(); + assertThat(dao.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", asList("DispatcherWithGlobalAndProjectSubscribers"))).isTrue(); } @Test @@ -194,6 +206,51 @@ public class PropertiesDaoTest { } @Test + public void select_global_properties_by_keys() throws Exception { + ComponentDto project = ComponentTesting.newProjectDto(); + dbClient.componentDao().insert(session, project); + UserDto user = UserTesting.newUserDto(); + dbClient.userDao().insert(session, user); + + String key = "key"; + String anotherKey = "anotherKey"; + insertProperties( + newGlobalPropertyDto().setKey(key), + newComponentPropertyDto(project).setKey(key), + newUserPropertyDto(user).setKey(key), + newGlobalPropertyDto().setKey(anotherKey)); + + assertThat(dao.selectGlobalPropertiesByKeys(session, newHashSet(key))).extracting("key").containsOnly(key); + assertThat(dao.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey))).extracting("key").containsOnly(key, anotherKey); + assertThat(dao.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey, "unknown"))).extracting("key").containsOnly(key, anotherKey); + + assertThat(dao.selectGlobalPropertiesByKeys(session, newHashSet("unknown"))).isEmpty(); + } + + @Test + public void select_component_properties_by_keys() throws Exception { + ComponentDto project = ComponentTesting.newProjectDto(); + dbClient.componentDao().insert(session, project); + UserDto user = UserTesting.newUserDto(); + dbClient.userDao().insert(session, user); + + String key = "key"; + String anotherKey = "anotherKey"; + insertProperties( + newGlobalPropertyDto().setKey(key), + newComponentPropertyDto(project).setKey(key), + newUserPropertyDto(user).setKey(key), + newComponentPropertyDto(project).setKey(anotherKey)); + + assertThat(dao.selectComponentPropertiesByKeys(session, newHashSet(key), project.getId())).extracting("key").containsOnly(key); + assertThat(dao.selectComponentPropertiesByKeys(session, newHashSet(key, anotherKey), project.getId())).extracting("key").containsOnly(key, anotherKey); + assertThat(dao.selectComponentPropertiesByKeys(session, newHashSet(key, anotherKey, "unknown"), project.getId())).extracting("key").containsOnly(key, anotherKey); + + assertThat(dao.selectComponentPropertiesByKeys(session, newHashSet("unknown"), project.getId())).isEmpty(); + assertThat(dao.selectComponentPropertiesByKeys(session, newHashSet(key), 123456789L)).isEmpty(); + } + + @Test public void setProperty_update() { dbTester.prepareDbUnit(getClass(), "update.xml"); @@ -336,4 +393,11 @@ public class PropertiesDaoTest { } return null; } + + private void insertProperties(PropertyDto... properties) { + for (PropertyDto propertyDto : properties) { + dao.insertProperty(session, propertyDto); + } + session.commit(); + } } diff --git a/sonar-db/src/test/java/org/sonar/db/property/PropertyTesting.java b/sonar-db/src/test/java/org/sonar/db/property/PropertyTesting.java deleted file mode 100644 index cb7a0897663..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/property/PropertyTesting.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.db.property; - -public class PropertyTesting { - - public static PropertyDto newGlobalProperty(String key, String value) { - return new PropertyDto().setKey(key).setValue(value); - } - - public static PropertyDto newProjectProperty(String key, String value, long componentId) { - return new PropertyDto().setKey(key).setValue(value).setResourceId(componentId); - } -} |