001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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.utils.Duration;
029
030import static com.google.common.base.Preconditions.checkArgument;
031import static com.google.common.base.Preconditions.checkNotNull;
032
033@Immutable
034public class TestIssue implements Issue {
035
036  private String key;
037  private String status;
038  private String resolution;
039  private String severity;
040  private RuleKey ruleKey;
041  private Duration debt;
042
043  private TestIssue(Builder builder) {
044    this.key = builder.key;
045    this.status = builder.status;
046    this.resolution = builder.resolution;
047    this.severity = builder.severity;
048    this.ruleKey = builder.ruleKey;
049    this.debt = builder.debt;
050  }
051
052  @Override
053  public String key() {
054    return key;
055  }
056
057  @Override
058  public RuleKey ruleKey() {
059    return ruleKey;
060  }
061
062  @Override
063  public String status() {
064    return status;
065  }
066
067  @Override
068  @CheckForNull
069  public String resolution() {
070    return resolution;
071  }
072
073  @Override
074  public String severity() {
075    return severity;
076  }
077
078  @Override
079  @CheckForNull
080  public Duration debt() {
081    return debt;
082  }
083
084  public static class Builder {
085    private String key;
086    private String status;
087    private String resolution;
088    private String severity;
089    private RuleKey ruleKey;
090    private Duration debt;
091
092    public Builder setKey(String key) {
093      this.key = validateKey(key);
094      return this;
095    }
096
097    public Builder setResolution(@Nullable String resolution) {
098      this.resolution = validateResolution(resolution);
099      return this;
100    }
101
102    public Builder setSeverity(String severity) {
103      this.severity = validateSeverity(severity);
104      return this;
105    }
106
107    public Builder setStatus(String status) {
108      this.status = validateStatus(status);
109      return this;
110    }
111
112    public Builder setRuleKey(RuleKey ruleKey) {
113      this.ruleKey = validateRuleKey(ruleKey);
114      return this;
115    }
116
117    public Builder setDebt(@Nullable Duration debt) {
118      this.debt = debt;
119      return this;
120    }
121
122    private static String validateKey(String key){
123      checkNotNull(key, "key cannot be null");
124      return key;
125    }
126
127    private static RuleKey validateRuleKey(RuleKey ruleKey){
128      checkNotNull(ruleKey, "ruleKey cannot be null");
129      return ruleKey;
130    }
131
132    private static String validateResolution(@Nullable String resolution){
133      checkArgument(resolution == null || org.sonar.api.issue.Issue.RESOLUTIONS.contains(resolution), String.format("resolution '%s' is invalid", resolution));
134      return resolution;
135    }
136
137    private static String validateSeverity(String severity){
138      checkNotNull(severity, "severity cannot be null");
139      checkArgument(Severity.ALL.contains(severity), String.format("severity '%s' is invalid", severity));
140      return severity;
141    }
142
143    private static String validateStatus(String status){
144      checkNotNull(status, "status cannot be null");
145      checkArgument(org.sonar.api.issue.Issue.STATUSES.contains(status), String.format("status '%s' is invalid", status));
146      return status;
147    }
148
149    public Issue build(){
150      validateKey(key);
151      validateResolution(resolution);
152      validateSeverity(severity);
153      validateStatus(status);
154      validateRuleKey(ruleKey);
155      return new TestIssue(this);
156    }
157  }
158}