]> source.dussan.org Git - sonarqube.git/commitdiff
Refactoring of package org.sonar.server.computation.ws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 7 Jan 2015 14:32:41 +0000 (15:32 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 7 Jan 2015 14:46:15 +0000 (15:46 +0100)
- class renaming
- add class ComputationWsAction to simplify ComputationWebService

23 files changed:
server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActiveAnalysisReportsAction.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/AnalysisReportHistorySearchAction.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/AnalysisReportWebService.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWebService.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/HistoryWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsAnalysisReportQueueEmptyAction.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/QueueWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/package-info.java
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActiveAnalysisReportsActionTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/AnalysisReportHistorySearchActionMediumTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/AnalysisReportWebServiceTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComputationWebServiceTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryWsActionMediumTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsAnalysisReportQueueEmptyActionTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsQueueEmptyWsActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueWsActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/computation/ws/ActiveAnalysisReportsActionTest/list_active_reports.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/computation/ws/AnalysisReportHistorySearchActionMediumTest/list_history_reports.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/computation/ws/HistoryWsActionMediumTest/list_history_reports.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/computation/ws/QueueWsActionTest/list_active_reports.json [new file with mode: 0644]

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActiveAnalysisReportsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActiveAnalysisReportsAction.java
deleted file mode 100644 (file)
index 71186dc..0000000
+++ /dev/null
@@ -1,78 +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.computation.ws;
-
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.RequestHandler;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.computation.db.AnalysisReportDto;
-import org.sonar.server.computation.AnalysisReportQueue;
-
-import java.util.List;
-
-/**
- * @since 5.0
- */
-public class ActiveAnalysisReportsAction implements RequestHandler {
-  private final AnalysisReportQueue queue;
-
-  public ActiveAnalysisReportsAction(AnalysisReportQueue queue) {
-    this.queue = queue;
-  }
-
-  @Override
-  public void handle(Request request, Response response) throws Exception {
-    List<AnalysisReportDto> reports = queue.all();
-
-    JsonWriter json = response.newJsonWriter().beginObject();
-    writeReports(reports, json);
-    json.endObject();
-    json.close();
-  }
-
-  private void writeReports(List<AnalysisReportDto> reports, JsonWriter json) {
-    json.name("reports").beginArray();
-    for (AnalysisReportDto report : reports) {
-      json.beginObject();
-      json.prop("id", report.getId());
-      json.prop("project", report.getProjectKey());
-      json.prop("projectName", report.getProjectKey());
-      json.propDateTime("startedAt", report.getStartedAt());
-      json.propDateTime("finishedAt", report.getFinishedAt());
-      json.propDateTime("submittedAt", report.getCreatedAt());
-      json.prop("status", report.getStatus().toString());
-      json.endObject();
-    }
-    json.endArray();
-  }
-
-  void define(WebService.NewController controller) {
-    controller
-      .createAction("active")
-      .setDescription("List all the active analysis reports")
-      .setSince("5.0")
-      .setInternal(true)
-      .setHandler(this);
-  }
-
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/AnalysisReportHistorySearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/AnalysisReportHistorySearchAction.java
deleted file mode 100644 (file)
index 7417e39..0000000
+++ /dev/null
@@ -1,101 +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.computation.ws;
-
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.RequestHandler;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.activity.Activity;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.server.activity.ActivityService;
-import org.sonar.server.activity.index.ActivityQuery;
-import org.sonar.server.activity.ws.ActivityMapping;
-import org.sonar.server.search.QueryContext;
-import org.sonar.server.search.Result;
-import org.sonar.server.search.ws.SearchOptions;
-import org.sonar.server.user.UserSession;
-
-import java.util.Arrays;
-import java.util.Map;
-
-public class AnalysisReportHistorySearchAction implements RequestHandler {
-
-  public static final String PARAM_TYPE = "type";
-
-  public static final String SEARCH_ACTION = "history";
-
-  private final ActivityService logService;
-  private final ActivityMapping mapping;
-
-  public AnalysisReportHistorySearchAction(ActivityService logService, ActivityMapping mapping) {
-    this.logService = logService;
-    this.mapping = mapping;
-  }
-
-  void define(WebService.NewController controller) {
-    WebService.NewAction action = controller
-      .createAction(SEARCH_ACTION)
-      .setDescription("Search for activities")
-      .setSince("5.0")
-      .setInternal(true)
-      .setHandler(this);
-
-    // Generic search parameters
-    SearchOptions.defineFieldsParam(action, mapping.supportedFields());
-    SearchOptions.definePageParams(action);
-  }
-
-  @Override
-  public void handle(Request request, Response response) {
-    checkUserRights();
-
-    ActivityQuery query = logService.newActivityQuery();
-    query.setTypes(Arrays.asList(Activity.Type.ANALYSIS_REPORT));
-
-    SearchOptions searchOptions = SearchOptions.create(request);
-    QueryContext queryContext = mapping.newQueryOptions(searchOptions);
-
-    Result<Activity> results = logService.search(query, queryContext);
-
-    JsonWriter json = response.newJsonWriter().beginObject();
-    searchOptions.writeStatistics(json, results);
-    writeReports(results, json);
-    json.endObject().close();
-  }
-
-  private void checkUserRights() {
-    UserSession.get().checkGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
-  }
-
-  private void writeReports(Result<Activity> result, JsonWriter json) {
-    json.name("reports").beginArray();
-    for (Activity reportActivity : result.getHits()) {
-      json.beginObject();
-      for (Map.Entry<String, String> detail : reportActivity.details().entrySet()) {
-        json.prop(detail.getKey(), detail.getValue());
-      }
-      json.endObject();
-    }
-    json.endArray();
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/AnalysisReportWebService.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/AnalysisReportWebService.java
deleted file mode 100644 (file)
index 78fbd10..0000000
+++ /dev/null
@@ -1,51 +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.computation.ws;
-
-import org.sonar.api.server.ws.WebService;
-
-public class AnalysisReportWebService implements WebService {
-  public static final String API_ENDPOINT = "api/analysis_reports";
-
-  private final ActiveAnalysisReportsAction activeAnalysisReportsAction;
-  private final IsAnalysisReportQueueEmptyAction isAnalysisReportQueueEmptyAction;
-  private final AnalysisReportHistorySearchAction historySearchAction;
-
-  public AnalysisReportWebService(ActiveAnalysisReportsAction activeReports, IsAnalysisReportQueueEmptyAction isAnalysisReportQueueEmptyAction,
-    AnalysisReportHistorySearchAction historySearchAction) {
-    this.activeAnalysisReportsAction = activeReports;
-    this.isAnalysisReportQueueEmptyAction = isAnalysisReportQueueEmptyAction;
-    this.historySearchAction = historySearchAction;
-  }
-
-  @Override
-  public void define(Context context) {
-    NewController controller = context
-      .createController(API_ENDPOINT)
-      .setDescription("Analysis reports processed");
-
-    activeAnalysisReportsAction.define(controller);
-    isAnalysisReportQueueEmptyAction.define(controller);
-    historySearchAction.define(controller);
-
-    controller.done();
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWebService.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWebService.java
new file mode 100644 (file)
index 0000000..62f579c
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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.computation.ws;
+
+import org.sonar.api.server.ws.WebService;
+
+/**
+ * Web service to interact with the "computation" stack :
+ * <ul>
+ *   <li>queue of analysis reports to be integrated</li>
+ *   <li>consolidation and aggregation of analysis measures</li>
+ *   <li>persistence in datastores (database/elasticsearch)</li>
+ * </ul>
+ */
+public class ComputationWebService implements WebService {
+  public static final String API_ENDPOINT = "api/analysis_reports";
+
+  private final ComputationWsAction[] actions;
+
+  public ComputationWebService(ComputationWsAction... actions) {
+    this.actions = actions;
+  }
+
+  @Override
+  public void define(Context context) {
+    NewController controller = context
+      .createController(API_ENDPOINT)
+      .setDescription("Analysis reports processed");
+    for (ComputationWsAction action : actions) {
+      action.define(controller);
+    }
+    controller.done();
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWsAction.java
new file mode 100644 (file)
index 0000000..4cf4afc
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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.computation.ws;
+
+import org.sonar.api.server.ws.WebService;
+
+/**
+ * Used by {@link ComputationWebService} to
+ * loop over all its actions
+ */
+interface ComputationWsAction {
+  void define(WebService.NewController controller);
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/HistoryWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/HistoryWsAction.java
new file mode 100644 (file)
index 0000000..b1448a5
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * 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.computation.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.activity.Activity;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.activity.ActivityService;
+import org.sonar.server.activity.index.ActivityQuery;
+import org.sonar.server.activity.ws.ActivityMapping;
+import org.sonar.server.search.QueryContext;
+import org.sonar.server.search.Result;
+import org.sonar.server.search.ws.SearchOptions;
+import org.sonar.server.user.UserSession;
+
+import java.util.Arrays;
+import java.util.Map;
+
+public class HistoryWsAction implements ComputationWsAction, RequestHandler {
+
+  public static final String PARAM_TYPE = "type";
+
+  public static final String SEARCH_ACTION = "history";
+
+  private final ActivityService logService;
+  private final ActivityMapping mapping;
+
+  public HistoryWsAction(ActivityService logService, ActivityMapping mapping) {
+    this.logService = logService;
+    this.mapping = mapping;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    WebService.NewAction action = controller
+      .createAction(SEARCH_ACTION)
+      .setDescription("Search for activities")
+      .setSince("5.0")
+      .setInternal(true)
+      .setHandler(this);
+
+    // Generic search parameters
+    SearchOptions.defineFieldsParam(action, mapping.supportedFields());
+    SearchOptions.definePageParams(action);
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    checkUserRights();
+
+    ActivityQuery query = logService.newActivityQuery();
+    query.setTypes(Arrays.asList(Activity.Type.ANALYSIS_REPORT));
+
+    SearchOptions searchOptions = SearchOptions.create(request);
+    QueryContext queryContext = mapping.newQueryOptions(searchOptions);
+
+    Result<Activity> results = logService.search(query, queryContext);
+
+    JsonWriter json = response.newJsonWriter().beginObject();
+    searchOptions.writeStatistics(json, results);
+    writeReports(results, json);
+    json.endObject().close();
+  }
+
+  private void checkUserRights() {
+    UserSession.get().checkGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
+  }
+
+  private void writeReports(Result<Activity> result, JsonWriter json) {
+    json.name("reports").beginArray();
+    for (Activity reportActivity : result.getHits()) {
+      json.beginObject();
+      for (Map.Entry<String, String> detail : reportActivity.details().entrySet()) {
+        json.prop(detail.getKey(), detail.getValue());
+      }
+      json.endObject();
+    }
+    json.endArray();
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsAnalysisReportQueueEmptyAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsAnalysisReportQueueEmptyAction.java
deleted file mode 100644 (file)
index fe7bbd0..0000000
+++ /dev/null
@@ -1,55 +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.computation.ws;
-
-import org.apache.commons.io.IOUtils;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.RequestHandler;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.core.computation.db.AnalysisReportDto;
-import org.sonar.server.computation.AnalysisReportQueue;
-
-import java.util.List;
-
-public class IsAnalysisReportQueueEmptyAction implements RequestHandler {
-  private final AnalysisReportQueue queue;
-
-  public IsAnalysisReportQueueEmptyAction(AnalysisReportQueue queue) {
-    this.queue = queue;
-  }
-
-  void define(WebService.NewController controller) {
-    controller
-      .createAction("is_queue_empty")
-      .setDescription("Check is the analysis report queue is empty")
-      .setInternal(true)
-      .setHandler(this);
-  }
-
-  @Override
-  public void handle(Request request, Response response) throws Exception {
-    List<AnalysisReportDto> reports = queue.all();
-    boolean isQueueEmpty = reports.isEmpty();
-
-    IOUtils.write(String.valueOf(isQueueEmpty), response.stream().output());
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWsAction.java
new file mode 100644 (file)
index 0000000..1870d6b
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.computation.ws;
+
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.core.computation.db.AnalysisReportDto;
+import org.sonar.server.computation.AnalysisReportQueue;
+
+import java.util.List;
+
+public class IsQueueEmptyWsAction implements ComputationWsAction, RequestHandler {
+  private final AnalysisReportQueue queue;
+
+  public IsQueueEmptyWsAction(AnalysisReportQueue queue) {
+    this.queue = queue;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    controller
+      .createAction("is_queue_empty")
+      .setDescription("Check if the analysis report queue is empty")
+      .setInternal(true)
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    List<AnalysisReportDto> reports = queue.all();
+    boolean isQueueEmpty = reports.isEmpty();
+
+    IOUtils.write(String.valueOf(isQueueEmpty), response.stream().output());
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/QueueWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/QueueWsAction.java
new file mode 100644 (file)
index 0000000..d488814
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * 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.computation.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.computation.db.AnalysisReportDto;
+import org.sonar.server.computation.AnalysisReportQueue;
+
+import java.util.List;
+
+/**
+ * @since 5.0
+ */
+public class QueueWsAction implements ComputationWsAction, RequestHandler {
+  private final AnalysisReportQueue queue;
+
+  public QueueWsAction(AnalysisReportQueue queue) {
+    this.queue = queue;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    controller
+      .createAction("active")
+      .setDescription("List all the active analysis reports")
+      .setSince("5.0")
+      .setInternal(true)
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    List<AnalysisReportDto> reports = queue.all();
+
+    JsonWriter json = response.newJsonWriter().beginObject();
+    writeReports(reports, json);
+    json.endObject();
+    json.close();
+  }
+
+  private void writeReports(List<AnalysisReportDto> reports, JsonWriter json) {
+    json.name("reports").beginArray();
+    for (AnalysisReportDto report : reports) {
+      json.beginObject();
+      // TODO naming consistency -> rename id to key
+      json.prop("id", report.getId());
+      // TODO rename to projectKey
+      json.prop("project", report.getProjectKey());
+      json.prop("projectName", report.getProjectKey());
+      json.propDateTime("startedAt", report.getStartedAt());
+      json.propDateTime("finishedAt", report.getFinishedAt());
+      json.propDateTime("submittedAt", report.getCreatedAt());
+      json.prop("status", report.getStatus().toString());
+      json.endObject();
+    }
+    json.endArray();
+  }
+
+}
index 6e275239e42557099ee29aaf1a59419ab6d7d50a..fe9ed4cc11e4b7d5c3e6c63356deab2c89f1abe8 100644 (file)
@@ -22,3 +22,4 @@
 package org.sonar.server.computation.ws;
 
 import javax.annotation.ParametersAreNonnullByDefault;
+
index ebc918253cc6f2d84a941c11833fe71f4bc6b624..17ccbaaba93b88bdd1447c1d515821126268c69b 100644 (file)
@@ -92,10 +92,10 @@ import org.sonar.server.component.ws.*;
 import org.sonar.server.computation.*;
 import org.sonar.server.computation.db.AnalysisReportDao;
 import org.sonar.server.computation.step.*;
-import org.sonar.server.computation.ws.ActiveAnalysisReportsAction;
-import org.sonar.server.computation.ws.AnalysisReportHistorySearchAction;
-import org.sonar.server.computation.ws.AnalysisReportWebService;
-import org.sonar.server.computation.ws.IsAnalysisReportQueueEmptyAction;
+import org.sonar.server.computation.ws.QueueWsAction;
+import org.sonar.server.computation.ws.HistoryWsAction;
+import org.sonar.server.computation.ws.ComputationWebService;
+import org.sonar.server.computation.ws.IsQueueEmptyWsAction;
 import org.sonar.server.config.ws.PropertiesWs;
 import org.sonar.server.dashboard.db.DashboardDao;
 import org.sonar.server.dashboard.db.WidgetDao;
@@ -636,10 +636,10 @@ class ServerComponents {
     pico.addSingleton(AnalysisReportService.class);
     pico.addSingleton(AnalysisReportQueue.class);
     pico.addSingleton(AnalysisReportTaskLauncher.class);
-    pico.addSingleton(AnalysisReportWebService.class);
-    pico.addSingleton(ActiveAnalysisReportsAction.class);
-    pico.addSingleton(IsAnalysisReportQueueEmptyAction.class);
-    pico.addSingleton(AnalysisReportHistorySearchAction.class);
+    pico.addSingleton(ComputationWebService.class);
+    pico.addSingleton(QueueWsAction.class);
+    pico.addSingleton(IsQueueEmptyWsAction.class);
+    pico.addSingleton(HistoryWsAction.class);
     pico.addSingleton(DefaultPeriodCleaner.class);
     pico.addSingleton(DefaultPurgeTask.class);
     pico.addSingleton(ProjectCleaner.class);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActiveAnalysisReportsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActiveAnalysisReportsActionTest.java
deleted file mode 100644 (file)
index f817a19..0000000
+++ /dev/null
@@ -1,71 +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.computation.ws;
-
-import com.google.common.collect.Lists;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.core.computation.db.AnalysisReportDto;
-import org.sonar.server.computation.AnalysisReportQueue;
-import org.sonar.server.ws.WsTester;
-
-import java.util.List;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.sonar.core.computation.db.AnalysisReportDto.Status.PENDING;
-
-public class ActiveAnalysisReportsActionTest {
-
-  WsTester tester;
-  private AnalysisReportQueue queue;
-
-  @Before
-  public void setup() throws Exception {
-    queue = mock(AnalysisReportQueue.class);
-    tester = new WsTester(new AnalysisReportWebService(new ActiveAnalysisReportsAction(queue), new IsAnalysisReportQueueEmptyAction(queue),
-      mock(AnalysisReportHistorySearchAction.class)));
-  }
-
-  @Test
-  public void list_active_reports() throws Exception {
-    AnalysisReportDto report = AnalysisReportDto
-      .newForTests(1L)
-      .setProjectKey("project-name")
-      .setStatus(PENDING)
-      .setData(null)
-      .setCreatedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"))
-      .setStartedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"))
-      .setFinishedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"));
-    List<AnalysisReportDto> reports = Lists.newArrayList(report);
-    when(queue.all()).thenReturn(reports);
-
-    WsTester.TestRequest request = tester.newGetRequest(AnalysisReportWebService.API_ENDPOINT, "active");
-    request.execute().assertJson(getClass(), "list_active_reports.json", false);
-  }
-
-  @Test
-  public void define() throws Exception {
-    assertThat(tester.controller(AnalysisReportWebService.API_ENDPOINT).action("active")).isNotNull();
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/AnalysisReportHistorySearchActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/AnalysisReportHistorySearchActionMediumTest.java
deleted file mode 100644 (file)
index b25a1f5..0000000
+++ /dev/null
@@ -1,144 +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.computation.ws;
-
-import org.apache.commons.io.IOUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.sonar.api.security.DefaultGroups;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.activity.Activity;
-import org.sonar.core.component.ComponentDto;
-import org.sonar.core.computation.db.AnalysisReportDto;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.core.permission.PermissionFacade;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.user.UserDto;
-import org.sonar.server.activity.ActivityService;
-import org.sonar.server.component.ComponentTesting;
-import org.sonar.server.computation.AnalysisReportLog;
-import org.sonar.server.computation.AnalysisReportQueue;
-import org.sonar.server.computation.ComputationService;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.tester.ServerTester;
-import org.sonar.server.user.MockUserSession;
-import org.sonar.server.ws.WsTester;
-
-import java.util.List;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class AnalysisReportHistorySearchActionMediumTest {
-  private static final String DEFAULT_PROJECT_KEY = "DefaultProjectKey";
-  private static final String DEFAULT_PROJECT_NAME = "DefaultProjectName";
-  private static final String DEFAULT_REPORT_DATA = "default-project";
-
-  @ClassRule
-  public static ServerTester tester = new ServerTester();
-
-  private DbClient dbClient;
-  private DbSession session;
-  private WsTester wsTester;
-  private AnalysisReportQueue queue;
-  private MockUserSession userSession;
-  private ComputationService computationService;
-  private ActivityService activityService;
-
-  @Before
-  public void before() {
-    tester.clearDbAndIndexes();
-    dbClient = tester.get(DbClient.class);
-    wsTester = tester.get(WsTester.class);
-    session = dbClient.openSession(false);
-    queue = tester.get(AnalysisReportQueue.class);
-    activityService = tester.get(ActivityService.class);
-
-    UserDto connectedUser = new UserDto().setLogin("gandalf").setName("Gandalf");
-    dbClient.userDao().insert(session, connectedUser);
-
-    userSession = MockUserSession.set()
-      .setLogin(connectedUser.getLogin())
-      .setUserId(connectedUser.getId().intValue())
-      .setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-  }
-
-  @After
-  public void after() {
-    MyBatis.closeQuietly(session);
-  }
-
-  @Test
-  public void add_and_try_to_retrieve_activities() throws Exception {
-    insertPermissionsForProject(DEFAULT_PROJECT_KEY);
-    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
-    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
-    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
-
-    List<AnalysisReportDto> reports = queue.all();
-    ComponentDto project = ComponentTesting.newProjectDto()
-      .setName(DEFAULT_PROJECT_NAME)
-      .setKey(DEFAULT_PROJECT_KEY);
-    for (AnalysisReportDto report : reports) {
-      report.succeed();
-      activityService.write(session, Activity.Type.ANALYSIS_REPORT, new AnalysisReportLog(report, project));
-    }
-
-    session.commit();
-    userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
-
-    WsTester.TestRequest request = wsTester.newGetRequest(AnalysisReportWebService.API_ENDPOINT, AnalysisReportHistorySearchAction.SEARCH_ACTION);
-    WsTester.Result result = request.execute();
-
-    assertThat(result).isNotNull();
-    result.assertJson(getClass(), "list_history_reports.json", false);
-  }
-
-  private ComponentDto insertPermissionsForProject(String projectKey) {
-    ComponentDto project = new ComponentDto().setKey(projectKey).setId(1L);
-    dbClient.componentDao().insert(session, project);
-
-    tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), DefaultGroups.ANYONE, UserRole.USER, session);
-    userSession.addProjectPermissions(UserRole.ADMIN, project.key());
-    userSession.addProjectPermissions(UserRole.USER, project.key());
-
-    session.commit();
-
-    return project;
-  }
-
-  @Test(expected = ForbiddenException.class)
-  public void user_rights_is_not_enough_throw_ForbiddenException() throws Exception {
-    insertPermissionsForProject(DEFAULT_PROJECT_KEY);
-    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
-
-    AnalysisReportDto report = queue.all().get(0);
-    report.succeed();
-    queue.remove(report);
-    userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
-
-    WsTester.TestRequest sut = wsTester.newGetRequest(AnalysisReportWebService.API_ENDPOINT, AnalysisReportHistorySearchAction.SEARCH_ACTION);
-    sut.execute();
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/AnalysisReportWebServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/AnalysisReportWebServiceTest.java
deleted file mode 100644 (file)
index 40cf08e..0000000
+++ /dev/null
@@ -1,52 +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.computation.ws;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.server.activity.ActivityService;
-import org.sonar.server.activity.ws.ActivityMapping;
-import org.sonar.server.computation.AnalysisReportQueue;
-import org.sonar.server.ws.WsTester;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
-public class AnalysisReportWebServiceTest {
-
-  private WsTester tester;
-
-  @Before
-  public void setUp() throws Exception {
-    this.tester = new WsTester(new AnalysisReportWebService(new ActiveAnalysisReportsAction(mock(AnalysisReportQueue.class)), new IsAnalysisReportQueueEmptyAction(
-      mock(AnalysisReportQueue.class)), new AnalysisReportHistorySearchAction(mock(ActivityService.class), mock(ActivityMapping.class))));
-  }
-
-  @Test
-  public void define() throws Exception {
-    WebService.Controller controller = tester.controller(AnalysisReportWebService.API_ENDPOINT);
-
-    assertThat(controller).isNotNull();
-    assertThat(controller.description()).isNotEmpty();
-    assertThat(controller.actions()).hasSize(3);
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComputationWebServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComputationWebServiceTest.java
new file mode 100644 (file)
index 0000000..bec6203
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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.computation.ws;
+
+import org.junit.Test;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.WebService;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class ComputationWebServiceTest {
+
+  @Test
+  public void define() throws Exception {
+    ComputationWebService ws = new ComputationWebService(new ComputationWsAction() {
+      @Override
+      public void define(WebService.NewController controller) {
+        WebService.NewAction upload = controller.createAction("upload");
+        upload.setHandler(mock(RequestHandler.class));
+      }
+    });
+    WebService.Context context = new WebService.Context();
+    ws.define(context);
+
+    WebService.Controller controller = context.controller("api/analysis_reports");
+    assertThat(controller).isNotNull();
+    assertThat(controller.description()).isNotEmpty();
+    assertThat(controller.actions()).hasSize(1);
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryWsActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/HistoryWsActionMediumTest.java
new file mode 100644 (file)
index 0000000..a5129dd
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * 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.computation.ws;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.security.DefaultGroups;
+import org.sonar.api.web.UserRole;
+import org.sonar.core.activity.Activity;
+import org.sonar.core.component.ComponentDto;
+import org.sonar.core.computation.db.AnalysisReportDto;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.permission.PermissionFacade;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.user.UserDto;
+import org.sonar.server.activity.ActivityService;
+import org.sonar.server.component.ComponentTesting;
+import org.sonar.server.computation.AnalysisReportLog;
+import org.sonar.server.computation.AnalysisReportQueue;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.tester.ServerTester;
+import org.sonar.server.user.MockUserSession;
+import org.sonar.server.ws.WsTester;
+
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+/**
+ * TODO replace this medium test by a small test
+ */
+public class HistoryWsActionMediumTest {
+  private static final String DEFAULT_PROJECT_KEY = "DefaultProjectKey";
+  private static final String DEFAULT_PROJECT_NAME = "DefaultProjectName";
+  private static final String DEFAULT_REPORT_DATA = "default-project";
+
+  @ClassRule
+  public static ServerTester tester = new ServerTester();
+
+  private DbClient dbClient;
+  private DbSession session;
+  private WsTester wsTester;
+  private AnalysisReportQueue queue;
+  private MockUserSession userSession;
+  private ActivityService activityService;
+
+  @Before
+  public void before() {
+    tester.clearDbAndIndexes();
+    dbClient = tester.get(DbClient.class);
+    wsTester = tester.get(WsTester.class);
+    session = dbClient.openSession(false);
+    queue = tester.get(AnalysisReportQueue.class);
+    activityService = tester.get(ActivityService.class);
+
+    UserDto connectedUser = new UserDto().setLogin("gandalf").setName("Gandalf");
+    dbClient.userDao().insert(session, connectedUser);
+
+    userSession = MockUserSession.set()
+      .setLogin(connectedUser.getLogin())
+      .setUserId(connectedUser.getId().intValue())
+      .setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
+  }
+
+  @After
+  public void after() {
+    MyBatis.closeQuietly(session);
+  }
+
+  @Test
+  public void add_and_try_to_retrieve_activities() throws Exception {
+    insertPermissionsForProject(DEFAULT_PROJECT_KEY);
+    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
+    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
+    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
+
+    List<AnalysisReportDto> reports = queue.all();
+    ComponentDto project = ComponentTesting.newProjectDto()
+      .setName(DEFAULT_PROJECT_NAME)
+      .setKey(DEFAULT_PROJECT_KEY);
+    for (AnalysisReportDto report : reports) {
+      report.succeed();
+      activityService.write(session, Activity.Type.ANALYSIS_REPORT, new AnalysisReportLog(report, project));
+    }
+
+    session.commit();
+    userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+
+    WsTester.TestRequest request = wsTester.newGetRequest(ComputationWebService.API_ENDPOINT, HistoryWsAction.SEARCH_ACTION);
+    WsTester.Result result = request.execute();
+
+    assertThat(result).isNotNull();
+    result.assertJson(getClass(), "list_history_reports.json", false);
+  }
+
+  private ComponentDto insertPermissionsForProject(String projectKey) {
+    ComponentDto project = new ComponentDto().setKey(projectKey).setId(1L);
+    dbClient.componentDao().insert(session, project);
+
+    tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), DefaultGroups.ANYONE, UserRole.USER, session);
+    userSession.addProjectPermissions(UserRole.ADMIN, project.key());
+    userSession.addProjectPermissions(UserRole.USER, project.key());
+
+    session.commit();
+
+    return project;
+  }
+
+  @Test(expected = ForbiddenException.class)
+  public void user_rights_is_not_enough_throw_ForbiddenException() throws Exception {
+    insertPermissionsForProject(DEFAULT_PROJECT_KEY);
+    queue.add(DEFAULT_PROJECT_KEY, 123L, IOUtils.toInputStream(DEFAULT_REPORT_DATA));
+
+    AnalysisReportDto report = queue.all().get(0);
+    report.succeed();
+    queue.remove(report);
+    userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
+
+    WsTester.TestRequest sut = wsTester.newGetRequest(ComputationWebService.API_ENDPOINT, HistoryWsAction.SEARCH_ACTION);
+    sut.execute();
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsAnalysisReportQueueEmptyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsAnalysisReportQueueEmptyActionTest.java
deleted file mode 100644 (file)
index bca8ac9..0000000
+++ /dev/null
@@ -1,98 +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.computation.ws;
-
-import com.google.common.collect.Lists;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.core.computation.db.AnalysisReportDto;
-import org.sonar.server.computation.AnalysisReportQueue;
-
-import java.io.ByteArrayOutputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class IsAnalysisReportQueueEmptyActionTest {
-
-  IsAnalysisReportQueueEmptyAction sut;
-  AnalysisReportQueue queue;
-  Response response;
-
-  @Before
-  public void before() throws Exception {
-    queue = mock(AnalysisReportQueue.class);
-    sut = new IsAnalysisReportQueueEmptyAction(queue);
-
-    response = mock(Response.class);
-    when(response.stream()).thenReturn(new FakeStream());
-  }
-
-  @Test
-  public void send_true_when_queue_is_empty() throws Exception {
-    when(queue.all()).thenReturn(new ArrayList<AnalysisReportDto>());
-
-    sut.handle(mock(Request.class), response);
-
-    assertThat(response.stream().toString()).isEqualTo("true");
-  }
-
-  @Test
-  public void send_false_when_queue_is_not_empty() throws Exception {
-    when(queue.all()).thenReturn(Lists.newArrayList(AnalysisReportDto.newForTests(1L)));
-
-    sut.handle(mock(Request.class), response);
-
-    assertThat(response.stream().toString()).isEqualTo("false");
-  }
-
-  private class FakeStream implements Response.Stream {
-    private ByteArrayOutputStream stream;
-
-    private FakeStream() {
-      this.stream = new ByteArrayOutputStream();
-    }
-
-    public String toString() {
-      return stream.toString();
-    }
-
-    @Override
-    public Response.Stream setMediaType(String s) {
-      return null;
-    }
-
-    @Override
-    public Response.Stream setStatus(int httpStatus) {
-      return null;
-    }
-
-    @Override
-    public OutputStream output() {
-      return stream;
-    }
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsQueueEmptyWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsQueueEmptyWsActionTest.java
new file mode 100644 (file)
index 0000000..3882643
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * 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.computation.ws;
+
+import com.google.common.collect.Lists;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.core.computation.db.AnalysisReportDto;
+import org.sonar.server.computation.AnalysisReportQueue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class IsQueueEmptyWsActionTest {
+
+  IsQueueEmptyWsAction sut;
+  AnalysisReportQueue queue;
+  Response response;
+
+  @Before
+  public void before() throws Exception {
+    queue = mock(AnalysisReportQueue.class);
+    sut = new IsQueueEmptyWsAction(queue);
+
+    response = mock(Response.class);
+    when(response.stream()).thenReturn(new FakeStream());
+  }
+
+  @Test
+  public void send_true_when_queue_is_empty() throws Exception {
+    when(queue.all()).thenReturn(new ArrayList<AnalysisReportDto>());
+
+    sut.handle(mock(Request.class), response);
+
+    assertThat(response.stream().toString()).isEqualTo("true");
+  }
+
+  @Test
+  public void send_false_when_queue_is_not_empty() throws Exception {
+    when(queue.all()).thenReturn(Lists.newArrayList(AnalysisReportDto.newForTests(1L)));
+
+    sut.handle(mock(Request.class), response);
+
+    assertThat(response.stream().toString()).isEqualTo("false");
+  }
+
+  private class FakeStream implements Response.Stream {
+    private ByteArrayOutputStream stream;
+
+    private FakeStream() {
+      this.stream = new ByteArrayOutputStream();
+    }
+
+    public String toString() {
+      return stream.toString();
+    }
+
+    @Override
+    public Response.Stream setMediaType(String s) {
+      return null;
+    }
+
+    @Override
+    public Response.Stream setStatus(int httpStatus) {
+      return null;
+    }
+
+    @Override
+    public OutputStream output() {
+      return stream;
+    }
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueWsActionTest.java
new file mode 100644 (file)
index 0000000..094f175
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * 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.computation.ws;
+
+import com.google.common.collect.Lists;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.core.computation.db.AnalysisReportDto;
+import org.sonar.server.computation.AnalysisReportQueue;
+import org.sonar.server.ws.WsTester;
+
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.core.computation.db.AnalysisReportDto.Status.PENDING;
+
+public class QueueWsActionTest {
+
+  WsTester tester;
+  private AnalysisReportQueue queue;
+
+  @Before
+  public void setup() throws Exception {
+    queue = mock(AnalysisReportQueue.class);
+    tester = new WsTester(new ComputationWebService(new QueueWsAction(queue)));
+  }
+
+  @Test
+  public void list_active_reports() throws Exception {
+    AnalysisReportDto report = AnalysisReportDto
+      .newForTests(1L)
+      .setProjectKey("project-name")
+      .setStatus(PENDING)
+      .setData(null)
+      .setCreatedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"))
+      .setStartedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"))
+      .setFinishedAt(DateUtils.parseDateTime("2014-10-13T00:00:00+0200"));
+    List<AnalysisReportDto> reports = Lists.newArrayList(report);
+    when(queue.all()).thenReturn(reports);
+
+    WsTester.TestRequest request = tester.newGetRequest(ComputationWebService.API_ENDPOINT, "active");
+    request.execute().assertJson(getClass(), "list_active_reports.json", false);
+  }
+
+  @Test
+  public void define() throws Exception {
+    assertThat(tester.controller(ComputationWebService.API_ENDPOINT).action("active")).isNotNull();
+  }
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/ActiveAnalysisReportsActionTest/list_active_reports.json b/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/ActiveAnalysisReportsActionTest/list_active_reports.json
deleted file mode 100644 (file)
index a82f67d..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-{
-  "reports": [
-    {
-      "id": 1,
-      "status": "PENDING",
-      "projectName": "project-name",
-      "project": "project-name",
-      "submittedAt": "2014-10-13T00:00:00+0200",
-      "startedAt": "2014-10-13T00:00:00+0200",
-      "finishedAt": "2014-10-13T00:00:00+0200"
-    }
-  ]
-}
\ No newline at end of file
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/AnalysisReportHistorySearchActionMediumTest/list_history_reports.json b/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/AnalysisReportHistorySearchActionMediumTest/list_history_reports.json
deleted file mode 100644 (file)
index b0ed60a..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-{
-  "reports": [
-    {
-      "status": "SUCCESS",
-      "projectName": "DefaultProjectName",
-      "projectKey": "DefaultProjectKey"
-    },
-    {
-      "status": "SUCCESS",
-      "projectName": "DefaultProjectName",
-      "projectKey": "DefaultProjectKey"
-    },
-    {
-      "status": "SUCCESS",
-      "projectName": "DefaultProjectName",
-      "projectKey": "DefaultProjectKey"
-    }
-  ]
-}
\ No newline at end of file
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/HistoryWsActionMediumTest/list_history_reports.json b/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/HistoryWsActionMediumTest/list_history_reports.json
new file mode 100644 (file)
index 0000000..b0ed60a
--- /dev/null
@@ -0,0 +1,19 @@
+{
+  "reports": [
+    {
+      "status": "SUCCESS",
+      "projectName": "DefaultProjectName",
+      "projectKey": "DefaultProjectKey"
+    },
+    {
+      "status": "SUCCESS",
+      "projectName": "DefaultProjectName",
+      "projectKey": "DefaultProjectKey"
+    },
+    {
+      "status": "SUCCESS",
+      "projectName": "DefaultProjectName",
+      "projectKey": "DefaultProjectKey"
+    }
+  ]
+}
\ No newline at end of file
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/QueueWsActionTest/list_active_reports.json b/server/sonar-server/src/test/resources/org/sonar/server/computation/ws/QueueWsActionTest/list_active_reports.json
new file mode 100644 (file)
index 0000000..a82f67d
--- /dev/null
@@ -0,0 +1,13 @@
+{
+  "reports": [
+    {
+      "id": 1,
+      "status": "PENDING",
+      "projectName": "project-name",
+      "project": "project-name",
+      "submittedAt": "2014-10-13T00:00:00+0200",
+      "startedAt": "2014-10-13T00:00:00+0200",
+      "finishedAt": "2014-10-13T00:00:00+0200"
+    }
+  ]
+}
\ No newline at end of file