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.

MetricFinderTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.metric;
  21. import java.util.Arrays;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.measures.Metric;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.db.metric.MetricDto;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.sonar.db.metric.MetricTesting.newMetricDto;
  30. public class MetricFinderTest {
  31. @Rule
  32. public DbTester db = DbTester.create(System2.INSTANCE);
  33. private final MetricFinder underTest = new MetricFinder(db.getDbClient());
  34. @Test
  35. public void findAll_enabled() {
  36. db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  37. db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  38. db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setEnabled(false));
  39. db.commit();
  40. assertThat(underTest.findAll()).hasSize(2);
  41. }
  42. @Test
  43. public void findAll_by_keys() {
  44. db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("ncloc"));
  45. db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("foo"));
  46. db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("coverage"));
  47. db.commit();
  48. assertThat(underTest.findAll(Arrays.asList("ncloc", "foo"))).extracting(Metric::getKey).containsExactlyInAnyOrder("ncloc", "foo")
  49. .doesNotContain("coverage");
  50. }
  51. @Test
  52. public void findById() {
  53. MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  54. MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  55. db.commit();
  56. assertThat(underTest.findByUuid(firstMetric.getUuid())).extracting(Metric::getKey).isEqualTo(firstMetric.getKey());
  57. }
  58. @Test
  59. public void findById_filters_out_disabled() {
  60. MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  61. MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setEnabled(false));
  62. db.commit();
  63. assertThat(underTest.findByUuid(secondMetric.getUuid())).isNull();
  64. }
  65. @Test
  66. public void findById_doesnt_find_anything() {
  67. MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  68. MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  69. db.commit();
  70. assertThat(underTest.findByUuid("non existing")).isNull();
  71. }
  72. @Test
  73. public void findByKey() {
  74. MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  75. MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  76. db.commit();
  77. assertThat(underTest.findByKey(secondMetric.getKey())).extracting(Metric::getKey).isEqualTo(secondMetric.getKey());
  78. }
  79. @Test
  80. public void findByKey_filters_out_disabled() {
  81. MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  82. MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setEnabled(false));
  83. db.commit();
  84. assertThat(underTest.findByKey(secondMetric.getKey())).isNull();
  85. }
  86. @Test
  87. public void findByKey_doesnt_find_anything() {
  88. MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  89. MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto());
  90. db.commit();
  91. assertThat(underTest.findByKey("doesnt exist")).isNull();
  92. }
  93. }