]> source.dussan.org Git - sonarqube.git/blob
86c431bdacec3ac31258b6a53764f6e756854b59
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.ce.task.projectanalysis.measure.qualitygatedetails;
21
22 import com.google.common.collect.ImmutableList;
23 import java.util.Collections;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.measure.Measure;
26 import org.sonar.ce.task.projectanalysis.metric.Metric;
27 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
28 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
29 import org.sonar.test.JsonAssert;
30
31 public class QualityGateDetailsDataTest {
32   @Test(expected = NullPointerException.class)
33   public void constructor_throws_NPE_if_Level_arg_is_null() {
34     new QualityGateDetailsData(null, Collections.emptyList(), false);
35   }
36
37   @Test(expected = NullPointerException.class)
38   public void constructor_throws_NPE_if_Iterable_arg_is_null() {
39     new QualityGateDetailsData(Measure.Level.OK, null, false);
40   }
41
42   @Test
43   public void verify_json_when_there_is_no_condition() {
44     String actualJson = new QualityGateDetailsData(Measure.Level.OK, Collections.emptyList(), false).toJson();
45
46     JsonAssert.assertJson(actualJson).isSimilarTo("{" +
47       "\"level\":\"OK\"," +
48       "\"conditions\":[]" +
49       "}");
50   }
51
52   @Test
53   public void verify_json_for_each_type_of_condition() {
54     String value = "actualValue";
55     Condition condition = new Condition(new MetricImpl(1, "key1", "name1", Metric.MetricType.STRING), Condition.Operator.GREATER_THAN.getDbValue(), "errorTh", "warnTh");
56     ImmutableList<EvaluatedCondition> evaluatedConditions = ImmutableList.of(
57       new EvaluatedCondition(condition, Measure.Level.OK, value),
58       new EvaluatedCondition(condition, Measure.Level.WARN, value),
59       new EvaluatedCondition(condition, Measure.Level.ERROR, value));
60     String actualJson = new QualityGateDetailsData(Measure.Level.OK, evaluatedConditions, false).toJson();
61
62     JsonAssert.assertJson(actualJson).isSimilarTo("{" +
63       "\"level\":\"OK\"," +
64       "\"conditions\":[" +
65       "  {" +
66       "    \"metric\":\"key1\"," +
67       "    \"op\":\"GT\"," +
68       "    \"warning\":\"warnTh\"," +
69       "    \"error\":\"errorTh\"," +
70       "    \"actual\":\"actualValue\"," +
71       "    \"level\":\"OK\"" +
72       "  }," +
73       "  {" +
74       "    \"metric\":\"key1\"," +
75       "    \"op\":\"GT\"," +
76       "    \"warning\":\"warnTh\"," +
77       "    \"error\":\"errorTh\"," +
78       "    \"actual\":\"actualValue\"," +
79       "    \"level\":\"WARN\"" +
80       "  }," +
81       "  {" +
82       "    \"metric\":\"key1\"," +
83       "    \"op\":\"GT\"," +
84       "    \"warning\":\"warnTh\"," +
85       "    \"error\":\"errorTh\"," +
86       "    \"actual\":\"actualValue\"," +
87       "    \"level\":\"ERROR\"" +
88       "  }" +
89       "]" +
90       "}");
91   }
92
93   @Test
94   public void verify_json_for_small_leak() {
95     String actualJson = new QualityGateDetailsData(Measure.Level.OK, Collections.emptyList(), false).toJson();
96     JsonAssert.assertJson(actualJson).isSimilarTo("{\"ignoredConditions\": false}");
97
98     String actualJson2 = new QualityGateDetailsData(Measure.Level.OK, Collections.emptyList(), true).toJson();
99     JsonAssert.assertJson(actualJson2).isSimilarTo("{\"ignoredConditions\": true}");
100   }
101
102 }