]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 replace RuleResult and LogResult by Result<>
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 13 Jun 2014 21:46:13 +0000 (23:46 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 13 Jun 2014 21:46:13 +0000 (23:46 +0200)
17 files changed:
sonar-server/src/main/java/org/sonar/server/log/LogService.java
sonar-server/src/main/java/org/sonar/server/log/index/LogIndex.java
sonar-server/src/main/java/org/sonar/server/log/index/LogResult.java [deleted file]
sonar-server/src/main/java/org/sonar/server/log/ws/SearchAction.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
sonar-server/src/main/java/org/sonar/server/rule/RuleService.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleResult.java [deleted file]
sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/main/java/org/sonar/server/search/Index.java
sonar-server/src/main/java/org/sonar/server/search/Result.java
sonar-server/src/test/java/org/sonar/server/log/LogServiceMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java

index fdca3b85f9d2df59b7db1224390a41836df319f8..994ebb218f17bc123c5e7468a5e063843f1f6422 100644 (file)
@@ -26,9 +26,9 @@ import org.sonar.core.persistence.DbSession;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.log.index.LogIndex;
 import org.sonar.server.log.index.LogQuery;
-import org.sonar.server.log.index.LogResult;
 import org.sonar.server.search.IndexClient;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 import org.sonar.server.user.UserSession;
 
 import java.util.List;
@@ -83,7 +83,7 @@ public class LogService {
     return new LogQuery();
   }
 
-  public LogResult search(LogQuery query, QueryOptions options) {
+  public Result<Log> search(LogQuery query, QueryOptions options) {
     return indexClient.get(LogIndex.class).search(query, options);
   }
 }
index b57bfef5dac6a1a6cb6868bbf0ed44f9d43647a5..bd5f6b2c7174984e561b37df47c366e908e6aae7 100644 (file)
@@ -36,6 +36,7 @@ import org.sonar.server.search.ESNode;
 import org.sonar.server.search.IndexDefinition;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 
 import java.io.IOException;
 import java.util.HashMap;
@@ -52,15 +53,15 @@ public class LogIndex extends BaseIndex<Log, LogDto, LogKey> {
 
   @Override
   protected String getKeyValue(LogKey key) {
-    //FIXME too many collision with key.toString() due to lack of time precision
-    return null;//return key.toString();
+    // FIXME too many collision with key.toString() due to lack of time precision
+    return null;// return key.toString();
   }
 
   @Override
   protected Map mapKey() {
     return null;
-//    Map<String, Object> mapping = new HashMap<String, Object>();
-//    return mapping;
+    // Map<String, Object> mapping = new HashMap<String, Object>();
+    // return mapping;
   }
 
   @Override
@@ -82,21 +83,20 @@ public class LogIndex extends BaseIndex<Log, LogDto, LogKey> {
     return new LogDoc(fields);
   }
 
-  public LogResult findAll() {
-    return new LogResult(getClient().prepareSearch(this.getIndexName())
+  public Result<Log> findAll() {
+    return new Result<Log>(this, getClient().prepareSearch(this.getIndexName())
       .setQuery(QueryBuilders.matchAllQuery())
       .setTypes(this.getIndexType())
       .get());
   }
 
-  public LogResult search(LogQuery query, QueryOptions options) {
-
+  public Result<Log> search(LogQuery query, QueryOptions options) {
     SearchRequestBuilder esSearch = getClient()
       .prepareSearch(this.getIndexName())
       .setTypes(this.getIndexType())
       .setIndices(this.getIndexName());
 
-    //TODO implement query and filters based on LogQuery
+    // TODO implement query and filters based on LogQuery
     esSearch.setQuery(QueryBuilders.matchAllQuery());
 
     if (options.isScroll()) {
@@ -106,10 +106,6 @@ public class LogIndex extends BaseIndex<Log, LogDto, LogKey> {
 
     SearchResponse esResult = esSearch.get();
 
-    if (options.isScroll()) {
-      return new LogResult(this, esResult);
-    } else {
-      return new LogResult(esResult);
-    }
+    return new Result<Log>(this, esResult);
   }
 }
diff --git a/sonar-server/src/main/java/org/sonar/server/log/index/LogResult.java b/sonar-server/src/main/java/org/sonar/server/log/index/LogResult.java
deleted file mode 100644 (file)
index 82229a0..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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.
- */
-package org.sonar.server.log.index;
-
-import org.elasticsearch.action.search.SearchResponse;
-import org.sonar.core.log.Log;
-import org.sonar.server.search.Index;
-import org.sonar.server.search.Result;
-
-import java.util.Map;
-
-/**
- * @since 4.4
- */
-public class LogResult extends Result<Log> {
-
-  public LogResult(Index<Log, ?, ?> index, SearchResponse response) {
-    super(index, response);
-  }
-
-  public LogResult(SearchResponse response) {
-    super(response);
-  }
-
-  @Override
-  protected Log getSearchResult(Map<String, Object> fields) {
-    return new LogDoc(fields);
-  }
-}
index 975369b70409c5f16f7c9cb0d09e68414ce45d9a..f4defda00af9f771b98dbc89c9ffe1701c528aa4 100644 (file)
@@ -29,8 +29,8 @@ import org.sonar.core.log.Log;
 import org.sonar.server.log.LogService;
 import org.sonar.server.log.index.LogDoc;
 import org.sonar.server.log.index.LogQuery;
-import org.sonar.server.log.index.LogResult;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 import org.sonar.server.search.ws.SearchOptions;
 
 /**
@@ -75,7 +75,7 @@ public class SearchAction implements RequestHandler {
     SearchOptions searchOptions = SearchOptions.create(request);
     QueryOptions queryOptions = mapping.newQueryOptions(searchOptions);
 
-    LogResult results = logService.search(query, queryOptions);
+    Result<Log> results = logService.search(query, queryOptions);
 
     JsonWriter json = response.newJsonWriter().beginObject();
     searchOptions.writeStatistics(json, results);
@@ -88,7 +88,7 @@ public class SearchAction implements RequestHandler {
     return query;
   }
 
-  private void writeLogs(LogResult result, JsonWriter json, SearchOptions options) {
+  private void writeLogs(Result<Log> result, JsonWriter json, SearchOptions options) {
     json.name("logs").beginArray();
     for (Log log : result.getHits()) {
       mapping.write((LogDoc) log, json, options);
index 535ef221b8d10546b5b37fdbe3e6e2ade8dfdfe0..944748270d6ed35f10defa319f80927e604bb702 100644 (file)
@@ -27,7 +27,11 @@ import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.core.log.Log;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.preview.PreviewCache;
-import org.sonar.core.qualityprofile.db.*;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
 import org.sonar.server.db.DbClient;
@@ -37,9 +41,9 @@ import org.sonar.server.qualityprofile.db.ActiveRuleDao;
 import org.sonar.server.rule.Rule;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.rule.index.RuleResult;
 import org.sonar.server.search.IndexClient;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 import org.sonar.server.util.TypeValidations;
 
 import javax.annotation.Nullable;
@@ -321,7 +325,7 @@ public class RuleActivator implements ServerComponent {
     RuleIndex ruleIndex = index.get(RuleIndex.class);
     DbSession dbSession = db.openSession(false);
     try {
-      RuleResult ruleSearchResult = ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
+      Result<Rule> ruleSearchResult = ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
       Iterator<Rule> rules = ruleSearchResult.scroll();
       while (rules.hasNext()) {
         Rule rule = rules.next();
@@ -351,7 +355,7 @@ public class RuleActivator implements ServerComponent {
     try {
       RuleIndex ruleIndex = index.get(RuleIndex.class);
       BulkChangeResult result = new BulkChangeResult();
-      RuleResult ruleSearchResult = ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
+      Result<Rule> ruleSearchResult = ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
       Iterator<Rule> rules = ruleSearchResult.scroll();
       while (rules.hasNext()) {
         Rule rule = rules.next();
index 52c7f5a7ac855d5ebea818974e6776320e1cbc61..acc610c586336ee44cce1e0f15bcc904fde0250e 100644 (file)
@@ -174,7 +174,6 @@ public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, Active
     return IndexDefinition.RULE.getIndexType();
   }
 
-
   public Long countByQualityProfileKey(QualityProfileKey key) {
     CountRequestBuilder request = getClient().prepareCount(getIndexName())
       .setQuery(QueryBuilders.termQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key.toString()))
index dac17bac24a8c76d35073ac74ae067a53266d86e..66a27e4ab80e7f740baca2b88a8c26180b7f7473 100644 (file)
@@ -29,15 +29,14 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
 import org.sonar.server.paging.PagedResult;
 import org.sonar.server.paging.PagingResult;
-import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleNormalizer;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.rule.index.RuleResult;
 import org.sonar.server.search.QueryOptions;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.util.RubyUtils;
 
 import javax.annotation.CheckForNull;
+
 import java.util.List;
 import java.util.Map;
 
@@ -81,22 +80,8 @@ public class RubyRuleService implements ServerComponent, Startable {
     query.setHasDebtCharacteristic(RubyUtils.toBoolean(params.get("hasDebtCharacteristic")));
     query.setSortField(RuleNormalizer.RuleField.NAME);
 
-    QueryOptions options = new QueryOptions();
-    options.setLimit(QueryOptions.MAX_LIMIT);
-
-    List<Rule> rules = Lists.newArrayList();
-    boolean more = true;
-    int offset = 0;
-    while (more) {
-      options.setOffset(offset);
-      RuleResult result = service.search(query, options);
-      rules.addAll(result.getRules());
-      if (result.getHits().size() < QueryOptions.MAX_LIMIT) {
-        more = false;
-      } else {
-        offset += QueryOptions.MAX_LIMIT;
-      }
-    }
+    QueryOptions options = new QueryOptions().setScroll(true);
+    List<Rule> rules = Lists.newArrayList(service.search(query, options).scroll());
     return new PagedResult<org.sonar.server.rule.Rule>(rules, PagingResult.create(Integer.MAX_VALUE, 1, rules.size()));
   }
 
@@ -109,10 +94,10 @@ public class RubyRuleService implements ServerComponent, Startable {
       update.setDebtRemediationFunction(null);
     } else {
       update.setDebtRemediationFunction(new DefaultDebtRemediationFunction(
-          DebtRemediationFunction.Type.valueOf(fn),
-          Strings.emptyToNull((String) params.get("debtRemediationCoefficient")),
-          Strings.emptyToNull((String) params.get("debtRemediationOffset")))
-      );
+        DebtRemediationFunction.Type.valueOf(fn),
+        Strings.emptyToNull((String) params.get("debtRemediationCoefficient")),
+        Strings.emptyToNull((String) params.get("debtRemediationOffset")))
+        );
     }
     updater.update(update, UserSession.get());
   }
index 1727644195420ceabb41fbfb4af8b2d16fbdc354..133c066d56f12c4bd5dc3ab48c6dbb53f6058031 100644 (file)
@@ -25,8 +25,8 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleNormalizer;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.rule.index.RuleResult;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 import org.sonar.server.user.UserSession;
 
 import javax.annotation.CheckForNull;
@@ -59,7 +59,7 @@ public class RuleService implements ServerComponent {
     return new RuleQuery();
   }
 
-  public RuleResult search(RuleQuery query, QueryOptions options) {
+  public Result<Rule> search(RuleQuery query, QueryOptions options) {
     return index.search(query, options);
   }
 
index 2b861ca1075f199a6ba4edf76ba8126ebe3f3acd..eb6a4d4eb054d79646d82794bb50d2d36ecfd0af 100644 (file)
@@ -53,6 +53,7 @@ import org.sonar.server.search.ESNode;
 import org.sonar.server.search.IndexDefinition;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 
 import javax.annotation.Nullable;
 import java.io.IOException;
@@ -311,7 +312,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
   }
 
 
-  public RuleResult search(RuleQuery query, QueryOptions options) {
+  public Result<Rule> search(RuleQuery query, QueryOptions options) {
     StopWatch profile = profiling.start("es", Profiling.Level.BASIC);
 
     SearchRequestBuilder esSearch = getClient()
@@ -336,11 +337,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     SearchResponse esResult = esSearch.get();
     profile.stop("query: " + esSearch + "\nresult:" + esResult);
 
-    if (options.isScroll()) {
-      return new RuleResult(this, esResult);
-    } else {
-      return new RuleResult(esResult);
-    }
+    return new Result<Rule>(this, esResult);
   }
 
 
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleResult.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleResult.java
deleted file mode 100644 (file)
index fee5cc1..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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.
- */
-package org.sonar.server.rule.index;
-
-import org.elasticsearch.action.search.SearchResponse;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.search.Result;
-
-import java.util.Collection;
-import java.util.Map;
-
-public class RuleResult extends Result<Rule> {
-
-  public RuleResult(RuleIndex index, SearchResponse response) {
-    super(index, response);
-  }
-
-  public RuleResult(SearchResponse response) {
-    super(response);
-  }
-
-  @Override
-  protected Rule getSearchResult(Map<String, Object> fields) {
-    return new RuleDoc(fields);
-  }
-
-  public Collection<Rule> getRules() {
-    return super.getHits();
-  }
-}
index 38e6d821b8c7b5095ca63e6202ba5fe2f8c5f94a..944576127be411ad93639a2f01864367bab4e6b4 100644 (file)
@@ -34,9 +34,9 @@ import org.sonar.server.rule.RuleService;
 import org.sonar.server.rule.index.RuleDoc;
 import org.sonar.server.rule.index.RuleNormalizer;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.rule.index.RuleResult;
 import org.sonar.server.search.FacetValue;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 import org.sonar.server.search.ws.SearchOptions;
 
 import java.util.Collection;
@@ -209,13 +209,13 @@ public class SearchAction implements RequestHandler {
     QueryOptions queryOptions = mapping.newQueryOptions(searchOptions);
     queryOptions.setFacet(request.mandatoryParamAsBoolean(PARAM_FACETS));
 
-    RuleResult results = ruleService.search(query, queryOptions);
+    Result<Rule> results = ruleService.search(query, queryOptions);
 
     JsonWriter json = response.newJsonWriter().beginObject();
     searchOptions.writeStatistics(json, results);
     writeRules(results, json, searchOptions);
     if (searchOptions.hasField("actives")) {
-      activeRuleCompleter.completeSearch(query, results.getRules(), json);
+      activeRuleCompleter.completeSearch(query, results.getHits(), json);
     }
     if (queryOptions.isFacet()) {
       writeFacets(results, json);
@@ -244,7 +244,7 @@ public class SearchAction implements RequestHandler {
     return query;
   }
 
-  private void writeRules(RuleResult result, JsonWriter json, SearchOptions options) {
+  private void writeRules(Result<Rule> result, JsonWriter json, SearchOptions options) {
     json.name("rules").beginArray();
     for (Rule rule : result.getHits()) {
       mapping.write((RuleDoc) rule, json, options);
@@ -252,7 +252,7 @@ public class SearchAction implements RequestHandler {
     json.endArray();
   }
 
-  private void writeFacets(RuleResult results, JsonWriter json) {
+  private void writeFacets(Result<Rule> results, JsonWriter json) {
     json.name("facets").beginArray();
     for (Map.Entry<String, Collection<FacetValue>> facet : results.getFacets().entrySet()) {
       json.beginObject();
index 112cef4dfa17323a5a564c12aed90cde3bce36be..9fb2751d880d55b3018721d3c678096a5de8af0e 100644 (file)
@@ -48,6 +48,7 @@ import org.sonar.core.persistence.Dto;
 import org.sonar.core.profiling.Profiling;
 
 import javax.annotation.Nullable;
+
 import java.io.IOException;
 import java.io.Serializable;
 import java.util.ArrayDeque;
@@ -73,7 +74,7 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
   protected final Profiling profiling;
 
   protected BaseIndex(IndexDefinition indexDefinition, BaseNormalizer<DTO, KEY> normalizer,
-                      WorkQueue workQueue, ESNode node, Profiling profiling) {
+    WorkQueue workQueue, ESNode node, Profiling profiling) {
     this.normalizer = normalizer;
     this.node = node;
     this.indexDefinition = indexDefinition;
@@ -152,26 +153,25 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
     };
   }
 
-
   /* Cluster And ES Stats/Client methods */
 
   private void initializeManagementIndex() {
     LOG.debug("Setup of Management Index for ES");
 
-//    String index = indexDefinition.getManagementIndex();
-//
-//    IndicesExistsResponse indexExistsResponse = getClient().admin().indices()
-//      .prepareExists(index).execute().actionGet();
-//
-//    if (!indexExistsResponse.isExists()) {
-//      getClient().admin().indices().prepareCreate(index)
-//        .setSettings(ImmutableSettings.builder()
-//          .put("mapper.dynamic", true)
-//          .put("number_of_replicas", 1)
-//          .put("number_of_shards", 1)
-//          .build())
-//        .get();
-//    }
+    // String index = indexDefinition.getManagementIndex();
+    //
+    // IndicesExistsResponse indexExistsResponse = getClient().admin().indices()
+    // .prepareExists(index).execute().actionGet();
+    //
+    // if (!indexExistsResponse.isExists()) {
+    // getClient().admin().indices().prepareCreate(index)
+    // .setSettings(ImmutableSettings.builder()
+    // .put("mapper.dynamic", true)
+    // .put("number_of_replicas", 1)
+    // .put("number_of_shards", 1)
+    // .build())
+    // .get();
+    // }
   }
 
   protected void initializeIndex() {
@@ -189,10 +189,8 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
         getClient().admin().indices().prepareCreate(index)
           .setSettings(getIndexSettings())
           .execute().actionGet();
-
       }
 
-
       LOG.debug("Update of index {} for type {}", this.getIndexName(), this.getIndexType());
       getClient().admin().indices().preparePutMapping(index)
         .setType(getIndexType())
@@ -212,8 +210,7 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
     stat.setDocumentCount(
       getClient().prepareCount(this.getIndexName())
         .setQuery(QueryBuilders.matchAllQuery())
-        .get().getCount()
-    );
+        .get().getCount());
 
     /** get Management information */
     stat.setLastUpdate(getLastSynchronization());
@@ -316,8 +313,8 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
 
   protected boolean needMultiField(IndexField field) {
     return ((field.type() == IndexField.Type.TEXT
-      || field.type() == IndexField.Type.STRING)
-      && (field.sortable() || field.searchable()));
+    || field.type() == IndexField.Type.STRING)
+    && (field.sortable() || field.searchable()));
   }
 
   protected Map mapSortField(IndexField field) {
@@ -436,7 +433,6 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
     bulkRequest.get();
   }
 
-
   @Override
   public void upsert(KEY key, Object object, Object... objects) throws Exception {
     long t0 = System.currentTimeMillis();
@@ -532,7 +528,6 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
 
   /* ES QueryHelper Methods */
 
-
   protected void addMatchField(XContentBuilder mapping, String field, String type) throws IOException {
     mapping.startObject(field)
       .field("type", type)
@@ -555,7 +550,6 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
     return filter;
   }
 
-
   protected BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable Collection<String> values) {
     if (values != null && !values.isEmpty()) {
       BoolFilterBuilder valuesFilter = FilterBuilders.boolFilter();
index 716cddc6158d02f759147efae1d8cbe121d5d893..0f6838c3994892f09d4f427fe868defbdf5649a6 100644 (file)
@@ -24,6 +24,7 @@ import org.sonar.api.ServerComponent;
 import org.sonar.core.persistence.Dto;
 
 import javax.annotation.CheckForNull;
+
 import java.io.Serializable;
 import java.util.Date;
 import java.util.Iterator;
index 028ff68e64370236f962601b0d105360074b609c..491fdfc088debd7d577bac57c01f9a331bde41fd 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.search;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.LinkedListMultimap;
 import com.google.common.collect.Multimap;
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
@@ -28,40 +29,32 @@ import org.elasticsearch.search.aggregations.Aggregation;
 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 
 import javax.annotation.CheckForNull;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-public abstract class Result<K> {
+public class Result<K> {
 
   private final List<K> hits;
   private final Multimap<String, FacetValue> facets;
-  private long total;
-  private long timeInMillis;
-
-  //scrollable iterable
+  private final long total;
+  private final long timeInMillis;
   private final String scrollId;
-  private Index<K, ?, ?> index;
+  private final BaseIndex<K, ?, ?> index;
 
-  public Result(Index<K, ?, ?> index, SearchResponse response) {
-    this(response);
+  public Result(BaseIndex<K, ?, ?> index, SearchResponse response) {
     this.index = index;
-  }
-
-  public Result(SearchResponse response) {
-
-    scrollId = response.getScrollId();
-
-    this.hits = new ArrayList<K>();
+    this.scrollId = response.getScrollId();
     this.facets = LinkedListMultimap.create();
     this.total = (int) response.getHits().totalHits();
     this.timeInMillis = response.getTookInMillis();
+    this.hits = new ArrayList<K>();
     for (SearchHit hit : response.getHits()) {
-      this.hits.add(getSearchResult(hit.getSource()));
+      this.hits.add(index.toDoc(hit.getSource()));
     }
-
     if (response.getAggregations() != null) {
       for (Map.Entry<String, Aggregation> facet : response.getAggregations().asMap().entrySet()) {
         Terms aggregation = (Terms) facet.getValue();
@@ -73,15 +66,10 @@ public abstract class Result<K> {
   }
 
   public Iterator<K> scroll() {
-    if (scrollId == null || index == null) {
-      throw new IllegalStateException("Result is not scrollable. Please use QueryOptions.setScroll()");
-    } else {
-      return index.scroll(this.scrollId);
-    }
+    Preconditions.checkState(scrollId != null, "Result is not scrollable. Please use QueryOptions.setScroll()");
+    return index.scroll(scrollId);
   }
 
-  protected abstract K getSearchResult(Map<String, Object> fields);
-
   public List<K> getHits() {
     return hits;
   }
index 04095decbf0e1927c2dcc41db301558e07c051d2..872b9e19d3a3ecf39bb934f762e0b49befd4032a 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.log;
 
-
 import org.elasticsearch.common.collect.Iterables;
 import org.junit.After;
 import org.junit.Before;
@@ -32,8 +31,8 @@ import org.sonar.server.db.DbClient;
 import org.sonar.server.log.db.LogDao;
 import org.sonar.server.log.index.LogIndex;
 import org.sonar.server.log.index.LogQuery;
-import org.sonar.server.log.index.LogResult;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 import org.sonar.server.tester.ServerTester;
 
 import java.util.Iterator;
@@ -82,13 +81,13 @@ public class LogServiceMediumTest {
     dbSession.commit();
     assertThat(index.findAll().getTotal()).isEqualTo(1);
 
-    LogResult result = index.search(new LogQuery(), new QueryOptions());
+    Result<Log> result = index.search(new LogQuery(), new QueryOptions());
     assertThat(result.getTotal()).isEqualTo(1L);
   }
 
   @Test
   @Ignore
-  //TODO fix missing logs in ES.
+  // TODO fix missing logs in ES.
   public void iterate_all() throws InterruptedException {
     int max = QueryOptions.DEFAULT_LIMIT + 3;
     final String testValue = "hello world";
@@ -100,7 +99,7 @@ public class LogServiceMediumTest {
     // 0. assert Base case
     assertThat(dao.findAll(dbSession)).hasSize(max);
 
-    LogResult result = index.search(new LogQuery(), new QueryOptions().setScroll(true));
+    Result<Log> result = index.search(new LogQuery(), new QueryOptions().setScroll(true));
     assertThat(result.getTotal()).isEqualTo(max);
     assertThat(result.getHits()).hasSize(0);
     int count = 0;
@@ -111,4 +110,4 @@ public class LogServiceMediumTest {
     }
     assertThat(count).isEqualTo(max);
   }
-}
\ No newline at end of file
+}
index 1ef6b2688244a8f73dc96811fbf34fef01ceed71..ea968ae0764ac61e05a1741700f5aead2ec44eec 100644 (file)
@@ -43,8 +43,8 @@ import org.sonar.server.qualityprofile.QProfileService;
 import org.sonar.server.qualityprofile.RuleActivation;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.rule.index.RuleResult;
 import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.user.MockUserSession;
 
@@ -86,7 +86,7 @@ public class RegisterRulesMediumTest {
 
     RuleIndex index = tester.get(RuleIndex.class);
 
-    RuleResult searchResult = index.search(new RuleQuery(), new QueryOptions());
+    Result<Rule> searchResult = index.search(new RuleQuery(), new QueryOptions());
     assertThat(searchResult.getTotal()).isEqualTo(3);
     assertThat(searchResult.getHits()).hasSize(3);
   }
@@ -107,7 +107,7 @@ public class RegisterRulesMediumTest {
     // clear ES but keep db
     tester.clearIndexes();
     verifyRulesInDb();
-    RuleResult searchResult = index.search(new RuleQuery(), new QueryOptions());
+    Result<Rule> searchResult = index.search(new RuleQuery(), new QueryOptions());
     assertThat(searchResult.getTotal()).isEqualTo(0);
     assertThat(searchResult.getHits()).hasSize(0);
 
@@ -202,14 +202,14 @@ public class RegisterRulesMediumTest {
     // Update custom rule
     RuleDto customRuleDto = db.ruleDao().getByKey(dbSession, customRuleKey);
     db.ruleDao().update(dbSession, customRuleDto
-        .setLanguage("other language")
-        .setConfigKey("other config key")
-        .setDefaultSubCharacteristicId(45)
-        .setDefaultRemediationFunction("LINEAR_OFFSET")
-        .setDefaultRemediationCoefficient("1h")
-        .setDefaultRemediationOffset("5min")
-        .setEffortToFixDescription("effort to fix desc")
-    );
+      .setLanguage("other language")
+      .setConfigKey("other config key")
+      .setDefaultSubCharacteristicId(45)
+      .setDefaultRemediationFunction("LINEAR_OFFSET")
+      .setDefaultRemediationCoefficient("1h")
+      .setDefaultRemediationOffset("5min")
+      .setEffortToFixDescription("effort to fix desc")
+      );
     dbSession.commit();
     dbSession.clearCache();
 
index 73eb674593cdef483deeb68f1bc132ccdf331fb2..37f50d1d03ce2a4f29f3772f98fe600f18db7b6f 100644 (file)
@@ -30,7 +30,6 @@ import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rule.Severity;
-import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.qualityprofile.db.ActiveRuleDto;
 import org.sonar.core.qualityprofile.db.QualityProfileDto;
@@ -41,13 +40,19 @@ import org.sonar.server.db.DbClient;
 import org.sonar.server.debt.DebtTesting;
 import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.rule.Rule;
+import org.sonar.server.rule.RuleTesting;
 import org.sonar.server.rule.db.RuleDao;
 import org.sonar.server.search.FacetValue;
 import org.sonar.server.search.QueryOptions;
 import org.sonar.server.search.Result;
 import org.sonar.server.tester.ServerTester;
 
-import java.util.*;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.fest.assertions.Fail.fail;
@@ -76,7 +81,7 @@ public class RuleIndexMediumTest {
 
   @Test
   public void getByKey() throws InterruptedException {
-    RuleDto ruleDto = newRuleDto(RuleKey.of("javascript", "S001"));
+    RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("javascript", "S001"));
     dao.insert(dbSession, ruleDto);
     dbSession.commit();
 
@@ -85,9 +90,9 @@ public class RuleIndexMediumTest {
     assertThat(rule.htmlDescription()).isEqualTo(ruleDto.getDescription());
     assertThat(rule.key()).isEqualTo(ruleDto.getKey());
 
-    //TODO
-//    assertThat(rule.debtSubCharacteristicKey())
-//      .isEqualTo(ruleDto.getDefaultSubCharacteristicId().toString());
+    // TODO
+    // assertThat(rule.debtSubCharacteristicKey())
+    // .isEqualTo(ruleDto.getDefaultSubCharacteristicId().toString());
     assertThat(rule.debtRemediationFunction().type().name())
       .isEqualTo(ruleDto.getRemediationFunction());
 
@@ -103,12 +108,16 @@ public class RuleIndexMediumTest {
   }
 
   @Test
-  public void global_facet_on_repositories() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "S001")).setRuleKey("X001"));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("php", "S001"))
-      .setSystemTags(ImmutableSet.of("sysTag")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "S002")).setRuleKey("X002")
-      .setTags(ImmutableSet.of("tag1")));
+  public void global_facet_on_repositories_and_tags() {
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("php", "S001"))
+      .setSystemTags(ImmutableSet.of("sysTag")))
+      .setTags(ImmutableSet.<String>of());
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("php", "S002"))
+      .setSystemTags(ImmutableSet.<String>of()))
+      .setTags(ImmutableSet.of("tag1"));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S002"))
+      .setTags(ImmutableSet.of("tag1", "tag2")))
+      .setSystemTags(ImmutableSet.<String>of());
     dbSession.commit();
     index.refresh();
 
@@ -125,38 +134,43 @@ public class RuleIndexMediumTest {
     // Verify the value of a given facet
     Collection<FacetValue> repoFacets = result.getFacetValues("repositories");
     assertThat(repoFacets).hasSize(2);
-    assertThat(Iterables.get(repoFacets, 0).getKey()).isEqualTo("javascript");
+    assertThat(Iterables.get(repoFacets, 0).getKey()).isEqualTo("php");
     assertThat(Iterables.get(repoFacets, 0).getValue()).isEqualTo(2);
-    assertThat(Iterables.get(repoFacets, 1).getKey()).isEqualTo("php");
+    assertThat(Iterables.get(repoFacets, 1).getKey()).isEqualTo("javascript");
     assertThat(Iterables.get(repoFacets, 1).getValue()).isEqualTo(1);
 
     // Check that tag facet has both Tags and SystemTags values
     Collection<FacetValue> tagFacet = result.getFacetValues("tags");
-    assertThat(tagFacet).hasSize(2);
+    assertThat(tagFacet).hasSize(3);
+    assertThat(Iterables.get(tagFacet, 0).getKey()).isEqualTo("tag1");
+    assertThat(Iterables.get(tagFacet, 0).getValue()).isEqualTo(2);
   }
 
   @Test
   public void return_all_doc_fields_by_default() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "S001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S001")));
     dbSession.commit();
 
-
     QueryOptions options = new QueryOptions().setFieldsToReturn(null);
     Result<Rule> results = index.search(new RuleQuery(), options);
     assertThat(results.getHits()).hasSize(1);
     Rule hit = Iterables.getFirst(results.getHits(), null);
-    // TODO complete
+    assertThat(hit.key()).isNotNull();
+    assertThat(hit.htmlDescription()).isNotNull();
+    assertThat(hit.name()).isNotNull();
 
     options = new QueryOptions().setFieldsToReturn(Collections.<String>emptyList());
     results = index.search(new RuleQuery(), options);
     assertThat(results.getHits()).hasSize(1);
     hit = Iterables.getFirst(results.getHits(), null);
-    // TODO complete
+    assertThat(hit.key()).isNotNull();
+    assertThat(hit.htmlDescription()).isNotNull();
+    assertThat(hit.name()).isNotNull();
   }
 
   @Test
   public void select_doc_fields_to_return() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "S001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S001")));
     dbSession.commit();
 
     QueryOptions options = new QueryOptions();
@@ -178,7 +192,7 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_name_by_query() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "S001"))
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S001"))
       .setName("testing the partial match and matching of rule"));
     dbSession.commit();
 
@@ -201,19 +215,18 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_key_by_query() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "X001")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("cobol", "X001")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("php", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "X001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("cobol", "X001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("php", "S002")));
     dbSession.commit();
 
-
     // key
     RuleQuery query = new RuleQuery().setQueryText("X001");
     assertThat(index.search(query, new QueryOptions()).getHits()).hasSize(2);
 
     // partial key does not match
     query = new RuleQuery().setQueryText("X00");
-    //TODO fix non-partial match for Key search
+    // TODO fix non-partial match for Key search
     assertThat(index.search(query, new QueryOptions()).getHits()).isEmpty();
 
     // repo:key -> nice-to-have !
@@ -223,26 +236,25 @@ public class RuleIndexMediumTest {
 
   @Test
   public void filter_by_key() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "X001")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("cobol", "X001")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("php", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "X001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("cobol", "X001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("php", "S002")));
     dbSession.commit();
 
-
     // key
     RuleQuery query = new RuleQuery().setKey(RuleKey.of("javascript", "X001").toString());
     assertThat(index.search(query, new QueryOptions()).getHits()).hasSize(1);
 
     // partial key does not match
     query = new RuleQuery().setKey("X001");
-    //TODO fix non-partial match for Key search
+    // TODO fix non-partial match for Key search
     assertThat(index.search(query, new QueryOptions()).getHits()).isEmpty();
   }
 
   @Test
   public void search_all_rules() throws InterruptedException {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "S001")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")));
     dbSession.commit();
 
     Result results = index.search(new RuleQuery(), new QueryOptions());
@@ -255,7 +267,7 @@ public class RuleIndexMediumTest {
   public void scroll_all_rules() throws InterruptedException {
     int max = 100;
     for (int i = 0; i < max; i++) {
-      dao.insert(dbSession, newRuleDto(RuleKey.of("java", "scroll_" + i)));
+      dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "scroll_" + i)));
     }
     dbSession.commit();
 
@@ -288,9 +300,9 @@ public class RuleIndexMediumTest {
       .setParentId(char1.getId());
     db.debtCharacteristicDao().insert(char11, dbSession);
 
-    dao.insert(dbSession, newRuleDto(RuleKey.of("findbugs", "S001"))
-    .setSubCharacteristicId(char11.getId()));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("pmd", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("findbugs", "S001"))
+      .setSubCharacteristicId(char11.getId()));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("pmd", "S002")));
     dbSession.commit();
 
     // 0. assert base case
@@ -304,11 +316,10 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_by_any_of_repositories() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("findbugs", "S001")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("pmd", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("findbugs", "S001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("pmd", "S002")));
     dbSession.commit();
 
-
     RuleQuery query = new RuleQuery().setRepositories(Arrays.asList("checkstyle", "pmd"));
     Result<Rule> results = index.search(query, new QueryOptions());
     assertThat(results.getHits()).hasSize(1);
@@ -326,8 +337,8 @@ public class RuleIndexMediumTest {
   @Test
   public void search_by_any_of_languages() throws InterruptedException {
     dao.insert(dbSession,
-      newRuleDto(RuleKey.of("java", "S001")).setLanguage("java"),
-      newRuleDto(RuleKey.of("javascript", "S002")).setLanguage("js"));
+      RuleTesting.newDto(RuleKey.of("java", "S001")).setLanguage("java"),
+      RuleTesting.newDto(RuleKey.of("javascript", "S002")).setLanguage("js"));
     dbSession.commit();
 
     RuleQuery query = new RuleQuery().setLanguages(Arrays.asList("cobol", "js"));
@@ -349,7 +360,6 @@ public class RuleIndexMediumTest {
     assertThat(index.search(query, new QueryOptions()).getHits()).hasSize(2);
   }
 
-
   @Test
   public void search_by_characteristics() throws InterruptedException {
     CharacteristicDto char1 = DebtTesting.newCharacteristicDto("char1");
@@ -360,14 +370,13 @@ public class RuleIndexMediumTest {
     db.debtCharacteristicDao().insert(char11, dbSession);
     dbSession.commit();
 
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001"))
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001"))
       .setSubCharacteristicId(char11.getId()));
 
-    dao.insert(dbSession, newRuleDto(RuleKey.of("javascript", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S002")));
 
     dbSession.commit();
 
-
     RuleQuery query;
     Result<Rule> results;
 
@@ -406,11 +415,10 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_by_any_of_severities() throws InterruptedException {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")).setSeverity(Severity.BLOCKER));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")).setSeverity(Severity.INFO));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001")).setSeverity(Severity.BLOCKER));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")).setSeverity(Severity.INFO));
     dbSession.commit();
 
-
     RuleQuery query = new RuleQuery().setSeverities(Arrays.asList(Severity.INFO, Severity.MINOR));
     Result<Rule> results = index.search(query, new QueryOptions());
     assertThat(results.getHits()).hasSize(1);
@@ -431,11 +439,10 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_by_any_of_statuses() throws InterruptedException {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")).setStatus(RuleStatus.BETA));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")).setStatus(RuleStatus.READY));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001")).setStatus(RuleStatus.BETA));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")).setStatus(RuleStatus.READY));
     dbSession.commit();
 
-
     RuleQuery query = new RuleQuery().setStatuses(Arrays.asList(RuleStatus.DEPRECATED, RuleStatus.READY));
     Result<Rule> results = index.search(query, new QueryOptions());
     assertThat(results.getHits()).hasSize(1);
@@ -456,12 +463,11 @@ public class RuleIndexMediumTest {
 
   @Test
   public void sort_by_name() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")).setName("abcd"));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")).setName("ABC"));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S003")).setName("FGH"));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001")).setName("abcd"));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")).setName("ABC"));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S003")).setName("FGH"));
     dbSession.commit();
 
-
     // ascending
     RuleQuery query = new RuleQuery().setSortField(RuleNormalizer.RuleField.NAME);
     Result<Rule> results = index.search(query, new QueryOptions());
@@ -494,26 +500,21 @@ public class RuleIndexMediumTest {
     QualityProfileDto qualityProfileDto2 = QualityProfileDto.createFor("profile2", "java");
     db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);
 
-    RuleDto rule1 = newRuleDto(RuleKey.of("java", "S001"));
-    RuleDto rule2 = newRuleDto(RuleKey.of("java", "S002"));
-    RuleDto rule3 = newRuleDto(RuleKey.of("java", "S003"));
+    RuleDto rule1 = RuleTesting.newDto(RuleKey.of("java", "S001"));
+    RuleDto rule2 = RuleTesting.newDto(RuleKey.of("java", "S002"));
+    RuleDto rule3 = RuleTesting.newDto(RuleKey.of("java", "S003"));
     dao.insert(dbSession, rule1, rule2, rule3);
 
     db.activeRuleDao().insert(
       dbSession,
-      ActiveRuleDto.createFor(qualityProfileDto1, rule1)
-        .setSeverity("BLOCKER"),
-      ActiveRuleDto.createFor(qualityProfileDto2, rule1)
-        .setSeverity("BLOCKER"),
-      ActiveRuleDto.createFor(qualityProfileDto1, rule2)
-        .setSeverity("BLOCKER")
-    );
+      ActiveRuleDto.createFor(qualityProfileDto1, rule1).setSeverity("BLOCKER"),
+      ActiveRuleDto.createFor(qualityProfileDto2, rule1).setSeverity("BLOCKER"),
+      ActiveRuleDto.createFor(qualityProfileDto1, rule2).setSeverity("BLOCKER"));
 
     dbSession.commit();
-    RuleResult result;
 
     // 1. get all active rules.
-    result = index.search(new RuleQuery().setActivation(true),
+    Result<Rule> result = index.search(new RuleQuery().setActivation(true),
       new QueryOptions());
     assertThat(result.getHits()).hasSize(2);
 
@@ -527,14 +528,13 @@ public class RuleIndexMediumTest {
     index.search(new RuleQuery().setActivation(false).setQProfileKey(qualityProfileDto2.getKey().toString()),
       new QueryOptions());
     // TODO
-    assertThat(result.getRules()).hasSize(1);
+    assertThat(result.getHits()).hasSize(1);
 
     // 4. get all active rules on profile
     result = index.search(new RuleQuery().setActivation(true)
-        .setQProfileKey(qualityProfileDto2.getKey().toString()),
-      new QueryOptions()
-    );
-    assertThat(result.getRules()).hasSize(1);
+      .setQProfileKey(qualityProfileDto2.getKey().toString()),
+      new QueryOptions());
+    assertThat(result.getHits()).hasSize(1);
     assertThat(result.getHits().get(0).name()).isEqualTo(rule1.getName());
 
   }
@@ -546,10 +546,10 @@ public class RuleIndexMediumTest {
       .setParent(qualityProfileDto1.getName());
     db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);
 
-    RuleDto rule1 = newRuleDto(RuleKey.of("java", "S001"));
-    RuleDto rule2 = newRuleDto(RuleKey.of("java", "S002"));
-    RuleDto rule3 = newRuleDto(RuleKey.of("java", "S003"));
-    RuleDto rule4 = newRuleDto(RuleKey.of("java", "S004"));
+    RuleDto rule1 = RuleTesting.newDto(RuleKey.of("java", "S001"));
+    RuleDto rule2 = RuleTesting.newDto(RuleKey.of("java", "S002"));
+    RuleDto rule3 = RuleTesting.newDto(RuleKey.of("java", "S003"));
+    RuleDto rule4 = RuleTesting.newDto(RuleKey.of("java", "S004"));
     dao.insert(dbSession, rule1, rule2, rule3, rule4);
 
     db.activeRuleDao().insert(
@@ -561,7 +561,6 @@ public class RuleIndexMediumTest {
       ActiveRuleDto.createFor(qualityProfileDto1, rule3)
         .setSeverity("BLOCKER"),
 
-
       ActiveRuleDto.createFor(qualityProfileDto2, rule1)
         .setSeverity("MINOR")
         .setInheritance(ActiveRule.Inheritance.INHERITED.name()),
@@ -571,13 +570,12 @@ public class RuleIndexMediumTest {
       ActiveRuleDto.createFor(qualityProfileDto2, rule3)
         .setSeverity("BLOCKER")
         .setInheritance(ActiveRule.Inheritance.INHERITED.name())
-    );
+      );
 
     dbSession.commit();
-    RuleResult result;
 
     // 0. get all rules
-    result = index.search(new RuleQuery(),
+    Result<Rule> result = index.search(new RuleQuery(),
       new QueryOptions());
     assertThat(result.getHits()).hasSize(4);
 
@@ -594,53 +592,53 @@ public class RuleIndexMediumTest {
 
     // 3. get Inherited Rules on profile1
     result = index.search(new RuleQuery().setActivation(true)
-        .setQProfileKey(qualityProfileDto1.getKey().toString())
-        .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
+      .setQProfileKey(qualityProfileDto1.getKey().toString())
+      .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
       new QueryOptions()
-    );
-    assertThat(result.getRules()).hasSize(0);
+      );
+    assertThat(result.getHits()).hasSize(0);
 
     // 4. get Inherited Rules on profile2
     result = index.search(new RuleQuery().setActivation(true)
-        .setQProfileKey(qualityProfileDto2.getKey().toString())
-        .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
+      .setQProfileKey(qualityProfileDto2.getKey().toString())
+      .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
       new QueryOptions()
-    );
-    assertThat(result.getRules()).hasSize(2);
+      );
+    assertThat(result.getHits()).hasSize(2);
 
     // 5. get Overridden Rules on profile1
     result = index.search(new RuleQuery().setActivation(true)
-        .setQProfileKey(qualityProfileDto1.getKey().toString())
-        .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
+      .setQProfileKey(qualityProfileDto1.getKey().toString())
+      .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
       new QueryOptions()
-    );
-    assertThat(result.getRules()).hasSize(0);
+      );
+    assertThat(result.getHits()).hasSize(0);
 
     // 6. get Overridden Rules on profile2
     result = index.search(new RuleQuery().setActivation(true)
-        .setQProfileKey(qualityProfileDto2.getKey().toString())
-        .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
+      .setQProfileKey(qualityProfileDto2.getKey().toString())
+      .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
       new QueryOptions()
-    );
-    assertThat(result.getRules()).hasSize(1);
+      );
+    assertThat(result.getHits()).hasSize(1);
 
     // 7. get Inherited AND Overridden Rules on profile1
     result = index.search(new RuleQuery().setActivation(true)
-        .setQProfileKey(qualityProfileDto1.getKey().toString())
-        .setInheritance(ImmutableSet.of(
-          ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
+      .setQProfileKey(qualityProfileDto1.getKey().toString())
+      .setInheritance(ImmutableSet.of(
+        ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
       new QueryOptions()
-    );
-    assertThat(result.getRules()).hasSize(0);
+      );
+    assertThat(result.getHits()).hasSize(0);
 
     // 8. get Inherited AND Overridden Rules on profile2
     result = index.search(new RuleQuery().setActivation(true)
-        .setQProfileKey(qualityProfileDto2.getKey().toString())
-        .setInheritance(ImmutableSet.of(
-          ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
+      .setQProfileKey(qualityProfileDto2.getKey().toString())
+      .setInheritance(ImmutableSet.of(
+        ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
       new QueryOptions()
-    );
-    assertThat(result.getRules()).hasSize(3);
+      );
+    assertThat(result.getHits()).hasSize(3);
   }
 
   @Test
@@ -650,7 +648,7 @@ public class RuleIndexMediumTest {
     QualityProfileDto profile = QualityProfileDto.createFor("name", "Language");
     db.qualityProfileDao().insert(dbSession, profile);
 
-    RuleDto rule = newRuleDto(RuleKey.of("java", "S001"));
+    RuleDto rule = RuleTesting.newDto(RuleKey.of("java", "S001"));
     dao.insert(dbSession, rule);
 
     RuleParamDto param = RuleParamDto.createFor(rule)
@@ -667,11 +665,10 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_by_tag() throws InterruptedException {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")).setTags(ImmutableSet.of("tag1")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")).setTags(ImmutableSet.of("tag2")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001")).setTags(ImmutableSet.of("tag1")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")).setTags(ImmutableSet.of("tag2")));
     dbSession.commit();
 
-
     // find all
     RuleQuery query = new RuleQuery();
     assertThat(index.search(query, new QueryOptions()).getHits()).hasSize(2);
@@ -710,8 +707,8 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_by_is_template() throws InterruptedException {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")).setIsTemplate(false));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")).setIsTemplate(true));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(false));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")).setIsTemplate(true));
     dbSession.commit();
 
     // find all
@@ -740,9 +737,9 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_by_template_key() throws InterruptedException {
-    RuleDto templateRule = newRuleDto(RuleKey.of("java", "S001")).setIsTemplate(true);
+    RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true);
     dao.insert(dbSession, templateRule);
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001_MY_CUSTOM")).setTemplateId(templateRule.getId()));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")).setTemplateId(templateRule.getId()));
     dbSession.commit();
 
     // find all
@@ -764,12 +761,12 @@ public class RuleIndexMediumTest {
 
   @Test
   public void search_by_template_key_with_params() throws InterruptedException {
-    RuleDto templateRule = newRuleDto(RuleKey.of("java", "S001")).setIsTemplate(true);
+    RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true);
     RuleParamDto ruleParamDto = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
     dao.insert(dbSession, templateRule);
     dao.addRuleParam(dbSession, templateRule, ruleParamDto);
 
-    RuleDto customRule = newRuleDto(RuleKey.of("java", "S001_MY_CUSTOM")).setTemplateId(templateRule.getId());
+    RuleDto customRule = RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")).setTemplateId(templateRule.getId());
     RuleParamDto customRuleParam = RuleParamDto.createFor(customRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue("a.*");
     dao.insert(dbSession, customRule);
     dao.addRuleParam(dbSession, customRule, customRuleParam);
@@ -787,9 +784,9 @@ public class RuleIndexMediumTest {
 
   @Test
   public void show_custom_rule() throws InterruptedException {
-    RuleDto templateRule = newRuleDto(RuleKey.of("java", "S001")).setIsTemplate(true);
+    RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true);
     dao.insert(dbSession, templateRule);
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001_MY_CUSTOM")).setTemplateId(templateRule.getId()));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")).setTemplateId(templateRule.getId()));
     dbSession.commit();
 
     // find all
@@ -803,12 +800,11 @@ public class RuleIndexMediumTest {
 
   @Test
   public void paging() {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")));
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S003")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S003")));
     dbSession.commit();
 
-
     // from 0 to 1 included
     QueryOptions options = new QueryOptions();
     options.setOffset(0).setLimit(2);
@@ -831,11 +827,11 @@ public class RuleIndexMediumTest {
 
   @Test
   public void available_since() throws InterruptedException {
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001")));
     dbSession.commit();
-    Thread.sleep(1000);
+    Thread.sleep(500);
     Date since = new Date();
-    dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S002")));
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002")));
     dbSession.commit();
 
     // 0. find all rules;
@@ -853,24 +849,4 @@ public class RuleIndexMediumTest {
       .setAvailableSince(DateUtils.addDays(since, 1));
     assertThat(index.search(availableSinceNowQuery, new QueryOptions()).getHits()).hasSize(0);
   }
-
-  private RuleDto newRuleDto(RuleKey ruleKey) {
-    return new RuleDto()
-      .setRuleKey(ruleKey.rule())
-      .setRepositoryKey(ruleKey.repository())
-      .setName("Rule " + ruleKey.rule())
-      .setDescription("Description " + ruleKey.rule())
-      .setStatus(RuleStatus.READY)
-      .setConfigKey("InternalKey" + ruleKey.rule())
-      .setSeverity(Severity.INFO)
-      .setIsTemplate(false)
-      .setLanguage("js")
-      .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
-      .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
-      .setRemediationCoefficient("1h")
-      .setDefaultRemediationCoefficient("5d")
-      .setRemediationOffset("5min")
-      .setDefaultRemediationOffset("10h")
-      .setEffortToFixDescription(ruleKey.repository() + "." + ruleKey.rule() + ".effortToFix");
-  }
 }