diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2019-10-08 17:51:53 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-11-06 10:04:19 +0100 |
commit | 58d04f69bb33f17cfe6db3d9885fe4c9adabd2d1 (patch) | |
tree | bc48a5c0b0e2884ffa6ec2b61c99326844f030ba /server/sonar-db-dao/src | |
parent | f8c9b9c8b3e7040907e0ded89511f86b827449ad (diff) | |
download | sonarqube-58d04f69bb33f17cfe6db3d9885fe4c9adabd2d1.tar.gz sonarqube-58d04f69bb33f17cfe6db3d9885fe4c9adabd2d1.zip |
SONAR-12512 Allow configuration of multiple GitHub instances
Diffstat (limited to 'server/sonar-db-dao/src')
15 files changed, 644 insertions, 1 deletions
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 f5cca91191f..add145bf020 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 @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.List; import org.sonar.core.platform.Module; import org.sonar.db.alm.AlmAppInstallDao; +import org.sonar.db.alm.setting.AlmSettingDao; import org.sonar.db.alm.OrganizationAlmBindingDao; import org.sonar.db.alm.ProjectAlmBindingDao; import org.sonar.db.ce.CeActivityDao; @@ -115,6 +116,7 @@ public class DaoModule extends Module { GroupMembershipDao.class, GroupPermissionDao.class, AlmAppInstallDao.class, + AlmSettingDao.class, ProjectAlmBindingDao.class, InternalComponentPropertiesDao.class, InternalPropertiesDao.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 33fbf8d2662..fea23b1b590 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 @@ -24,6 +24,7 @@ import java.util.Map; import org.sonar.db.alm.AlmAppInstallDao; import org.sonar.db.alm.OrganizationAlmBindingDao; import org.sonar.db.alm.ProjectAlmBindingDao; +import org.sonar.db.alm.setting.AlmSettingDao; import org.sonar.db.ce.CeActivityDao; import org.sonar.db.ce.CeQueueDao; import org.sonar.db.ce.CeScannerContextDao; @@ -99,6 +100,7 @@ public class DbClient { private final QualityProfileExportDao qualityProfileExportDao; private final PropertiesDao propertiesDao; private final AlmAppInstallDao almAppInstallDao; + private final AlmSettingDao almSettingDao; private final ProjectAlmBindingDao projectAlmBindingDao; private final InternalComponentPropertiesDao internalComponentPropertiesDao; private final InternalPropertiesDao internalPropertiesDao; @@ -165,6 +167,7 @@ public class DbClient { map.put(dao.getClass(), dao); } almAppInstallDao = getDao(map, AlmAppInstallDao.class); + almSettingDao = getDao(map, AlmSettingDao.class); projectAlmBindingDao = getDao(map, ProjectAlmBindingDao.class); schemaMigrationDao = getDao(map, SchemaMigrationDao.class); authorizationDao = getDao(map, AuthorizationDao.class); @@ -241,6 +244,10 @@ public class DbClient { return almAppInstallDao; } + public AlmSettingDao almSettingDao() { + return almSettingDao; + } + public ProjectAlmBindingDao projectAlmBindingsDao() { return projectAlmBindingDao; } 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 e9bf8807ba5..09cce2fab5a 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 @@ -38,6 +38,7 @@ import org.sonar.db.alm.AlmAppInstallMapper; import org.sonar.db.alm.OrganizationAlmBindingMapper; import org.sonar.db.alm.ProjectAlmBindingDto; import org.sonar.db.alm.ProjectAlmBindingMapper; +import org.sonar.db.alm.setting.AlmSettingMapper; import org.sonar.db.ce.CeActivityMapper; import org.sonar.db.ce.CeQueueMapper; import org.sonar.db.ce.CeScannerContextMapper; @@ -224,6 +225,7 @@ public class MyBatis implements Startable { Class<?>[] mappers = { ActiveRuleMapper.class, AlmAppInstallMapper.class, + AlmSettingMapper.class, AnalysisPropertiesMapper.class, AuthorizationMapper.class, BranchMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ALM.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ALM.java new file mode 100644 index 00000000000..e99482e3ed8 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/ALM.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.alm.setting; + +import java.util.Locale; + +public enum ALM { + GITHUB, + BITBUCKET, + AZURE_DEVOPS; + + public static ALM fromId(String almId) { + return ALM.valueOf(almId.toUpperCase(Locale.ENGLISH)); + } + + public String getId() { + return this.name().toLowerCase(Locale.ENGLISH); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java new file mode 100644 index 00000000000..d416dcb72f7 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java @@ -0,0 +1,73 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.alm.setting; + +import java.util.List; +import java.util.Optional; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +public class AlmSettingDao implements Dao { + + private final System2 system2; + private final UuidFactory uuidFactory; + + public AlmSettingDao(System2 system2, UuidFactory uuidFactory) { + this.system2 = system2; + this.uuidFactory = uuidFactory; + } + + private static AlmSettingMapper getMapper(DbSession dbSession) { + return dbSession.getMapper(AlmSettingMapper.class); + } + + public void insert(DbSession dbSession, AlmSettingDto almSettingDto) { + String uuid = uuidFactory.create(); + long now = system2.now(); + getMapper(dbSession).insert(almSettingDto, uuid, now); + almSettingDto.setUuid(uuid); + almSettingDto.setCreatedAt(now); + almSettingDto.setUpdatedAt(now); + } + + public Optional<AlmSettingDto> selectByUuid(DbSession dbSession, String uuid) { + return Optional.ofNullable(getMapper(dbSession).selectByUuid(uuid)); + } + + public Optional<AlmSettingDto> selectByKey(DbSession dbSession, String key) { + return Optional.ofNullable(getMapper(dbSession).selectByKey(key)); + } + + public List<AlmSettingDto> selectAll(DbSession dbSession) { + return getMapper(dbSession).selectAll(); + } + + public void delete(DbSession dbSession, AlmSettingDto almSettingDto){ + getMapper(dbSession).deleteByKey(almSettingDto.getKey()); + } + + public void update(DbSession dbSession, AlmSettingDto almSettingDto) { + long now = system2.now(); + getMapper(dbSession).update(almSettingDto, now); + almSettingDto.setUpdatedAt(now); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java new file mode 100644 index 00000000000..82f5db0b6d2 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDto.java @@ -0,0 +1,127 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.alm.setting; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +public class AlmSettingDto { + + private String uuid; + private String key; + private String rawAlm; + private String url; + private String appId; + private String privateKey; + private String personalAccessToken; + private long updatedAt; + private long createdAt; + + String getUuid() { + return uuid; + } + + void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getKey() { + return key; + } + + public AlmSettingDto setKey(String key) { + this.key = key; + return this; + } + + public ALM getAlm() { + return ALM.fromId(rawAlm); + } + + public AlmSettingDto setAlm(ALM alm) { + rawAlm = alm.getId(); + return this; + } + + public String getRawAlm() { + return rawAlm; + } + + public AlmSettingDto setRawAlm(String rawAlm) { + this.rawAlm = rawAlm; + return this; + } + + @CheckForNull + public String getUrl() { + return url; + } + + public AlmSettingDto setUrl(@Nullable String url) { + this.url = url; + return this; + } + + @CheckForNull + public String getAppId() { + return appId; + } + + public AlmSettingDto setAppId(@Nullable String appId) { + this.appId = appId; + return this; + } + + @CheckForNull + public String getPrivateKey() { + return privateKey; + } + + public AlmSettingDto setPrivateKey(@Nullable String privateKey) { + this.privateKey = privateKey; + return this; + } + + @CheckForNull + public String getPersonalAccessToken() { + return personalAccessToken; + } + + public AlmSettingDto setPersonalAccessToken(@Nullable String personalAccessToken) { + this.personalAccessToken = personalAccessToken; + return this; + } + + long getUpdatedAt() { + return updatedAt; + } + + void setUpdatedAt(long updatedAt) { + this.updatedAt = updatedAt; + } + + long getCreatedAt() { + return createdAt; + } + + void setCreatedAt(long createdAt) { + this.createdAt = createdAt; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java new file mode 100644 index 00000000000..584cd4a648c --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java @@ -0,0 +1,41 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.alm.setting; + +import java.util.List; +import javax.annotation.CheckForNull; +import org.apache.ibatis.annotations.Param; + +public interface AlmSettingMapper { + + @CheckForNull + AlmSettingDto selectByUuid(@Param("uuid") String uuid); + + @CheckForNull + AlmSettingDto selectByKey(@Param("key") String key); + + List<AlmSettingDto> selectAll(); + + void insert(@Param("dto") AlmSettingDto almSettingDto, @Param("uuid") String uuid, @Param("now") long now); + + void update(@Param("dto") AlmSettingDto almSettingDto, @Param("now") long now); + + void deleteByKey(@Param("key") String key); +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/package-info.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/package-info.java new file mode 100644 index 00000000000..1e3bea4ce3a --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.db.alm.setting; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml new file mode 100644 index 00000000000..bf66e2983fe --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml @@ -0,0 +1,86 @@ +<?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.alm.setting.AlmSettingMapper"> + + <sql id="sqlColumns"> + a.kee as key, + a.uuid as uuid, + a.alm_id as rawAlm, + a.url as url, + a.app_id as appId, + a.private_key as privateKey, + a.pat as personalAccessToken, + a.created_at as createdAt, + a.updated_at as updatedAt + </sql> + + <select id="selectByUuid" parameterType="string" resultType="org.sonar.db.alm.setting.AlmSettingDto"> + select <include refid="sqlColumns"/> + from + alm_settings a + where + a.uuid = #{uuid, jdbcType=VARCHAR} + </select> + + <select id="selectByKey" parameterType="string" resultType="org.sonar.db.alm.setting.AlmSettingDto"> + select <include refid="sqlColumns"/> + from + alm_settings a + where + a.kee = #{key, jdbcType=VARCHAR} + </select> + + <select id="selectAll" resultType="org.sonar.db.alm.setting.AlmSettingDto"> + select <include refid="sqlColumns"/> + from alm_settings a + </select> + + + <insert id="insert" parameterType="Map" useGeneratedKeys="false"> + INSERT INTO alm_settings + ( + uuid, + kee, + alm_id, + url, + app_id, + private_key, + pat, + created_at, + updated_at + ) + VALUES ( + #{uuid, jdbcType=VARCHAR}, + #{dto.key, jdbcType=VARCHAR}, + #{dto.rawAlm, jdbcType=VARCHAR}, + #{dto.url, jdbcType=VARCHAR}, + #{dto.appId, jdbcType=VARCHAR}, + #{dto.privateKey, jdbcType=VARCHAR}, + #{dto.personalAccessToken, jdbcType=VARCHAR}, + #{now, jdbcType=BIGINT}, + #{now, jdbcType=BIGINT} + ) + </insert> + + <update id="update" parameterType="Map"> + UPDATE alm_settings + <set> + kee = #{dto.key, jdbcType=VARCHAR}, + url = #{dto.url, jdbcType=VARCHAR}, + app_id = #{dto.appId, jdbcType=VARCHAR}, + private_key = #{dto.privateKey, jdbcType=VARCHAR}, + pat = #{dto.personalAccessToken, jdbcType=VARCHAR}, + updated_at = #{now, jdbcType=BIGINT} + </set> + <where> + uuid = #{dto.uuid, jdbcType=VARCHAR} + </where> + </update> + + <delete id="deleteByKey" parameterType="String"> + DELETE FROM alm_settings WHERE kee = #{key, jdbcType=VARCHAR} + </delete> + + +</mapper> diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 9f5c9f136b8..b286019ea08 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -50,6 +50,20 @@ CREATE UNIQUE INDEX ALM_APP_INSTALLS_OWNER ON ALM_APP_INSTALLS(ALM_ID, OWNER_ID) CREATE UNIQUE INDEX ALM_APP_INSTALLS_INSTALL ON ALM_APP_INSTALLS(ALM_ID, INSTALL_ID); CREATE INDEX ALM_APP_INSTALLS_EXTERNAL_ID ON ALM_APP_INSTALLS(USER_EXTERNAL_ID); +CREATE TABLE ALM_SETTINGS( + UUID VARCHAR(40) NOT NULL, + ALM_ID VARCHAR(40) NOT NULL, + KEE VARCHAR(40) NOT NULL, + URL VARCHAR(2000), + APP_ID VARCHAR(80), + PRIVATE_KEY VARCHAR(2000), + PAT VARCHAR(2000), + UPDATED_AT BIGINT NOT NULL, + CREATED_AT BIGINT NOT NULL +); +ALTER TABLE ALM_SETTINGS ADD CONSTRAINT PK_ALM_SETTINGS PRIMARY KEY(UUID); +CREATE UNIQUE INDEX UNIQ_ALM_SETTINGS ON ALM_SETTINGS(KEE); + CREATE TABLE ANALYSIS_PROPERTIES( UUID VARCHAR(40) NOT NULL, ANALYSIS_UUID VARCHAR(40) NOT NULL, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java index 5063ba33bd1..d805141a0f2 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java @@ -30,6 +30,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 63); + assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 64); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java new file mode 100644 index 00000000000..14dd5311aa4 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java @@ -0,0 +1,140 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.alm.setting; + +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.core.util.UuidFactory; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.almsettings.AlmSettingsTesting; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto; + +public class AlmSettingDaoTest { + + public static final long NOW = 1000000L; + private static final String A_UUID = "SOME_UUID"; + @Rule + public ExpectedException expectedException = ExpectedException.none(); + private System2 system2 = mock(System2.class); + @Rule + public DbTester db = DbTester.create(system2); + + private DbSession dbSession = db.getSession(); + private UuidFactory uuidFactory = mock(UuidFactory.class); + private AlmSettingDao underTest = new AlmSettingDao(system2, uuidFactory); + + @Test + public void selectByUuid() { + when(uuidFactory.create()).thenReturn(A_UUID); + when(system2.now()).thenReturn(NOW); + + AlmSettingDto almSettingDto = newGithubAlmSettingDto(); + underTest.insert(dbSession, almSettingDto); + + assertThat(underTest.selectByUuid(dbSession, A_UUID).get()) + .extracting(AlmSettingDto::getUuid, AlmSettingDto::getKey, AlmSettingDto::getRawAlm, AlmSettingDto::getUrl, + AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getPersonalAccessToken, + AlmSettingDto::getCreatedAt, AlmSettingDto::getUpdatedAt) + .containsExactly(A_UUID, almSettingDto.getKey(), ALM.GITHUB.getId(), almSettingDto.getUrl(), + almSettingDto.getAppId(), almSettingDto.getPrivateKey(), + almSettingDto.getPersonalAccessToken(), NOW, NOW); + + assertThat(underTest.selectByUuid(dbSession, "foo")).isNotPresent(); + } + + @Test + public void selectByKey() { + when(uuidFactory.create()).thenReturn(A_UUID); + when(system2.now()).thenReturn(NOW); + + AlmSettingDto almSettingDto = AlmSettingsTesting.newGithubAlmSettingDto(); + underTest.insert(dbSession, almSettingDto); + + assertThat(underTest.selectByKey(dbSession, almSettingDto.getKey()).get()) + .extracting(AlmSettingDto::getUuid, AlmSettingDto::getKey, AlmSettingDto::getRawAlm, AlmSettingDto::getUrl, + AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getPersonalAccessToken, + AlmSettingDto::getCreatedAt, AlmSettingDto::getUpdatedAt) + .containsExactly(A_UUID, almSettingDto.getKey(), ALM.GITHUB.getId(), almSettingDto.getUrl(), + almSettingDto.getAppId(), almSettingDto.getPrivateKey(), + almSettingDto.getPersonalAccessToken(), NOW, NOW); + + assertThat(underTest.selectByKey(dbSession, "foo")).isNotPresent(); + } + + @Test + public void selectAll() { + when(uuidFactory.create()).thenReturn(A_UUID); + when(system2.now()).thenReturn(NOW); + underTest.insert(dbSession, newGithubAlmSettingDto()); + when(uuidFactory.create()).thenReturn(A_UUID + "bis"); + underTest.insert(dbSession, newGithubAlmSettingDto()); + + List<AlmSettingDto> almSettings = underTest.selectAll(dbSession); + assertThat(almSettings).isNotNull(); + assertThat(almSettings).size().isEqualTo(2); + } + + @Test + public void update() { + when(uuidFactory.create()).thenReturn(A_UUID); + when(system2.now()).thenReturn(NOW); + AlmSettingDto almSettingDto = newGithubAlmSettingDto(); + underTest.insert(dbSession, almSettingDto); + + almSettingDto.setPrivateKey("updated private key"); + almSettingDto.setAppId("updated app id"); + almSettingDto.setUrl("updated url"); + almSettingDto.setPersonalAccessToken("updated pat"); + almSettingDto.setKey("updated key"); + + when(system2.now()).thenReturn(NOW + 1); + underTest.update(dbSession, almSettingDto); + + AlmSettingDto result = underTest.selectByUuid(dbSession, A_UUID).get(); + assertThat(result) + .extracting(AlmSettingDto::getUuid, AlmSettingDto::getKey, AlmSettingDto::getRawAlm, AlmSettingDto::getUrl, + AlmSettingDto::getAppId, AlmSettingDto::getPrivateKey, AlmSettingDto::getPersonalAccessToken, + AlmSettingDto::getCreatedAt, AlmSettingDto::getUpdatedAt) + .containsExactly(A_UUID, almSettingDto.getKey(), ALM.GITHUB.getId(), almSettingDto.getUrl(), + almSettingDto.getAppId(), almSettingDto.getPrivateKey(), + almSettingDto.getPersonalAccessToken(), NOW, NOW + 1); + } + + @Test + public void delete() { + when(uuidFactory.create()).thenReturn(A_UUID); + when(system2.now()).thenReturn(NOW); + AlmSettingDto almSettingDto = newGithubAlmSettingDto(); + underTest.insert(dbSession, almSettingDto); + + underTest.delete(dbSession, almSettingDto); + + assertThat(underTest.selectByKey(dbSession, almSettingDto.getKey()).isPresent()).isFalse(); + } + +} diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java index 1fc19513ada..a39cd1dea64 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java @@ -33,6 +33,7 @@ import org.picocontainer.containers.TransientPicoContainer; import org.sonar.api.utils.System2; import org.sonar.core.util.SequenceUuidFactory; import org.sonar.db.alm.AlmDbTester; +import org.sonar.db.almsettings.AlmSettingsDbTester; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ProjectLinkDbTester; import org.sonar.db.event.EventDbTester; @@ -96,6 +97,7 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { private final WebhookDeliveryDbTester webhookDeliveryDbTester; private final AlmDbTester almDbTester; private final InternalComponentPropertyDbTester internalComponentPropertyTester; + private final AlmSettingsDbTester almSettingsDbTester; private DbTester(System2 system2, @Nullable String schemaPath, MyBatisConfExtension... confExtensions) { super(TestDbImpl.create(schemaPath, confExtensions)); @@ -124,6 +126,7 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { this.almDbTester = new AlmDbTester(this); this.internalComponentPropertyTester = new InternalComponentPropertyDbTester(this); this.newCodePeriodTester = new NewCodePeriodDbTester(this); + this.almSettingsDbTester = new AlmSettingsDbTester(this); } public static DbTester create() { @@ -294,6 +297,10 @@ public class DbTester extends AbstractDbTester<TestDbImpl> { return internalComponentPropertyTester; } + public AlmSettingsDbTester almSettings() { + return almSettingsDbTester; + } + @Override protected void after() { if (session != null) { diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsDbTester.java new file mode 100644 index 00000000000..0a74acc40cf --- /dev/null +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsDbTester.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.almsettings; + +import java.util.function.Consumer; +import org.sonar.db.DbTester; +import org.sonar.db.alm.setting.AlmSettingDto; + +import static java.util.Arrays.stream; +import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto; + +public class AlmSettingsDbTester { + + private final DbTester db; + + public AlmSettingsDbTester(DbTester db) { + this.db = db; + } + + @SafeVarargs + public final AlmSettingDto insertGitHubAlmSetting(Consumer<AlmSettingDto>... populators) { + AlmSettingDto dto = newGithubAlmSettingDto(); + stream(populators).forEach(p -> p.accept(dto)); + + db.getDbClient().almSettingDao().insert(db.getSession(), dto); + db.commit(); + return dto; + } + +} diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java new file mode 100644 index 00000000000..e799a859f8f --- /dev/null +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java @@ -0,0 +1,37 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.almsettings; + +import org.sonar.db.alm.setting.ALM; +import org.sonar.db.alm.setting.AlmSettingDto; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; + +public class AlmSettingsTesting { + + public static AlmSettingDto newGithubAlmSettingDto() { + return new AlmSettingDto() + .setKey(randomAlphanumeric(40)) + .setUrl(randomAlphanumeric(2000)) + .setAppId(randomAlphanumeric(80)) + .setAlm(ALM.GITHUB) + .setPrivateKey(randomAlphanumeric(2000)); + } +} |