diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-03 17:41:13 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-09-07 08:33:31 +0200 |
commit | da4d725ebe9e870943405ba503c90331241db9a2 (patch) | |
tree | 332f7486ea3007b6d722e86a57a32cbed5c9f45c /server/sonar-db-dao | |
parent | cf6cbbb26136ef38430fab84ac8cc38ae974a0a6 (diff) | |
download | sonarqube-da4d725ebe9e870943405ba503c90331241db9a2.tar.gz sonarqube-da4d725ebe9e870943405ba503c90331241db9a2.zip |
SONAR-9662 Store plugin hash + last modification date
Diffstat (limited to 'server/sonar-db-dao')
11 files changed, 428 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 bc5efc71ccb..44b741739d7 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 @@ -48,6 +48,7 @@ import org.sonar.db.permission.GroupPermissionDao; import org.sonar.db.permission.UserPermissionDao; import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao; import org.sonar.db.permission.template.PermissionTemplateDao; +import org.sonar.db.plugin.PluginDao; import org.sonar.db.property.InternalPropertiesDao; import org.sonar.db.property.PropertiesDao; import org.sonar.db.purge.PurgeDao; @@ -105,6 +106,7 @@ public class DaoModule extends Module { OrganizationMemberDao.class, PermissionTemplateCharacteristicDao.class, PermissionTemplateDao.class, + PluginDao.class, ProjectQgateAssociationDao.class, PropertiesDao.class, PurgeDao.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 5d31776cf6e..246bb15f559 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 @@ -47,6 +47,7 @@ import org.sonar.db.permission.GroupPermissionDao; import org.sonar.db.permission.UserPermissionDao; import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao; import org.sonar.db.permission.template.PermissionTemplateDao; +import org.sonar.db.plugin.PluginDao; import org.sonar.db.property.InternalPropertiesDao; import org.sonar.db.property.PropertiesDao; import org.sonar.db.purge.PurgeDao; @@ -121,6 +122,7 @@ public class DbClient { private final WebhookDeliveryDao webhookDeliveryDao; private final DefaultQProfileDao defaultQProfileDao; private final EsQueueDao esQueueDao; + private final PluginDao pluginDao; public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) { this.database = database; @@ -178,6 +180,7 @@ public class DbClient { webhookDeliveryDao = getDao(map, WebhookDeliveryDao.class); defaultQProfileDao = getDao(map, DefaultQProfileDao.class); esQueueDao = getDao(map, EsQueueDao.class); + pluginDao = getDao(map, PluginDao.class); } public DbSession openSession(boolean batch) { @@ -376,6 +379,10 @@ public class DbClient { return esQueueDao; } + public PluginDao pluginDao() { + return pluginDao; + } + protected <K extends Dao> K getDao(Map<Class, Dao> map, Class<K> clazz) { return (K) map.get(clazz); } @@ -384,4 +391,5 @@ public class DbClient { public MyBatis getMyBatis() { return myBatis; } + } 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 73f39eea73b..ee23994014b 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 @@ -81,6 +81,8 @@ import org.sonar.db.permission.template.PermissionTemplateDto; import org.sonar.db.permission.template.PermissionTemplateGroupDto; import org.sonar.db.permission.template.PermissionTemplateMapper; import org.sonar.db.permission.template.PermissionTemplateUserDto; +import org.sonar.db.plugin.PluginDto; +import org.sonar.db.plugin.PluginMapper; import org.sonar.db.property.InternalPropertiesMapper; import org.sonar.db.property.InternalPropertyDto; import org.sonar.db.property.PropertiesMapper; @@ -164,6 +166,7 @@ public class MyBatis implements Startable { confBuilder.loadAlias("PermissionTemplateGroup", PermissionTemplateGroupDto.class); confBuilder.loadAlias("PermissionTemplate", PermissionTemplateDto.class); confBuilder.loadAlias("PermissionTemplateUser", PermissionTemplateUserDto.class); + confBuilder.loadAlias("Plugin", PluginDto.class); confBuilder.loadAlias("ProjectQgateAssociation", ProjectQgateAssociationDto.class); confBuilder.loadAlias("PurgeableAnalysis", PurgeableAnalysisDto.class); confBuilder.loadAlias("QualityGateCondition", QualityGateConditionDto.class); @@ -216,6 +219,7 @@ public class MyBatis implements Startable { OrganizationMemberMapper.class, PermissionTemplateCharacteristicMapper.class, PermissionTemplateMapper.class, + PluginMapper.class, ProjectQgateAssociationMapper.class, PropertiesMapper.class, PurgeMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDao.java new file mode 100644 index 00000000000..68705f81a33 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDao.java @@ -0,0 +1,52 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.plugin; + +import java.util.List; +import java.util.Optional; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +public class PluginDao implements Dao { + + public List<PluginDto> selectAll(DbSession dbSession) { + return mapper(dbSession).selectAll(); + } + + public Optional<PluginDto> selectByKey(DbSession dbSession, String key) { + return Optional.ofNullable(mapper(dbSession).selectByKey(key)); + } + + public void insert(DbSession dbSession, PluginDto dto) { + mapper(dbSession).insert(dto); + } + + public void update(DbSession dbSession, PluginDto dto) { + mapper(dbSession).update(dto); + } + + public void delete(DbSession dbSession, PluginDto dto) { + mapper(dbSession).delete(dto.getUuid()); + } + + private static PluginMapper mapper(DbSession dbSession) { + return dbSession.getMapper(PluginMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDto.java new file mode 100644 index 00000000000..13c512ff328 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginDto.java @@ -0,0 +1,105 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.plugin; + +import javax.annotation.CheckForNull; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class PluginDto { + /** Technical unique identifier, can't be null */ + private String uuid; + /** Plugin key, unique, can't be null */ + private String kee; + /** Base plugin key, can be null */ + private String basePluginKey; + /** JAR file MD5 checksum, can't be null */ + private String hash; + /** Time plugin was first installed */ + private long createdAt; + /** Time of last plugin update (=md5 change) */ + private long updatedAt; + + public String getUuid() { + return uuid; + } + + public PluginDto setUuid(String s) { + this.uuid = s; + return this; + } + + public String getKee() { + return kee; + } + + public PluginDto setKee(String s) { + this.kee = s; + return this; + } + + @CheckForNull + public String getBasePluginKey() { + return basePluginKey; + } + + public PluginDto setBasePluginKey(String s) { + this.basePluginKey = s; + return this; + } + + public String getHash() { + return hash; + } + + public PluginDto setHash(String s) { + this.hash = s; + return this; + } + + public long getCreatedAt() { + return createdAt; + } + + public PluginDto setCreatedAt(long l) { + this.createdAt = l; + return this; + } + + public long getUpdatedAt() { + return updatedAt; + } + + public PluginDto setUpdatedAt(long l) { + this.updatedAt = l; + return this; + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("uuid", uuid) + .append("key", kee) + .append("basePluginKey", basePluginKey) + .append("jarMd5", hash) + .append("createdAt", createdAt) + .append("updatedAt", updatedAt) + .toString(); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginMapper.java new file mode 100644 index 00000000000..66dcd44d345 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/PluginMapper.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.plugin; + +import java.util.List; +import javax.annotation.CheckForNull; +import org.apache.ibatis.annotations.Param; + +public interface PluginMapper { + + List<PluginDto> selectAll(); + + @CheckForNull + PluginDto selectByKey(@Param("key") String key); + + void insert(PluginDto dto); + + void update(PluginDto dto); + + void delete(@Param("uuid") String uuid); +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/package-info.java b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/package-info.java new file mode 100644 index 00000000000..deefe58baeb --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/plugin/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.plugin; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/plugin/PluginMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/plugin/PluginMapper.xml new file mode 100644 index 00000000000..a6fdb0c199a --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/plugin/PluginMapper.xml @@ -0,0 +1,60 @@ +<?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.plugin.PluginMapper"> + + <sql id="sqlColumns"> + uuid, + kee, + base_plugin_key as basePluginKey, + hash, + created_at as createdAt, + updated_at as updatedAt + </sql> + + <select id="selectByKey" parameterType="String" resultType="org.sonar.db.plugin.PluginDto"> + select + <include refid="sqlColumns" /> + from plugins + where kee = #{key,jdbcType=VARCHAR} + </select> + + <select id="selectAll" resultType="org.sonar.db.plugin.PluginDto"> + select <include refid="sqlColumns" /> + from plugins + </select> + + <insert id="insert" parameterType="org.sonar.db.plugin.PluginDto" useGeneratedKeys="false"> + insert into plugins ( + uuid, + kee, + base_plugin_key, + hash, + created_at, + updated_at + ) values ( + #{uuid,jdbcType=VARCHAR}, + #{kee,jdbcType=VARCHAR}, + #{basePluginKey,jdbcType=VARCHAR}, + #{hash,jdbcType=VARCHAR}, + #{createdAt,jdbcType=TIMESTAMP}, + #{updatedAt,jdbcType=TIMESTAMP} + ) + </insert> + + <update id="update" parameterType="org.sonar.db.plugin.PluginDto"> + update plugins set + base_plugin_key=#{basePluginKey,jdbcType=VARCHAR}, + hash=#{hash,jdbcType=VARCHAR}, + updated_at=#{updatedAt,jdbcType=BIGINT} + where + uuid=#{uuid,jdbcType=VARCHAR} + </update> + + <delete id="delete" parameterType="String"> + delete from plugins + where + uuid = #{uuid,jdbcType=VARCHAR} + </delete> +</mapper> 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 2ffc454abd9..0fb6690bd21 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 @@ -29,6 +29,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(2 + 47); + assertThat(container.size()).isEqualTo(2 + 48); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoTest.java new file mode 100644 index 00000000000..fda7a9581d1 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/plugin/PluginDaoTest.java @@ -0,0 +1,116 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.plugin; + +import java.util.Optional; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PluginDaoTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + + private PluginDao underTest = db.getDbClient().pluginDao(); + + @Test + public void selectByKey() { + db.prepareDbUnit(getClass(), "shared.xml"); + + assertThat(underTest.selectByKey(db.getSession(), "java2")).isEmpty(); + + Optional<PluginDto> plugin = underTest.selectByKey(db.getSession(), "java"); + assertThat(plugin.isPresent()).isTrue(); + assertThat(plugin.get().getUuid()).isEqualTo("a"); + assertThat(plugin.get().getKee()).isEqualTo("java"); + assertThat(plugin.get().getBasePluginKey()).isNull(); + assertThat(plugin.get().getHash()).isEqualTo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + assertThat(plugin.get().getCreatedAt()).isEqualTo(1500000000000L); + assertThat(plugin.get().getUpdatedAt()).isEqualTo(1600000000000L); + } + + @Test + public void selectAll() { + db.prepareDbUnit(getClass(), "shared.xml"); + + assertThat(underTest.selectAll(db.getSession())).hasSize(2); + } + + @Test + public void insert() { + db.prepareDbUnit(getClass(), "shared.xml"); + + underTest.insert(db.getSession(), new PluginDto() + .setUuid("c") + .setKee("javascript") + .setBasePluginKey("java") + .setHash("cccccccccccccccccccccccccccccccc") + .setCreatedAt(1L) + .setUpdatedAt(2L)); + + Optional<PluginDto> plugin = underTest.selectByKey(db.getSession(), "javascript"); + assertThat(plugin.isPresent()).isTrue(); + assertThat(plugin.get().getUuid()).isEqualTo("c"); + assertThat(plugin.get().getKee()).isEqualTo("javascript"); + assertThat(plugin.get().getBasePluginKey()).isEqualTo("java"); + assertThat(plugin.get().getHash()).isEqualTo("cccccccccccccccccccccccccccccccc"); + assertThat(plugin.get().getCreatedAt()).isEqualTo(1L); + assertThat(plugin.get().getUpdatedAt()).isEqualTo(2L); + } + + @Test + public void update() { + db.prepareDbUnit(getClass(), "shared.xml"); + + PluginDto plugin = underTest.selectByKey(db.getSession(), "java").get(); + + plugin.setBasePluginKey("foo"); + plugin.setHash("abc"); + plugin.setUpdatedAt(3L); + + underTest.update(db.getSession(), plugin); + + plugin = underTest.selectByKey(db.getSession(), "java").get(); + assertThat(plugin.getUuid()).isEqualTo("a"); + assertThat(plugin.getKee()).isEqualTo("java"); + assertThat(plugin.getBasePluginKey()).isEqualTo("foo"); + assertThat(plugin.getHash()).isEqualTo("abc"); + assertThat(plugin.getCreatedAt()).isEqualTo(1500000000000L); + assertThat(plugin.getUpdatedAt()).isEqualTo(3L); + } + + @Test + public void delete() { + db.prepareDbUnit(getClass(), "shared.xml"); + + underTest.delete(db.getSession(), new PluginDto() + .setUuid("a")); + + assertThat(underTest.selectAll(db.getSession())).hasSize(1); + } + +} diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/plugin/PluginDaoTest/shared.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/plugin/PluginDaoTest/shared.xml new file mode 100644 index 00000000000..016bddf0d08 --- /dev/null +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/plugin/PluginDaoTest/shared.xml @@ -0,0 +1,18 @@ +<dataset> + + <plugins uuid="a" + kee="java" + base_plugin_key="[null]" + hash="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + created_at="1500000000000" + updated_at="1600000000000" + /> + + <plugins uuid="b" + kee="javacustom" + base_plugin_key="java" + hash="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + created_at="1500000000000" + updated_at="1600000000000" + /> +</dataset> |