]> source.dussan.org Git - sonarqube.git/blob
46a97eb845ec53bc0fa6e9d7a476f509835aeb8b
[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.computation.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.server.computation.task.projectanalysis.measure.Measure;
26 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
27 import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
28 import org.sonar.server.computation.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.<EvaluatedCondition>emptyList());
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);
40   }
41
42   @Test
43   public void verify_json_when_there_is_no_condition() {
44     String actualJson = new QualityGateDetailsData(Measure.Level.OK, Collections.<EvaluatedCondition>emptyList()).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", true);
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).toJson();
61
62     JsonAssert.assertJson(actualJson).isSimilarTo("{" +
63       "\"level\":\"OK\"," +
64       "\"conditions\":[" +
65       "  {" +
66       "    \"metric\":\"key1\"," +
67       "    \"op\":\"GT\"," +
68       "    \"period\":1," +
69       "    \"warning\":\"warnTh\"," +
70       "    \"error\":\"errorTh\"," +
71       "    \"actual\":\"actualValue\"," +
72       "    \"level\":\"OK\"" +
73       "  }," +
74       "  {" +
75       "    \"metric\":\"key1\"," +
76       "    \"op\":\"GT\"," +
77       "    \"period\":1," +
78       "    \"warning\":\"warnTh\"," +
79       "    \"error\":\"errorTh\"," +
80       "    \"actual\":\"actualValue\"," +
81       "    \"level\":\"WARN\"" +
82       "  }," +
83       "  {" +
84       "    \"metric\":\"key1\"," +
85       "    \"op\":\"GT\"," +
86       "    \"period\":1," +
87       "    \"warning\":\"warnTh\"," +
88       "    \"error\":\"errorTh\"," +
89       "    \"actual\":\"actualValue\"," +
90       "    \"level\":\"ERROR\"" +
91       "  }" +
92       "]" +
93       "}");
94   }
95 }