001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.ce.measure.test;
021
022import javax.annotation.CheckForNull;
023import javax.annotation.Nullable;
024import javax.annotation.concurrent.Immutable;
025import org.sonar.api.ce.measure.Issue;
026import org.sonar.api.rule.RuleKey;
027import org.sonar.api.rule.Severity;
028import org.sonar.api.rules.RuleType;
029import org.sonar.api.utils.Duration;
030
031import static com.google.common.base.Preconditions.checkArgument;
032import static com.google.common.base.Preconditions.checkNotNull;
033
034@Immutable
035public class TestIssue implements Issue {
036
037  private String key;
038  private String status;
039  private String resolution;
040  private String severity;
041  private RuleKey ruleKey;
042  private Duration effort;
043  private RuleType type;
044
045  private TestIssue(Builder builder) {
046    this.key = builder.key;
047    this.status = builder.status;
048    this.resolution = builder.resolution;
049    this.severity = builder.severity;
050    this.ruleKey = builder.ruleKey;
051    this.effort = builder.effort;
052    this.type = builder.type;
053  }
054
055  @Override
056  public String key() {
057    return key;
058  }
059
060  @Override
061  public RuleKey ruleKey() {
062    return ruleKey;
063  }
064
065  @Override
066  public String status() {
067    return status;
068  }
069
070  @Override
071  @CheckForNull
072  public String resolution() {
073    return resolution;
074  }
075
076  @Override
077  public String severity() {
078    return severity;
079  }
080
081  /**
082   * @deprecated since 5.5, replaced by {@link #effort}
083   */
084  @Override
085  @CheckForNull
086  @Deprecated
087  public Duration debt() {
088    return effort();
089  }
090
091  /**
092   * @since 5.5
093   */
094  @Override
095  public Duration effort() {
096    return effort;
097  }
098
099  /**
100   * @since 5.5
101   */
102  @Override
103  public RuleType type() {
104    return type;
105  }
106
107  public static class Builder {
108    private String key;
109    private String status;
110    private String resolution;
111    private String severity;
112    private RuleKey ruleKey;
113    private Duration effort;
114    private RuleType type;
115
116    public Builder setKey(String key) {
117      this.key = validateKey(key);
118      return this;
119    }
120
121    public Builder setResolution(@Nullable String resolution) {
122      this.resolution = validateResolution(resolution);
123      return this;
124    }
125
126    public Builder setSeverity(String severity) {
127      this.severity = validateSeverity(severity);
128      return this;
129    }
130
131    public Builder setStatus(String status) {
132      this.status = validateStatus(status);
133      return this;
134    }
135
136    public Builder setRuleKey(RuleKey ruleKey) {
137      this.ruleKey = validateRuleKey(ruleKey);
138      return this;
139    }
140
141    /**
142     * @deprecated since 5.5, use {@link #setEffort(Duration)} instead
143     */
144    @Deprecated
145    public Builder setDebt(@Nullable Duration debt) {
146      return setEffort(debt);
147    }
148
149    /**
150     * @since 5.5
151     */
152    public Builder setEffort(@Nullable Duration effort) {
153      this.effort = effort;
154      return this;
155    }
156
157    /**
158     * @since 5.5
159     */
160    public Builder setType(RuleType type) {
161      this.type = validateType(type);
162      return this;
163    }
164
165    private static String validateKey(String key) {
166      checkNotNull(key, "key cannot be null");
167      return key;
168    }
169
170    private static RuleKey validateRuleKey(RuleKey ruleKey) {
171      checkNotNull(ruleKey, "ruleKey cannot be null");
172      return ruleKey;
173    }
174
175    private static String validateResolution(@Nullable String resolution) {
176      checkArgument(resolution == null || org.sonar.api.issue.Issue.RESOLUTIONS.contains(resolution), String.format("resolution '%s' is invalid", resolution));
177      return resolution;
178    }
179
180    private static String validateSeverity(String severity) {
181      checkNotNull(severity, "severity cannot be null");
182      checkArgument(Severity.ALL.contains(severity), String.format("severity '%s' is invalid", severity));
183      return severity;
184    }
185
186    private static String validateStatus(String status) {
187      checkNotNull(status, "status cannot be null");
188      checkArgument(org.sonar.api.issue.Issue.STATUSES.contains(status), String.format("status '%s' is invalid", status));
189      return status;
190    }
191
192    private static RuleType validateType(RuleType type) {
193      checkNotNull(type, "type cannot be null");
194      return type;
195    }
196
197    public Issue build() {
198      validateKey(key);
199      validateResolution(resolution);
200      validateSeverity(severity);
201      validateStatus(status);
202      validateRuleKey(ruleKey);
203      validateType(type);
204      return new TestIssue(this);
205    }
206  }
207}