You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeletePersonMeasuresTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.v70;
  21. import java.sql.SQLException;
  22. import java.util.concurrent.atomic.AtomicInteger;
  23. import org.apache.commons.lang.math.RandomUtils;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.api.impl.config.MapSettings;
  27. import org.sonar.db.CoreDbTester;
  28. import org.sonar.server.platform.db.migration.step.DataChange;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. public class DeletePersonMeasuresTest {
  31. private static final AtomicInteger GENERATOR = new AtomicInteger();
  32. @Rule
  33. public CoreDbTester db = CoreDbTester.createForSchema(DeletePersonMeasuresTest.class, "initial.sql");
  34. private DataChange underTest = new DeletePersonMeasures(db.database());
  35. @Test
  36. public void delete_person_measures() throws SQLException {
  37. insertComponent("P1", "PRJ", "TRK");
  38. insertComponent("D1", "DIR", "DIR");
  39. insertComponent("F1", "FIL", "FIL");
  40. insertComponent("F2", "FIL", "UTS");
  41. insertSnapshot("S1", "P1", false);
  42. insertSnapshot("S2", "P1", true);
  43. // past measures
  44. long m1 = insertMeasure("P1", "S1");
  45. long m2 = insertMeasure("D1", "S1");
  46. long m3 = insertMeasure("F1", "S1");
  47. long m4 = insertMeasure("F2", "S1");
  48. long m5 = insertPersonMeasure("P1", "S1");
  49. long m6 = insertPersonMeasure("F1", "S1");
  50. // last measures
  51. long m7 = insertMeasure("P1", "S2");
  52. long m8 = insertMeasure("D1", "S2");
  53. long m9 = insertMeasure("F1", "S2");
  54. long m10 = insertMeasure("F2", "S2");
  55. long m11 = insertPersonMeasure("P1", "S2");
  56. long m12 = insertPersonMeasure("F1", "S2");
  57. underTest.execute();
  58. assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
  59. assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
  60. assertThatMeasuresAreExactly(m1, m2, m3, m4, m7, m8, m9, m10);
  61. // migration is re-entrant
  62. underTest.execute();
  63. assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
  64. assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
  65. assertThatMeasuresAreExactly(m1, m2, m3, m4, m7, m8, m9, m10);
  66. }
  67. private void assertThatMeasuresAreExactly(long... expectedMeasureIds) {
  68. long[] ids = db.select("select id as \"id\" from project_measures")
  69. .stream()
  70. .mapToLong(m -> (Long) m.get("id"))
  71. .toArray();
  72. assertThat(ids).containsOnly(expectedMeasureIds);
  73. }
  74. private void insertComponent(String uuid, String scope, String qualifier) {
  75. db.executeInsert("PROJECTS",
  76. "ORGANIZATION_UUID", "O1",
  77. "KEE", "" + GENERATOR.incrementAndGet(),
  78. "UUID", uuid,
  79. "PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
  80. "MAIN_BRANCH_PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
  81. "UUID_PATH", ".",
  82. "ROOT_UUID", "" + GENERATOR.incrementAndGet(),
  83. "PRIVATE", "true",
  84. "QUALIFIER", qualifier,
  85. "SCOPE", scope);
  86. }
  87. private void insertSnapshot(String uuid, String projectUuid, boolean last) {
  88. db.executeInsert("SNAPSHOTS",
  89. "UUID", uuid,
  90. "COMPONENT_UUID", projectUuid,
  91. "STATUS", "P",
  92. "ISLAST", last);
  93. }
  94. private long insertMeasure(String componentUuid, String analysisUuid) {
  95. long id = GENERATOR.incrementAndGet();
  96. db.executeInsert("PROJECT_MEASURES",
  97. "ID", id,
  98. "METRIC_ID", "42",
  99. "COMPONENT_UUID", componentUuid,
  100. "ANALYSIS_UUID", analysisUuid);
  101. return id;
  102. }
  103. private long insertPersonMeasure(String componentUuid, String analysisUuid) {
  104. long id = GENERATOR.incrementAndGet();
  105. db.executeInsert("PROJECT_MEASURES",
  106. "ID", id,
  107. "METRIC_ID", "42",
  108. "COMPONENT_UUID", componentUuid,
  109. "ANALYSIS_UUID", analysisUuid,
  110. "PERSON_ID", RandomUtils.nextInt(100));
  111. return id;
  112. }
  113. }