diff options
author | Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> | 2023-05-04 09:16:57 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-05-11 20:03:14 +0000 |
commit | ca468111e1e8425cdbc9b9c23e80c3ab563f6115 (patch) | |
tree | e75854dd50a97f10ee9dd9a200f171c48839d29b /server/sonar-db-dao/src | |
parent | 75c9f37081c78eff7c7452aa5b00bc2f40970e21 (diff) | |
download | sonarqube-ca468111e1e8425cdbc9b9c23e80c3ab563f6115.tar.gz sonarqube-ca468111e1e8425cdbc9b9c23e80c3ab563f6115.zip |
SONAR-19085 Add step to fetch groups from DB
Diffstat (limited to 'server/sonar-db-dao/src')
8 files changed, 229 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java new file mode 100644 index 00000000000..8b790881b36 --- /dev/null +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java @@ -0,0 +1,88 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info 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.user; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ExternalGroupDaoIT { + + @Rule + public final DbTester db = DbTester.create(); + + private final DbSession dbSession = db.getSession(); + + private final GroupDao groupDao = db.getDbClient().groupDao(); + + private final ExternalGroupDao underTest = db.getDbClient().externalGroupDao(); + + @Test + public void insert_savesExternalGroup() { + GroupDto localGroup = insertGroup("12345"); + insertGroup("67689"); + ExternalGroupDto externalGroupDto = externalGroup("12345", "providerId"); + underTest.insert(dbSession, externalGroupDto); + List<ExternalGroupDto> savedGroups = underTest.selectByIdentityProvider(dbSession, "providerId"); + assertThat(savedGroups) + .hasSize(1) + .contains(createExternalGroupDto(localGroup.getName(), externalGroupDto)); + } + + @Test + public void selectByIdentityProvider_returnOnlyGroupForTheIdentityProvider() { + List<ExternalGroupDto> expectedGroups = createAndInsertExternalGroupDtos("provider1", 3); + createAndInsertExternalGroupDtos("provider2", 1); + List<ExternalGroupDto> savedGroup = underTest.selectByIdentityProvider(dbSession, "provider1"); + assertThat(savedGroup).containsExactlyInAnyOrderElementsOf(expectedGroups); + } + + private List<ExternalGroupDto> createAndInsertExternalGroupDtos(String provider, int numberOfGroups) { + List<ExternalGroupDto> expectedExternalGroupDtos = new ArrayList<>(); + for (int i = 1; i <= numberOfGroups; i++) { + ExternalGroupDto externalGroupDto = externalGroup(provider + "_" + i, provider); + GroupDto localGroup = insertGroup(externalGroupDto.groupUuid()); + underTest.insert(dbSession, externalGroupDto); + expectedExternalGroupDtos.add(createExternalGroupDto(localGroup.getName(), externalGroupDto)); + } + return expectedExternalGroupDtos; + } + + private static ExternalGroupDto externalGroup(String groupUuid, String identityProvider) { + return new ExternalGroupDto(groupUuid, "external_" + groupUuid, identityProvider); + } + + private GroupDto insertGroup(String groupUuid) { + GroupDto group = new GroupDto(); + group.setUuid(groupUuid); + group.setName("name" + groupUuid); + return groupDao.insert(dbSession, group); + } + + private ExternalGroupDto createExternalGroupDto(String name, ExternalGroupDto externalGroupDto) { + return new ExternalGroupDto(externalGroupDto.groupUuid(), externalGroupDto.externalId(), externalGroupDto.externalIdentityProvider(), name); + } + +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index c3598e3b832..a58e3b8d522 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -84,6 +84,7 @@ import org.sonar.db.schemamigration.SchemaMigrationDao; import org.sonar.db.scim.ScimGroupDao; import org.sonar.db.scim.ScimUserDao; import org.sonar.db.source.FileSourceDao; +import org.sonar.db.user.ExternalGroupDao; import org.sonar.db.user.GroupDao; import org.sonar.db.user.GroupMembershipDao; import org.sonar.db.user.RoleDao; @@ -120,6 +121,7 @@ public class DaoModule extends Module { EsQueueDao.class, EventDao.class, EventComponentChangeDao.class, + ExternalGroupDao.class, FileSourceDao.class, GroupDao.class, GroupMembershipDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index 906edafaee2..4179b7dc7e1 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -84,6 +84,7 @@ import org.sonar.db.schemamigration.SchemaMigrationDao; import org.sonar.db.scim.ScimGroupDao; import org.sonar.db.scim.ScimUserDao; import org.sonar.db.source.FileSourceDao; +import org.sonar.db.user.ExternalGroupDao; import org.sonar.db.user.GroupDao; import org.sonar.db.user.GroupMembershipDao; import org.sonar.db.user.RoleDao; @@ -149,6 +150,7 @@ public class DbClient { private final NotificationQueueDao notificationQueueDao; private final MetricDao metricDao; private final GroupDao groupDao; + private final ExternalGroupDao externalGroupDao; private final RuleDao ruleDao; private final RuleRepositoryDao ruleRepositoryDao; private final ActiveRuleDao activeRuleDao; @@ -232,6 +234,7 @@ public class DbClient { notificationQueueDao = getDao(map, NotificationQueueDao.class); metricDao = getDao(map, MetricDao.class); groupDao = getDao(map, GroupDao.class); + externalGroupDao = getDao(map, ExternalGroupDao.class); ruleDao = getDao(map, RuleDao.class); ruleRepositoryDao = getDao(map, RuleRepositoryDao.class); activeRuleDao = getDao(map, ActiveRuleDao.class); @@ -470,6 +473,10 @@ public class DbClient { return groupDao; } + public ExternalGroupDao externalGroupDao() { + return externalGroupDao; + } + public RuleDao ruleDao() { return ruleDao; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index 78dca8e791c..c87a74221e7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -145,6 +145,8 @@ import org.sonar.db.schemamigration.SchemaMigrationMapper; import org.sonar.db.scim.ScimGroupMapper; import org.sonar.db.scim.ScimUserMapper; import org.sonar.db.source.FileSourceMapper; +import org.sonar.db.user.ExternalGroupDto; +import org.sonar.db.user.ExternalGroupMapper; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupMapper; import org.sonar.db.user.GroupMembershipDto; @@ -194,6 +196,7 @@ public class MyBatis { confBuilder.loadAlias("Component", ComponentDto.class); confBuilder.loadAlias("DuplicationUnit", DuplicationUnitDto.class); confBuilder.loadAlias("Event", EventDto.class); + confBuilder.loadAlias("ExternalGroup", ExternalGroupDto.class); confBuilder.loadAlias("FilePathWithHash", FilePathWithHashDto.class); confBuilder.loadAlias("KeyWithUuid", KeyWithUuidDto.class); confBuilder.loadAlias("Group", GroupDto.class); @@ -269,6 +272,7 @@ public class MyBatis { EsQueueMapper.class, EventMapper.class, EventComponentChangeMapper.class, + ExternalGroupMapper.class, FileSourceMapper.class, GroupMapper.class, GroupMembershipMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java new file mode 100644 index 00000000000..e208eba651d --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java @@ -0,0 +1,39 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info 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.user; + +import java.util.List; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +public class ExternalGroupDao implements Dao { + + public void insert(DbSession dbSession, ExternalGroupDto externalGroupDto) { + mapper(dbSession).insert(externalGroupDto); + } + + public List<ExternalGroupDto> selectByIdentityProvider(DbSession dbSession, String identityProvider) { + return mapper(dbSession).selectByIdentityProvider(identityProvider); + } + + private static ExternalGroupMapper mapper(DbSession session) { + return session.getMapper(ExternalGroupMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDto.java new file mode 100644 index 00000000000..03d29082fbf --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDto.java @@ -0,0 +1,29 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info 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.user; + +import javax.annotation.Nullable; + +public record ExternalGroupDto(String groupUuid, String externalId, String externalIdentityProvider, @Nullable String name) { + + public ExternalGroupDto(String groupUuid, String externalId, String externalIdentityProvider) { + this(groupUuid, externalId, externalIdentityProvider, null); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupMapper.java new file mode 100644 index 00000000000..58f632c0eba --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupMapper.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info 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.user; + +import java.util.List; + +public interface ExternalGroupMapper { + + void insert(ExternalGroupDto externalGroupDto); + + List<ExternalGroupDto> selectByIdentityProvider(String identityProvider); + +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/ExternalGroupMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/ExternalGroupMapper.xml new file mode 100644 index 00000000000..561a24093a6 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/ExternalGroupMapper.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.db.user.ExternalGroupMapper"> + + <insert id="insert" useGeneratedKeys="false" parameterType="ExternalGroup"> + insert into external_groups ( + group_uuid, + external_group_id, + external_identity_provider + ) values ( + #{groupUuid,jdbcType=VARCHAR}, + #{externalId,jdbcType=VARCHAR}, + #{externalIdentityProvider,jdbcType=VARCHAR} + ) + </insert> + + <select id="selectByIdentityProvider" parameterType="String" resultType="ExternalGroup"> + SELECT + eg.group_uuid as groupUuid, + eg.external_group_id as external_id, + eg.external_identity_provider as externalIdentityProvider, + g.name as name + FROM external_groups eg + LEFT JOIN groups g ON eg.group_uuid = g.uuid + WHERE eg.external_identity_provider=#{identityProvider,jdbcType=VARCHAR} + </select> + +</mapper> |