3 * Copyright (C) 2009-2017 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 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.ProjectStatusResponse;
32 import org.sonarqube.ws.Qualitygates.ProjectStatusResponse.ProjectStatus;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.api.utils.DateUtils.formatDateTime;
37 public class QualityGateDetailsFormatterTest {
39 public ExpectedException expectedException = ExpectedException.none();
41 private QualityGateDetailsFormatter underTest;
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);
52 ProjectStatus result = underTest.format();
54 assertThat(result.getStatus()).isEqualTo(ProjectStatusResponse.Status.ERROR);
56 assertThat(result.getConditionsCount()).isEqualTo(4);
57 List<ProjectStatusResponse.Condition> conditions = result.getConditionsList();
58 assertThat(conditions).extracting("status").containsExactly(
59 ProjectStatusResponse.Status.ERROR,
60 ProjectStatusResponse.Status.WARN,
61 ProjectStatusResponse.Status.OK,
62 ProjectStatusResponse.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 ProjectStatusResponse.Comparator.LT,
66 ProjectStatusResponse.Comparator.GT,
67 ProjectStatusResponse.Comparator.NE,
68 ProjectStatusResponse.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");
75 assertThat(result.getPeriodsCount()).isEqualTo(1);
76 List<ProjectStatusResponse.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()));
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);
92 ProjectStatus result = underTest.format();
95 assertThat(result.getConditionsCount()).isEqualTo(1);
96 List<ProjectStatusResponse.Condition> conditions = result.getConditionsList();
97 assertThat(conditions).extracting("status").containsExactly(ProjectStatusResponse.Status.ERROR);
98 assertThat(conditions).extracting("metricKey").containsExactly("new_coverage");
99 assertThat(conditions).extracting("comparator").containsExactly(ProjectStatusResponse.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");
107 public void fail_when_measure_level_is_unknown() {
108 String measureData = "{\n" +
109 " \"level\": \"UNKNOWN\",\n" +
110 " \"conditions\": [\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" +
122 underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
123 expectedException.expect(IllegalStateException.class);
124 expectedException.expectMessage("Unknown quality gate status 'UNKNOWN'");
130 public void fail_when_measure_op_is_unknown() {
131 String measureData = "{\n" +
132 " \"level\": \"ERROR\",\n" +
133 " \"conditions\": [\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" +
145 underTest = newQualityGateDetailsFormatter(measureData, new SnapshotDto());
146 expectedException.expect(IllegalStateException.class);
147 expectedException.expectMessage("Unknown quality gate comparator 'UNKNOWN'");
152 private static QualityGateDetailsFormatter newQualityGateDetailsFormatter(@Nullable String measureData, @Nullable SnapshotDto snapshotDto) {
153 return new QualityGateDetailsFormatter(Optional.fromNullable(measureData), Optional.fromNullable(snapshotDto));