2 * SonarQube :: Database
3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.db.version.v54;
22 import javax.annotation.Nullable;
23 import org.junit.Before;
24 import org.junit.ClassRule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.version.MigrationStep;
30 import static java.lang.String.format;
31 import static org.assertj.core.api.Assertions.assertThat;
33 public class InsertGateAdminPermissionForEachProfileAdminTest {
35 private static final String TABLE_GROUP_ROLES = "group_roles";
36 private static final String TABLE_USER_ROLES = "user_roles";
37 private static final int SOME_GROUP_ID = 964;
38 private static final int SOME_USER_ID = 112;
39 private static final Integer ANYONE_GROUP_ID = null;
40 private static final int SOME_RESOURCE_ID = 25;
41 private static final String PROFILEADMIN_ROLE = "profileadmin";
42 private static final String NOT_PROFILE_ADMIN_ROLE = "ProfileAdmin";
43 private static final String GATE_ADMIN_ROLE = "gateadmin";
46 public static DbTester db = DbTester.createForSchema(System2.INSTANCE, InsertGateAdminPermissionForEachProfileAdminTest.class, "schema.sql");
48 MigrationStep migration;
52 truncate(TABLE_GROUP_ROLES);
53 truncate(TABLE_USER_ROLES);
55 migration = new InsertGateAdminPermissionForEachProfileAdmin(db.database());
59 public void migrate_without_error_on_empty_db() throws Exception {
62 assertGroupRoleTableSize(0);
63 assertUserRoleTableSize(0);
67 public void migrate_group_AnyOne() throws Exception {
68 insertGroupRole(ANYONE_GROUP_ID, null, PROFILEADMIN_ROLE);
72 assertGroupRoleContainsRow(ANYONE_GROUP_ID, null, PROFILEADMIN_ROLE);
73 assertGroupRoleContainsRow(ANYONE_GROUP_ID, null, GATE_ADMIN_ROLE);
74 assertGroupRoleTableSize(2);
75 assertUserRoleTableSize(0);
79 public void do_not_migrate_group_AnyOne_for_resource() throws Exception {
80 insertGroupRole(ANYONE_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
84 assertGroupRoleContainsRow(ANYONE_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
85 assertGroupRoleDoesNotContainRow(ANYONE_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
86 assertGroupRoleTableSize(1);
87 assertUserRoleTableSize(0);
91 public void do_not_migrate_group_AnyOne_when_not_profileadmin() throws Exception {
92 insertGroupRole(ANYONE_GROUP_ID, SOME_RESOURCE_ID, NOT_PROFILE_ADMIN_ROLE);
96 assertGroupRoleContainsRow(ANYONE_GROUP_ID, SOME_RESOURCE_ID, NOT_PROFILE_ADMIN_ROLE);
97 assertGroupRoleDoesNotContainRow(ANYONE_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
98 assertGroupRoleTableSize(1);
99 assertUserRoleTableSize(0);
103 public void do_not_migrate_group_AnyOne_already_has_role_gateadmin() throws Exception {
104 insertGroupRole(ANYONE_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
105 insertGroupRole(ANYONE_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
109 assertGroupRoleContainsRow(ANYONE_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
110 assertGroupRoleContainsRow(ANYONE_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
111 assertGroupRoleTableSize(2);
112 assertUserRoleTableSize(0);
116 public void migrate_group() throws Exception {
117 insertGroupRole(SOME_GROUP_ID, null, PROFILEADMIN_ROLE);
121 assertGroupRoleContainsRow(SOME_GROUP_ID, null, PROFILEADMIN_ROLE);
122 assertGroupRoleContainsRow(SOME_GROUP_ID, null, GATE_ADMIN_ROLE);
123 assertGroupRoleTableSize(2);
124 assertUserRoleTableSize(0);
128 public void do_not_migrate_group_for_resource() throws Exception {
129 insertGroupRole(SOME_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
133 assertGroupRoleContainsRow(SOME_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
134 assertGroupRoleDoesNotContainRow(SOME_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
135 assertGroupRoleTableSize(1);
136 assertUserRoleTableSize(0);
140 public void do_not_migrate_group_when_not_profileadmin() throws Exception {
141 insertGroupRole(SOME_GROUP_ID, SOME_RESOURCE_ID, NOT_PROFILE_ADMIN_ROLE);
145 assertGroupRoleContainsRow(SOME_GROUP_ID, SOME_RESOURCE_ID, NOT_PROFILE_ADMIN_ROLE);
146 assertGroupRoleDoesNotContainRow(SOME_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
147 assertGroupRoleTableSize(1);
148 assertUserRoleTableSize(0);
152 public void do_not_migrate_group_already_has_role_gateadmin() throws Exception {
153 insertGroupRole(SOME_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
154 insertGroupRole(SOME_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
158 assertGroupRoleContainsRow(SOME_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
159 assertGroupRoleContainsRow(SOME_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
160 assertGroupRoleTableSize(2);
161 assertUserRoleTableSize(0);
165 public void migrate_user() throws Exception {
166 insertUserRole(SOME_USER_ID, null, PROFILEADMIN_ROLE);
170 assertUserRoleContainsRow(SOME_USER_ID, null, PROFILEADMIN_ROLE);
171 assertUserRoleContainsRow(SOME_USER_ID, null, GATE_ADMIN_ROLE);
172 assertUserRoleTableSize(2);
173 assertGroupRoleTableSize(0);
177 public void do_not_migrate_user_for_resource() throws Exception {
178 insertUserRole(SOME_USER_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
182 assertUserRoleContainsRow(SOME_USER_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
183 assertUserRoleDoesNotContainRow(SOME_USER_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
184 assertUserRoleTableSize(1);
185 assertGroupRoleTableSize(0);
189 public void do_not_migrate_user_when_not_profileadmin() throws Exception {
190 insertUserRole(SOME_GROUP_ID, SOME_RESOURCE_ID, NOT_PROFILE_ADMIN_ROLE);
194 assertUserRoleContainsRow(SOME_GROUP_ID, SOME_RESOURCE_ID, NOT_PROFILE_ADMIN_ROLE);
195 assertUserRoleDoesNotContainRow(SOME_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
196 assertUserRoleTableSize(1);
197 assertGroupRoleTableSize(0);
201 public void do_not_migrate_user_already_has_role_gateadmin() throws Exception {
202 insertUserRole(SOME_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
203 insertUserRole(SOME_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
207 assertUserRoleContainsRow(SOME_GROUP_ID, SOME_RESOURCE_ID, PROFILEADMIN_ROLE);
208 assertUserRoleContainsRow(SOME_GROUP_ID, SOME_RESOURCE_ID, GATE_ADMIN_ROLE);
209 assertUserRoleTableSize(2);
210 assertGroupRoleTableSize(0);
213 private void assertGroupRoleTableSize(int expected) {
214 assertThat(db.countRowsOfTable(TABLE_GROUP_ROLES)).isEqualTo(expected);
217 private void assertUserRoleTableSize(int expected) {
218 assertThat(db.countRowsOfTable(TABLE_USER_ROLES)).isEqualTo(expected);
221 private void assertGroupRoleDoesNotContainRow(@Nullable Integer groupId, @Nullable Integer resourceId, String role) {
222 String sql = groupRoleRowSql(groupId, resourceId, role);
223 assertThat(db.countSql(sql)).isEqualTo(0);
226 private void assertGroupRoleContainsRow(@Nullable Integer groupId, @Nullable Integer resourceId, String role) {
227 String sql = groupRoleRowSql(groupId, resourceId, role);
228 assertThat(db.countSql(sql)).isEqualTo(1);
231 private static String groupRoleRowSql(@Nullable Integer groupId, @Nullable Integer resourceId, String role) {
233 "select count(*) from group_roles where group_id %s and resource_id %s and role = '%s'",
234 whereClauseFromInteger(groupId),
235 whereClauseFromInteger(resourceId),
239 private void assertUserRoleDoesNotContainRow(@Nullable Integer groupId, @Nullable Integer resourceId, String role) {
240 String sql = userRoleRowSql(groupId, resourceId, role);
241 assertThat(db.countSql(sql)).isEqualTo(0);
244 private void assertUserRoleContainsRow(@Nullable Integer groupId, @Nullable Integer resourceId, String role) {
245 String sql = userRoleRowSql(groupId, resourceId, role);
246 assertThat(db.countSql(sql)).isEqualTo(1);
249 private static String userRoleRowSql(@Nullable Integer userId, @Nullable Integer resourceId, String role) {
251 "select count(*) from user_roles where user_id %s and resource_id %s and role = '%s'",
252 whereClauseFromInteger(userId),
253 whereClauseFromInteger(resourceId),
257 private void insertGroupRole(@Nullable Integer groupId, @Nullable Integer resourceId, String role) {
258 db.executeUpdateSql(format("insert into group_roles (group_id,resource_id,role) values(%s,%s,'%s')", nullValueFromInteger(groupId), nullValueFromInteger(resourceId), role));
261 private void insertUserRole(@Nullable Integer userId, @Nullable Integer resourceId, String role) {
262 db.executeUpdateSql(format("insert into user_roles (user_id,resource_id,role) values(%s,%s,'%s')", nullValueFromInteger(userId), nullValueFromInteger(resourceId), role));
265 private static String whereClauseFromInteger(@Nullable Integer id) {
272 private String nullValueFromInteger(@Nullable Integer value) {
276 return String.valueOf(value);
279 private void truncate(String tableName) {
280 db.executeUpdateSql("truncate table " + tableName);