From 982713f8d4749a7c9fa867e772e532069da2b186 Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Thu, 23 Nov 2023 11:38:56 +0100 Subject: SONAR-21073 Add endpoint /api/v2/authorizations/group-memberships (GET/POST/DELETE) --- .../version/v104/AddUuidColumnToGroupsUsersIT.java | 51 ++++++++++ .../v104/CreatePrimaryKeyOnGroupsUsersTableIT.java | 52 ++++++++++ .../v104/MakeUuidInGroupsUsersNotNullableIT.java | 51 ++++++++++ .../version/v104/PopulateGroupsUsersUuidIT.java | 107 +++++++++++++++++++++ .../platform/db/migration/step/MassUpdate.java | 2 +- .../version/v104/AddUuidColumnToGroupsUsers.java | 55 +++++++++++ .../v104/CreatePrimaryKeyOnGroupsUsersTable.java | 52 ++++++++++ .../db/migration/version/v104/DbVersion104.java | 6 +- .../v104/MakeUuidInGroupsUsersNotNullable.java | 48 +++++++++ .../version/v104/PopulateGroupsUsersUuid.java | 69 +++++++++++++ 10 files changed, 491 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/AddUuidColumnToGroupsUsersIT.java create mode 100644 server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/CreatePrimaryKeyOnGroupsUsersTableIT.java create mode 100644 server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/MakeUuidInGroupsUsersNotNullableIT.java create mode 100644 server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/PopulateGroupsUsersUuidIT.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/AddUuidColumnToGroupsUsers.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/CreatePrimaryKeyOnGroupsUsersTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/MakeUuidInGroupsUsersNotNullable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v104/PopulateGroupsUsersUuid.java (limited to 'server/sonar-db-migration/src') diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/AddUuidColumnToGroupsUsersIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/AddUuidColumnToGroupsUsersIT.java new file mode 100644 index 00000000000..d6a1bfd39c0 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/AddUuidColumnToGroupsUsersIT.java @@ -0,0 +1,51 @@ +/* + * 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.server.platform.db.migration.version.v104; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.MigrationDbTester; + +import static org.assertj.core.api.Assertions.assertThatCode; + +public class AddUuidColumnToGroupsUsersIT { + + private static final String TABLE_NAME = "groups_users"; + private static final String COLUMN_NAME = "uuid"; + + @Rule + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(AddUuidColumnToGroupsUsers.class); + private final AddUuidColumnToGroupsUsers underTest = new AddUuidColumnToGroupsUsers(db.database()); + + @Test + public void execute_whenColumnDoesNotExist_shouldCreateColumn() throws SQLException { + db.assertColumnDoesNotExist(TABLE_NAME, COLUMN_NAME); + underTest.execute(); + db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, Types.VARCHAR, 40, true); + } + + @Test + public void execute_whenColumnsAlreadyExists_shouldNotFail() throws SQLException { + underTest.execute(); + assertThatCode(underTest::execute).doesNotThrowAnyException(); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/CreatePrimaryKeyOnGroupsUsersTableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/CreatePrimaryKeyOnGroupsUsersTableIT.java new file mode 100644 index 00000000000..85b9a4534b8 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/CreatePrimaryKeyOnGroupsUsersTableIT.java @@ -0,0 +1,52 @@ +/* + * 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.server.platform.db.migration.version.v104; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.MigrationDbTester; + +import static org.sonar.server.platform.db.migration.version.v104.AddUuidColumnToGroupsUsers.GROUPS_USERS_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v104.AddUuidColumnToGroupsUsers.GROUPS_USERS_UUID_COLUMN_NAME; +import static org.sonar.server.platform.db.migration.version.v104.CreatePrimaryKeyOnGroupsUsersTable.PK_NAME; + +public class CreatePrimaryKeyOnGroupsUsersTableIT { + @Rule + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreatePrimaryKeyOnGroupsUsersTable.class); + private final CreatePrimaryKeyOnGroupsUsersTable createIndex = new CreatePrimaryKeyOnGroupsUsersTable(db.database()); + + @Test + public void execute_whenPrimaryKeyDoesntExist_shouldCreatePrimaryKey() throws SQLException { + db.assertNoPrimaryKey(GROUPS_USERS_TABLE_NAME); + + createIndex.execute(); + db.assertPrimaryKey(GROUPS_USERS_TABLE_NAME, PK_NAME, GROUPS_USERS_UUID_COLUMN_NAME); + } + + @Test + public void execute_whenPrimaryKeyAlreadyExist_shouldKeepThePrimaryKeyAndNotFail() throws SQLException { + createIndex.execute(); + createIndex.execute(); + + db.assertPrimaryKey(GROUPS_USERS_TABLE_NAME, PK_NAME, GROUPS_USERS_UUID_COLUMN_NAME); + } + +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/MakeUuidInGroupsUsersNotNullableIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/MakeUuidInGroupsUsersNotNullableIT.java new file mode 100644 index 00000000000..88c3f6d1005 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/MakeUuidInGroupsUsersNotNullableIT.java @@ -0,0 +1,51 @@ +/* + * 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.server.platform.db.migration.version.v104; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.MigrationDbTester; + +import static java.sql.Types.VARCHAR; +import static org.sonar.server.platform.db.migration.version.v104.AddUuidColumnToGroupsUsers.GROUPS_USERS_TABLE_NAME; +import static org.sonar.server.platform.db.migration.version.v104.AddUuidColumnToGroupsUsers.GROUPS_USERS_UUID_COLUMN_NAME; + +public class MakeUuidInGroupsUsersNotNullableIT { + + @Rule + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep( MakeUuidInGroupsUsersNotNullable.class); + private final MakeUuidInGroupsUsersNotNullable underTest = new MakeUuidInGroupsUsersNotNullable(db.database()); + + @Test + public void execute_whenUuidColumnIsNullable_shouldMakeItNonNullable() throws SQLException { + db.assertColumnDefinition(GROUPS_USERS_TABLE_NAME, GROUPS_USERS_UUID_COLUMN_NAME, VARCHAR, null, true); + underTest.execute(); + db.assertColumnDefinition(GROUPS_USERS_TABLE_NAME, GROUPS_USERS_UUID_COLUMN_NAME, VARCHAR, null, false); + } + + @Test + public void execute_whenUuidColumnIsNullable_shouldKeepItNullableAndNotFail() throws SQLException { + db.assertColumnDefinition(GROUPS_USERS_TABLE_NAME, GROUPS_USERS_UUID_COLUMN_NAME, VARCHAR, null, true); + underTest.execute(); + underTest.execute(); + db.assertColumnDefinition(GROUPS_USERS_TABLE_NAME, GROUPS_USERS_UUID_COLUMN_NAME, VARCHAR, null, false); + } +} diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/PopulateGroupsUsersUuidIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/PopulateGroupsUsersUuidIT.java new file mode 100644 index 00000000000..ab8060739a3 --- /dev/null +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v104/PopulateGroupsUsersUuidIT.java @@ -0,0 +1,107 @@ +/* + * 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.server.platform.db.migration.version.v104; + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import org.assertj.core.groups.Tuple; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.UuidFactoryFast; +import org.sonar.db.MigrationDbTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class PopulateGroupsUsersUuidIT { + + private static final String GROUPS_USERS_TABLE_NAME = "groups_users"; + @Rule + public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(PopulateGroupsUsersUuid.class); + + private final PopulateGroupsUsersUuid migration = new PopulateGroupsUsersUuid(db.database(), UuidFactoryFast.getInstance()); + + @Test + public void execute_whenTableIsEmpty_shouldPopulate() throws SQLException { + insertRowsWithoutUuid(); + + migration.execute(); + + verifyUuidPresentAndUnique(); + } + + + + @Test + public void execute_isReentrant() throws SQLException { + insertRowsWithoutUuid(); + migration.execute(); + List existingUuids = getExistingUuids(); + + migration.execute(); + verifyUuidsNotChanged(existingUuids); + + migration.execute(); + verifyUuidsNotChanged(existingUuids); + } + + private void insertRowsWithoutUuid() { + db.executeInsert(GROUPS_USERS_TABLE_NAME, + "uuid", null, + "group_uuid", "group1_uuid", + "user_uuid", "user1_uuid"); + + db.executeInsert(GROUPS_USERS_TABLE_NAME, + "uuid", null, + "group_uuid", "group2_uuid", + "user_uuid", "user2_uuid"); + + db.executeInsert(GROUPS_USERS_TABLE_NAME, + "uuid", null, + "group_uuid", "group3_uuid", + "user_uuid", "user3_uuid"); + } + + private void verifyUuidPresentAndUnique() { + List> rows = db.select("select uuid, group_uuid, user_uuid from groups_users"); + rows + .forEach(stringObjectMap -> assertThat(stringObjectMap.get("UUID")).isNotNull()); + long uniqueCount = rows.stream().map(row -> row.get("UUID")).distinct().count(); + assertThat(uniqueCount).isEqualTo(rows.size()); + + } + + private List getExistingUuids() { + return db.select("select uuid, group_uuid, user_uuid from groups_users") + .stream() + .map(stringObjectMap -> tuple(stringObjectMap.get("UUID"), stringObjectMap.get("GROUP_UUID"), stringObjectMap.get("USER_UUID"))) + .toList(); + } + + private void verifyUuidsNotChanged(List existingUuids) { + assertThat(db.select("select uuid, group_uuid, user_uuid from groups_users")) + .extracting(stringObjectMap -> tuple(stringObjectMap.get("UUID"), stringObjectMap.get("GROUP_UUID"), stringObjectMap.get("USER_UUID"))) + .containsExactlyInAnyOrderElementsOf(existingUuids); + } + + + +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MassUpdate.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MassUpdate.java index fc08adfe0a4..117ca3271e6 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MassUpdate.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MassUpdate.java @@ -67,7 +67,7 @@ public class MassUpdate { this.writeConnection = writeConnection; } - public SqlStatement select(String sql) throws SQLException { + public SqlStatement select = massUpdate.select(SELECT_QUERY); + Upsert setUuid = massUpdate.update(SET_UUID_STATEMENT); + try (select; setUuid) { + massUpdate.execute((row, update, index) -> { + String groupUuid = row.getString(1); + String userUuid = row.getString(2); + String uuid = uuidFactory.create(); + update.setString(1, uuid); + update.setString(2, groupUuid); + update.setString(3, userUuid); + return true; + }); + } + } +} -- cgit v1.2.3