]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 Add pagination on changelog
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 23 Jun 2014 16:07:59 +0000 (18:07 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 23 Jun 2014 16:08:15 +0000 (18:08 +0200)
sonar-server/src/main/java/org/sonar/server/activity/RubyActivityService.java [deleted file]
sonar-server/src/main/java/org/sonar/server/activity/RubyQProfileActivityService.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
sonar-server/src/main/java/org/sonar/server/search/Result.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/views/profiles/changelog.html.erb
sonar-server/src/test/java/org/sonar/server/activity/RubyActivityServiceTest.java [deleted file]
sonar-server/src/test/java/org/sonar/server/activity/RubyQProfileActivityServiceTest.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java

diff --git a/sonar-server/src/main/java/org/sonar/server/activity/RubyActivityService.java b/sonar-server/src/main/java/org/sonar/server/activity/RubyActivityService.java
deleted file mode 100644 (file)
index 43b98ea..0000000
+++ /dev/null
@@ -1,76 +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.activity;
-
-import org.picocontainer.Startable;
-import org.sonar.api.ServerComponent;
-import org.sonar.server.qualityprofile.QProfileActivity;
-import org.sonar.server.qualityprofile.QProfileActivityQuery;
-import org.sonar.server.qualityprofile.QProfileService;
-import org.sonar.server.search.QueryOptions;
-import org.sonar.server.util.RubyUtils;
-
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @deprecated in 4.4 because Ruby on Rails is deprecated too !
- */
-@Deprecated
-public class RubyActivityService implements ServerComponent, Startable {
-
-  private final QProfileService service;
-
-  public RubyActivityService(QProfileService service) {
-    this.service = service;
-  }
-
-  /**
-   * Used in profiles_controller.rb
-   */
-  public List<QProfileActivity> search(Map<String, Object> params) {
-    QProfileActivityQuery query = new QProfileActivityQuery();
-    List<String> profileKeys = RubyUtils.toStrings(params.get("profileKeys"));
-    if (profileKeys != null) {
-      query.setQprofileKeys(profileKeys);
-    }
-    Date since = RubyUtils.toDate(params.get("since"));
-    if (since != null) {
-      query.setSince(since);
-    }
-    Date to = RubyUtils.toDate(params.get("to"));
-    if (to != null) {
-      query.setTo(to);
-    }
-    return service.findActivities(query, new QueryOptions().setMaxLimit());
-  }
-
-  @Override
-  public void start() {
-    // used to force pico to instantiate the singleton at startup
-  }
-
-  @Override
-  public void stop() {
-    // implement startable
-  }
-}
diff --git a/sonar-server/src/main/java/org/sonar/server/activity/RubyQProfileActivityService.java b/sonar-server/src/main/java/org/sonar/server/activity/RubyQProfileActivityService.java
new file mode 100644 (file)
index 0000000..d5715a2
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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.activity;
+
+import org.picocontainer.Startable;
+import org.sonar.api.ServerComponent;
+import org.sonar.api.utils.Paging;
+import org.sonar.server.qualityprofile.QProfileActivity;
+import org.sonar.server.qualityprofile.QProfileActivityQuery;
+import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
+import org.sonar.server.util.RubyUtils;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @deprecated in 4.4 because Ruby on Rails is deprecated too !
+ */
+@Deprecated
+public class RubyQProfileActivityService implements ServerComponent, Startable {
+
+  private final QProfileService service;
+
+  public RubyQProfileActivityService(QProfileService service) {
+    this.service = service;
+  }
+
+  /**
+   * Used in profiles_controller.rb
+   */
+  public QProfileActivityResult search(Map<String, Object> params) {
+    QProfileActivityQuery query = new QProfileActivityQuery();
+    QueryOptions queryOptions = new QueryOptions().setMaxLimit();
+    List<String> profileKeys = RubyUtils.toStrings(params.get("profileKeys"));
+    if (profileKeys != null) {
+      query.setQprofileKeys(profileKeys);
+    }
+    Date since = RubyUtils.toDate(params.get("since"));
+    if (since != null) {
+      query.setSince(since);
+    }
+    Date to = RubyUtils.toDate(params.get("to"));
+    if (to != null) {
+      query.setTo(to);
+    }
+    Integer page = RubyUtils.toInteger(params.get("p"));
+    int pageIndex = page != null ? page : 1;
+    queryOptions.setPage(pageIndex, 50);
+
+    Result<QProfileActivity> result = service.searchActivities(query, queryOptions);
+    return new QProfileActivityResult(result.getHits(), Paging.create(queryOptions.getLimit(), pageIndex, Long.valueOf(result.getTotal()).intValue()));
+  }
+
+  @Override
+  public void start() {
+    // used to force pico to instantiate the singleton at startup
+  }
+
+  @Override
+  public void stop() {
+    // implement startable
+  }
+
+  public static class QProfileActivityResult {
+
+    private final List<QProfileActivity> activities;
+
+    private final Paging paging;
+
+    public QProfileActivityResult(List<QProfileActivity> activities, Paging paging) {
+      this.activities = activities;
+      this.paging = paging;
+    }
+
+    public List<QProfileActivity> activities() {
+      return activities;
+    }
+
+    public Paging paging() {
+      return paging;
+    }
+
+  }
+}
index 40685bd9646ba7a430fcb3544aa76bf655f524b3..1bb53f1316c166665cd596bec61431e3861e65a5 100644 (file)
@@ -72,7 +72,7 @@ import org.sonar.jpa.session.DatabaseSessionProvider;
 import org.sonar.jpa.session.DefaultDatabaseConnector;
 import org.sonar.jpa.session.ThreadLocalDatabaseSessionFactory;
 import org.sonar.server.activity.ActivityService;
-import org.sonar.server.activity.RubyActivityService;
+import org.sonar.server.activity.RubyQProfileActivityService;
 import org.sonar.server.activity.db.ActivityDao;
 import org.sonar.server.activity.index.ActivityIndex;
 import org.sonar.server.activity.index.ActivityNormalizer;
@@ -320,7 +320,7 @@ class ServerComponents {
     pico.addSingleton(QProfileCopier.class);
     pico.addSingleton(QProfileBackuper.class);
     pico.addSingleton(QProfileReset.class);
-    pico.addSingleton(RubyActivityService.class);
+    pico.addSingleton(RubyQProfileActivityService.class);
 
     // rule
     pico.addSingleton(AnnotationRuleParser.class);
index 91a4e342fbeed4192bcabf0abd733d2c8d16feed..9ab2e196ba820f6e86aec8d622cd39d10945d4a8 100644 (file)
@@ -43,6 +43,7 @@ import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.search.FacetValue;
 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 javax.annotation.CheckForNull;
@@ -279,8 +280,7 @@ public class QProfileService implements ServerComponent {
       new QueryOptions().setLimit(0)).getTotal();
   }
 
-  public List<QProfileActivity> findActivities(QProfileActivityQuery query, QueryOptions options) {
-    List<QProfileActivity> results = Lists.newArrayList();
+  public Result<QProfileActivity> searchActivities(QProfileActivityQuery query, QueryOptions options) {
     DbSession session = db.openSession(false);
     try {
       OrFilterBuilder activityFilter = FilterBuilders.orFilter();
@@ -290,6 +290,7 @@ public class QProfileService implements ServerComponent {
       }
 
       SearchResponse response = index.get(ActivityIndex.class).search(query, options, activityFilter);
+      Result<QProfileActivity> result = new Result<QProfileActivity>(response);
       for (SearchHit hit : response.getHits().getHits()) {
         QProfileActivity profileActivity = new QProfileActivity(hit.getSource());
         RuleDto ruleDto = db.ruleDao().getNullableByKey(session, profileActivity.ruleKey());
@@ -297,9 +298,9 @@ public class QProfileService implements ServerComponent {
 
         UserDto user = db.userDao().selectActiveUserByLogin(profileActivity.login(), session);
         profileActivity.authorName(user != null ? user.getName() : null);
-        results.add(profileActivity);
+        result.getHits().add(profileActivity);
       }
-      return results;
+      return result;
     } finally {
       session.close();
     }
index 491fdfc088debd7d577bac57c01f9a331bde41fd..85a32c26cf4c887f594222e7752e13f185f60842 100644 (file)
@@ -29,12 +29,9 @@ import org.elasticsearch.search.aggregations.Aggregation;
 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 
 import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 public class Result<K> {
 
@@ -45,15 +42,21 @@ public class Result<K> {
   private final String scrollId;
   private final BaseIndex<K, ?, ?> index;
 
-  public Result(BaseIndex<K, ?, ?> index, SearchResponse response) {
+  public Result(SearchResponse response) {
+    this(null, response);
+  }
+
+  public Result(@Nullable BaseIndex<K, ?, ?> index, SearchResponse response) {
     this.index = index;
     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(index.toDoc(hit.getSource()));
+    if (index != null) {
+      for (SearchHit hit : response.getHits()) {
+        this.hits.add(index.toDoc(hit.getSource()));
+      }
     }
     if (response.getAggregations() != null) {
       for (Map.Entry<String, Aggregation> facet : response.getAggregations().asMap().entrySet()) {
index 04f4cd80389e46e06e5cbfb9a78da535fac06f6a..9c9d485beb8b5cfefa8d8a84eee2dc92bba5f0f4 100644 (file)
@@ -246,8 +246,10 @@ class ProfilesController < ApplicationController
     require_parameters 'key'
 
     @profile = Internal.component(Java::OrgSonarServerQualityprofile::QProfileService.java_class).getByKey(params[:key])
-    search = {'profileKeys' => @profile.key().to_s, 'since' => params[:since], 'to' => params[:to]}
-    @changes = Internal.component(Java::OrgSonarServerActivity::RubyActivityService.java_class).search(search)
+    search = {'profileKeys' => @profile.key().to_s, 'since' => params[:since], 'to' => params[:to], 'p' => params[:p]}
+    result = Internal.component(Java::OrgSonarServerActivity::RubyQProfileActivityService.java_class).search(search)
+    @changes = result.activities
+    @paging = result.paging
 
     set_profile_breadcrumbs
   end
index 77d8b728da82044d022851784775ea2d3a042e45..ce962dbc83f6f4dff4ee1700f02bdcdfd01720b1 100644 (file)
@@ -2,17 +2,18 @@
 <%= render :partial => 'profiles/tabs', :locals => {:selected_tab=>'changelog'} %>
 
 <div class="tabs-panel marginbottom10">
+  <form class="marginbottom10" method="get" action="<%= ApplicationController.root_context %>/profiles/changelog">
+    <input name="key" type="hidden" value="<%= @profile.key() %>"/>
+    <%= message('quality_profiles.changelog_from') -%>
+    <input name="since" type="text" value="<%= params['since'] %>" placeholder="1970-01-31"/>
+    <%= message('to').downcase -%>
+    <input name="to" type="text" value="<%= params['to'] %>" placeholder="1970-01-31"/>
+    <input type="submit" value="<%= h message('search_verb') -%>" id="submit"/>
+  </form>
+
   <% if @changes.empty? %>
     <%= message('quality_profiles.changelog.empty') -%>
   <% else %>
-    <form class="marginbottom10" method="get" action="<%= ApplicationController.root_context %>/profiles/changelog">
-      <input name="key" type="hidden" value="<%= @profile.key() %>"/>
-      <%= message('quality_profiles.changelog_from') -%>
-      <input name="since" type="text" value="<%= params['since'] %>" placeholder="1970-01-31"/>
-      <%= message('to').downcase -%>
-      <input name="to" type="text" value="<%= params['to'] %>" placeholder="1970-01-31"/>
-      <input type="submit" value="<%= h message('load_verb') -%>" id="submit"/>
-    </form>
 
     <table id="profile-changelog" class="data width100">
       <thead>
           <th><%= message('parameters') -%></th>
         </tr>
       </thead>
-      <%
-        @changes.each do |change|
-      %>
-      <tr class="<%= cycle('even', 'odd') -%>">
+      <tbody>
         <%
-           action = change.action()
-           action_message = message('quality_profiles.changelog.' + action.downcase) if action
-           author = change.authorName() ? change.authorName() : change.login() ? !change.login().emtpy? : 'System'
-           rule = change.ruleName() ? change.ruleName() : change.ruleKey()
+          @changes.each do |change|
         %>
-        <td valign="top" width="1%" nowrap><%= Internal.i18n.formatDateTime(change.time()) -%></td>
-        <td valign="top" width="1%" nowrap><%= author %></td>
-        <td valign="top" width="1%" nowrap><%= action_message %></td>
-        <td valign="top"><%= rule %></td>
-        <td valign="top">
-          <% if change.severity() %>
-            <%= message('quality_profiles.severity_set_to_x', :params => ["<i class=\"icon-severity-#{change.severity().downcase}\"></i>", change.severity()]) -%>
-            <br/>
-          <% end %>
-          <% change.parameters().each do |param_key, param_value| %>
-            <% unless param_value.empty? %>
-              <%= message('quality_profiles.parameter_set_to_x', :params => [param_key, param_value]) -%>
-            <% else %>
-              <%= message('quality_profiles.changelog.parameter_reset_to_default_value_x', :params => [param_key]) -%>
+        <tr class="<%= cycle('even', 'odd') -%>">
+          <%
+             action = change.action()
+             action_message = message('quality_profiles.changelog.' + action.downcase) if action
+             author = change.authorName() ? change.authorName() : change.login() ? !change.login().emtpy? : 'System'
+             rule = change.ruleName() ? change.ruleName() : change.ruleKey()
+          %>
+          <td valign="top" width="1%" nowrap><%= Internal.i18n.formatDateTime(change.time()) -%></td>
+          <td valign="top" width="1%" nowrap><%= author %></td>
+          <td valign="top" width="1%" nowrap><%= action_message %></td>
+          <td valign="top"><%= rule %></td>
+          <td valign="top">
+            <% if change.severity() %>
+              <%= message('quality_profiles.severity_set_to_x', :params => ["<i class=\"icon-severity-#{change.severity().downcase}\"></i>", change.severity()]) -%>
+              <br/>
+            <% end %>
+            <% change.parameters().each do |param_key, param_value| %>
+              <% unless param_value.empty? %>
+                <%= message('quality_profiles.parameter_set_to_x', :params => [param_key, param_value]) -%>
+              <% else %>
+                <%= message('quality_profiles.changelog.parameter_reset_to_default_value_x', :params => [param_key]) -%>
+              <% end %>
+              <br/>
             <% end %>
-            <br/>
-          <% end %>
-        </td>
-      </tr>
-      <% end %>
+          </td>
+        </tr>
+        <% end %>
+      </tbody>
+      <%= paginate_java(@paging, :colspan => 5, :include_loading_icon => true) { |label, page_id|
+        link_to(label, params.merge({:p => page_id}), :style => 'text-decoration:underline')
+      }
+      %>
     </table>
 
   <% end %>
diff --git a/sonar-server/src/test/java/org/sonar/server/activity/RubyActivityServiceTest.java b/sonar-server/src/test/java/org/sonar/server/activity/RubyActivityServiceTest.java
deleted file mode 100644 (file)
index c4617ba..0000000
+++ /dev/null
@@ -1,89 +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.activity;
-
-import com.google.common.collect.ImmutableMap;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.core.activity.Activity;
-import org.sonar.server.qualityprofile.QProfileActivityQuery;
-import org.sonar.server.qualityprofile.QProfileService;
-import org.sonar.server.search.QueryOptions;
-
-import java.util.Date;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.verify;
-
-@RunWith(MockitoJUnitRunner.class)
-public class RubyActivityServiceTest {
-
-  @Mock
-  QProfileService service;
-
-  @Captor
-  ArgumentCaptor<QProfileActivityQuery> activityArgumentCaptor;
-
-  @Captor
-  ArgumentCaptor<QueryOptions> queryOptionsArgumentCaptor;
-
-  RubyActivityService rubyActivityService;
-
-  @Before
-  public void setUp() throws Exception {
-    rubyActivityService = new RubyActivityService(service);
-  }
-
-  @Test
-  public void search() throws Exception {
-    Date since = DateUtils.parseDate("2014-05-19");
-    Date to = DateUtils.parseDate("2014-06-19");
-    rubyActivityService.search(ImmutableMap.<String, Object>of("profileKeys", "PROFILE_KEY", "since", since, "to", to));
-
-    verify(service).findActivities(activityArgumentCaptor.capture(), queryOptionsArgumentCaptor.capture());
-
-    assertThat(queryOptionsArgumentCaptor.getValue().getLimit()).isEqualTo(QueryOptions.MAX_LIMIT);
-
-    assertThat(activityArgumentCaptor.getValue().getQprofileKeys()).containsOnly("PROFILE_KEY");
-    assertThat(activityArgumentCaptor.getValue().getTypes()).containsOnly(Activity.Type.QPROFILE);
-    assertThat(activityArgumentCaptor.getValue().getSince()).isEqualTo(since);
-    assertThat(activityArgumentCaptor.getValue().getTo()).isEqualTo(to);
-  }
-
-  @Test
-  public void search_with_empty_fields() throws Exception {
-    rubyActivityService.search(ImmutableMap.<String, Object>of());
-
-    verify(service).findActivities(activityArgumentCaptor.capture(), queryOptionsArgumentCaptor.capture());
-
-    assertThat(queryOptionsArgumentCaptor.getValue().getLimit()).isEqualTo(QueryOptions.MAX_LIMIT);
-
-    assertThat(activityArgumentCaptor.getValue().getQprofileKeys()).isEmpty();
-    assertThat(activityArgumentCaptor.getValue().getSince()).isNull();
-    assertThat(activityArgumentCaptor.getValue().getTo()).isNull();
-  }
-}
diff --git a/sonar-server/src/test/java/org/sonar/server/activity/RubyQProfileActivityServiceTest.java b/sonar-server/src/test/java/org/sonar/server/activity/RubyQProfileActivityServiceTest.java
new file mode 100644 (file)
index 0000000..6a7d5cc
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * 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.activity;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.core.activity.Activity;
+import org.sonar.server.qualityprofile.QProfileActivity;
+import org.sonar.server.qualityprofile.QProfileActivityQuery;
+import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.server.search.QueryOptions;
+import org.sonar.server.search.Result;
+
+import java.util.Date;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.*;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RubyQProfileActivityServiceTest {
+
+  @Mock
+  QProfileService service;
+
+  @Captor
+  ArgumentCaptor<QProfileActivityQuery> activityArgumentCaptor;
+
+  @Captor
+  ArgumentCaptor<QueryOptions> queryOptionsArgumentCaptor;
+
+  RubyQProfileActivityService rubyQProfileActivityService;
+
+  @Before
+  public void setUp() throws Exception {
+    rubyQProfileActivityService = new RubyQProfileActivityService(service);
+  }
+
+  @Test
+  public void search() throws Exception {
+    Date since = DateUtils.parseDate("2014-05-19");
+    Date to = DateUtils.parseDate("2014-06-19");
+
+    Result<QProfileActivity> result = mock(Result.class);
+    when(result.getHits()).thenReturn(Lists.<QProfileActivity>newArrayList());
+    when(result.getTotal()).thenReturn(10L);
+    when(service.searchActivities(any(QProfileActivityQuery.class), any(QueryOptions.class))).thenReturn(result);
+
+    rubyQProfileActivityService.search(ImmutableMap.<String, Object>of("profileKeys", "PROFILE_KEY", "since", since, "to", to));
+
+    verify(service).searchActivities(activityArgumentCaptor.capture(), queryOptionsArgumentCaptor.capture());
+
+    assertThat(queryOptionsArgumentCaptor.getValue().getLimit()).isEqualTo(50);
+
+    assertThat(activityArgumentCaptor.getValue().getQprofileKeys()).containsOnly("PROFILE_KEY");
+    assertThat(activityArgumentCaptor.getValue().getTypes()).containsOnly(Activity.Type.QPROFILE);
+    assertThat(activityArgumentCaptor.getValue().getSince()).isEqualTo(since);
+    assertThat(activityArgumentCaptor.getValue().getTo()).isEqualTo(to);
+  }
+
+  @Test
+  public void search_with_empty_fields() throws Exception {
+    Result<QProfileActivity> result = mock(Result.class);
+    when(result.getHits()).thenReturn(Lists.<QProfileActivity>newArrayList());
+    when(result.getTotal()).thenReturn(10L);
+    when(service.searchActivities(any(QProfileActivityQuery.class), any(QueryOptions.class))).thenReturn(result);
+
+    rubyQProfileActivityService.search(ImmutableMap.<String, Object>of());
+
+    verify(service).searchActivities(activityArgumentCaptor.capture(), queryOptionsArgumentCaptor.capture());
+
+    assertThat(queryOptionsArgumentCaptor.getValue().getLimit()).isEqualTo(50);
+
+    assertThat(activityArgumentCaptor.getValue().getQprofileKeys()).isEmpty();
+    assertThat(activityArgumentCaptor.getValue().getSince()).isNull();
+    assertThat(activityArgumentCaptor.getValue().getTo()).isNull();
+  }
+}
index 72f743c0c8db1bb8dc6860aa21178c311ad8ce2f..184c4ca0232fd457db6e238b9cb2a394aa78ad5b 100644 (file)
@@ -40,6 +40,7 @@ import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
 import org.sonar.server.rule.RuleTesting;
 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 org.sonar.server.user.MockUserSession;
 
@@ -151,10 +152,10 @@ public class QProfileServiceMediumTest {
     );
     dbSession.commit();
 
-    List<QProfileActivity> activities = service.findActivities(new QProfileActivityQuery(), new QueryOptions());
-    assertThat(activities).hasSize(1);
+    Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new QueryOptions());
+    assertThat(activities.getHits()).hasSize(1);
 
-    QProfileActivity activity = activities.get(0);
+    QProfileActivity activity = activities.getHits().get(0);
     assertThat(activity.type()).isEqualTo(Activity.Type.QPROFILE);
     assertThat(activity.action()).isEqualTo(ActiveRuleChange.Type.ACTIVATED.name());
     assertThat(activity.ruleKey()).isEqualTo(RuleTesting.XOO_X1);
@@ -180,10 +181,10 @@ public class QProfileServiceMediumTest {
     );
     dbSession.commit();
 
-    List<QProfileActivity> activities = service.findActivities(new QProfileActivityQuery(), new QueryOptions());
-    assertThat(activities).hasSize(1);
+    Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new QueryOptions());
+    assertThat(activities.getHits()).hasSize(1);
 
-    QProfileActivity activity = activities.get(0);
+    QProfileActivity activity = activities.getHits().get(0);
     assertThat(activity.severity()).isNull();
     assertThat(activity.parameters()).hasSize(1);
     assertThat(activity.parameters().get("max")).isEqualTo("10");
@@ -203,10 +204,10 @@ public class QProfileServiceMediumTest {
     );
     dbSession.commit();
 
-    List<QProfileActivity> activities = service.findActivities(new QProfileActivityQuery(), new QueryOptions());
-    assertThat(activities).hasSize(1);
+    Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new QueryOptions());
+    assertThat(activities.getHits()).hasSize(1);
 
-    QProfileActivity activity = activities.get(0);
+    QProfileActivity activity = activities.getHits().get(0);
     assertThat(activity.login()).isEqualTo("david");
     assertThat(activity.authorName()).isNull();
   }
@@ -224,10 +225,10 @@ public class QProfileServiceMediumTest {
     );
     dbSession.commit();
 
-    List<QProfileActivity> activities = service.findActivities(new QProfileActivityQuery(), new QueryOptions());
-    assertThat(activities).hasSize(1);
+    Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new QueryOptions());
+    assertThat(activities.getHits()).hasSize(1);
 
-    QProfileActivity activity = activities.get(0);
+    QProfileActivity activity = activities.getHits().get(0);
     assertThat(activity.ruleKey()).isEqualTo(ruleKey);
     assertThat(activity.ruleName()).isNull();
   }
@@ -242,18 +243,18 @@ public class QProfileServiceMediumTest {
     dbSession.commit();
 
     // 0. Base case verify 2 activities in index
-    assertThat(service.findActivities(new QProfileActivityQuery(), new QueryOptions()))
+    assertThat(service.searchActivities(new QProfileActivityQuery(), new QueryOptions()).getHits())
       .hasSize(2);
 
     // 1. filter by QProfile
-    List<QProfileActivity> result = service.findActivities(new QProfileActivityQuery()
-      .setQprofileKeys(ImmutableSet.of(XOO_P1_KEY)), new QueryOptions());
+    List<QProfileActivity> result = service.searchActivities(new QProfileActivityQuery()
+      .setQprofileKeys(ImmutableSet.of(XOO_P1_KEY)), new QueryOptions()).getHits();
     assertThat(result).hasSize(1);
 
     // 1. filter by QProfiles
-    assertThat(service.findActivities(new QProfileActivityQuery()
+    assertThat(service.searchActivities(new QProfileActivityQuery()
       .setQprofileKeys(ImmutableSet.of(XOO_P1_KEY, XOO_P2_KEY))
-      , new QueryOptions())).hasSize(2);
+      , new QueryOptions()).getHits()).hasSize(2);
   }
 
   @Test
@@ -265,18 +266,18 @@ public class QProfileServiceMediumTest {
     dbSession.commit();
 
     // 0. Base case verify 2 activities in index
-    assertThat(service.findActivities(new QProfileActivityQuery(), new QueryOptions()))
+    assertThat(service.searchActivities(new QProfileActivityQuery(), new QueryOptions()).getHits())
       .hasSize(2);
 
     // 1. filter by QProfile
-    List<QProfileActivity> result = service.findActivities(new QProfileActivityQuery()
-      .setQprofileKeys(ImmutableSet.of("java-default")), new QueryOptions());
+    List<QProfileActivity> result = service.searchActivities(new QProfileActivityQuery()
+      .setQprofileKeys(ImmutableSet.of("java-default")), new QueryOptions()).getHits();
     assertThat(result).hasSize(1);
 
     // 1. filter by QProfiles
-    assertThat(service.findActivities(new QProfileActivityQuery()
+    assertThat(service.searchActivities(new QProfileActivityQuery()
       .setQprofileKeys(ImmutableSet.of("java-default", "java-toto"))
-      , new QueryOptions())).hasSize(2);
+      , new QueryOptions()).getHits()).hasSize(2);
   }
 
 }