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