3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualitygate.ws;
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.Optional;
25 import javax.annotation.Nullable;
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Test;
28 import org.sonar.db.component.SnapshotDto;
29 import org.sonarqube.ws.Qualitygates.ProjectStatusResponse;
30 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;
36 public class QualityGateDetailsFormatterTest {
38 private QualityGateDetailsFormatter underTest;
41 public void map_level_conditions_and_periods() throws IOException {
42 String measureData = IOUtils.toString(getClass().getResource("QualityGateDetailsFormatterTest/quality_gate_details.json"));
43 SnapshotDto snapshot = new SnapshotDto()
44 .setPeriodMode("last_version")
45 .setPeriodParam("2015-12-07")
46 .setPeriodDate(1449404331764L);
47 underTest = newQualityGateDetailsFormatter(measureData, snapshot);
49 ProjectStatus result = underTest.format();
51 assertThat(result.getStatus()).isEqualTo(ProjectStatusResponse.Status.ERROR);
53 assertThat(result.getConditionsCount()).isEqualTo(3);
54 List<ProjectStatusResponse.Condition> conditions = result.getConditionsList();
55 assertThat(conditions).extracting("status").containsExactly(
56 ProjectStatusResponse.Status.ERROR,
57 ProjectStatusResponse.Status.WARN,
58 ProjectStatusResponse.Status.OK);
59 assertThat(conditions).extracting("metricKey").containsExactly("new_coverage", "new_blocker_violations", "new_critical_violations");
60 assertThat(conditions).extracting("comparator").containsExactly(
61 ProjectStatusResponse.Comparator.LT,
62 ProjectStatusResponse.Comparator.GT,
63 ProjectStatusResponse.Comparator.GT);
64 assertThat(conditions).extracting("periodIndex").containsExactly(1, 1, 1);
65 assertThat(conditions).extracting("warningThreshold").containsOnly("80", "");
66 assertThat(conditions).extracting("errorThreshold").containsOnly("85", "0", "0");
67 assertThat(conditions).extracting("actualValue").containsExactly("82.2985024398452", "1", "0");
70 assertThat(result.getPeriodsCount()).isOne();
71 List<ProjectStatusResponse.Period> periods = result.getPeriodsList();
72 assertThat(periods).extracting("index").containsExactly(1);
73 assertThat(periods).extracting("mode").containsExactly("last_version");
74 assertThat(periods).extracting("parameter").containsExactly("2015-12-07");
75 assertThat(periods.get(0).getDate()).isEqualTo(formatDateTime(snapshot.getPeriodDate()));
79 public void ignore_period_not_set_on_leak_period() throws IOException {
80 String measureData = IOUtils.toString(getClass().getResource("QualityGateDetailsFormatterTest/non_leak_period.json"));
81 SnapshotDto snapshot = new SnapshotDto()
82 .setPeriodMode("last_version")
83 .setPeriodParam("2015-12-07")
84 .setPeriodDate(1449404331764L);
85 underTest = newQualityGateDetailsFormatter(measureData, snapshot);
87 ProjectStatus result = underTest.format();
90 assertThat(result.getConditionsCount()).isOne();
91 List<ProjectStatusResponse.Condition> conditions = result.getConditionsList();
92 assertThat(conditions).extracting("status").containsExactly(ProjectStatusResponse.Status.ERROR);
93 assertThat(conditions).extracting("metricKey").containsExactly("new_coverage");
94 assertThat(conditions).extracting("comparator").containsExactly(ProjectStatusResponse.Comparator.LT);
95 assertThat(conditions).extracting("periodIndex").containsExactly(1);
96 assertThat(conditions).extracting("errorThreshold").containsOnly("85");
97 assertThat(conditions).extracting("actualValue").containsExactly("82.2985024398452");
101 public void fail_when_measure_level_is_unknown() {
102 String measureData = "{\n" +
103 " \"level\": \"UNKNOWN\",\n" +
104 " \"conditions\": [\n" +
106 " \"metric\": \"new_coverage\",\n" +
107 " \"op\": \"LT\",\n" +
108 " \"period\": 1,\n" +
109 " \"warning\": \"80\",\n" +
110 " \"error\": \"85\",\n" +
111 " \"actual\": \"82.2985024398452\",\n" +
112 " \"level\": \"ERROR\"\n" +
116 underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
118 assertThatThrownBy(() -> underTest.format())
119 .isInstanceOf(IllegalStateException.class)
120 .hasMessageContaining("Unknown quality gate status 'UNKNOWN'");
124 public void fail_when_measure_op_is_unknown() {
125 String measureData = "{\n" +
126 " \"level\": \"ERROR\",\n" +
127 " \"conditions\": [\n" +
129 " \"metric\": \"new_coverage\",\n" +
130 " \"op\": \"UNKNOWN\",\n" +
131 " \"period\": 1,\n" +
132 " \"warning\": \"80\",\n" +
133 " \"error\": \"85\",\n" +
134 " \"actual\": \"82.2985024398452\",\n" +
135 " \"level\": \"ERROR\"\n" +
139 underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
141 assertThatThrownBy(() -> underTest.format())
142 .isInstanceOf(IllegalStateException.class)
143 .hasMessageContaining("Unknown quality gate comparator 'UNKNOWN'");
146 private static QualityGateDetailsFormatter newQualityGateDetailsFormatter(@Nullable String measureData, @Nullable SnapshotDto snapshotDto) {
147 return new QualityGateDetailsFormatter(Optional.ofNullable(measureData), Optional.ofNullable(snapshotDto));