diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-06-07 16:34:13 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-06-09 11:46:02 +0200 |
commit | 73549aca8ea8fbc9eff34d7187873ebe89ae76b6 (patch) | |
tree | cb639a2bf1176aa65c34bc940f14089d7718a8ad | |
parent | 5b82ca5d5aa0c2ca080ddb115df2a37821c22923 (diff) | |
download | sonarqube-73549aca8ea8fbc9eff34d7187873ebe89ae76b6.tar.gz sonarqube-73549aca8ea8fbc9eff34d7187873ebe89ae76b6.zip |
SONAR-7723 Create dao for perm_tpl_characteristics
15 files changed, 500 insertions, 6 deletions
diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 81ec0f8e478..b80ef4aebed 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -100,7 +100,7 @@ public class ComputeEngineContainerImplTest { assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize( COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION + 22 // level 1 - + 45 // content of DaoModule + + 46 // content of DaoModule + 1 // content of EsSearchModule + 57 // content of CorePropertyDefinitions + 1 // content of CePropertyDefinitions diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1200_create_perm_templates_characteristics.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1200_create_perm_templates_characteristics.rb new file mode 100644 index 00000000000..6886e400507 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1200_create_perm_templates_characteristics.rb @@ -0,0 +1,38 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube 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. +# +# SonarQube 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. +# +# +# SonarQube 6.0 +# +class CreatePermTemplatesCharacteristics < ActiveRecord::Migration + + def self.up + create_table 'perm_tpl_characteristics' do |t| + t.column :template_id, :integer, :null => false + t.column :permission_key, :string, :null => false, :limit => 64 + t.column :with_project_creator, :boolean, :null => false, :default => false + t.column :created_at, :big_integer, :null => false + t.column :updated_at, :big_integer, :null => false + end + + add_index 'perm_tpl_characteristics', ['template_id','permission_key'], :name => 'uniq_perm_tpl_charac', :unique => true + end + +end + diff --git a/sonar-db/src/main/java/org/sonar/db/DaoModule.java b/sonar-db/src/main/java/org/sonar/db/DaoModule.java index a3895860f78..effe78d17d0 100644 --- a/sonar-db/src/main/java/org/sonar/db/DaoModule.java +++ b/sonar-db/src/main/java/org/sonar/db/DaoModule.java @@ -50,6 +50,7 @@ import org.sonar.db.metric.MetricDao; import org.sonar.db.notification.NotificationQueueDao; import org.sonar.db.permission.PermissionDao; import org.sonar.db.permission.PermissionTemplateDao; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao; import org.sonar.db.property.PropertiesDao; import org.sonar.db.purge.PurgeDao; import org.sonar.db.qualitygate.ProjectQgateAssociationDao; @@ -97,6 +98,7 @@ public class DaoModule extends Module { NotificationQueueDao.class, PermissionDao.class, PermissionTemplateDao.class, + PermissionTemplateCharacteristicDao.class, PropertiesDao.class, QualityGateDao.class, QualityGateConditionDao.class, diff --git a/sonar-db/src/main/java/org/sonar/db/DbClient.java b/sonar-db/src/main/java/org/sonar/db/DbClient.java index 600e68689b8..f14e36ed786 100644 --- a/sonar-db/src/main/java/org/sonar/db/DbClient.java +++ b/sonar-db/src/main/java/org/sonar/db/DbClient.java @@ -50,6 +50,7 @@ import org.sonar.db.metric.MetricDao; import org.sonar.db.notification.NotificationQueueDao; import org.sonar.db.permission.PermissionDao; import org.sonar.db.permission.PermissionTemplateDao; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao; import org.sonar.db.property.PropertiesDao; import org.sonar.db.purge.PurgeDao; import org.sonar.db.qualitygate.ProjectQgateAssociationDao; @@ -91,6 +92,7 @@ public class DbClient { private final RoleDao roleDao; private final PermissionDao permissionDao; private final PermissionTemplateDao permissionTemplateDao; + private final PermissionTemplateCharacteristicDao permissionTemplateCharacteristicDao; private final IssueDao issueDao; private final IssueFilterDao issueFilterDao; private final IssueFilterFavouriteDao issueFilterFavouriteDao; @@ -145,6 +147,7 @@ public class DbClient { roleDao = getDao(map, RoleDao.class); permissionDao = getDao(map, PermissionDao.class); permissionTemplateDao = getDao(map, PermissionTemplateDao.class); + permissionTemplateCharacteristicDao = getDao(map, PermissionTemplateCharacteristicDao.class); issueDao = getDao(map, IssueDao.class); issueFilterDao = getDao(map, IssueFilterDao.class); issueFilterFavouriteDao = getDao(map, IssueFilterFavouriteDao.class); @@ -283,6 +286,10 @@ public class DbClient { return permissionTemplateDao; } + public PermissionTemplateCharacteristicDao permissionTemplateCharacteristicDao() { + return permissionTemplateCharacteristicDao; + } + public CeQueueDao ceQueueDao() { return ceQueueDao; } diff --git a/sonar-db/src/main/java/org/sonar/db/MyBatis.java b/sonar-db/src/main/java/org/sonar/db/MyBatis.java index 3b02b65e6ed..ad0a84f0ae3 100644 --- a/sonar-db/src/main/java/org/sonar/db/MyBatis.java +++ b/sonar-db/src/main/java/org/sonar/db/MyBatis.java @@ -88,6 +88,8 @@ import org.sonar.db.permission.PermissionTemplateGroupDto; import org.sonar.db.permission.PermissionTemplateMapper; import org.sonar.db.permission.PermissionTemplateUserDto; import org.sonar.db.permission.UserWithPermissionDto; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper; import org.sonar.db.property.PropertiesMapper; import org.sonar.db.property.PropertyDto; import org.sonar.db.purge.IdUuidPair; @@ -196,6 +198,7 @@ public class MyBatis { confBuilder.loadAlias("PermissionTemplateGroup", PermissionTemplateGroupDto.class); confBuilder.loadAlias("UserWithPermission", UserWithPermissionDto.class); confBuilder.loadAlias("GroupWithPermission", GroupWithPermissionDto.class); + confBuilder.loadAlias("TemplatePermission", PermissionTemplateCharacteristicDto.class); confBuilder.loadAlias("QualityProfile", QualityProfileDto.class); confBuilder.loadAlias("ActiveRule", ActiveRuleDto.class); confBuilder.loadAlias("ActiveRuleParam", ActiveRuleParamDto.class); @@ -220,8 +223,9 @@ public class MyBatis { DuplicationMapper.class, IssueMapper.class, IssueChangeMapper.class, IssueFilterMapper.class, IssueFilterFavouriteMapper.class, IsAliveMapper.class, - LoadedTemplateMapper.class, MeasureFilterMapper.class, MeasureFilterFavouriteMapper.class, PermissionTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, - ResourceKeyUpdaterMapper.class, ResourceIndexMapper.class, RoleMapper.class, RuleMapper.class, + LoadedTemplateMapper.class, MeasureFilterMapper.class, MeasureFilterFavouriteMapper.class, + PermissionTemplateMapper.class, PermissionTemplateCharacteristicMapper.class, + PropertiesMapper.class, PurgeMapper.class, ResourceKeyUpdaterMapper.class, ResourceIndexMapper.class, RoleMapper.class, RuleMapper.class, SchemaMigrationMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, UserMapper.class, GroupMapper.class, UserGroupMapper.class, UserTokenMapper.class, FileSourceMapper.class, 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 new file mode 100644 index 00000000000..8f73544e6cd --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.permission.template; + +import java.util.List; +import java.util.Optional; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +import static com.google.common.base.Preconditions.checkArgument; +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 Optional<PermissionTemplateCharacteristicDto> selectByPermissionAndTemplateId(DbSession dbSession, String permission, long templateId) { + PermissionTemplateCharacteristicDto dto = mapper(dbSession).selectByPermissionAndTemplateId(permission, templateId); + return Optional.ofNullable(dto); + } + + public PermissionTemplateCharacteristicDto insert(DbSession dbSession, PermissionTemplateCharacteristicDto dto) { + checkArgument(dto.getCreatedAt() != 0L && dto.getUpdatedAt() != 0L); + mapper(dbSession).insert(dto); + return dto; + } + + public PermissionTemplateCharacteristicDto update(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermissionDto) { + requireNonNull(templatePermissionDto.getId()); + mapper(dbSession).update(templatePermissionDto); + return templatePermissionDto; + } + + private static PermissionTemplateCharacteristicMapper mapper(DbSession dbSession) { + return dbSession.getMapper(PermissionTemplateCharacteristicMapper.class); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDto.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDto.java new file mode 100644 index 00000000000..1f617c32982 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDto.java @@ -0,0 +1,91 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.permission.template; + +import static com.google.common.base.Preconditions.checkArgument; + +public class PermissionTemplateCharacteristicDto { + + private static final int MAX_PERMISSION_KEY_LENGTH = 64; + + private Long id; + private long templateId; + private String permission; + private boolean withProjectCreator; + private long createdAt; + private long updatedAt; + + public Long getId() { + return id; + } + + public PermissionTemplateCharacteristicDto setId(Long id) { + this.id = id; + return this; + } + + public long getTemplateId() { + return templateId; + } + + public PermissionTemplateCharacteristicDto setTemplateId(long templateId) { + this.templateId = templateId; + return this; + } + + public String getPermission() { + return permission; + } + + public PermissionTemplateCharacteristicDto setPermission(String permission) { + checkArgument(permission.length() <= MAX_PERMISSION_KEY_LENGTH, "Permission key length (%s) is longer than the maximum authorized (%s). '%s' was provided.", + permission.length(), MAX_PERMISSION_KEY_LENGTH, permission); + this.permission = permission; + return this; + } + + public boolean getWithProjectCreator() { + return withProjectCreator; + } + + public PermissionTemplateCharacteristicDto setWithProjectCreator(boolean withProjectCreator) { + this.withProjectCreator = withProjectCreator; + return this; + } + + public long getCreatedAt() { + return createdAt; + } + + public PermissionTemplateCharacteristicDto setCreatedAt(long createdAt) { + this.createdAt = createdAt; + return this; + } + + public long getUpdatedAt() { + return updatedAt; + } + + public PermissionTemplateCharacteristicDto setUpdatedAt(long updatedAt) { + this.updatedAt = updatedAt; + return this; + } +} 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 new file mode 100644 index 00000000000..416f204c7d8 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.permission.template; + +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface PermissionTemplateCharacteristicMapper { + List<PermissionTemplateCharacteristicDto> selectByTemplateId(long templateId); + + PermissionTemplateCharacteristicDto selectByPermissionAndTemplateId(@Param("permission") String permission, @Param("templateId") long templateId); + + PermissionTemplateCharacteristicDto selectById(long id); + + void insert(PermissionTemplateCharacteristicDto templatePermissionDto); + + void update(PermissionTemplateCharacteristicDto templatePermissionDto); +} diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/package-info.java b/sonar-db/src/main/java/org/sonar/db/permission/template/package-info.java new file mode 100644 index 00000000000..c15d7dd5320 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/package-info.java @@ -0,0 +1,25 @@ +/* + * 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. + */ + +@ParametersAreNonnullByDefault +package org.sonar.db.permission.template; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index 4302118b853..c6d0c3ea08f 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -30,7 +30,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1_152; + public static final int LAST_VERSION = 1_200; /** * The minimum supported version which can be upgraded. Lower @@ -72,6 +72,7 @@ public class DatabaseVersion { "permission_templates", "perm_templates_users", "perm_templates_groups", + "perm_tpl_characteristics", "quality_gates", "quality_gate_conditions", "projects", 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 new file mode 100644 index 00000000000..f6d352f2fe3 --- /dev/null +++ b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> +<mapper namespace="org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper"> + <sql id="columns"> + ptc.id, + ptc.template_id as templateId, + ptc.permission_key as permission, + ptc.with_project_creator as withProjectCreator, + ptc.created_at as createdAt, + ptc.updated_at as updatedAt + </sql> + + <select id="selectByTemplateId" parameterType="long" resultType="TemplatePermission"> + select + <include refid="columns" /> + from perm_tpl_characteristics ptc + where ptc.template_id=#{templateId} + order by id + </select> + + <select id="selectByPermissionAndTemplateId" parameterType="map" resultType="TemplatePermission"> + select + <include refid="columns" /> + from perm_tpl_characteristics ptc + where ptc.template_id=#{templateId} + and ptc.permission_key=#{permission} + order by id + </select> + + <select id="selectById" parameterType="long" resultType="TemplatePermission"> + select + <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"> + insert into perm_tpl_characteristics(template_id, permission_key, with_project_creator, created_at, updated_at) + values(#{templateId, jdbcType=BIGINT}, #{permission, jdbcType=VARCHAR}, #{withProjectCreator, jdbcType=BOOLEAN}, #{createdAt, jdbcType=BIGINT}, #{updatedAt, jdbcType=BIGINT}) + </insert> + + <update id="update" parameterType="TemplatePermission" useGeneratedKeys="false"> + update perm_tpl_characteristics set + with_project_creator=#{withProjectCreator, jdbcType=BOOLEAN}, + updated_at=#{updatedAt, jdbcType=BIGINT} + where id=#{id} + </update> +</mapper> diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 072ed69e954..c7c52e0b332 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -402,10 +402,11 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1122'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1123'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1124'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1125'); - INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1150'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1151'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1152'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1200'); + INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index b56666e9637..d888aacbd41 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -451,6 +451,15 @@ CREATE TABLE "PERMISSION_TEMPLATES" ( "UPDATED_AT" TIMESTAMP ); +CREATE TABLE "PERM_TPL_CHARACTERISTICS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "TEMPLATE_ID" INTEGER NOT NULL, + "PERMISSION_KEY" VARCHAR(64) NOT NULL, + "WITH_PROJECT_CREATOR" BOOLEAN NOT NULL DEFAULT FALSE, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); + CREATE TABLE "PERM_TEMPLATES_USERS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "USER_ID" INTEGER NOT NULL, @@ -678,3 +687,5 @@ CREATE UNIQUE INDEX "USER_TOKENS_LOGIN_NAME" ON "USER_TOKENS" ("LOGIN", "NAME"); CREATE INDEX "CE_ACTIVITY_ISLASTKEY" ON "CE_ACTIVITY" ("IS_LAST_KEY"); CREATE INDEX "CE_ACTIVITY_ISLAST_STATUS" ON "CE_ACTIVITY" ("IS_LAST", "STATUS"); + +CREATE UNIQUE INDEX "UNIQ_PERM_TPL_CHARAC" ON "PERM_TPL_CHARACTERISTICS" ("TEMPLATE_ID", "PERMISSION_KEY"); diff --git a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java index e666e3a2435..c76d29e2dc1 100644 --- a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/sonar-db/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 + 45); + assertThat(container.size()).isEqualTo(2 + 46); } } 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 new file mode 100644 index 00000000000..b99d29b7d9b --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java @@ -0,0 +1,173 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.permission.template; + +import java.util.List; +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.api.web.UserRole; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PermissionTemplateCharacteristicDaoTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + DbSession dbSession = db.getSession(); + PermissionTemplateCharacteristicMapper mapper = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class); + + PermissionTemplateCharacteristicDao underTest = new PermissionTemplateCharacteristicDao(); + + @Test + public void selectByTemplateId_filter_by_template_id() { + PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.ADMIN) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setCreatedAt(1_000_000_000L) + .setUpdatedAt(2_000_000_000L)); + PermissionTemplateCharacteristicDto templatePermission2 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(false) + .setCreatedAt(1_000_000_000L) + .setUpdatedAt(2_000_000_000L)); + PermissionTemplateCharacteristicDto templatePermissionForAnotherTemplate = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.ADMIN) + .setTemplateId(42L) + .setWithProjectCreator(true) + .setCreatedAt(1_000_000_000L) + .setUpdatedAt(2_000_000_000L)); + + List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateId(dbSession, 1L); + assertThat(result) + .hasSize(2) + .extracting("id") + .doesNotContain(templatePermissionForAnotherTemplate.getId()) + .containsOnly(templatePermission1.getId(), templatePermission2.getId()); + assertThat(result.get(0)) + .isEqualToComparingFieldByField(templatePermission1); + } + + @Test + public void selectByPermissionAndTemplateId() { + PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.ADMIN) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setCreatedAt(1_000_000_000L) + .setUpdatedAt(2_000_000_000L)); + underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(false) + .setCreatedAt(1_000_000_000L) + .setUpdatedAt(2_000_000_000L)); + underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.ADMIN) + .setTemplateId(42L) + .setWithProjectCreator(true) + .setCreatedAt(1_000_000_000L) + .setUpdatedAt(2_000_000_000L)); + + Optional<PermissionTemplateCharacteristicDto> result = underTest.selectByPermissionAndTemplateId(dbSession, UserRole.ADMIN, 1L); + + assertThat(result).isPresent(); + assertThat(result.get()).isEqualToComparingFieldByField(templatePermission1); + } + + @Test + public void insert() { + PermissionTemplateCharacteristicDto expectedResult = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setCreatedAt(123_456_789L) + .setUpdatedAt(2_000_000_000L)); + + PermissionTemplateCharacteristicDto result = mapper.selectById(expectedResult.getId()); + assertThat(result.getId()).isNotNull(); + assertThat(result).isEqualToComparingFieldByField(expectedResult); + } + + @Test + public void update_only_change_with_project_creator_and_updated_at() { + PermissionTemplateCharacteristicDto insertedDto = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setCreatedAt(123_456_789L) + .setUpdatedAt(2_000_000_000L)); + + underTest.update(dbSession, new PermissionTemplateCharacteristicDto() + .setId(insertedDto.getId()) + .setPermission("PERMISSION_ARE_NOT_UPDATABLE") + .setTemplateId(42L) + .setCreatedAt(42L) + .setWithProjectCreator(false) + .setUpdatedAt(3_000_000_000L)); + + PermissionTemplateCharacteristicDto result = mapper.selectById(insertedDto.getId()); + assertThat(result).extracting("id", "permission", "templateId", "createdAt") + .containsExactly(insertedDto.getId(), insertedDto.getPermission(), insertedDto.getTemplateId(), insertedDto.getCreatedAt()); + assertThat(result).extracting("withProjectCreator", "updatedAt") + .containsExactly(false, 3_000_000_000L); + } + + @Test + public void fail_insert_if_created_at_is_equal_to_0() { + expectedException.expect(IllegalArgumentException.class); + + underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setUpdatedAt(2_000_000_000L)); + } + @Test + public void fail_insert_if_updated_at_is_equal_to_0() { + expectedException.expect(IllegalArgumentException.class); + + underTest.insert(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setCreatedAt(2_000_000_000L)); + } + + @Test + public void fail_update_if_id_is_null() { + expectedException.expect(NullPointerException.class); + + underTest.update(dbSession, new PermissionTemplateCharacteristicDto() + .setPermission(UserRole.USER) + .setTemplateId(1L) + .setWithProjectCreator(true) + .setCreatedAt(123_456_789L) + .setUpdatedAt(2_000_000_000L)); + } +} |