]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8951 fix some quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 30 Mar 2017 20:05:14 +0000 (22:05 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 31 Mar 2017 09:43:25 +0000 (11:43 +0200)
server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/ShowAction.java

index d5c88312602bf8171dad3782daca410fc8eb7e19..77f329e5f8fb5144f261ce57f646eb4d478d9124 100644 (file)
  */
 package org.sonar.server.rule.ws;
 
-import com.google.common.base.Function;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.rule.RuleKey;
@@ -38,6 +36,7 @@ import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.util.stream.Collectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.ActiveRuleDto;
@@ -55,6 +54,7 @@ import static com.google.common.base.Strings.nullToEmpty;
 import static com.google.common.collect.FluentIterable.from;
 import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Collections.singletonList;
+import static org.sonar.core.util.Protobuf.setNullable;
 
 /**
  * Add details about active rules to api/rules/search and api/rules/show
@@ -79,14 +79,14 @@ public class ActiveRuleCompleter {
   }
 
   private Collection<String> writeActiveRules(DbSession dbSession, SearchResponse.Builder response, RuleQuery query, List<RuleDto> rules) {
-    Collection<String> qProfileKeys = newHashSet();
+    Collection<String> qProfileKeys = new HashSet<>();
     Rules.Actives.Builder activesBuilder = response.getActivesBuilder();
 
     String profileKey = query.getQProfileKey();
     if (profileKey != null) {
       // Load details of active rules on the selected profile
       List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profileKey);
-      Map<RuleKey, ActiveRuleDto> activeRuleByRuleKey = from(activeRuleDtos).uniqueIndex(ActiveRuleToRuleKey.INSTANCE);
+      Map<RuleKey, ActiveRuleDto> activeRuleByRuleKey = activeRuleDtos.stream().collect(Collectors.uniqueIndex(d -> d.getKey().ruleKey()));
       ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
 
       for (RuleDto rule : rules) {
@@ -98,7 +98,7 @@ public class ActiveRuleCompleter {
     } else {
       // Load details of all active rules
       List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleIds(dbSession, Lists.transform(rules, RuleDto::getId));
-      Multimap<RuleKey, ActiveRuleDto> activeRulesByRuleKey = from(activeRuleDtos).index(ActiveRuleToRuleKey.INSTANCE);
+      Multimap<RuleKey, ActiveRuleDto> activeRulesByRuleKey = from(activeRuleDtos).index(d -> d.getKey().ruleKey());
       ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
       for (RuleDto rule : rules) {
         qProfileKeys = writeActiveRules(rule.getKey(), activeRulesByRuleKey.get(rule.getKey()), activeRuleParamsByActiveRuleKey, activesBuilder);
@@ -175,7 +175,7 @@ public class ActiveRuleCompleter {
   }
 
   private Rules.QProfiles.Builder buildQProfiles(DbSession dbSession, Collection<String> harvestedProfileKeys) {
-    Map<String, QualityProfileDto> qProfilesByKey = Maps.newHashMap();
+    Map<String, QualityProfileDto> qProfilesByKey = new HashMap<>();
     for (String qProfileKey : harvestedProfileKeys) {
       if (!qProfilesByKey.containsKey(qProfileKey)) {
         QualityProfileDto profile = loadProfile(dbSession, qProfileKey);
@@ -201,34 +201,22 @@ public class ActiveRuleCompleter {
   }
 
   @CheckForNull
-  QualityProfileDto loadProfile(DbSession dbSession, String qProfileKey) {
+  private QualityProfileDto loadProfile(DbSession dbSession, String qProfileKey) {
     return dbClient.qualityProfileDao().selectByKey(dbSession, qProfileKey);
   }
 
   private void writeProfile(Map<String, Rules.QProfile> profilesResponse, QualityProfileDto profile) {
     Rules.QProfile.Builder profileResponse = Rules.QProfile.newBuilder();
-    if (profile.getName() != null) {
-      profileResponse.setName(profile.getName());
-    }
+    setNullable(profile.getName(), profileResponse::setName);
+
     if (profile.getLanguage() != null) {
       profileResponse.setLang(profile.getLanguage());
       Language language = languages.get(profile.getLanguage());
       String langName = language == null ? profile.getLanguage() : language.getName();
       profileResponse.setLangName(langName);
     }
-    if (profile.getParentKee() != null) {
-      profileResponse.setParent(profile.getParentKee());
-    }
+    setNullable(profile.getParentKee(), profileResponse::setParent);
 
     profilesResponse.put(profile.getKey(), profileResponse.build());
   }
-
-  private enum ActiveRuleToRuleKey implements Function<ActiveRuleDto, RuleKey> {
-    INSTANCE;
-
-    @Override
-    public RuleKey apply(@Nonnull ActiveRuleDto input) {
-      return input.getKey().ruleKey();
-    }
-  }
 }
index 222d0c36fd088677d73ec4f7a56888c9a680e527..95be46d533c20f98073a082df3c12427603a12c0 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.server.rule.ws;
 
-import com.google.common.base.Function;
-import com.google.common.base.Predicates;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ListMultimap;
@@ -29,15 +27,14 @@ import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.google.common.io.Resources;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
@@ -46,6 +43,7 @@ import org.sonar.api.rules.RuleType;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.core.util.stream.Collectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.rule.RuleDefinitionDto;
@@ -63,7 +61,7 @@ import org.sonarqube.ws.Common;
 import org.sonarqube.ws.Rules.SearchResponse;
 import org.sonarqube.ws.client.rule.SearchWsRequest;
 
-import static com.google.common.collect.FluentIterable.from;
+import static java.lang.String.format;
 import static org.sonar.api.server.ws.WebService.Param.ASCENDING;
 import static org.sonar.api.server.ws.WebService.Param.FACETS;
 import static org.sonar.api.server.ws.WebService.Param.FIELDS;
@@ -98,13 +96,19 @@ import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TAGS;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TEMPLATE_KEY;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TYPES;
 
-/**
- * @since 4.4
- */
 public class SearchAction implements RulesWsAction {
   public static final String ACTION = "search";
 
   private static final Collection<String> DEFAULT_FACETS = ImmutableSet.of(PARAM_LANGUAGES, PARAM_REPOSITORIES, "tags");
+  private static final String[] POSSIBLE_FACETS = new String[] {
+    FACET_LANGUAGES,
+    FACET_REPOSITORIES,
+    FACET_TAGS,
+    FACET_SEVERITIES,
+    FACET_ACTIVE_SEVERITIES,
+    FACET_STATUSES,
+    FACET_TYPES,
+    FACET_OLD_DEFAULT};
 
   private final RuleQueryFactory ruleQueryFactory;
   private final DbClient dbClient;
@@ -129,14 +133,10 @@ public class SearchAction implements RulesWsAction {
       .addPagingParams(100, MAX_LIMIT)
       .setHandler(this);
 
-    Collection<String> possibleFacets = possibleFacets();
-    WebService.NewParam paramFacets = action.createParam(FACETS)
+    action.createParam(FACETS)
       .setDescription("Comma-separated list of the facets to be computed. No facet is computed by default.")
-      .setPossibleValues(possibleFacets);
-    if (possibleFacets != null && possibleFacets.size() > 1) {
-      Iterator<String> it = possibleFacets.iterator();
-      paramFacets.setExampleValue(String.format("%s,%s", it.next(), it.next()));
-    }
+      .setPossibleValues(POSSIBLE_FACETS)
+      .setExampleValue(format("%s,%s", POSSIBLE_FACETS[0], POSSIBLE_FACETS[1]));
 
     WebService.NewParam paramFields = action.createParam(FIELDS)
       .setDescription("Comma-separated list of the fields to be returned in response. All the fields are returned by default, except actives." +
@@ -149,7 +149,7 @@ public class SearchAction implements RulesWsAction {
         "</ul>")
       .setPossibleValues(OPTIONAL_FIELDS);
     Iterator<String> it = OPTIONAL_FIELDS.iterator();
-    paramFields.setExampleValue(String.format("%s,%s", it.next(), it.next()));
+    paramFields.setExampleValue(format("%s,%s", it.next(), it.next()));
 
     doDefinition(action);
   }
@@ -176,13 +176,13 @@ public class SearchAction implements RulesWsAction {
     return responseBuilder.build();
   }
 
-  protected void writeStatistics(SearchResponse.Builder response, SearchResult searchResult, SearchOptions context) {
+  private static void writeStatistics(SearchResponse.Builder response, SearchResult searchResult, SearchOptions context) {
     response.setTotal(searchResult.total);
     response.setP(context.getPage());
     response.setPs(context.getLimit());
   }
 
-  protected void doDefinition(WebService.NewAction action) {
+  private void doDefinition(WebService.NewAction action) {
     action.setDescription("Search for a collection of relevant rules matching a specified query.<br/>" +
       "Since 5.5, following fields in the response have been deprecated :" +
       "<ul>" +
@@ -201,22 +201,6 @@ public class SearchAction implements RulesWsAction {
     defineRuleSearchParameters(action);
   }
 
-  @CheckForNull
-  protected Collection<String> possibleFacets() {
-    return Arrays.asList(
-      FACET_LANGUAGES,
-      FACET_REPOSITORIES,
-      FACET_TAGS,
-      FACET_SEVERITIES,
-      FACET_ACTIVE_SEVERITIES,
-      FACET_STATUSES,
-      FACET_TYPES,
-      FACET_OLD_DEFAULT);
-  }
-
-  /**
-   * public visibility because used by {@link org.sonar.server.qualityprofile.ws.BulkRuleActivationActions}
-   */
   public static void defineRuleSearchParameters(WebService.NewAction action) {
     action
       .createParam(TEXT_QUERY)
@@ -324,7 +308,7 @@ public class SearchAction implements RulesWsAction {
     }
   }
 
-  protected SearchOptions buildSearchOptions(SearchWsRequest request) {
+  private static SearchOptions buildSearchOptions(SearchWsRequest request) {
     SearchOptions context = loadCommonContext(request);
     SearchOptions searchOptions = new SearchOptions()
       .setLimit(context.getLimit())
@@ -351,7 +335,7 @@ public class SearchAction implements RulesWsAction {
     return context;
   }
 
-  protected SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
+  private SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
     SearchIdResult<RuleKey> result = ruleIndex.search(query, context);
     List<RuleKey> ruleKeys = result.getIds();
     // rule order is managed by ES
@@ -364,11 +348,11 @@ public class SearchAction implements RulesWsAction {
         rules.add(rule);
       }
     }
-    List<Integer> ruleIds = from(rules).transform(RuleDtoToId.INSTANCE).toList();
-    List<Integer> templateRuleIds = from(rules)
-      .transform(RuleDtoToTemplateId.INSTANCE)
-      .filter(Predicates.notNull())
-      .toList();
+    List<Integer> ruleIds = rules.stream().map(RuleDto::getId).collect(Collectors.toList());
+    List<Integer> templateRuleIds = rules.stream()
+      .map(RuleDto::getTemplateId)
+      .filter(Objects::nonNull)
+      .collect(Collectors.toList());
     List<RuleDefinitionDto> templateRules = dbClient.ruleDao().selectDefinitionByIds(dbSession, templateRuleIds);
     List<RuleParamDto> ruleParamDtos = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, ruleIds);
     return new SearchResult()
@@ -379,7 +363,7 @@ public class SearchAction implements RulesWsAction {
       .setTotal(result.getTotal());
   }
 
-  protected void doContextResponse(DbSession dbSession, SearchWsRequest request, SearchResult result, SearchResponse.Builder response, RuleQuery query) {
+  private void doContextResponse(DbSession dbSession, SearchWsRequest request, SearchResult result, SearchResponse.Builder response, RuleQuery query) {
     SearchOptions contextForResponse = loadCommonContext(request);
     writeRules(response, result, contextForResponse);
     if (contextForResponse.getFields().contains("actives")) {
@@ -387,7 +371,7 @@ public class SearchAction implements RulesWsAction {
     }
   }
 
-  protected void writeFacets(SearchResponse.Builder response, SearchWsRequest request, SearchOptions context, SearchResult results) {
+  private static void writeFacets(SearchResponse.Builder response, SearchWsRequest request, SearchOptions context, SearchResult results) {
     addMandatoryFacetValues(results, FACET_LANGUAGES, request.getLanguages());
     addMandatoryFacetValues(results, FACET_REPOSITORIES, request.getRepositories());
     addMandatoryFacetValues(results, FACET_STATUSES, ALL_STATUSES_EXCEPT_REMOVED);
@@ -438,7 +422,7 @@ public class SearchAction implements RulesWsAction {
     }
   }
 
-  protected void addMandatoryFacetValues(SearchResult results, String facetName, @Nullable Collection<String> mandatoryValues) {
+  private static void addMandatoryFacetValues(SearchResult results, String facetName, @Nullable Collection<String> mandatoryValues) {
     Map<String, Long> facetValues = results.facets.get(facetName);
     if (facetValues != null) {
       Collection<String> valuesToAdd = mandatoryValues == null ? Lists.newArrayList() : mandatoryValues;
@@ -542,22 +526,4 @@ public class SearchAction implements RulesWsAction {
     }
   }
 
-  private enum RuleDtoToId implements Function<RuleDto, Integer> {
-    INSTANCE;
-
-    @Override
-    public Integer apply(@Nonnull RuleDto input) {
-      return input.getId();
-    }
-  }
-
-  private enum RuleDtoToTemplateId implements Function<RuleDto, Integer> {
-    INSTANCE;
-
-    @Override
-    public Integer apply(@Nonnull RuleDto input) {
-      return input.getTemplateId();
-    }
-  }
-
 }
index 8a1a488c91ec755d3f61f70d5c5fadba0dbf393b..264ca51aad33af4266aaa0dfd6fca7085fb01aa8 100644 (file)
@@ -120,7 +120,7 @@ public class ShowAction implements RulesWsAction {
   private ShowResponse buildResponse(DbSession dbSession, Request request, SearchAction.SearchResult searchResult) {
     ShowResponse.Builder responseBuilder = ShowResponse.newBuilder();
     RuleDto rule = searchResult.getRules().get(0);
-    responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.<String>emptySet()));
+    responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet()));
 
     if (request.mandatoryParamAsBoolean(PARAM_ACTIVES)) {
       activeRuleCompleter.completeShow(dbSession, rule, responseBuilder);