Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DeleteSecurityReviewRatingProjectMeasuresTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.v84.metrics.projectmeasures;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.sql.SQLException;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import org.apache.commons.lang.math.RandomUtils;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.sonar.db.CoreDbTester;
  30. import org.sonar.server.platform.db.migration.step.DataChange;
  31. import static java.lang.String.format;
  32. import static java.util.Collections.singletonList;
  33. import static java.util.stream.Collectors.toList;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. public class DeleteSecurityReviewRatingProjectMeasuresTest {
  36. private static final String PROJECT_MEASURES_TABLE = "project_measures";
  37. @Rule
  38. public CoreDbTester db = CoreDbTester.createForSchema(DeleteSecurityReviewRatingProjectMeasuresTest.class, "schema.sql");
  39. private DataChange underTest = new DeleteSecurityReviewRatingProjectMeasures(db.database());
  40. @Test
  41. public void removes_project_measure_for_review_rating_and_review_rating_effort_metrics() throws SQLException {
  42. String codeSmellMetric = insertMetric(1L, "security_review_rating");
  43. insertProjectMeasure(codeSmellMetric);
  44. String reviewRatingEffort = insertMetric(2L, "security_review_rating_effort");
  45. insertProjectMeasure(reviewRatingEffort);
  46. String anotherMetric = insertMetric(3L, "another_metric");
  47. String anotherMetricProjectMeasure = insertProjectMeasure(anotherMetric);
  48. underTest.execute();
  49. verifyProjectMeasureIds(singletonList(anotherMetricProjectMeasure));
  50. }
  51. private String insertMetric(Long id, String key) {
  52. db.executeInsert("metrics",
  53. "id", id,
  54. "uuid", id,
  55. "name", key);
  56. return (String) db.selectFirst(format("select uuid as \"uuid\" from metrics where name='%s'", key)).get("uuid");
  57. }
  58. private String insertProjectMeasure(String metricUuid) {
  59. double projectMeasureUUid = RandomUtils.nextDouble();
  60. Map<String, Object> values = new HashMap<>(ImmutableMap.of("uuid", projectMeasureUUid, "metric_uuid", metricUuid,
  61. "analysis_uuid", "analysis_uuid", "component_uuid", "component_uuid"));
  62. db.executeInsert(PROJECT_MEASURES_TABLE, values);
  63. String sql = format("select uuid as \"uuid\" from %s where metric_uuid='%s'", PROJECT_MEASURES_TABLE, metricUuid);
  64. return (String) db
  65. .selectFirst(sql)
  66. .get("uuid");
  67. }
  68. private void verifyProjectMeasureIds(List<String> expectedProjectMeasureIds) {
  69. List<Map<String, Object>> results = db.select("select uuid from " + PROJECT_MEASURES_TABLE);
  70. assertThat(results.stream()
  71. .map(map -> (String) map.get("UUID"))
  72. .collect(toList()))
  73. .containsExactlyInAnyOrderElementsOf(expectedProjectMeasureIds);
  74. }
  75. }