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