aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org/sonar/batch/qualitygate/ConditionUtils.java
blob: 7515efc5b5a87ab78b49c729bd061add8822ba09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.batch.qualitygate;

import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;

class ConditionUtils {

  private ConditionUtils() {
    // only static stuff
  }

  /**
   * Get the matching alert level for the given measure
   */
  static Metric.Level getLevel(ResolvedCondition condition, Measure measure) {
    if (evaluateCondition(condition, measure, Metric.Level.ERROR)) {
      return Metric.Level.ERROR;
    }
    if (evaluateCondition(condition, measure, Metric.Level.WARN)) {
      return Metric.Level.WARN;
    }
    return Metric.Level.OK;
  }

  private static boolean evaluateCondition(ResolvedCondition condition, Measure measure, Metric.Level alertLevel) {
    String valueToEval = getValueToEval(condition, alertLevel);
    if (StringUtils.isEmpty(valueToEval)) {
      return false;
    }

    Comparable criteriaValue = getValueForComparison(condition.metric(), valueToEval);
    Comparable measureValue = getMeasureValue(condition, measure);
    if (measureValue != null) {
      return doesReachThresholds(measureValue, criteriaValue, condition);
    }
    return false;
  }

  private static boolean doesReachThresholds(Comparable measureValue, Comparable criteriaValue, ResolvedCondition condition) {
    int comparison = measureValue.compareTo(criteriaValue);
    return !(isNotEquals(comparison, condition)
        || isGreater(comparison, condition)
        || isSmaller(comparison, condition)
        || isEquals(comparison, condition));
  }

  private static boolean isNotEquals(int comparison, ResolvedCondition condition) {
    return "NE".equals(condition.operator()) && comparison == 0;
  }

  private static boolean isGreater(int comparison, ResolvedCondition condition) {
    return "GT".equals(condition.operator()) && comparison != 1;
  }

  private static boolean isSmaller(int comparison, ResolvedCondition condition) {
    return "LT".equals(condition.operator()) && comparison != -1;
  }

  private static boolean isEquals(int comparison, ResolvedCondition condition) {
    return "EQ".equals(condition.operator()) && comparison != 0;
  }

  private static String getValueToEval(ResolvedCondition condition, Metric.Level alertLevel) {
    if (alertLevel.equals(Metric.Level.ERROR)) {
      return condition.errorThreshold();
    } else if (alertLevel.equals(Metric.Level.WARN)) {
      return condition.warningThreshold();
    } else {
      throw new IllegalStateException(alertLevel.toString());
    }
  }

  private static Comparable getValueForComparison(Metric metric, String value) {
    Comparable valueToCompare = null;
    try {
      if (isADouble(metric)) {
        valueToCompare = Double.parseDouble(value);
      } else if (isAInteger(metric)) {
        valueToCompare = parseInteger(value);
      } else if (isAString(metric)) {
        valueToCompare = value;
      } else if (isABoolean(metric)) {
        valueToCompare = Integer.parseInt(value);
      } else if (isAWorkDuration(metric)) {
        valueToCompare = Long.parseLong(value);
      } else {
        throw new NotImplementedException(metric.getType().toString());
      }
    } catch (NumberFormatException badValueFormat) {
      throw new IllegalArgumentException(String.format("Quality Gate: Unable to parse value '%s' to compare against %s", value, metric.getName()));
    }
    return valueToCompare;
  }

  private static Comparable<Integer> parseInteger(String value) {
    return value.contains(".") ? Integer.parseInt(value.substring(0, value.indexOf('.'))) : Integer.parseInt(value);
  }

  private static Comparable getMeasureValue(ResolvedCondition condition, Measure measure) {
    Metric metric = condition.metric();
    if (isADouble(metric)) {
      return getValue(condition, measure);
    }
    if (isAInteger(metric)) {
      return parseInteger(condition, measure);
    }
    if (isAWorkDuration(metric)) {
      return parseLong(condition, measure);
    }
    if (condition.period() == null) {
      return getMeasureValueForStringOrBoolean(metric, measure);
    }
    throw new NotImplementedException(metric.getType().toString());
  }

  private static Comparable getMeasureValueForStringOrBoolean(Metric metric, Measure measure) {
    if (isAString(metric)) {
      return measure.getData();
    }
    if (isABoolean(metric)) {
      return measure.getValue().intValue();
    }
    throw new NotImplementedException(metric.getType().toString());
  }

  private static Comparable<Integer> parseInteger(ResolvedCondition condition, Measure measure) {
    Double value = getValue(condition, measure);
    return value != null ? value.intValue() : null;
  }

  private static Comparable<Long> parseLong(ResolvedCondition condition, Measure measure) {
    Double value = getValue(condition, measure);
    return value != null ? value.longValue() : null;
  }

  private static boolean isADouble(Metric metric) {
    return metric.getType() == Metric.ValueType.FLOAT ||
        metric.getType() == Metric.ValueType.PERCENT ||
        metric.getType() == Metric.ValueType.RATING;
  }

  private static boolean isAInteger(Metric metric) {
    return metric.getType() == Metric.ValueType.INT ||
        metric.getType() == Metric.ValueType.MILLISEC;
  }

  private static boolean isAString(Metric metric) {
    return metric.getType() == Metric.ValueType.STRING ||
        metric.getType() == Metric.ValueType.LEVEL;
  }

  private static boolean isABoolean(Metric metric) {
    return metric.getType() == Metric.ValueType.BOOL;
  }

  private static boolean isAWorkDuration(Metric metric) {
    return metric.getType() == Metric.ValueType.WORK_DUR;
  }

  private static Double getValue(ResolvedCondition condition, Measure measure) {
    Integer period = condition.period();
    if (period == null) {
      return measure.getValue();
    } else if (period == 1) {
      return measure.getVariation1();
    } else if (period == 2) {
      return measure.getVariation2();
    } else if (period == 3) {
      return measure.getVariation3();
    } else if (period == 4) {
      return measure.getVariation4();
    } else if (period == 5) {
      return measure.getVariation5();
    } else {
      throw new IllegalStateException("Following index period is not allowed : " + Double.toString(period));
    }
  }
}