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.

ImpactMeasureBuilderTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.measure;
  21. import java.util.Map;
  22. import org.junit.jupiter.api.Test;
  23. import org.sonar.api.issue.impact.Severity;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  26. class ImpactMeasureBuilderTest {
  27. @Test
  28. void createEmptyMeasure_shouldReturnMeasureWithAllFields() {
  29. ImpactMeasureBuilder builder = ImpactMeasureBuilder.createEmpty();
  30. assertThat(builder.buildAsMap())
  31. .containsAllEntriesOf(getImpactMap(0L, 0L, 0L, 0L));
  32. }
  33. private static Map<String, Long> getImpactMap(Long total, Long high, Long medium, Long low) {
  34. return Map.of("total", total, Severity.HIGH.name(), high, Severity.MEDIUM.name(), medium, Severity.LOW.name(), low);
  35. }
  36. @Test
  37. void fromMap_shouldInitializeCorrectlyTheBuilder() {
  38. Map<String, Long> map = getImpactMap(6L, 3L, 2L, 1L);
  39. ImpactMeasureBuilder builder = ImpactMeasureBuilder.fromMap(map);
  40. assertThat(builder.buildAsMap())
  41. .isEqualTo(map);
  42. }
  43. @Test
  44. void fromMap_whenMissingField_shouldThrowException() {
  45. Map<String, Long> map = Map.of();
  46. assertThatThrownBy(() -> ImpactMeasureBuilder.fromMap(map))
  47. .isInstanceOf(IllegalArgumentException.class)
  48. .hasMessage("Map must contain a total key");
  49. }
  50. @Test
  51. void toString_shouldInitializeCorrectlyTheBuilder() {
  52. ImpactMeasureBuilder builder = ImpactMeasureBuilder.fromString("""
  53. {
  54. total: 6,
  55. HIGH: 3,
  56. MEDIUM: 2,
  57. LOW: 1
  58. }
  59. """);
  60. assertThat(builder.buildAsMap())
  61. .isEqualTo(getImpactMap(6L, 3L, 2L, 1L));
  62. }
  63. @Test
  64. void buildAsMap_whenIsEmpty_shouldThrowException() {
  65. ImpactMeasureBuilder impactMeasureBuilder = ImpactMeasureBuilder.newInstance();
  66. assertThatThrownBy(impactMeasureBuilder::buildAsMap)
  67. .isInstanceOf(IllegalArgumentException.class)
  68. .hasMessage("Map must contain a total key");
  69. }
  70. @Test
  71. void buildAsMap_whenMissingSeverity_shouldThrowException() {
  72. ImpactMeasureBuilder impactMeasureBuilder = ImpactMeasureBuilder.newInstance()
  73. .setTotal(1L)
  74. .setSeverity(Severity.HIGH, 1L)
  75. .setSeverity(Severity.MEDIUM, 1L);
  76. assertThatThrownBy(impactMeasureBuilder::buildAsMap)
  77. .isInstanceOf(IllegalArgumentException.class)
  78. .hasMessage("Map must contain a key for severity LOW");
  79. }
  80. @Test
  81. void buildAsString_whenMissingSeverity_shouldThrowException() {
  82. ImpactMeasureBuilder impactMeasureBuilder = ImpactMeasureBuilder.newInstance()
  83. .setTotal(1L)
  84. .setSeverity(Severity.HIGH, 1L)
  85. .setSeverity(Severity.MEDIUM, 1L);
  86. assertThatThrownBy(impactMeasureBuilder::buildAsString)
  87. .isInstanceOf(IllegalArgumentException.class)
  88. .hasMessage("Map must contain a key for severity LOW");
  89. }
  90. @Test
  91. void setSeverity_shouldInitializeSeverityValues() {
  92. ImpactMeasureBuilder builder = ImpactMeasureBuilder.newInstance()
  93. .setSeverity(Severity.HIGH, 3L)
  94. .setSeverity(Severity.MEDIUM, 2L)
  95. .setSeverity(Severity.LOW, 1L)
  96. .setTotal(6L);
  97. assertThat(builder.buildAsMap())
  98. .isEqualTo(getImpactMap(6L, 3L, 2L, 1L));
  99. }
  100. @Test
  101. void add_shouldSumImpactsAndTotal() {
  102. ImpactMeasureBuilder builder = ImpactMeasureBuilder.fromMap(getImpactMap(6L, 3L, 2L, 1L))
  103. .add(ImpactMeasureBuilder.newInstance().setTotal(6L).setSeverity(Severity.HIGH, 3L).setSeverity(Severity.MEDIUM, 2L).setSeverity(Severity.LOW, 1L));
  104. assertThat(builder.buildAsMap())
  105. .isEqualTo(getImpactMap(12L, 6L, 4L, 2L));
  106. }
  107. @Test
  108. void add_whenOtherMapHasMissingField_shouldThrowException() {
  109. ImpactMeasureBuilder impactMeasureBuilder = ImpactMeasureBuilder.newInstance();
  110. ImpactMeasureBuilder otherBuilder = ImpactMeasureBuilder.newInstance();
  111. assertThatThrownBy(() -> impactMeasureBuilder.add(otherBuilder))
  112. .isInstanceOf(IllegalArgumentException.class)
  113. .hasMessage("Map must contain a total key");
  114. }
  115. @Test
  116. void getTotal_shoudReturnExpectedTotal() {
  117. ImpactMeasureBuilder builder = ImpactMeasureBuilder.fromMap(getImpactMap(6L, 3L, 2L, 1L));
  118. assertThat(builder.getTotal()).isEqualTo(6L);
  119. }
  120. }