]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 RubyRuleService is now using RuleDao 795/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 11:13:32 +0000 (12:13 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 12:26:54 +0000 (13:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
server/sonar-server/src/main/java/org/sonar/server/rule/RuleService.java
server/sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java

index 1bafcec918e622659c290cac14944d4e5a0a530c..75414cb5f98e1bfd5e6af26f106f0c14ffb6fd2a 100644 (file)
@@ -24,23 +24,23 @@ import java.util.List;
 import java.util.Map;
 import javax.annotation.CheckForNull;
 import org.picocontainer.Startable;
-import org.sonar.api.server.ServerSide;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.server.ServerSide;
 import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.rule.RuleDto;
+import org.sonar.server.es.SearchIdResult;
+import org.sonar.server.es.SearchOptions;
 import org.sonar.server.paging.PagedResult;
 import org.sonar.server.paging.PagingResult;
-import org.sonar.server.rule.index.RuleDoc;
-import org.sonar.server.rule.index.RuleNormalizer;
+import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.search.QueryContext;
-import org.sonar.server.search.Result;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.util.RubyUtils;
 
-import static com.google.common.collect.Lists.newArrayList;
-
 /**
  * Used through ruby code <pre>Internal.rules</pre>
  *
@@ -50,11 +50,13 @@ import static com.google.common.collect.Lists.newArrayList;
 @ServerSide
 public class RubyRuleService implements Startable {
 
+  private final DbClient dbClient;
   private final RuleService service;
   private final RuleUpdater updater;
   private final UserSession userSession;
 
-  public RubyRuleService(RuleService service, RuleUpdater updater, UserSession userSession) {
+  public RubyRuleService(DbClient dbClient, RuleService service, RuleUpdater updater, UserSession userSession) {
+    this.dbClient = dbClient;
     this.service = service;
     this.updater = updater;
     this.userSession = userSession;
@@ -64,15 +66,19 @@ public class RubyRuleService implements Startable {
    * Used in issues_controller.rb and in manual_rules_controller.rb and in SQALE
    */
   @CheckForNull
-  public Rule findByKey(String ruleKey) {
-    return service.getByKey(RuleKey.parse(ruleKey));
+  public RuleDto findByKey(String ruleKey) {
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      return dbClient.ruleDao().selectByKey(dbSession, RuleKey.parse(ruleKey)).orNull();
+    } finally {
+      dbClient.closeSession(dbSession);
+    }
   }
 
   /**
    * Used in SQALE
-   * If 'pageSize' params is set no -1, all rules are returned (using scrolling)
    */
-  public PagedResult<Rule> find(Map<String, Object> params) {
+  public PagedResult<RuleDto> find(Map<String, Object> params) {
     RuleQuery query = new RuleQuery();
     query.setQueryText(Strings.emptyToNull((String) params.get("searchQuery")));
     query.setKey(Strings.emptyToNull((String) params.get("key")));
@@ -81,34 +87,22 @@ public class RubyRuleService implements Startable {
     query.setSeverities(RubyUtils.toStrings(params.get("severities")));
     query.setStatuses(RubyUtils.toEnums(params.get("statuses"), RuleStatus.class));
     query.setTags(RubyUtils.toStrings(params.get("tags")));
-    query.setSortField(RuleNormalizer.RuleField.NAME);
+    query.setSortField(RuleIndexDefinition.FIELD_RULE_NAME);
     String profile = Strings.emptyToNull((String) params.get("profile"));
     if (profile != null) {
       query.setQProfileKey(profile);
       query.setActivation(true);
     }
 
-    QueryContext options = new QueryContext(userSession);
+    SearchOptions options = new SearchOptions();
     Integer pageSize = RubyUtils.toInteger(params.get("pageSize"));
     int size = pageSize != null ? pageSize : 50;
-    if (size > -1) {
-      Integer page = RubyUtils.toInteger(params.get("p"));
-      int pageIndex = page != null ? page : 1;
-      options.setPage(pageIndex, size);
-      Result<Rule> result = service.search(query, options);
-      return new PagedResult<>(result.getHits(), PagingResult.create(options.getLimit(), pageIndex, result.getTotal()));
-    } else {
-      List<Rule> rules = newArrayList(service.search(query, new QueryContext(userSession).setScroll(true)).scroll());
-      return new PagedResult<>(rules, PagingResult.create(Integer.MAX_VALUE, 1, rules.size()));
-    }
-  }
-
-  /**
-   * Used in manual_rules_controller.rb
-   */
-  public List<Rule> searchManualRules() {
-    return service.search(new RuleQuery().setRepositories(newArrayList(RuleDoc.MANUAL_REPOSITORY))
-      .setSortField(RuleNormalizer.RuleField.NAME), new QueryContext(userSession)).getHits();
+    Integer page = RubyUtils.toInteger(params.get("p"));
+    int pageIndex = page != null ? page : 1;
+    options.setPage(pageIndex, size);
+    SearchIdResult<RuleKey> result = service.search(query, options);
+    List<RuleDto> ruleDtos = loadDtos(result.getIds());
+    return new PagedResult<>(ruleDtos, PagingResult.create(options.getLimit(), pageIndex, result.getTotal()));
   }
 
   // sqale
@@ -154,6 +148,15 @@ public class RubyRuleService implements Startable {
     service.delete(RuleKey.parse(ruleKey));
   }
 
+  private List<RuleDto> loadDtos(List<RuleKey> ruleKeys) {
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      return dbClient.ruleDao().selectByKeys(dbSession, ruleKeys);
+    } finally {
+      dbClient.closeSession(dbSession);
+    }
+  }
+
   @Override
   public void start() {
     // used to force pico to instantiate the singleton at startup
index fec94c4e02bc453bdb27736254f83e4277184527..21a5fba93daa70d2ecc44c049d334067224c1aa1 100644 (file)
@@ -24,6 +24,8 @@ import javax.annotation.Nullable;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.ServerSide;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.es.SearchIdResult;
+import org.sonar.server.es.SearchOptions;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleQuery;
@@ -35,12 +37,14 @@ import org.sonar.server.user.UserSession;
 @ServerSide
 public class RuleService {
 
+  private final RuleUpdater ruleUpdater;
   private final RuleIndex index;
   private final RuleCreator ruleCreator;
   private final RuleDeleter ruleDeleter;
   private final UserSession userSession;
 
-  public RuleService(RuleIndex index, RuleCreator ruleCreator, RuleDeleter ruleDeleter, UserSession userSession) {
+  public RuleService(RuleUpdater ruleUpdater, RuleIndex index, RuleCreator ruleCreator, RuleDeleter ruleDeleter, UserSession userSession) {
+    this.ruleUpdater = ruleUpdater;
     this.index = index;
     this.ruleCreator = ruleCreator;
     this.ruleDeleter = ruleDeleter;
@@ -67,6 +71,10 @@ public class RuleService {
     return index.terms(RuleIndexDefinition.FIELD_RULE_ALL_TAGS, query, size);
   }
 
+  public SearchIdResult<RuleKey> search(RuleQuery query, SearchOptions options) {
+     return index.search(query, options);
+  }
+
   public RuleKey create(NewRule newRule) {
     checkPermission();
     return ruleCreator.create(newRule);
index 886a6b4012fd0672cb5f9d623c60115b8125ceba..acceb7736f17249ff1de1f94f9106dffe656fe05 100644 (file)
@@ -19,9 +19,9 @@
  */
 package org.sonar.server.rule;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
 import java.util.HashMap;
-import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -32,14 +32,17 @@ import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.server.ws.WebService.Param;
-import org.sonar.server.paging.PagedResult;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.rule.RuleDao;
+import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleTesting;
+import org.sonar.server.es.SearchIdResult;
+import org.sonar.server.es.SearchOptions;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.search.QueryContext;
-import org.sonar.server.search.Result;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.user.ThreadLocalUserSession;
 
-import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Maps.newHashMap;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.any;
@@ -56,11 +59,20 @@ public class RubyRuleServiceTest {
   @Mock
   RuleService ruleService;
 
+  @Mock
+  DbClient dbClient;
+
+  @Mock
+  DbSession dbSession;
+
+  @Mock
+  RuleDao ruleDao;
+
   @Mock
   RuleUpdater updater;
 
   @Captor
-  ArgumentCaptor<QueryContext> optionsCaptor;
+  ArgumentCaptor<SearchOptions> optionsCaptor;
 
   @Captor
   ArgumentCaptor<RuleQuery> ruleQueryCaptor;
@@ -69,18 +81,24 @@ public class RubyRuleServiceTest {
 
   @Before
   public void setUp() {
-    service = new RubyRuleService(ruleService, updater, userSessionRule);
+    when(dbClient.openSession(false)).thenReturn(dbSession);
+    when(dbClient.ruleDao()).thenReturn(ruleDao);
+    service = new RubyRuleService(dbClient, ruleService, updater, userSessionRule);
   }
 
   @Test
   public void find_by_key() {
-    service.findByKey("squid:S001");
-    verify(ruleService).getByKey(RuleKey.of("squid", "S001"));
+    RuleKey ruleKey = RuleKey.of("squid", "S001");
+    RuleDto ruleDto = RuleTesting.newXooX1();
+
+    when(ruleDao.selectByKey(dbSession, ruleKey)).thenReturn(Optional.of(ruleDto));
+
+    assertThat(service.findByKey("squid:S001")).isEqualTo(ruleDto);
   }
 
   @Test
   public void search_rules() {
-    when(ruleService.search(any(RuleQuery.class), any(QueryContext.class))).thenReturn(mock(Result.class));
+    when(ruleService.search(any(RuleQuery.class), any(SearchOptions.class))).thenReturn(mock(SearchIdResult.class));
 
     HashMap<String, Object> params = newHashMap();
     params.put("searchQuery", "Exception");
@@ -113,7 +131,7 @@ public class RubyRuleServiceTest {
 
   @Test
   public void search_rules_activated_on_a_profile() {
-    when(ruleService.search(any(RuleQuery.class), any(QueryContext.class))).thenReturn(mock(Result.class));
+    when(ruleService.search(any(RuleQuery.class), any(SearchOptions.class))).thenReturn(mock(SearchIdResult.class));
 
     HashMap<String, Object> params = newHashMap();
     params.put("profile", "xoo-profile");
@@ -127,7 +145,7 @@ public class RubyRuleServiceTest {
 
   @Test
   public void search_rules_without_page_size_param() {
-    when(ruleService.search(any(RuleQuery.class), any(QueryContext.class))).thenReturn(mock(Result.class));
+    when(ruleService.search(any(RuleQuery.class), any(SearchOptions.class))).thenReturn(mock(SearchIdResult.class));
 
     HashMap<String, Object> params = newHashMap();
     params.put(Param.PAGE, "1");
@@ -139,42 +157,15 @@ public class RubyRuleServiceTest {
     assertThat(optionsCaptor.getValue().getOffset()).isEqualTo(0);
   }
 
-  @Test
-  public void search_all_rules() {
-    List<Rule> rules = newArrayList(mock(Rule.class));
-    Result serviceResult = mock(Result.class);
-    when(serviceResult.scroll()).thenReturn(rules.iterator());
-
-    when(ruleService.search(any(RuleQuery.class), any(QueryContext.class))).thenReturn(serviceResult);
-
-    HashMap<String, Object> params = newHashMap();
-    params.put("pageSize", "-1");
-    PagedResult<Rule> result = service.find(params);
-
-    verify(serviceResult).scroll();
-
-    verify(ruleService).search(ruleQueryCaptor.capture(), optionsCaptor.capture());
-    assertThat(result.paging().pageSize()).isEqualTo(Integer.MAX_VALUE);
-  }
-
   @Test
   public void update_rule() {
-    when(ruleService.search(any(RuleQuery.class), any(QueryContext.class))).thenReturn(mock(Result.class));
+    when(ruleService.search(any(RuleQuery.class), any(SearchOptions.class))).thenReturn(mock(SearchIdResult.class));
 
     service.updateRule(ImmutableMap.<String, Object>of("ruleKey", "squid:S001"));
 
     verify(updater).update(any(RuleUpdate.class), any(ThreadLocalUserSession.class));
   }
 
-  @Test
-  public void search_manual_rules() {
-    when(ruleService.search(any(RuleQuery.class), any(QueryContext.class))).thenReturn(mock(Result.class));
-
-    service.searchManualRules();
-
-    verify(ruleService).search(any(RuleQuery.class), any(QueryContext.class));
-  }
-
   @Test
   public void create_manual_rules() {
     service.createManualRule(ImmutableMap.<String, Object>of("manualKey", "MY_MANUAL"));