]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 23 Sep 2014 13:50:23 +0000 (15:50 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 23 Sep 2014 14:09:43 +0000 (16:09 +0200)
17 files changed:
server/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java
server/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueService.java
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
server/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
server/sonar-server/src/main/java/org/sonar/server/search/Index.java
server/sonar-server/src/main/java/org/sonar/server/search/ws/SearchOptions.java
server/sonar-server/src/main/java/org/sonar/server/search/ws/SearchRequestHandler.java
server/sonar-server/src/test/java/org/sonar/server/issue/IssueTesting.java
server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java
sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java
sonar-core/src/main/java/org/sonar/core/issue/db/IssueDto.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/package-info.java [new file with mode: 0644]

index 8a34b02c8528ae74235517b3bc5ea3b9e23dd4cb..38c7599b9288f64c4433e773bad066f1286d27ba 100644 (file)
@@ -62,7 +62,8 @@ public class ActionService implements ServerComponent {
   private final PropertiesDao propertiesDao;
   private final Actions actions;
 
-  public ActionService(DbClient dbClient, IssueService issueService, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao, Actions actions) {
+  public ActionService(DbClient dbClient, IssueService issueService, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao,
+    Actions actions) {
     this.dbClient = dbClient;
     this.issueService = issueService;
     this.issueStorage = issueStorage;
index cb151d9c92fbbb25ba208535390ea67df9186d2f..fd24180b2de2ed1a4ec62f348af76b69066b3a8d 100644 (file)
@@ -362,7 +362,6 @@ public class DefaultIssueService implements IssueService {
   public List<Issue> search(List<String> issueKeys) {
     // Load from index to check permission
     List<Issue> authorizedIndexIssues = search(IssueQuery.builder().issueKeys(issueKeys).build(), new QueryContext().setMaxLimit()).getHits();
-    // return ;
     List<String> authorizedIssueKeys = newArrayList(Iterables.transform(authorizedIndexIssues, new Function<Issue, String>() {
       @Override
       public String apply(@Nullable Issue input) {
index a2f2489fd73d64ae0759839fa99f55128e7737c8..86ff17b4bdb371e6f30858b6b634bc1e9d59f580 100644 (file)
@@ -148,10 +148,6 @@ public class IssueIndex extends BaseIndex<Issue, IssueDto, String> {
       esSearch.setQuery(esQuery);
     }
 
-    // Sample Functional aggregation
-    // esSearch.addAggregation(AggregationBuilders.sum("totalDuration")
-    // .field(IssueNormalizer.IssueField.DEBT.field()));
-
     SearchResponse response = getClient().execute(esSearch);
     return new Result<Issue>(this, response);
   }
index ebeb8da2aa5337c40897a75aa557c3a745935b44..39d83ba922e50d8053e2cf2c12f52add83ab8d65 100644 (file)
@@ -128,7 +128,7 @@ public class IssueShowAction implements RequestHandler {
 
   private void writeIssue(DbSession session, DefaultIssue issue, JsonWriter json) {
     String actionPlanKey = issue.actionPlanKey();
-    ActionPlan actionPlan = actionPlanService.findByKey(actionPlanKey, UserSession.get());
+    ActionPlan actionPlan = actionPlanKey != null ? actionPlanService.findByKey(actionPlanKey, UserSession.get()) : null;
     Duration debt = issue.debt();
     Rule rule = ruleService.getNonNullByKey(issue.ruleKey());
     Date updateDate = issue.updateDate();
@@ -164,7 +164,8 @@ public class IssueShowAction implements RequestHandler {
   private void addComponents(DbSession session, DefaultIssue issue, JsonWriter json) {
     // component, module and project can be null if they were removed
     ComponentDto component = dbClient.componentDao().getNullableByKey(session, issue.componentKey());
-    ComponentDto subProject = component != null ? dbClient.componentDao().getNullableById(component.subProjectId(), session) : null;
+    Long subProjectId = component != null ? component.subProjectId() : null;
+    ComponentDto subProject = subProjectId != null ? dbClient.componentDao().getNullableById(subProjectId, session) : null;
     ComponentDto project = component != null ? dbClient.componentDao().getNullableById(component.projectId(), session) : null;
 
     String projectName = project != null ? project.longName() != null ? project.longName() : project.name() : null;
index c02edc03c637b50ff0ec21b9ef9f88da608e9440..023d6a96affde0527ceef1d2cd0ef1ae7d46187e 100644 (file)
@@ -221,16 +221,20 @@ public class SearchAction extends SearchRequestHandler<IssueQuery, Issue> {
   }
 
   @Override
-  protected void doContextResponse(Request request, QueryContext context, Result<Issue> result, JsonWriter json) {
+  @CheckForNull
+  protected Collection<String> possibleFields() {
+    return null;
+  }
 
+  @Override
+  protected void doContextResponse(Request request, QueryContext context, Result<Issue> result, JsonWriter json) {
     // Insert the projects and component name;
     Set<RuleKey> ruleKeys = new HashSet<RuleKey>();
     Set<String> projectKeys = new HashSet<String>();
     Set<String> componentKeys = new HashSet<String>();
     Set<String> actionPlanKeys = new HashSet<String>();
     List<String> userLogins = new ArrayList<String>();
-    //
-    // DbSession session = dbClient.openSession(false);
+
     for (Issue issue : result.getHits()) {
       ruleKeys.add(issue.ruleKey());
       projectKeys.add(issue.projectKey());
index ef87de0f602d7a57ac1c0d67c028b0330a9beb8e..66f0990d3db0da8a4f703a67ca8b2b3c0ff11c0a 100644 (file)
@@ -59,7 +59,6 @@ public class DefaultRuleFinder implements RuleFinder {
     return null;
   }
 
-  @CheckForNull
   public Collection<org.sonar.api.rules.Rule> findByIds(Collection<Integer> ruleIds) {
     List<org.sonar.api.rules.Rule> rules = newArrayList();
     if (ruleIds.isEmpty()) {
@@ -71,7 +70,6 @@ public class DefaultRuleFinder implements RuleFinder {
     return rules;
   }
 
-  @CheckForNull
   public Collection<org.sonar.api.rules.Rule> findByKeys(Collection<RuleKey> ruleKeys) {
     List<org.sonar.api.rules.Rule> rules = newArrayList();
     if (ruleKeys.isEmpty()) {
index fe5df3dffbb8d229f932b01cd0af218740885428..38dca532a4bfc16f5b1924ac56e0a86128e766d9 100644 (file)
@@ -373,6 +373,7 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
   }
 
   @CheckForNull
+  @Override
   public DOMAIN getNullableByKey(KEY key) {
     GetRequestBuilder request = client.prepareGet()
       .setType(this.getIndexType())
index 13acb6a633292bb2885e77337534bc2ebe7710bf..f839a257a90ec496903e3cf02c67d33a2827cc22 100644 (file)
@@ -32,7 +32,7 @@ import java.util.Iterator;
 public interface Index<DOMAIN, DTO extends Dto<KEY>, KEY extends Serializable> extends Startable, ServerComponent {
 
   @CheckForNull
-  DOMAIN getByKey(KEY item);
+  DOMAIN getNullableByKey(KEY key);
 
   String getIndexType();
 
index 4b8e25f137d9c29595601980add82f2d4ddad7db..858116c2504b47a62ca639149294b5a51a835fb6 100644 (file)
@@ -34,6 +34,8 @@ import java.util.List;
 
 /**
  * Generic options for search web services
+ *
+ * TODO {@link org.sonar.server.search.ws.SearchRequestHandler} should be used instead
  */
 public class SearchOptions {
 
index e4104142fd035b3a95a0fce67b51e426c5d1070a..7ede3e3a093553e92a81d52f43b6402c1bc2db99 100644 (file)
@@ -31,12 +31,11 @@ import org.sonar.server.search.Result;
 import javax.annotation.CheckForNull;
 
 import java.util.Collection;
-import java.util.List;
+import java.util.Iterator;
 import java.util.Map;
 
 public abstract class SearchRequestHandler<QUERY, DOMAIN> implements RequestHandler {
 
-  public static final String PARAM_TEXT_QUERY = "q";
   public static final String PARAM_PAGE = "p";
   public static final String PARAM_PAGE_SIZE = "ps";
   public static final String PARAM_FIELDS = "f";
@@ -45,19 +44,8 @@ public abstract class SearchRequestHandler<QUERY, DOMAIN> implements RequestHand
 
   public static final String PARAM_FACETS = "facets";
 
-  private List<String> fields;
-
   private final String actionName;
 
-  /**
-   * The fields to be returned in JSON response. <code>null</code> means that
-   * all the fields must be returned.
-   */
-  @CheckForNull
-  public List<String> fields() {
-    return fields;
-  }
-
   protected SearchRequestHandler(String actionName) {
     this.actionName = actionName;
   }
@@ -72,6 +60,9 @@ public abstract class SearchRequestHandler<QUERY, DOMAIN> implements RequestHand
 
   protected abstract void doDefinition(WebService.NewAction action);
 
+  @CheckForNull
+  protected abstract Collection<String> possibleFields();
+
   public final void define(WebService.NewController controller) {
     WebService.NewAction action = controller.createAction(this.actionName)
       .setHandler(this);
@@ -95,15 +86,14 @@ public abstract class SearchRequestHandler<QUERY, DOMAIN> implements RequestHand
       .setBooleanPossibleValues()
       .setDefaultValue("false");
 
-    WebService.NewParam newParam = action
-      .createParam(PARAM_FIELDS)
-      .setDescription("Comma-separated list of the fields to be returned in response. All the fields are returned by default.");
-
-    // .setPossibleValues(possibleFields);
-    // if (possibleFields != null && possibleFields.size() > 1) {
-    // Iterator<String> it = possibleFields.iterator();
-    // newParam.setExampleValue(String.format("%s,%s", it.next(), it.next()));
-    // }
+    Collection<String> possibleFields = possibleFields();
+    WebService.NewParam paramFields = action.createParam(PARAM_FIELDS)
+      .setDescription("Comma-separated list of the fields to be returned in response. All the fields are returned by default.")
+      .setPossibleValues(possibleFields);
+    if (possibleFields != null && possibleFields.size() > 1) {
+      Iterator<String> it = possibleFields.iterator();
+      paramFields.setExampleValue(String.format("%s,%s", it.next(), it.next()));
+    }
 
     this.doDefinition(action);
   }
index ff23bcdd7d4ac66fadaf042616db7fc9545d2f60..0081b1fb7a5d03b922f1619f6cc246a1127fc362 100644 (file)
@@ -23,6 +23,7 @@ import org.sonar.api.issue.Issue;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.KeyValueFormat;
 import org.sonar.core.component.ComponentDto;
 import org.sonar.core.issue.db.IssueDto;
 import org.sonar.core.rule.RuleDto;
@@ -56,10 +57,10 @@ public class IssueTesting {
   }
 
   public static void assertIsEquivalent(IssueDto dto, Issue issue) {
-
     assertThat(issue).isNotNull();
     assertThat(dto).isNotNull();
 
+    assertThat(issue.key()).isEqualTo(dto.getKey());
     assertThat(issue.actionPlanKey()).isEqualTo(dto.getActionPlanKey());
     assertThat(issue.assignee()).isEqualTo(dto.getAssignee());
     assertThat(issue.authorLogin()).isEqualTo(dto.getAuthorLogin());
@@ -71,10 +72,14 @@ public class IssueTesting {
     assertThat(issue.line()).isEqualTo(dto.getLine());
     assertThat(issue.message()).isEqualTo(dto.getMessage());
     assertThat(issue.reporter()).isEqualTo(dto.getReporter());
-    assertThat(issue.key()).isEqualTo(dto.getKey());
-
-    // assertThat(issue.updateDate()).isEqualTo(dto.getIssueUpdateDate());
+    assertThat(issue.language()).isEqualTo(dto.getLanguage());
     assertThat(issue.status()).isEqualTo(dto.getStatus());
     assertThat(issue.severity()).isEqualTo(dto.getSeverity());
+
+    assertThat(issue.attributes()).isEqualTo(KeyValueFormat.parse(dto.getIssueAttributes()));
+
+    assertThat(issue.creationDate()).isEqualTo(dto.getIssueCreationDate());
+    assertThat(issue.updateDate()).isEqualTo(dto.getIssueUpdateDate());
+    assertThat(issue.closeDate()).isEqualTo(dto.getIssueCloseDate());
   }
 }
index 6b9d6a3ef79d755d1e9a39c598b19b063c9b91f0..a0229bee41346f909e592c358d0b9ba4563b62ca 100644 (file)
@@ -40,6 +40,7 @@ import org.sonar.core.user.UserDto;
 import org.sonar.server.component.db.ComponentDao;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.issue.IssueTesting;
 import org.sonar.server.issue.db.IssueDao;
 import org.sonar.server.rule.RuleTesting;
 import org.sonar.server.rule.db.RuleDao;
@@ -112,7 +113,18 @@ public class IssueIndexMediumTest {
     db.issueDao().insert(session, issue);
     session.commit();
 
-    assertThat(index.getByKey(issue.getKey())).isNotNull();
+    Issue result = index.getByKey(issue.getKey());
+    IssueTesting.assertIsEquivalent(issue, result);
+  }
+
+  @Test(expected = IllegalStateException.class)
+  public void is_new_field_is_not_available() throws Exception {
+    IssueDto issue = createIssue();
+    db.issueDao().insert(session, issue);
+    session.commit();
+
+    Issue result = index.getByKey(issue.getKey());
+    result.isNew();
   }
 
   @Test(expected = NotFoundException.class)
index 0edb74d1b0a39f08711eb29abbb2a630ae99bb2f..85628de373e449e5cb1e1ffb9fff25bac1facbfa 100644 (file)
@@ -247,7 +247,7 @@ public class IssueUpdater implements BatchComponent, ServerComponent {
     return false;
   }
 
-  public boolean setProject(DefaultIssue issue, @Nullable String projectKey, IssueChangeContext context) {
+  public boolean setProject(DefaultIssue issue, String projectKey, IssueChangeContext context) {
     if (!Objects.equal(projectKey, issue.projectKey())) {
       issue.setProjectKey(projectKey);
       issue.setUpdateDate(context.date());
index 15092940d6829f8fbe2368de1b6bfa01e205117f..d884f5f9fb3511263fa3082bb22a9fee4389876a 100644 (file)
@@ -424,6 +424,7 @@ public final class IssueDto extends Dto<String> implements Serializable {
   }
 
   public static IssueDto toDtoForInsert(DefaultIssue issue, Long componentId, Long rootComponentId, Integer ruleId, Date now) {
+    String projectKey = issue.projectKey();
     return new IssueDto()
       .setKee(issue.key())
       .setLine(issue.line())
index a6c528dc2a6c8b8d52030c721b4f04e8a427909f..14aabc2b3f90824cb64543a152f3a18b53a80226 100644 (file)
@@ -134,15 +134,11 @@ public class DefaultIssue implements Issue {
     return this;
   }
 
-  /**
-   * The project key is not always populated, that's why it's not present in the Issue API
-   */
-  @CheckForNull
   public String projectKey() {
     return projectKey;
   }
 
-  public DefaultIssue setProjectKey(@Nullable String projectKey) {
+  public DefaultIssue setProjectKey(String projectKey) {
     this.projectKey = projectKey;
     return this;
   }
index 26567597a97e2bcabcbf4aabef78f94d27dc9de4..5836dd9914c8908972992e01d368cd88f979665c 100644 (file)
@@ -62,14 +62,6 @@ public abstract class ValidatingRequest extends Request {
     return value;
   }
 
-//  @CheckForNull
-//  private WebService.Param getDefinition(String key){
-//    WebService.Param definition = action.param(key);
-//    if (definition == null) {
-//      return action.
-//    }
-//  }
-
   @CheckForNull
   @Override
   public List<String> paramAsStrings(String key) {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/package-info.java
new file mode 100644 (file)
index 0000000..43b80ca
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.api.server.ws.internal;
+
+import javax.annotation.ParametersAreNonnullByDefault;