diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-06-08 16:20:07 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-06-09 14:02:35 +0200 |
commit | 5aa9d2cd1f9d4bbab66f788f0817121698bb14f5 (patch) | |
tree | 2c757a77ad65740aea8e606d9466d2cd1702b166 | |
parent | 731a389cb1f4cc710bf8e11b6d763568c69628e9 (diff) | |
download | sonarqube-5aa9d2cd1f9d4bbab66f788f0817121698bb14f5.tar.gz sonarqube-5aa9d2cd1f9d4bbab66f788f0817121698bb14f5.zip |
SONAR-7726 WS api/permissions/search_templates returns with_project_creator
15 files changed, 176 insertions, 72 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/PermissionTemplateDtoToPermissionTemplateResponse.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/PermissionTemplateDtoToPermissionTemplateResponse.java index 38dd5c310ec..156c1b31385 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/PermissionTemplateDtoToPermissionTemplateResponse.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/PermissionTemplateDtoToPermissionTemplateResponse.java @@ -27,6 +27,10 @@ import org.sonarqube.ws.WsPermissions.PermissionTemplate; public class PermissionTemplateDtoToPermissionTemplateResponse { + private PermissionTemplateDtoToPermissionTemplateResponse() { + // prevent instantiation + } + public static PermissionTemplate toPermissionTemplateResponse(PermissionTemplateDto dto) { return Singleton.INSTANCE.apply(dto); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java index 7e2d03b7b70..17b9727d37f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java @@ -132,7 +132,8 @@ public class SearchTemplatesAction implements PermissionsWsAction { .clear() .setKey(permission) .setUsersCount(data.userCount(templateDto.getId(), permission)) - .setGroupsCount(data.groupCount(templateDto.getId(), permission))); + .setGroupsCount(data.groupCount(templateDto.getId(), permission)) + .setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission))); } response.addPermissionTemplates(templateBuilder); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesData.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesData.java index 5cf96f0d11a..e5ae45ff506 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesData.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesData.java @@ -34,15 +34,17 @@ class SearchTemplatesData { private final List<TemplateUuidQualifier> defaultTemplates; private final Table<Long, String, Integer> userCountByTemplateIdAndPermission; private final Table<Long, String, Integer> groupCountByTemplateIdAndPermission; + private final Table<Long, String, Boolean> withProjectCreatorByTemplateIdAndPermission; private SearchTemplatesData(Builder builder) { this.templates = copyOf(builder.templates); this.defaultTemplates = copyOf(builder.defaultTemplates); this.userCountByTemplateIdAndPermission = copyOf(builder.userCountByTemplateIdAndPermission); this.groupCountByTemplateIdAndPermission = copyOf(builder.groupCountByTemplateIdAndPermission); + this.withProjectCreatorByTemplateIdAndPermission = copyOf(builder.withProjectCreatorByTemplateIdAndPermission); } - public static Builder newBuilder() { + public static Builder builder() { return new Builder(); } @@ -62,11 +64,16 @@ class SearchTemplatesData { return firstNonNull(groupCountByTemplateIdAndPermission.get(templateId, permission), 0); } + public boolean withProjectCreator(long templateId, String permission) { + return firstNonNull(withProjectCreatorByTemplateIdAndPermission.get(templateId, permission), false); + } + public static class Builder { private List<PermissionTemplateDto> templates; private List<TemplateUuidQualifier> defaultTemplates; private Table<Long, String, Integer> userCountByTemplateIdAndPermission; private Table<Long, String, Integer> groupCountByTemplateIdAndPermission; + private Table<Long, String, Boolean> withProjectCreatorByTemplateIdAndPermission; private Builder() { // prevents instantiation outside main class @@ -77,6 +84,7 @@ class SearchTemplatesData { checkState(defaultTemplates != null); checkState(userCountByTemplateIdAndPermission != null); checkState(groupCountByTemplateIdAndPermission != null); + checkState(withProjectCreatorByTemplateIdAndPermission != null); return new SearchTemplatesData(this); } @@ -100,5 +108,10 @@ class SearchTemplatesData { this.groupCountByTemplateIdAndPermission = groupCountByTemplateIdAndPermission; return this; } + + public Builder withProjectCreatorByTemplateIdAndPermission(Table<Long, String, Boolean> withProjectCreatorByTemplateIdAndPermission) { + this.withProjectCreatorByTemplateIdAndPermission = withProjectCreatorByTemplateIdAndPermission; + return this; + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java index 93a445d65f7..1a08a623e52 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java @@ -19,22 +19,19 @@ */ package org.sonar.server.permission.ws.template; -import com.google.common.base.Function; import com.google.common.collect.Lists; import com.google.common.collect.Table; import com.google.common.collect.TreeBasedTable; import java.util.List; -import javax.annotation.Nonnull; -import org.apache.ibatis.session.ResultContext; -import org.apache.ibatis.session.ResultHandler; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.permission.CountByTemplateAndPermissionDto; import org.sonar.db.permission.PermissionTemplateDto; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder.TemplateUuidQualifier; import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest; -import static org.sonar.server.permission.ws.template.SearchTemplatesData.newBuilder; +import static org.sonar.server.permission.ws.template.SearchTemplatesData.builder; public class SearchTemplatesDataLoader { private final DbClient dbClient; @@ -48,15 +45,16 @@ public class SearchTemplatesDataLoader { public SearchTemplatesData load(SearchTemplatesWsRequest request) { DbSession dbSession = dbClient.openSession(false); try { - SearchTemplatesData.Builder data = newBuilder(); + SearchTemplatesData.Builder data = builder(); List<PermissionTemplateDto> templates = searchTemplates(dbSession, request); - List<Long> templateIds = Lists.transform(templates, TemplateToIdFunction.INSTANCE); + List<Long> templateIds = Lists.transform(templates, PermissionTemplateDto::getId); List<TemplateUuidQualifier> defaultTemplates = defaultPermissionTemplateFinder.getDefaultTemplatesByQualifier(); data.templates(templates) .defaultTemplates(defaultTemplates) .userCountByTemplateIdAndPermission(userCountByTemplateIdAndPermission(dbSession, templateIds)) - .groupCountByTemplateIdAndPermission(groupCountByTemplateIdAndPermission(dbSession, templateIds)); + .groupCountByTemplateIdAndPermission(groupCountByTemplateIdAndPermission(dbSession, templateIds)) + .withProjectCreatorByTemplateIdAndPermission(withProjectCreatorsByTemplateIdAndPermission(dbSession, templateIds)); return data.build(); } finally { @@ -74,12 +72,9 @@ public class SearchTemplatesDataLoader { private Table<Long, String, Integer> userCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) { final Table<Long, String, Integer> userCountByTemplateIdAndPermission = TreeBasedTable.create(); - dbClient.permissionTemplateDao().usersCountByTemplateIdAndPermission(dbSession, templateIds, new ResultHandler() { - @Override - public void handleResult(ResultContext context) { - CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject(); - userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount()); - } + dbClient.permissionTemplateDao().usersCountByTemplateIdAndPermission(dbSession, templateIds, context -> { + CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject(); + userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount()); }); return userCountByTemplateIdAndPermission; @@ -88,23 +83,22 @@ public class SearchTemplatesDataLoader { private Table<Long, String, Integer> groupCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) { final Table<Long, String, Integer> userCountByTemplateIdAndPermission = TreeBasedTable.create(); - dbClient.permissionTemplateDao().groupsCountByTemplateIdAndPermission(dbSession, templateIds, new ResultHandler() { - @Override - public void handleResult(ResultContext context) { - CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject(); - userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount()); - } + dbClient.permissionTemplateDao().groupsCountByTemplateIdAndPermission(dbSession, templateIds, context -> { + CountByTemplateAndPermissionDto row = (CountByTemplateAndPermissionDto) context.getResultObject(); + userCountByTemplateIdAndPermission.put(row.getTemplateId(), row.getPermission(), row.getCount()); }); return userCountByTemplateIdAndPermission; } - private enum TemplateToIdFunction implements Function<PermissionTemplateDto, Long> { - INSTANCE; + private Table<Long, String, Boolean> withProjectCreatorsByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) { + final Table<Long, String, Boolean> templatePermissionsByTemplateIdAndPermission = TreeBasedTable.create(); - @Override - public Long apply(@Nonnull PermissionTemplateDto template) { - return template.getId(); - } + List<PermissionTemplateCharacteristicDto> templatePermissions = dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, templateIds); + templatePermissions.stream() + .forEach(templatePermission -> templatePermissionsByTemplateIdAndPermission.put(templatePermission.getTemplateId(), templatePermission.getPermission(), + templatePermission.getWithProjectCreator())); + + return templatePermissionsByTemplateIdAndPermission; } } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template/search_templates-example.json b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template/search_templates-example.json index 1b6b6cccbff..558cc58ea39 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template/search_templates-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/template/search_templates-example.json @@ -10,27 +10,32 @@ { "key": "user", "usersCount": 0, - "groupsCount": 1 + "groupsCount": 1, + "withProjectCreator": false }, { "key": "admin", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "issueadmin", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "codeviewer", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "scan", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false } ] }, @@ -44,27 +49,32 @@ { "key": "user", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "admin", "usersCount": 0, - "groupsCount": 1 + "groupsCount": 1, + "withProjectCreator": true }, { "key": "issueadmin", "usersCount": 3, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "codeviewer", "usersCount": 1, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "scan", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false } ] }, @@ -79,27 +89,32 @@ { "key": "user", "usersCount": 2, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "admin", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "issueadmin", "usersCount": 0, - "groupsCount": 3 + "groupsCount": 3, + "withProjectCreator": false }, { "key": "codeviewer", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false }, { "key": "scan", "usersCount": 0, - "groupsCount": 0 + "groupsCount": 0, + "withProjectCreator": false } ] } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DefaultPermissionTemplateFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DefaultPermissionTemplateFinderTest.java index 12be411b196..7576ca01a3d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DefaultPermissionTemplateFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DefaultPermissionTemplateFinderTest.java @@ -27,10 +27,11 @@ import org.sonar.api.config.Settings; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.ResourceType; import org.sonar.api.resources.ResourceTypes; -import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder; +import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder.TemplateUuidQualifier; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY; @@ -63,8 +64,9 @@ public class DefaultPermissionTemplateFinderTest { @Test public void get_default_template_uuid_if_no_property() { - settings = new Settings(); - settings.setProperty(DEFAULT_TEMPLATE_PROPERTY, "default-template-uuid"); + settings + .clear() + .setProperty(DEFAULT_TEMPLATE_PROPERTY, "default-template-uuid"); underTest = new DefaultPermissionTemplateFinder(settings, resourceTypes); Set<String> result = underTest.getDefaultTemplateUuids(); @@ -72,6 +74,23 @@ public class DefaultPermissionTemplateFinderTest { assertThat(result).containsOnly("default-template-uuid"); } + @Test + public void get_default_project_template_uuid_if_no_property_for_views() { + settings + .clear() + .setProperty(DEFAULT_TEMPLATE_PROPERTY, "default-template-uuid") + .setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), "default-project-template-uuid") + .setProperty(defaultRootQualifierTemplateProperty("DEV"), "default-dev-template-uuid"); + + List<TemplateUuidQualifier> result = underTest.getDefaultTemplatesByQualifier(); + + assertThat(result).extracting(TemplateUuidQualifier::getQualifier, TemplateUuidQualifier::getTemplateUuid) + .containsOnly( + tuple(Qualifiers.PROJECT, "default-project-template-uuid"), + tuple(Qualifiers.VIEW, "default-project-template-uuid"), + tuple("DEV", "default-dev-template-uuid")); + } + private static List<ResourceType> rootResourceTypes() { ResourceType project = ResourceType.builder(Qualifiers.PROJECT).build(); ResourceType view = ResourceType.builder(Qualifiers.VIEW).build(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/PermissionTemplateDtoToPermissionTemplateResponseTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/PermissionTemplateDtoToPermissionTemplateResponseTest.java new file mode 100644 index 00000000000..b71ca321f02 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/PermissionTemplateDtoToPermissionTemplateResponseTest.java @@ -0,0 +1,34 @@ +/* + * 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.server.permission.ws.template; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.test.TestUtils.hasOnlyPrivateConstructors; + +public class PermissionTemplateDtoToPermissionTemplateResponseTest { + + @Test + public void only_private_constructors() { + assertThat(hasOnlyPrivateConstructors(PermissionTemplateDtoToPermissionTemplateResponse.class)).isTrue(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java index cc9459b6641..3a9610e4cab 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java @@ -20,7 +20,6 @@ package org.sonar.server.permission.ws.template; import java.util.Date; -import java.util.List; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Rule; @@ -28,13 +27,14 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.config.Settings; import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.ResourceType; -import org.sonar.api.resources.ResourceTypes; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.permission.PermissionTemplateDto; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.UnauthorizedException; @@ -42,10 +42,7 @@ import org.sonar.server.i18n.I18nRule; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; -import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02; @@ -69,14 +66,14 @@ public class SearchTemplatesActionTest { WsActionTester ws; I18nRule i18n = new I18nRule(); DbClient dbClient = db.getDbClient(); - ResourceTypes resourceTypes = mock(ResourceTypes.class); + DbSession dbSession = db.getSession(); + ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); SearchTemplatesDataLoader dataLoader; SearchTemplatesAction underTest; @Before public void setUp() { i18n.setProjectPermissions(); - when(resourceTypes.getRoots()).thenReturn(rootResourceTypes()); Settings settings = new Settings(); settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), UUID_EXAMPLE_01); @@ -112,6 +109,7 @@ public class SearchTemplatesActionTest { addUserToTemplate(projectTemplate.getId(), user3.getId(), UserRole.ISSUE_ADMIN); addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.CODEVIEWER); addGroupToTemplate(projectTemplate.getId(), group1.getId(), UserRole.ADMIN); + addPermissionTemplateWithProjectCreator(projectTemplate.getId(), UserRole.ADMIN); addUserToTemplate(viewsTemplate.getId(), user1.getId(), UserRole.USER); addUserToTemplate(viewsTemplate.getId(), user2.getId(), UserRole.USER); @@ -228,11 +226,13 @@ public class SearchTemplatesActionTest { dbClient.permissionTemplateDao().insertUserPermission(db.getSession(), templateId, userId, permission); } - private static List<ResourceType> rootResourceTypes() { - ResourceType project = ResourceType.builder(Qualifiers.PROJECT).build(); - ResourceType view = ResourceType.builder(Qualifiers.VIEW).build(); - ResourceType dev = ResourceType.builder("DEV").build(); - - return asList(project, view, dev); + private void addPermissionTemplateWithProjectCreator(long templateId, String permission) { + dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto() + .setWithProjectCreator(true) + .setTemplateId(templateId) + .setPermission(permission) + .setCreatedAt(1_000_000_000L) + .setUpdatedAt(2_000_000_000L)); + db.commit(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesDataTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesDataTest.java index a2b132a4f73..bcb6e08d27f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesDataTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesDataTest.java @@ -32,11 +32,12 @@ public class SearchTemplatesDataTest { @Rule public ExpectedException expectedException = ExpectedException.none(); - SearchTemplatesData.Builder underTest = SearchTemplatesData.newBuilder() + SearchTemplatesData.Builder underTest = SearchTemplatesData.builder() .defaultTemplates(singletonList(new DefaultPermissionTemplateFinder.TemplateUuidQualifier("template_uuid", Qualifiers.PROJECT))) .templates(singletonList(newPermissionTemplateDto())) - .userCountByTemplateIdAndPermission(HashBasedTable.<Long, String, Integer>create()) - .groupCountByTemplateIdAndPermission(HashBasedTable.<Long, String, Integer>create()); + .userCountByTemplateIdAndPermission(HashBasedTable.create()) + .groupCountByTemplateIdAndPermission(HashBasedTable.create()) + .withProjectCreatorByTemplateIdAndPermission(HashBasedTable.create()); @Test public void fail_if_templates_is_null() { @@ -69,4 +70,12 @@ public class SearchTemplatesDataTest { underTest.build(); } + + @Test + public void fail_if_with_project_creators_is_null() { + expectedException.expect(IllegalStateException.class); + underTest.withProjectCreatorByTemplateIdAndPermission(null); + + underTest.build(); + } } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java index 8f73544e6cd..05e9ff0360f 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java @@ -26,11 +26,12 @@ import org.sonar.db.Dao; import org.sonar.db.DbSession; import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Collections.emptyList; import static java.util.Objects.requireNonNull; public class PermissionTemplateCharacteristicDao implements Dao { - public List<PermissionTemplateCharacteristicDto> selectByTemplateId(DbSession dbSession, long templateId) { - return mapper(dbSession).selectByTemplateId(templateId); + public List<PermissionTemplateCharacteristicDto> selectByTemplateIds(DbSession dbSession, List<Long> templateIds) { + return templateIds.isEmpty() ? emptyList() : mapper(dbSession).selectByTemplateIds(templateIds); } public Optional<PermissionTemplateCharacteristicDto> selectByPermissionAndTemplateId(DbSession dbSession, String permission, long templateId) { diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java index 416f204c7d8..666a7d19fdb 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java @@ -24,7 +24,7 @@ import java.util.List; import org.apache.ibatis.annotations.Param; public interface PermissionTemplateCharacteristicMapper { - List<PermissionTemplateCharacteristicDto> selectByTemplateId(long templateId); + List<PermissionTemplateCharacteristicDto> selectByTemplateIds(@Param("templateIds") List<Long> templateId); PermissionTemplateCharacteristicDto selectByPermissionAndTemplateId(@Param("permission") String permission, @Param("templateId") long templateId); diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml index f6d352f2fe3..ae90a6cd456 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml @@ -10,11 +10,15 @@ ptc.updated_at as updatedAt </sql> - <select id="selectByTemplateId" parameterType="long" resultType="TemplatePermission"> + <select id="selectByTemplateIds" parameterType="long" resultType="TemplatePermission"> select <include refid="columns" /> from perm_tpl_characteristics ptc - where ptc.template_id=#{templateId} + where + ptc.template_id in + <foreach collection="templateIds" open="(" close=")" item="templateId" separator=","> + #{templateId} + </foreach> order by id </select> @@ -32,7 +36,6 @@ <include refid="columns" /> from perm_tpl_characteristics ptc where ptc.id=#{id} - order by id </select> <insert id="insert" parameterType="TemplatePermission" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> diff --git a/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java b/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java index 8f56a0e5192..43d0e575dfe 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import org.sonar.api.resources.ResourceType; @@ -54,7 +55,7 @@ public class ResourceTypesRule extends ResourceTypes { } public ResourceTypesRule setRootQualifiers(String... qualifiers) { - Set<ResourceType> resourceTypes = new HashSet<>(); + Set<ResourceType> resourceTypes = new LinkedHashSet<>(); for (String qualifier : qualifiers) { resourceTypes.add(ResourceType.builder(qualifier).build()); } diff --git a/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java index b99d29b7d9b..9d5f97bcedd 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java @@ -30,6 +30,8 @@ import org.sonar.api.web.UserRole; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import static com.google.common.collect.Lists.newArrayList; +import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; public class PermissionTemplateCharacteristicDaoTest { @@ -52,7 +54,7 @@ public class PermissionTemplateCharacteristicDaoTest { .setUpdatedAt(2_000_000_000L)); PermissionTemplateCharacteristicDto templatePermission2 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() .setPermission(UserRole.USER) - .setTemplateId(1L) + .setTemplateId(2L) .setWithProjectCreator(false) .setCreatedAt(1_000_000_000L) .setUpdatedAt(2_000_000_000L)); @@ -63,7 +65,7 @@ public class PermissionTemplateCharacteristicDaoTest { .setCreatedAt(1_000_000_000L) .setUpdatedAt(2_000_000_000L)); - List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateId(dbSession, 1L); + List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateIds(dbSession, newArrayList(1L, 2L)); assertThat(result) .hasSize(2) .extracting("id") @@ -74,6 +76,13 @@ public class PermissionTemplateCharacteristicDaoTest { } @Test + public void selectByTemplateId_for_empty_list_of_template_id() { + List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateIds(dbSession, emptyList()); + + assertThat(result).isEmpty(); + } + + @Test public void selectByPermissionAndTemplateId() { PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() .setPermission(UserRole.ADMIN) diff --git a/sonar-ws/src/main/protobuf/ws-permissions.proto b/sonar-ws/src/main/protobuf/ws-permissions.proto index 6c27cc37f0d..75b9f3a1c8d 100644 --- a/sonar-ws/src/main/protobuf/ws-permissions.proto +++ b/sonar-ws/src/main/protobuf/ws-permissions.proto @@ -83,6 +83,7 @@ message Permission { optional string description = 3; optional int32 usersCount = 4; optional int32 groupsCount = 5; + optional bool withProjectCreator = 6; } message PermissionTemplate { |