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.

PopulateNclocForProjectsTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.v99;
  21. import java.sql.SQLException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.core.util.UuidFactory;
  27. import org.sonar.core.util.UuidFactoryFast;
  28. import org.sonar.db.CoreDbTester;
  29. import org.sonar.server.platform.db.migration.step.DataChange;
  30. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. public class PopulateNclocForProjectsTest {
  33. private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
  34. @Rule
  35. public CoreDbTester db = CoreDbTester.createForSchema(PopulateNclocForProjectsTest.class, "schema.sql");
  36. private final DataChange underTest = new PopulateNclocForProjects(db.database());
  37. @Test
  38. public void migration_populates_ncloc_for_projects() throws SQLException {
  39. Map<String, Long> expectedNclocByProjectUuid = populateData();
  40. underTest.execute();
  41. verifyNclocCorrectlyPopulatedForProjects(expectedNclocByProjectUuid);
  42. }
  43. @Test
  44. public void migration_should_be_reentrant() throws SQLException {
  45. Map<String, Long> expectedNclocByProjectUuid = populateData();
  46. underTest.execute();
  47. // re-entrant
  48. underTest.execute();
  49. verifyNclocCorrectlyPopulatedForProjects(expectedNclocByProjectUuid);
  50. }
  51. private Map<String, Long> populateData() {
  52. String nclocMetricUuid = insertMetric("ncloc");
  53. String projectUuid1 = insertProject();
  54. String project1Branch1 = insertProjectBranch(projectUuid1);
  55. String project1Branch2 = insertProjectBranch(projectUuid1);
  56. long project1maxNcloc = 100;
  57. insertLiveMeasure(nclocMetricUuid, projectUuid1, project1Branch1, 80L);
  58. insertLiveMeasure(nclocMetricUuid, projectUuid1, project1Branch2, project1maxNcloc);
  59. String otherMetricUuid = insertMetric("other");
  60. insertLiveMeasure(otherMetricUuid, projectUuid1, project1Branch1, 5000L);
  61. insertLiveMeasure(otherMetricUuid, projectUuid1, project1Branch2, 6000L);
  62. String projectUuid2 = insertProject();
  63. String project2Branch1 = insertProjectBranch(projectUuid2);
  64. String project2Branch2 = insertProjectBranch(projectUuid2);
  65. String project2Branch3 = insertProjectBranch(projectUuid2);
  66. long project2maxNcloc = 60;
  67. insertLiveMeasure(nclocMetricUuid, projectUuid2, project2Branch1, 20L);
  68. insertLiveMeasure(nclocMetricUuid, projectUuid2, project2Branch2, 50L);
  69. insertLiveMeasure(nclocMetricUuid, projectUuid2, project2Branch3, project2maxNcloc);
  70. return Map.of(projectUuid1, project1maxNcloc, projectUuid2, project2maxNcloc);
  71. }
  72. private void verifyNclocCorrectlyPopulatedForProjects(Map<String, Long> expectedNclocByProjectUuid) {
  73. for (Map.Entry<String, Long> entry : expectedNclocByProjectUuid.entrySet()) {
  74. String query = String.format("select ncloc from projects where uuid='%s'", entry.getKey());
  75. Long nclocFromProject = (Long) db.selectFirst(query).get("NCLOC");
  76. assertThat(nclocFromProject).isEqualTo(entry.getValue());
  77. }
  78. }
  79. private String insertMetric(String name) {
  80. Map<String, Object> map = new HashMap<>();
  81. String uuid = uuidFactory.create();
  82. map.put("UUID", uuid);
  83. map.put("NAME", name);
  84. db.executeInsert("metrics", map);
  85. return uuid;
  86. }
  87. private String insertProject() {
  88. Map<String, Object> map = new HashMap<>();
  89. String uuid = uuidFactory.create();
  90. map.put("UUID", uuid);
  91. map.put("KEE", randomAlphabetic(20));
  92. map.put("QUALIFIER", "TRK");
  93. map.put("PRIVATE", true);
  94. map.put("UPDATED_AT", System.currentTimeMillis());
  95. db.executeInsert("projects", map);
  96. return uuid;
  97. }
  98. private String insertProjectBranch(String projectUuid) {
  99. Map<String, Object> map = new HashMap<>();
  100. String uuid = uuidFactory.create();
  101. map.put("UUID", uuid);
  102. map.put("PROJECT_UUID", projectUuid);
  103. map.put("KEE", randomAlphabetic(20));
  104. map.put("BRANCH_TYPE", "PULL_REQUEST");
  105. map.put("UPDATED_AT", System.currentTimeMillis());
  106. map.put("CREATED_AT", System.currentTimeMillis());
  107. map.put("NEED_ISSUE_SYNC", false);
  108. db.executeInsert("project_branches", map);
  109. return uuid;
  110. }
  111. private void insertLiveMeasure(String metricUuid, String projectUuid, String componentUuid, Long value) {
  112. Map<String, Object> map = new HashMap<>();
  113. String uuid = uuidFactory.create();
  114. map.put("UUID", uuid);
  115. map.put("PROJECT_UUID", projectUuid);
  116. map.put("COMPONENT_UUID", componentUuid);
  117. map.put("METRIC_UUID", metricUuid);
  118. map.put("VALUE", value);
  119. map.put("UPDATED_AT", System.currentTimeMillis());
  120. map.put("CREATED_AT", System.currentTimeMillis());
  121. db.executeInsert("live_measures", map);
  122. }
  123. }