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.

QualityGateDetailsFormatterTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.qualitygate.ws;
  21. import java.io.IOException;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import javax.annotation.Nullable;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.commons.lang.text.StrSubstitutor;
  28. import org.junit.Test;
  29. import org.sonar.db.component.SnapshotDto;
  30. import org.sonarqube.ws.Qualitygates.ProjectStatusResponse;
  31. import org.sonarqube.ws.Qualitygates.ProjectStatusResponse.ProjectStatus;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  34. import static org.sonar.api.utils.DateUtils.formatDateTime;
  35. public class QualityGateDetailsFormatterTest {
  36. private QualityGateDetailsFormatter underTest;
  37. @Test
  38. public void map_level_conditions_and_periods() throws IOException {
  39. String measureData = IOUtils.toString(getClass().getResource("QualityGateDetailsFormatterTest/quality_gate_details.json"));
  40. SnapshotDto snapshot = new SnapshotDto()
  41. .setPeriodMode("last_version")
  42. .setPeriodParam("2015-12-07")
  43. .setPeriodDate(1449404331764L);
  44. underTest = newQualityGateDetailsFormatter(measureData, snapshot);
  45. ProjectStatus result = underTest.format();
  46. assertThat(result.getStatus()).isEqualTo(ProjectStatusResponse.Status.ERROR);
  47. // check conditions
  48. assertThat(result.getConditionsCount()).isEqualTo(3);
  49. List<ProjectStatusResponse.Condition> conditions = result.getConditionsList();
  50. assertThat(conditions).extracting("status").containsExactly(
  51. ProjectStatusResponse.Status.ERROR,
  52. ProjectStatusResponse.Status.WARN,
  53. ProjectStatusResponse.Status.OK);
  54. assertThat(conditions).extracting("metricKey").containsExactly("new_coverage", "new_blocker_violations", "new_critical_violations");
  55. assertThat(conditions).extracting("comparator").containsExactly(
  56. ProjectStatusResponse.Comparator.LT,
  57. ProjectStatusResponse.Comparator.GT,
  58. ProjectStatusResponse.Comparator.GT);
  59. assertThat(conditions).extracting("periodIndex").containsExactly(1, 1, 1);
  60. assertThat(conditions).extracting("warningThreshold").containsOnly("80", "");
  61. assertThat(conditions).extracting("errorThreshold").containsOnly("85", "0", "0");
  62. assertThat(conditions).extracting("actualValue").containsExactly("82.2985024398452", "1", "0");
  63. // check periods
  64. assertThat(result.getPeriodsCount()).isOne();
  65. List<ProjectStatusResponse.Period> periods = result.getPeriodsList();
  66. assertThat(periods).extracting("index").containsExactly(1);
  67. assertThat(periods).extracting("mode").containsExactly("last_version");
  68. assertThat(periods).extracting("parameter").containsExactly("2015-12-07");
  69. assertThat(periods.get(0).getDate()).isEqualTo(formatDateTime(snapshot.getPeriodDate()));
  70. }
  71. @Test
  72. public void ignore_period_not_set_on_leak_period() throws IOException {
  73. String measureData = IOUtils.toString(getClass().getResource("QualityGateDetailsFormatterTest/non_leak_period.json"));
  74. SnapshotDto snapshot = new SnapshotDto()
  75. .setPeriodMode("last_version")
  76. .setPeriodParam("2015-12-07")
  77. .setPeriodDate(1449404331764L);
  78. underTest = newQualityGateDetailsFormatter(measureData, snapshot);
  79. ProjectStatus result = underTest.format();
  80. // check conditions
  81. assertThat(result.getConditionsCount()).isOne();
  82. List<ProjectStatusResponse.Condition> conditions = result.getConditionsList();
  83. assertThat(conditions).extracting("status").containsExactly(ProjectStatusResponse.Status.ERROR);
  84. assertThat(conditions).extracting("metricKey").containsExactly("new_coverage");
  85. assertThat(conditions).extracting("comparator").containsExactly(ProjectStatusResponse.Comparator.LT);
  86. assertThat(conditions).extracting("periodIndex").containsExactly(1);
  87. assertThat(conditions).extracting("errorThreshold").containsOnly("85");
  88. assertThat(conditions).extracting("actualValue").containsExactly("82.2985024398452");
  89. }
  90. @Test
  91. public void fail_when_measure_level_is_unknown() {
  92. String measureData = "{\n" +
  93. " \"level\": \"UNKNOWN\",\n" +
  94. " \"conditions\": [\n" +
  95. " {\n" +
  96. " \"metric\": \"new_coverage\",\n" +
  97. " \"op\": \"LT\",\n" +
  98. " \"period\": 1,\n" +
  99. " \"warning\": \"80\",\n" +
  100. " \"error\": \"85\",\n" +
  101. " \"actual\": \"82.2985024398452\",\n" +
  102. " \"level\": \"ERROR\"\n" +
  103. " }\n" +
  104. " ]\n" +
  105. "}";
  106. underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
  107. assertThatThrownBy(() -> underTest.format())
  108. .isInstanceOf(IllegalStateException.class)
  109. .hasMessageContaining("Unknown quality gate status 'UNKNOWN'");
  110. }
  111. @Test
  112. public void fail_when_measure_op_is_unknown() {
  113. String measureData = "{\n" +
  114. " \"level\": \"ERROR\",\n" +
  115. " \"conditions\": [\n" +
  116. " {\n" +
  117. " \"metric\": \"new_coverage\",\n" +
  118. " \"op\": \"UNKNOWN\",\n" +
  119. " \"period\": 1,\n" +
  120. " \"warning\": \"80\",\n" +
  121. " \"error\": \"85\",\n" +
  122. " \"actual\": \"82.2985024398452\",\n" +
  123. " \"level\": \"ERROR\"\n" +
  124. " }\n" +
  125. " ]\n" +
  126. "}";
  127. underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
  128. assertThatThrownBy(() -> underTest.format())
  129. .isInstanceOf(IllegalStateException.class)
  130. .hasMessageContaining("Unknown quality gate comparator 'UNKNOWN'");
  131. }
  132. @Test
  133. public void verify_cayc_quality_gate_checked() throws IOException {
  134. String measureDataRaw = IOUtils.toString(getClass().getResource("QualityGateDetailsFormatterTest/cayc_compliant_qg.json"));
  135. String measureDataCompliant = StrSubstitutor.replace(measureDataRaw, Map.of("nmr_error", "1.0"));
  136. underTest = newQualityGateDetailsFormatter(measureDataCompliant, null);
  137. ProjectStatus result = underTest.format();
  138. assertThat(result.getIsCaycCompliant()).isTrue();
  139. String measureDataNonCompliant = StrSubstitutor.replace(measureDataRaw, Map.of("nmr_error", "2.0"));
  140. underTest = newQualityGateDetailsFormatter(measureDataNonCompliant, null);
  141. result = underTest.format();
  142. assertThat(result.getIsCaycCompliant()).isFalse();
  143. }
  144. @Test
  145. public void verify_cayc_quality_gate_with_missing_metric() throws IOException {
  146. String measureData = IOUtils.toString(getClass().getResource("QualityGateDetailsFormatterTest/cayc_missing_metric.json"));
  147. underTest = newQualityGateDetailsFormatter(measureData, null);
  148. ProjectStatus result = underTest.format();
  149. assertThat(result.getIsCaycCompliant()).isFalse();
  150. }
  151. private static QualityGateDetailsFormatter newQualityGateDetailsFormatter(@Nullable String measureData, @Nullable SnapshotDto snapshotDto) {
  152. return new QualityGateDetailsFormatter(Optional.ofNullable(measureData), Optional.ofNullable(snapshotDto));
  153. }
  154. }