Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PopulateTableDefaultQProfilesTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.platform.db.migration.version.v65;
  21. import java.sql.SQLException;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.db.CoreDbTester;
  28. import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
  29. public class PopulateTableDefaultQProfilesTest {
  30. @Rule
  31. public ExpectedException expectedException = ExpectedException.none();
  32. @Rule
  33. public CoreDbTester db = CoreDbTester.createForSchema(PopulateTableDefaultQProfilesTest.class, "initial.sql");
  34. private System2 system2 = new AlwaysIncreasingSystem2();
  35. private PopulateTableDefaultQProfiles underTest = new PopulateTableDefaultQProfiles(db.database(), system2);
  36. @Test
  37. public void migration_is_reentrant() throws SQLException {
  38. insertRulesProfile("ORG_1", "java", "u1", true);
  39. insertRulesProfile("ORG_2", "js", "u2", true);
  40. // org1 is already processed
  41. insertDefaultQProfile("ORG_1", "java", "u1");
  42. underTest.execute();
  43. assertThat(countRows()).isEqualTo(2);
  44. assertThat(selectDefaultProfile("ORG_1", "java")).isEqualTo("u1");
  45. assertThat(selectDefaultProfile("ORG_2", "js")).isEqualTo("u2");
  46. }
  47. @Test
  48. public void DEFAULT_QPROFILES_is_populated_by_copying_the_RULES_PROFILES_marked_as_default() throws SQLException {
  49. insertRulesProfile("ORG_1", "java", "u1", false);
  50. insertRulesProfile("ORG_1", "java", "u2", true);
  51. insertRulesProfile("ORG_1", "js", "u3", true);
  52. underTest.execute();
  53. assertThat(countRows()).isEqualTo(2);
  54. assertThat(selectDefaultProfile("ORG_1", "java")).isEqualTo("u2");
  55. assertThat(selectDefaultProfile("ORG_1", "js")).isEqualTo("u3");
  56. }
  57. @Test
  58. public void duplicated_rows_of_table_RULES_PROFILES_are_ignored() throws SQLException {
  59. // two java profiles are marked as default.
  60. // The second one (as ordered by id) is ignored.
  61. insertRulesProfile("ORG_1", "java", "u1", false);
  62. insertRulesProfile("ORG_1", "java", "u2", true);
  63. insertRulesProfile("ORG_1", "java", "u3", true);
  64. underTest.execute();
  65. assertThat(countRows()).isEqualTo(1);
  66. assertThat(selectDefaultProfile("ORG_1", "java")).isEqualTo("u2");
  67. }
  68. private int countRows() {
  69. return db.countRowsOfTable("default_qprofiles");
  70. }
  71. private void insertRulesProfile(String orgUuid, String language, String uuid, boolean isDefault) {
  72. db.executeInsert("RULES_PROFILES",
  73. "NAME", "name_" + uuid,
  74. "KEE", uuid,
  75. "ORGANIZATION_UUID", orgUuid,
  76. "LANGUAGE", language,
  77. "IS_DEFAULT", isDefault,
  78. "IS_BUILT_IN", true);
  79. }
  80. private void insertDefaultQProfile(String orgUuid, String language, String uuid) {
  81. db.executeInsert("DEFAULT_QPROFILES",
  82. "ORGANIZATION_UUID", orgUuid,
  83. "LANGUAGE", language,
  84. "QPROFILE_UUID", uuid);
  85. }
  86. private String selectDefaultProfile(String orgUuid, String language) {
  87. return (String)db.selectFirst("select qprofile_uuid as QPROFILE_UUID from default_qprofiles where organization_uuid='" + orgUuid + "' and language='" + language + "'")
  88. .get("QPROFILE_UUID");
  89. }
  90. }