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.

ImpactSumFormulaTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.ce.task.projectanalysis.formula;
  21. import com.google.gson.Gson;
  22. import java.util.LinkedHashMap;
  23. import java.util.Optional;
  24. import javax.annotation.Nullable;
  25. import org.junit.Test;
  26. import org.sonar.api.issue.impact.Severity;
  27. import org.sonar.ce.task.projectanalysis.measure.Measure;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. public class ImpactSumFormulaTest {
  32. public static final ImpactSumFormula IMPACT_SUM_FORMULA = ImpactSumFormula.createImpactSumFormula("metricKey");
  33. private final Gson gson = new Gson();
  34. private final CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
  35. private final CreateMeasureContext createMeasureContext = mock(CreateMeasureContext.class);
  36. @Test
  37. public void getOutputMetricKeys_shouldReturnCorrectMetrics() {
  38. assertThat(IMPACT_SUM_FORMULA.getOutputMetricKeys()).containsExactly("metricKey");
  39. }
  40. @Test
  41. public void createMeasure_whenCounterReturnsValue_shouldReturnExpectedMeasure() {
  42. ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
  43. String value = newImpactJson(4, 2, 1, 1);
  44. addMeasure("metricKey", value);
  45. counter.initialize(counterInitializationContext);
  46. assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).get().extracting(Measure::getData)
  47. .isEqualTo(value);
  48. }
  49. @Test
  50. public void createMeasure_whenCounterReturnsNoValue_shouldReturnNoMeasure() {
  51. ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
  52. assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).isEmpty();
  53. }
  54. @Test
  55. public void createNewCounter_shouldReturnExpectedCounter() {
  56. assertThat(IMPACT_SUM_FORMULA.createNewCounter()).isNotNull()
  57. .isInstanceOf(ImpactSumFormula.ImpactCounter.class);
  58. }
  59. @Test
  60. public void getValue_whenInitialized_shouldReturnExpectedValue() {
  61. ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
  62. String value = newImpactJson(4, 2, 1, 1);
  63. addMeasure("metricKey", value);
  64. counter.initialize(counterInitializationContext);
  65. assertThat(counter.getValue()).get().isEqualTo(value);
  66. }
  67. @Test
  68. public void getValue_whenAggregatingExistingValue_shouldReturnAggregationResult() {
  69. ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
  70. String value = newImpactJson(4, 2, 1, 1);
  71. addMeasure("metricKey", value);
  72. counter.initialize(counterInitializationContext);
  73. ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
  74. String value2 = newImpactJson(3, 1, 1, 1);
  75. addMeasure("metricKey", value2);
  76. counter2.initialize(counterInitializationContext);
  77. counter.aggregate(counter2);
  78. assertThat(counter.getValue()).get().isEqualTo(newImpactJson(7, 3, 2, 2));
  79. }
  80. @Test
  81. public void getValue_whenAggregatingUninitializedValue_shouldReturnEmptyValue() {
  82. ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
  83. String value = newImpactJson(4, 2, 1, 1);
  84. addMeasure("metricKey", value);
  85. counter.initialize(counterInitializationContext);
  86. ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
  87. counter.aggregate(counter2);
  88. assertThat(counter.getValue()).isEmpty();
  89. }
  90. @Test
  91. public void getValue_whenAggregatingEmptyValue_shouldReturnEmptyValue() {
  92. ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
  93. String value = newImpactJson(4, 2, 1, 1);
  94. addMeasure("metricKey", value);
  95. counter.initialize(counterInitializationContext);
  96. ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
  97. addMeasure("metricKey", null);
  98. counter2.initialize(counterInitializationContext);
  99. counter.aggregate(counter2);
  100. assertThat(counter.getValue()).isEmpty();
  101. }
  102. @Test
  103. public void getValue_whenNotInitialized_shouldReturnEmpty() {
  104. ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
  105. assertThat(counter.getValue()).isEmpty();
  106. }
  107. public String newImpactJson(Integer total, Integer high, Integer medium, Integer low) {
  108. LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
  109. map.put(Severity.LOW.name(), low);
  110. map.put(Severity.MEDIUM.name(), medium);
  111. map.put(Severity.HIGH.name(), high);
  112. map.put("total", total);
  113. return gson.toJson(map);
  114. }
  115. private void addMeasure(String metricKey, @Nullable String value) {
  116. when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.ofNullable(value).map(v -> Measure.newMeasureBuilder().create(v)));
  117. }
  118. }