001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 */
020
021package org.sonar.api.ce.measure.test;
022
023import javax.annotation.CheckForNull;
024import javax.annotation.Nullable;
025import javax.annotation.concurrent.Immutable;
026import org.sonar.api.ce.measure.Issue;
027import org.sonar.api.rule.RuleKey;
028import org.sonar.api.rule.Severity;
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 debt;
043
044  private TestIssue(Builder builder) {
045    this.key = builder.key;
046    this.status = builder.status;
047    this.resolution = builder.resolution;
048    this.severity = builder.severity;
049    this.ruleKey = builder.ruleKey;
050    this.debt = builder.debt;
051  }
052
053  @Override
054  public String key() {
055    return key;
056  }
057
058  @Override
059  public RuleKey ruleKey() {
060    return ruleKey;
061  }
062
063  @Override
064  public String status() {
065    return status;
066  }
067
068  @Override
069  @CheckForNull
070  public String resolution() {
071    return resolution;
072  }
073
074  @Override
075  public String severity() {
076    return severity;
077  }
078
079  @Override
080  @CheckForNull
081  public Duration debt() {
082    return debt;
083  }
084
085  public static class Builder {
086    private String key;
087    private String status;
088    private String resolution;
089    private String severity;
090    private RuleKey ruleKey;
091    private Duration debt;
092
093    public Builder setKey(String key) {
094      this.key = validateKey(key);
095      return this;
096    }
097
098    public Builder setResolution(@Nullable String resolution) {
099      this.resolution = validateResolution(resolution);
100      return this;
101    }
102
103    public Builder setSeverity(String severity) {
104      this.severity = validateSeverity(severity);
105      return this;
106    }
107
108    public Builder setStatus(String status) {
109      this.status = validateStatus(status);
110      return this;
111    }
112
113    public Builder setRuleKey(RuleKey ruleKey) {
114      this.ruleKey = validateRuleKey(ruleKey);
115      return this;
116    }
117
118    public Builder setDebt(@Nullable Duration debt) {
119      this.debt = debt;
120      return this;
121    }
122
123    private static String validateKey(String key){
124      checkNotNull(key, "key cannot be null");
125      return key;
126    }
127
128    private static RuleKey validateRuleKey(RuleKey ruleKey){
129      checkNotNull(ruleKey, "ruleKey cannot be null");
130      return ruleKey;
131    }
132
133    private static String validateResolution(@Nullable String resolution){
134      checkArgument(resolution == null || org.sonar.api.issue.Issue.RESOLUTIONS.contains(resolution), String.format("resolution '%s' is invalid", resolution));
135      return resolution;
136    }
137
138    private static String validateSeverity(String severity){
139      checkNotNull(severity, "severity cannot be null");
140      checkArgument(Severity.ALL.contains(severity), String.format("severity '%s' is invalid", severity));
141      return severity;
142    }
143
144    private static String validateStatus(String status){
145      checkNotNull(status, "status cannot be null");
146      checkArgument(org.sonar.api.issue.Issue.STATUSES.contains(status), String.format("status '%s' is invalid", status));
147      return status;
148    }
149
150    public Issue build(){
151      validateKey(key);
152      validateResolution(resolution);
153      validateSeverity(severity);
154      validateStatus(status);
155      validateRuleKey(ruleKey);
156      return new TestIssue(this);
157    }
158  }
159}