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.

MetricDtoToMetricTest.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.ce.task.projectanalysis.metric;
  21. import org.junit.Test;
  22. import org.sonar.db.metric.MetricDto;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. public class MetricDtoToMetricTest {
  25. private static final double SOME_BEST_VALUE = 951;
  26. private MetricDtoToMetric underTest = MetricDtoToMetric.INSTANCE;
  27. @Test(expected = NullPointerException.class)
  28. public void apply_throws_NPE_if_arg_is_null() {
  29. underTest.apply(null);
  30. }
  31. @Test
  32. public void verify_mapping_from_dto() {
  33. for (Metric.MetricType metricType : Metric.MetricType.values()) {
  34. MetricDto metricDto = createMetricDto(metricType);
  35. Metric metric = underTest.apply(metricDto);
  36. assertThat(metric.getId()).isEqualTo(metricDto.getId());
  37. assertThat(metric.getKey()).isEqualTo(metricDto.getKey());
  38. assertThat(metric.getName()).isEqualTo(metricDto.getShortName());
  39. assertThat(metric.getType()).isEqualTo(metricType);
  40. assertThat(metric.isBestValueOptimized()).isFalse();
  41. assertThat(metric.getBestValue()).isEqualTo(SOME_BEST_VALUE);
  42. assertThat(metric.isDeleteHistoricalData()).isTrue();
  43. }
  44. }
  45. @Test
  46. public void verify_mapping_of_isBestValueOptimized() {
  47. assertThat(underTest.apply(createMetricDto(Metric.MetricType.INT).setOptimizedBestValue(true)).isBestValueOptimized()).isTrue();
  48. }
  49. @Test(expected = IllegalArgumentException.class)
  50. public void apply_throws_IAE_if_valueType_can_not_be_parsed() {
  51. underTest.apply(new MetricDto().setId(1).setKey("key").setValueType("trololo"));
  52. }
  53. private static MetricDto createMetricDto(Metric.MetricType metricType) {
  54. return new MetricDto()
  55. .setId(metricType.name().hashCode())
  56. .setKey(metricType.name() + "_key")
  57. .setShortName(metricType.name() + "_name")
  58. .setValueType(metricType.name())
  59. .setBestValue(SOME_BEST_VALUE)
  60. .setDeleteHistoricalData(true)
  61. .setEnabled(true);
  62. }
  63. }