]> source.dussan.org Git - sonarqube.git/blob
7df876bca08340309bfc9169295443641b9fb5d8
[sonarqube.git] /
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
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;
31
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
36 public class QualityGateDetailsFormatterTest {
37
38   private QualityGateDetailsFormatter underTest;
39
40   @Test
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);
48
49     ProjectStatus result = underTest.format();
50
51     assertThat(result.getStatus()).isEqualTo(ProjectStatusResponse.Status.ERROR);
52     // check conditions
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");
68
69     // check periods
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()));
76   }
77
78   @Test
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);
86
87     ProjectStatus result = underTest.format();
88
89     // check conditions
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");
98   }
99
100   @Test
101   public void fail_when_measure_level_is_unknown() {
102     String measureData = "{\n" +
103       "  \"level\": \"UNKNOWN\",\n" +
104       "  \"conditions\": [\n" +
105       "    {\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" +
113       "    }\n" +
114       "  ]\n" +
115       "}";
116     underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
117
118     assertThatThrownBy(() -> underTest.format())
119       .isInstanceOf(IllegalStateException.class)
120       .hasMessageContaining("Unknown quality gate status 'UNKNOWN'");
121   }
122
123   @Test
124   public void fail_when_measure_op_is_unknown() {
125     String measureData = "{\n" +
126       "  \"level\": \"ERROR\",\n" +
127       "  \"conditions\": [\n" +
128       "    {\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" +
136       "    }\n" +
137       "  ]\n" +
138       "}";
139     underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
140
141     assertThatThrownBy(() -> underTest.format())
142       .isInstanceOf(IllegalStateException.class)
143       .hasMessageContaining("Unknown quality gate comparator 'UNKNOWN'");
144   }
145
146   private static QualityGateDetailsFormatter newQualityGateDetailsFormatter(@Nullable String measureData, @Nullable SnapshotDto snapshotDto) {
147     return new QualityGateDetailsFormatter(Optional.ofNullable(measureData), Optional.ofNullable(snapshotDto));
148   }
149 }