From: Teryk Bellahsene Date: Wed, 13 May 2015 16:11:59 +0000 (+0200) Subject: rename web services with the xxxWs form X-Git-Tag: 5.2-RC1~1957 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d92546cf1bb534f2fe7cc045dbcdecb834000c4e;p=sonarqube.git rename web services with the xxxWs form --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/activity/ws/ActivitiesWebService.java b/server/sonar-server/src/main/java/org/sonar/server/activity/ws/ActivitiesWebService.java deleted file mode 100644 index 925a0456d2b..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/activity/ws/ActivitiesWebService.java +++ /dev/null @@ -1,43 +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.ws; - -import org.sonar.api.server.ws.WebService; - -public class ActivitiesWebService implements WebService { - - public static final String API_ENDPOINT = "api/activities"; - - private final SearchAction search; - - public ActivitiesWebService(SearchAction search) { - this.search = search; - } - - @Override - public void define(Context context) { - NewController controller = context - .createController(API_ENDPOINT) - .setDescription("Tracking of activities"); - - search.define(controller); - controller.done(); - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/activity/ws/ActivitiesWs.java b/server/sonar-server/src/main/java/org/sonar/server/activity/ws/ActivitiesWs.java new file mode 100644 index 00000000000..8dfb980175e --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/activity/ws/ActivitiesWs.java @@ -0,0 +1,43 @@ +/* + * 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.ws; + +import org.sonar.api.server.ws.WebService; + +public class ActivitiesWs implements WebService { + + public static final String ENDPOINT = "api/activities"; + + private final SearchAction search; + + public ActivitiesWs(SearchAction search) { + this.search = search; + } + + @Override + public void define(Context context) { + NewController controller = context + .createController(ENDPOINT) + .setDescription("Tracking of activities"); + + search.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 deleted file mode 100644 index e048ff397c7..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWebService.java +++ /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.sonar.api.server.ws.WebService; - -/** - * Web service to interact with the "computation" stack : - * - */ -public class ComputationWebService implements WebService { - public static final String API_ENDPOINT = "api/computation"; - - 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/ComputationWs.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWs.java new file mode 100644 index 00000000000..41507396418 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/ComputationWs.java @@ -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 : + * + */ +public class ComputationWs implements WebService { + public static final String ENDPOINT = "api/computation"; + + private final ComputationWsAction[] actions; + + public ComputationWs(ComputationWsAction... actions) { + this.actions = actions; + } + + @Override + public void define(Context context) { + NewController controller = context + .createController(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 index 4cf4afc3b67..a4a9dd772c1 100644 --- 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 @@ -22,7 +22,7 @@ package org.sonar.server.computation.ws; import org.sonar.api.server.ws.WebService; /** - * Used by {@link ComputationWebService} to + * Used by {@link ComputationWs} to * loop over all its actions */ interface ComputationWsAction { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWebService.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWebService.java deleted file mode 100644 index 611043076e6..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWebService.java +++ /dev/null @@ -1,77 +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.ReportQueue; - -import java.util.List; - -/** - * Internal WebService with one action - */ -public class IsQueueEmptyWebService implements WebService { - public static final String API_ENDPOINT = "api/analysis_reports"; - - private final IsQueueEmptyWsAction action; - - public IsQueueEmptyWebService(ReportQueue queue) { - this.action = new IsQueueEmptyWsAction(queue); - } - - @Override - public void define(Context context) { - NewController controller = context - .createController(API_ENDPOINT) - .setDescription("Analysis reports processed"); - action.define(controller); - controller.done(); - } - - static class IsQueueEmptyWsAction implements RequestHandler { - private final ReportQueue queue; - - public IsQueueEmptyWsAction(ReportQueue queue) { - this.queue = queue; - } - - 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 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/IsQueueEmptyWs.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWs.java new file mode 100644 index 00000000000..d9c83b0fff1 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/IsQueueEmptyWs.java @@ -0,0 +1,77 @@ +/* + * 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.ReportQueue; + +import java.util.List; + +/** + * Internal WebService with one action + */ +public class IsQueueEmptyWs implements WebService { + public static final String API_ENDPOINT = "api/analysis_reports"; + + private final IsQueueEmptyWsAction action; + + public IsQueueEmptyWs(ReportQueue queue) { + this.action = new IsQueueEmptyWsAction(queue); + } + + @Override + public void define(Context context) { + NewController controller = context + .createController(API_ENDPOINT) + .setDescription("Analysis reports processed"); + action.define(controller); + controller.done(); + } + + static class IsQueueEmptyWsAction implements RequestHandler { + private final ReportQueue queue; + + public IsQueueEmptyWsAction(ReportQueue queue) { + this.queue = queue; + } + + 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 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/dashboard/ws/DashboardsWebService.java b/server/sonar-server/src/main/java/org/sonar/server/dashboard/ws/DashboardsWebService.java deleted file mode 100644 index 3c523a6ddce..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/dashboard/ws/DashboardsWebService.java +++ /dev/null @@ -1,43 +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.dashboard.ws; - -import org.sonar.api.server.ws.WebService; - -public class DashboardsWebService implements WebService { - - private final DashboardsAction[] actions; - - public DashboardsWebService(DashboardsAction... actions) { - this.actions = actions; - } - - @Override - public void define(Context context) { - NewController controller = context.createController("api/dashboards"); - controller.setSince("5.0"); - controller.setDescription("Management of dashboards and widgets"); - for (DashboardsAction action : actions) { - action.define(controller); - } - controller.done(); - } - -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/dashboard/ws/DashboardsWs.java b/server/sonar-server/src/main/java/org/sonar/server/dashboard/ws/DashboardsWs.java new file mode 100644 index 00000000000..b3c8c7d93fa --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/dashboard/ws/DashboardsWs.java @@ -0,0 +1,43 @@ +/* + * 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.dashboard.ws; + +import org.sonar.api.server.ws.WebService; + +public class DashboardsWs implements WebService { + + private final DashboardsAction[] actions; + + public DashboardsWs(DashboardsAction... actions) { + this.actions = actions; + } + + @Override + public void define(Context context) { + NewController controller = context.createController("api/dashboards"); + controller.setSince("5.0"); + controller.setDescription("Management of dashboards and widgets"); + for (DashboardsAction action : actions) { + action.define(controller); + } + controller.done(); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index 4f1c92c55a7..cfaa9efc252 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -87,7 +87,7 @@ import org.sonar.server.activity.db.ActivityDao; import org.sonar.server.activity.index.ActivityIndex; import org.sonar.server.activity.index.ActivityIndexDefinition; import org.sonar.server.activity.index.ActivityIndexer; -import org.sonar.server.activity.ws.ActivitiesWebService; +import org.sonar.server.activity.ws.ActivitiesWs; import org.sonar.server.activity.ws.ActivityMapping; import org.sonar.server.authentication.ws.AuthenticationWs; import org.sonar.server.batch.BatchIndex; @@ -116,9 +116,9 @@ import org.sonar.server.computation.ComputationThreadLauncher; import org.sonar.server.computation.ReportQueue; import org.sonar.server.computation.ReportQueueCleaner; import org.sonar.server.computation.db.AnalysisReportDao; -import org.sonar.server.computation.ws.ComputationWebService; +import org.sonar.server.computation.ws.ComputationWs; import org.sonar.server.computation.ws.HistoryWsAction; -import org.sonar.server.computation.ws.IsQueueEmptyWebService; +import org.sonar.server.computation.ws.IsQueueEmptyWs; import org.sonar.server.computation.ws.QueueWsAction; import org.sonar.server.computation.ws.SubmitReportWsAction; import org.sonar.server.config.ws.PropertiesWs; @@ -126,7 +126,7 @@ import org.sonar.server.dashboard.db.DashboardDao; import org.sonar.server.dashboard.db.WidgetDao; import org.sonar.server.dashboard.db.WidgetPropertyDao; import org.sonar.server.dashboard.ws.DashboardsShowAction; -import org.sonar.server.dashboard.ws.DashboardsWebService; +import org.sonar.server.dashboard.ws.DashboardsWs; import org.sonar.server.db.DatabaseChecker; import org.sonar.server.db.DbClient; import org.sonar.server.db.EmbeddedDatabaseFactory; @@ -322,7 +322,7 @@ import org.sonar.server.rule.ws.AppAction; import org.sonar.server.rule.ws.DeleteAction; import org.sonar.server.rule.ws.RepositoriesAction; import org.sonar.server.rule.ws.RuleMapping; -import org.sonar.server.rule.ws.RulesWebService; +import org.sonar.server.rule.ws.RulesWs; import org.sonar.server.rule.ws.SearchAction; import org.sonar.server.rule.ws.TagsAction; import org.sonar.server.rule.ws.UpdateAction; @@ -625,7 +625,7 @@ class ServerComponents { pico.addSingleton(BatchWs.class); // Dashboard - pico.addSingleton(DashboardsWebService.class); + pico.addSingleton(DashboardsWs.class); pico.addSingleton(DashboardsShowAction.class); // update center @@ -691,7 +691,7 @@ class ServerComponents { pico.addSingleton(RuleCreator.class); pico.addSingleton(RuleDeleter.class); pico.addSingleton(UpdateAction.class); - pico.addSingleton(RulesWebService.class); + pico.addSingleton(RulesWs.class); pico.addSingleton(SearchAction.class); pico.addSingleton(org.sonar.server.rule.ws.ShowAction.class); pico.addSingleton(org.sonar.server.rule.ws.CreateAction.class); @@ -708,7 +708,7 @@ class ServerComponents { pico.addSingleton(ListAction.class); // activity - pico.addSingleton(ActivitiesWebService.class); + pico.addSingleton(ActivitiesWs.class); pico.addSingleton(org.sonar.server.activity.ws.SearchAction.class); pico.addSingleton(ActivityMapping.class); @@ -970,8 +970,8 @@ class ServerComponents { // Compute engine pico.addSingleton(ReportQueue.class); pico.addSingleton(ComputationThreadLauncher.class); - pico.addSingleton(ComputationWebService.class); - pico.addSingleton(IsQueueEmptyWebService.class); + pico.addSingleton(ComputationWs.class); + pico.addSingleton(IsQueueEmptyWs.class); pico.addSingleton(QueueWsAction.class); pico.addSingleton(HistoryWsAction.class); pico.addSingleton(DefaultPeriodCleaner.class); diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RulesWebService.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RulesWebService.java deleted file mode 100644 index 08b0374f84c..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RulesWebService.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.rule.ws; - -import org.sonar.api.server.ws.WebService; - -public class RulesWebService implements WebService { - - private final RulesAction[] actions; - - public RulesWebService(RulesAction... actions) { - this.actions = actions; - } - - @Override - public void define(Context context) { - NewController controller = context - .createController("api/rules") - .setDescription("Coding rules"); - - for (RulesAction action : actions) { - action.define(controller); - } - - controller.done(); - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RulesWs.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RulesWs.java new file mode 100644 index 00000000000..7b044dcbb4b --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RulesWs.java @@ -0,0 +1,44 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.rule.ws; + +import org.sonar.api.server.ws.WebService; + +public class RulesWs implements WebService { + + private final RulesAction[] actions; + + public RulesWs(RulesAction... actions) { + this.actions = actions; + } + + @Override + public void define(Context context) { + NewController controller = context + .createController("api/rules") + .setDescription("Coding rules"); + + for (RulesAction action : actions) { + action.define(controller); + } + + controller.done(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWebServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWebServiceMediumTest.java deleted file mode 100644 index 0ddc742eca4..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWebServiceMediumTest.java +++ /dev/null @@ -1,79 +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.ws; - -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.server.ws.WebService; -import org.sonar.server.activity.Activity; -import org.sonar.server.activity.ActivityService; -import org.sonar.server.tester.ServerTester; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsTester; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ActivitiesWebServiceMediumTest { - - @ClassRule - public static ServerTester tester = new ServerTester(); - @Rule - public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); - - ActivitiesWebService ws; - ActivityService service; - - @Before - public void setUp() { - tester.clearDbAndIndexes(); - ws = tester.get(ActivitiesWebService.class); - service = tester.get(ActivityService.class); - } - - @Test - public void define() { - WebService.Context context = new WebService.Context(); - ws.define(context); - - WebService.Controller controller = context.controller(ActivitiesWebService.API_ENDPOINT); - - assertThat(controller).isNotNull(); - assertThat(controller.actions()).hasSize(1); - assertThat(controller.action(SearchAction.SEARCH_ACTION)).isNotNull(); - } - - @Test - public void search() throws Exception { - Activity activity = new Activity(); - activity.setType(Activity.Type.ANALYSIS_REPORT); - activity.setAction("THE_ACTION"); - activity.setMessage("THE_MSG"); - activity.setData("foo", "bar"); - service.save(activity); - - WsTester.TestRequest request = tester.wsTester().newGetRequest("api/activities", "search"); - WsTester.Result result = request.execute(); - assertThat(result.outputAsString()).contains("\"total\":1"); - assertThat(result.outputAsString()).contains("\"type\":\"ANALYSIS_REPORT\""); - assertThat(result.outputAsString()).contains("\"details\":{\"foo\":\"bar\"}"); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java new file mode 100644 index 00000000000..b38d7754079 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/activity/ws/ActivitiesWsMediumTest.java @@ -0,0 +1,79 @@ +/* + * 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.ws; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.server.ws.WebService; +import org.sonar.server.activity.Activity; +import org.sonar.server.activity.ActivityService; +import org.sonar.server.tester.ServerTester; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ActivitiesWsMediumTest { + + @ClassRule + public static ServerTester tester = new ServerTester(); + @Rule + public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); + + ActivitiesWs ws; + ActivityService service; + + @Before + public void setUp() { + tester.clearDbAndIndexes(); + ws = tester.get(ActivitiesWs.class); + service = tester.get(ActivityService.class); + } + + @Test + public void define() { + WebService.Context context = new WebService.Context(); + ws.define(context); + + WebService.Controller controller = context.controller(ActivitiesWs.ENDPOINT); + + assertThat(controller).isNotNull(); + assertThat(controller.actions()).hasSize(1); + assertThat(controller.action(SearchAction.SEARCH_ACTION)).isNotNull(); + } + + @Test + public void search() throws Exception { + Activity activity = new Activity(); + activity.setType(Activity.Type.ANALYSIS_REPORT); + activity.setAction("THE_ACTION"); + activity.setMessage("THE_MSG"); + activity.setData("foo", "bar"); + service.save(activity); + + WsTester.TestRequest request = tester.wsTester().newGetRequest("api/activities", "search"); + WsTester.Result result = request.execute(); + assertThat(result.outputAsString()).contains("\"total\":1"); + assertThat(result.outputAsString()).contains("\"type\":\"ANALYSIS_REPORT\""); + assertThat(result.outputAsString()).contains("\"details\":{\"foo\":\"bar\"}"); + } +} 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 deleted file mode 100644 index 309a3c1ddf3..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComputationWebServiceTest.java +++ /dev/null @@ -1,49 +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.Test; -import org.sonar.api.server.ws.RequestHandler; -import org.sonar.api.server.ws.WebService; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -public class ComputationWebServiceTest { - - @Test - public void define() { - 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/computation"); - 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/ComputationWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComputationWsTest.java new file mode 100644 index 00000000000..8da36798f95 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/ComputationWsTest.java @@ -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.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class ComputationWsTest { + + @Test + public void define() { + ComputationWs ws = new ComputationWs(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/computation"); + 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/IsQueueEmptyWebServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsQueueEmptyWebServiceTest.java deleted file mode 100644 index 1fe9fcce857..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsQueueEmptyWebServiceTest.java +++ /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.ReportQueue; - -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; -import java.util.ArrayList; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class IsQueueEmptyWebServiceTest { - - IsQueueEmptyWebService.IsQueueEmptyWsAction sut; - ReportQueue queue; - Response response; - - @Before - public void before() { - queue = mock(ReportQueue.class); - sut = new IsQueueEmptyWebService.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()); - - 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/IsQueueEmptyWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsQueueEmptyWsTest.java new file mode 100644 index 00000000000..32c9e9432d9 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/IsQueueEmptyWsTest.java @@ -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.ReportQueue; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.util.ArrayList; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class IsQueueEmptyWsTest { + + IsQueueEmptyWs.IsQueueEmptyWsAction sut; + ReportQueue queue; + Response response; + + @Before + public void before() { + queue = mock(ReportQueue.class); + sut = new IsQueueEmptyWs.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()); + + 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 index 4183ce8e525..42f6f11a075 100644 --- 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 @@ -43,7 +43,7 @@ public class QueueWsActionTest { @Before public void setup() { queue = mock(ReportQueue.class); - tester = new WsTester(new ComputationWebService(new QueueWsAction(queue))); + tester = new WsTester(new ComputationWs(new QueueWsAction(queue))); } @Test @@ -59,12 +59,12 @@ public class QueueWsActionTest { List reports = Lists.newArrayList(report); when(queue.all()).thenReturn(reports); - WsTester.TestRequest request = tester.newGetRequest(ComputationWebService.API_ENDPOINT, "queue"); + WsTester.TestRequest request = tester.newGetRequest(ComputationWs.ENDPOINT, "queue"); request.execute().assertJson(getClass(), "list_queue_reports.json"); } @Test public void define() { - assertThat(tester.controller(ComputationWebService.API_ENDPOINT).action("queue")).isNotNull(); + assertThat(tester.controller(ComputationWs.ENDPOINT).action("queue")).isNotNull(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java index 66434bc154c..95910bcfdc0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/SubmitReportWsActionTest.java @@ -52,7 +52,7 @@ public class SubmitReportWsActionTest { @Before public void before() { sut = new SubmitReportWsAction(queue, workerLauncher, userSessionRule); - wsTester = new WsTester(new ComputationWebService(sut)); + wsTester = new WsTester(new ComputationWs(sut)); } @Test @@ -75,7 +75,7 @@ public class SubmitReportWsActionTest { when(queue.add(any(String.class), any(InputStream.class))).thenReturn(new ReportQueue.Item(dto, null)); WsTester.TestRequest request = wsTester - .newGetRequest(ComputationWebService.API_ENDPOINT, "submit_report") + .newGetRequest(ComputationWs.ENDPOINT, "submit_report") .setParam(SubmitReportWsAction.PARAM_PROJECT_KEY, "P1") .setParam(SubmitReportWsAction.PARAM_REPORT_DATA, null); WsTester.Result response = request.execute(); @@ -90,7 +90,7 @@ public class SubmitReportWsActionTest { userSessionRule.setGlobalPermissions(GlobalPermissions.DASHBOARD_SHARING); WsTester.TestRequest request = wsTester - .newGetRequest(ComputationWebService.API_ENDPOINT, "submit_report") + .newGetRequest(ComputationWs.ENDPOINT, "submit_report") .setParam(SubmitReportWsAction.PARAM_PROJECT_KEY, "P1") .setParam(SubmitReportWsAction.PARAM_REPORT_DATA, null); request.execute(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java index 56ac8898272..9d53a92a191 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java @@ -70,7 +70,7 @@ public class AppActionTest { @Test public void should_generate_app_init_info() throws Exception { AppAction app = new AppAction(languages, ruleRepositories, i18n, debtModel, profileLoader, userSessionRule); - WsTester tester = new WsTester(new RulesWebService(app)); + WsTester tester = new WsTester(new RulesWs(app)); userSessionRule.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/DeleteActionTest.java index b5d99e70294..16af4d3620b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/DeleteActionTest.java @@ -41,7 +41,7 @@ public class DeleteActionTest { @Before public void setUp() { - tester = new WsTester(new RulesWebService(new DeleteAction(ruleService))); + tester = new WsTester(new RulesWs(new DeleteAction(ruleService))); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java index 8eec2710eea..cee8a5412e7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java @@ -42,7 +42,7 @@ public class RepositoriesActionTest { @Before public void setUp() { - tester = new WsTester(new RulesWebService(new RepositoriesAction(repositories))); + tester = new WsTester(new RulesWs(new RepositoriesAction(repositories))); RuleRepositories.Repository repo1 = mock(RuleRepositories.Repository.class); when(repo1.key()).thenReturn("xoo"); @@ -67,7 +67,7 @@ public class RepositoriesActionTest { @Test public void should_list_repositories() throws Exception { - tester = new WsTester(new RulesWebService(new RepositoriesAction(repositories))); + tester = new WsTester(new RulesWs(new RepositoriesAction(repositories))); newRequest().execute().assertJson(this.getClass(), "repositories.json"); newRequest().setParam("language", "xoo").execute().assertJson(this.getClass(), "repositories_xoo.json"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java deleted file mode 100644 index 31eb6a06a61..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java +++ /dev/null @@ -1,657 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.rule.ws; - -import com.google.common.collect.ImmutableSet; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rule.RuleStatus; -import org.sonar.api.rule.Severity; -import org.sonar.api.server.debt.DebtRemediationFunction; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.DateUtils; -import org.sonar.core.persistence.DbSession; -import org.sonar.core.qualityprofile.db.ActiveRuleDto; -import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; -import org.sonar.core.qualityprofile.db.QualityProfileDao; -import org.sonar.core.qualityprofile.db.QualityProfileDto; -import org.sonar.core.rule.RuleDto; -import org.sonar.core.rule.RuleDto.Format; -import org.sonar.core.rule.RuleParamDto; -import org.sonar.core.technicaldebt.db.CharacteristicDto; -import org.sonar.server.db.DbClient; -import org.sonar.server.debt.DebtTesting; -import org.sonar.server.qualityprofile.QProfileTesting; -import org.sonar.server.qualityprofile.db.ActiveRuleDao; -import org.sonar.server.rule.RuleTesting; -import org.sonar.server.rule.db.RuleDao; -import org.sonar.server.rule.index.RuleNormalizer; -import org.sonar.server.search.ws.SearchOptions; -import org.sonar.server.tester.ServerTester; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsTester; - -import static org.assertj.core.api.Assertions.assertThat; - -public class RulesWebServiceMediumTest { - - @ClassRule - public static ServerTester tester = new ServerTester(); - @Rule - public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); - - private static final String API_ENDPOINT = "api/rules"; - private static final String API_SEARCH_METHOD = "search"; - private static final String API_SHOW_METHOD = "show"; - private static final String API_TAGS_METHOD = "tags"; - - DbClient db; - RulesWebService ws; - RuleDao ruleDao; - DbSession session; - int softReliabilityId, hardReliabilityId; - - @Before - public void setUp() { - tester.clearDbAndIndexes(); - db = tester.get(DbClient.class); - ruleDao = tester.get(RuleDao.class); - ws = tester.get(RulesWebService.class); - session = tester.get(DbClient.class).openSession(false); - } - - @After - public void after() { - session.close(); - } - - @Test - public void define() { - WebService.Context context = new WebService.Context(); - ws.define(context); - - WebService.Controller controller = context.controller(API_ENDPOINT); - - assertThat(controller).isNotNull(); - assertThat(controller.actions()).hasSize(8); - assertThat(controller.action(API_SEARCH_METHOD)).isNotNull(); - assertThat(controller.action(API_SHOW_METHOD)).isNotNull(); - assertThat(controller.action(API_TAGS_METHOD)).isNotNull(); - assertThat(controller.action("update")).isNotNull(); - assertThat(controller.action("create")).isNotNull(); - assertThat(controller.action("delete")).isNotNull(); - assertThat(controller.action("repositories")).isNotNull(); - assertThat(controller.action("app")).isNotNull(); - } - - @Test - public void show_rule() throws Exception { - QualityProfileDto profile = QProfileTesting.newXooP1(); - tester.get(QualityProfileDao.class).insert(session, profile); - - RuleDto rule = RuleTesting.newXooX1(); - ruleDao.insert(session, rule); - - ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(profile, rule).setSeverity("BLOCKER"); - tester.get(ActiveRuleDao.class).insert(session, activeRuleDto); - session.commit(); - session.clearCache(); - - // 1. With Activation - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SHOW_METHOD); - request.setParam(ShowAction.PARAM_KEY, rule.getKey().toString()); - request.setParam(ShowAction.PARAM_ACTIVES, "true"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "show_rule_active.json"); - - // 1. Default Activation (defaults to false) - request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SHOW_METHOD); - request.setParam(ShowAction.PARAM_KEY, rule.getKey().toString()); - result = request.execute(); - result.assertJson(this.getClass(), "show_rule_no_active.json"); - } - - @Test - public void search_no_rules() throws Exception { - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "actives"); - WsTester.Result result = request.execute(); - - result.assertJson(this.getClass(), "search_no_rules.json"); - } - - @Test - public void filter_by_key_rules() throws Exception { - ruleDao.insert(session, RuleTesting.newXooX1()); - ruleDao.insert(session, RuleTesting.newXooX2()); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchAction.PARAM_KEY, RuleTesting.XOO_X1.toString()); - request.setParam(SearchOptions.PARAM_FIELDS, "actives"); - WsTester.Result result = request.execute(); - result.assertJson("{\"total\":1,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x1\"}]}"); - - request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchAction.PARAM_KEY, RuleKey.of("xoo", "unknown").toString()); - request.setParam(SearchOptions.PARAM_FIELDS, "actives"); - result = request.execute(); - result.assertJson("{\"total\":0,\"p\":1,\"ps\":100,\"rules\":[],\"actives\":{}}"); - - } - - @Test - public void search_2_rules() throws Exception { - ruleDao.insert(session, RuleTesting.newXooX1()); - ruleDao.insert(session, RuleTesting.newXooX2()); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - WsTester.Result result = request.execute(); - - result.assertJson(getClass(), "search_2_rules.json"); - } - - @Test - public void search_2_rules_with_field_selection() throws Exception { - ruleDao.insert(session, RuleTesting.newXooX1()); - ruleDao.insert(session, RuleTesting.newXooX2().setDescription("A *Xoo* rule").setDescriptionFormat(Format.MARKDOWN)); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(SearchOptions.PARAM_FIELDS, "name,htmlDesc,mdDesc"); - WsTester.Result result = request.execute(); - - result.assertJson(getClass(), "search_2_rules_fields.json"); - } - - @Test - public void search_debt_rules() throws Exception { - insertDebtCharacteristics(session); - - ruleDao.insert(session, RuleTesting.newXooX1() - .setDefaultSubCharacteristicId(hardReliabilityId) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("1h") - .setDefaultRemediationOffset("15min") - - .setSubCharacteristicId(null) - .setRemediationFunction(null) - .setRemediationCoefficient(null) - .setRemediationOffset(null) - ); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); - request.setParam(SearchAction.PARAM_FACETS, "debt_characteristics"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_debt_rule.json"); - } - - @Test - public void search_debt_rules_with_default_and_overridden_debt_values() throws Exception { - insertDebtCharacteristics(session); - - ruleDao.insert(session, RuleTesting.newXooX1() - .setDefaultSubCharacteristicId(hardReliabilityId) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("1h") - .setDefaultRemediationOffset("15min") - - .setSubCharacteristicId(softReliabilityId) - .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setRemediationCoefficient("2h") - .setRemediationOffset("25min") - ); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_debt_rules_with_default_and_overridden_debt_values.json"); - } - - @Test - public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() throws Exception { - insertDebtCharacteristics(session); - - ruleDao.insert(session, RuleTesting.newXooX1() - .setDefaultSubCharacteristicId(hardReliabilityId) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("1h") - .setDefaultRemediationOffset("15min") - - .setSubCharacteristicId(softReliabilityId) - .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) - .setRemediationCoefficient(null) - .setRemediationOffset("5min") - ); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json"); - } - - @Test - public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() throws Exception { - insertDebtCharacteristics(session); - - ruleDao.insert(session, RuleTesting.newXooX1() - .setDefaultSubCharacteristicId(hardReliabilityId) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("1h") - .setDefaultRemediationOffset("15min") - - .setSubCharacteristicId(softReliabilityId) - .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) - .setRemediationCoefficient("1h") - .setRemediationOffset(null) - ); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json"); - } - - @Test - public void debt_characteristics_is_sticky_facet() throws Exception { - insertDebtCharacteristics(session); - - ruleDao.insert(session, RuleTesting.newXooX1() - .setDefaultSubCharacteristicId(hardReliabilityId) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("1h") - .setDefaultRemediationOffset("15min") - - .setSubCharacteristicId(null) - .setRemediationFunction(null) - .setRemediationCoefficient(null) - .setRemediationOffset(null) - ); - ruleDao.insert(session, RuleTesting.newXooX2() - .setDefaultSubCharacteristicId(hardReliabilityId) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("1h") - .setDefaultRemediationOffset("15min") - - .setSubCharacteristicId(softReliabilityId) - .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setRemediationCoefficient("30min") - .setRemediationOffset("5min") - ); - ruleDao.insert(session, RuleTesting.newXooX3() - .setDefaultSubCharacteristicId(null) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("2min") - .setDefaultRemediationOffset("1min") - - .setSubCharacteristicId(null) - .setRemediationFunction(null) - .setRemediationCoefficient(null) - .setRemediationOffset(null) - ); - ruleDao.insert(session, RuleTesting.newDto(RuleKey.of("xoo", "x4")).setLanguage("xoo") - .setDefaultSubCharacteristicId(softReliabilityId) - .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setDefaultRemediationCoefficient("2min") - .setDefaultRemediationOffset("1min") - - .setSubCharacteristicId(-1) - .setRemediationFunction(null) - .setRemediationCoefficient(null) - .setRemediationOffset(null) - ); - session.commit(); - - WsTester.Result result = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD) - .setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn") - .setParam("debt_characteristics", "SOFT_RELIABILITY") - .setParam(SearchAction.PARAM_FACETS, "debt_characteristics") - .execute(); - result.assertJson(this.getClass(), "search_debt_rules_sticky.json"); - } - - @Test - public void search_template_rules() throws Exception { - RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true); - ruleDao.insert(session, templateRule); - ruleDao.insert(session, RuleTesting.newXooX2()).setTemplateId(templateRule.getId()); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "isTemplate"); - request.setParam(SearchAction.PARAM_IS_TEMPLATE, "true"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_template_rules.json"); - } - - @Test - public void search_custom_rules_from_template_key() throws Exception { - RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true); - ruleDao.insert(session, templateRule); - ruleDao.insert(session, RuleTesting.newXooX2()).setTemplateId(templateRule.getId()); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "templateKey"); - request.setParam(SearchAction.PARAM_TEMPLATE_KEY, "xoo:x1"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_rules_from_template_key.json"); - } - - @Test - public void search_all_active_rules() throws Exception { - QualityProfileDto profile = QProfileTesting.newXooP1(); - tester.get(QualityProfileDao.class).insert(session, profile); - - RuleDto rule = RuleTesting.newXooX1(); - ruleDao.insert(session, rule); - - ActiveRuleDto activeRule = newActiveRule(profile, rule); - tester.get(ActiveRuleDao.class).insert(session, activeRule); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); - request.setParam(SearchAction.PARAM_ACTIVATION, "true"); - request.setParam(SearchOptions.PARAM_FIELDS, ""); - WsTester.Result result = request.execute(); - - result.assertJson(this.getClass(), "search_active_rules.json"); - } - - @Test - public void search_profile_active_rules() throws Exception { - QualityProfileDto profile = QProfileTesting.newXooP1(); - tester.get(QualityProfileDao.class).insert(session, profile); - - QualityProfileDto profile2 = QProfileTesting.newXooP2(); - tester.get(QualityProfileDao.class).insert(session, profile2); - - session.commit(); - - RuleDto rule = RuleTesting.newXooX1(); - ruleDao.insert(session, rule); - - ActiveRuleDto activeRule = newActiveRule(profile, rule); - tester.get(ActiveRuleDao.class).insert(session, activeRule); - ActiveRuleDto activeRule2 = newActiveRule(profile2, rule); - tester.get(ActiveRuleDao.class).insert(session, activeRule2); - - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); - request.setParam(SearchAction.PARAM_ACTIVATION, "true"); - request.setParam(SearchAction.PARAM_QPROFILE, profile2.getKey()); - request.setParam(SearchOptions.PARAM_FIELDS, "actives"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_profile_active_rules.json"); - - tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD) - .setParam(SearchAction.PARAM_ACTIVATION, "true") - .setParam(SearchAction.PARAM_QPROFILE, "unknown_profile") - .setParam(SearchOptions.PARAM_FIELDS, "actives") - .execute() - .assertJson(this.getClass(), "search_no_rules.json"); - } - - @Test - public void search_profile_active_rules_with_inheritance() throws Exception { - QualityProfileDto profile = QProfileTesting.newXooP1(); - tester.get(QualityProfileDao.class).insert(session, profile); - - QualityProfileDto profile2 = QProfileTesting.newXooP2().setParentKee(profile.getKee()); - tester.get(QualityProfileDao.class).insert(session, profile2); - - session.commit(); - - RuleDto rule = RuleTesting.newXooX1(); - ruleDao.insert(session, rule); - - ActiveRuleDto activeRule = newActiveRule(profile, rule); - tester.get(ActiveRuleDao.class).insert(session, activeRule); - ActiveRuleDto activeRule2 = newActiveRule(profile2, rule).setInheritance(ActiveRuleDto.OVERRIDES).setSeverity(Severity.CRITICAL); - tester.get(ActiveRuleDao.class).insert(session, activeRule2); - - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); - request.setParam(SearchAction.PARAM_ACTIVATION, "true"); - request.setParam(SearchAction.PARAM_QPROFILE, profile2.getKey()); - request.setParam(SearchOptions.PARAM_FIELDS, "actives"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "search_profile_active_rules_inheritance.json"); - } - - @Test - public void search_all_active_rules_params() throws Exception { - QualityProfileDto profile = QProfileTesting.newXooP1(); - tester.get(QualityProfileDao.class).insert(session, profile); - RuleDto rule = RuleTesting.newXooX1(); - ruleDao.insert(session, rule); - session.commit(); - - RuleParamDto param = RuleParamDto.createFor(rule) - .setDefaultValue("some value") - .setType("string") - .setDescription("My small description") - .setName("my_var"); - ruleDao.addRuleParam(session, rule, param); - - RuleParamDto param2 = RuleParamDto.createFor(rule) - .setDefaultValue("other value") - .setType("integer") - .setDescription("My small description") - .setName("the_var"); - ruleDao.addRuleParam(session, rule, param2); - - ActiveRuleDto activeRule = newActiveRule(profile, rule); - tester.get(ActiveRuleDao.class).insert(session, activeRule); - - ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param) - .setValue("The VALUE"); - tester.get(ActiveRuleDao.class).addParam(session, activeRule, activeRuleParam); - - ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2) - .setValue("The Other Value"); - tester.get(ActiveRuleDao.class).addParam(session, activeRule, activeRuleParam2); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); - request.setParam(SearchAction.PARAM_ACTIVATION, "true"); - request.setParam(SearchOptions.PARAM_FIELDS, "params"); - WsTester.Result result = request.execute(); - - result.assertJson(this.getClass(), "search_active_rules_params.json"); - } - - @Test - public void get_tags() throws Exception { - QualityProfileDto profile = QProfileTesting.newXooP1(); - tester.get(QualityProfileDao.class).insert(session, profile); - - RuleDto rule = RuleTesting.newXooX1(). - setTags(ImmutableSet.of("hello", "world")) - .setSystemTags(Collections.emptySet()); - ruleDao.insert(session, rule); - - RuleDto rule2 = RuleTesting.newXooX2() - .setTags(ImmutableSet.of("hello", "java")) - .setSystemTags(ImmutableSet.of("sys1")); - ruleDao.insert(session, rule2); - session.commit(); - - tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD).execute().assertJson(this.getClass(), "get_tags.json"); - tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD) - .setParam("ps", "1").execute().assertJson(this.getClass(), "get_tags_limited.json"); - tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD) - .setParam("q", "ll").execute().assertJson(this.getClass(), "get_tags_filtered.json"); - } - - @Test - public void get_note_as_markdown_and_html() throws Exception { - QualityProfileDto profile = QProfileTesting.newXooP1(); - tester.get(QualityProfileDao.class).insert(session, profile); - RuleDto rule = RuleTesting.newXooX1().setNoteData("this is *bold*"); - ruleDao.insert(session, rule); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, "htmlNote, mdNote"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "get_note_as_markdown_and_html.json"); - } - - @Test - public void filter_by_tags() throws Exception { - ruleDao.insert(session, RuleTesting.newXooX1() - .setTags(Collections.emptySet()) - .setSystemTags(ImmutableSet.of("tag1"))); - ruleDao.insert(session, RuleTesting.newXooX2() - .setTags(Collections.emptySet()) - .setSystemTags(ImmutableSet.of("tag2"))); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchAction.PARAM_TAGS, "tag1"); - request.setParam(SearchOptions.PARAM_FIELDS, "sysTags, tags"); - request.setParam(SearchAction.PARAM_FACETS, "tags"); - WsTester.Result result = request.execute(); - result.assertJson(this.getClass(), "filter_by_tags.json"); - } - - @Test - public void severities_facet_should_have_all_severities() throws Exception { - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchAction.PARAM_FACETS, "severities"); - request.execute().assertJson(this.getClass(), "severities_facet.json"); - } - - @Test - public void statuses_facet_should_have_all_statuses_except_removed() throws Exception { - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchAction.PARAM_FACETS, "statuses"); - request.execute().assertJson(this.getClass(), "statuses_facet.json"); - } - - @Test - public void statuses_facet_should_be_sticky() throws Exception { - ruleDao.insert(session, RuleTesting.newXooX1()); - ruleDao.insert(session, RuleTesting.newXooX2().setStatus(RuleStatus.BETA)); - ruleDao.insert(session, RuleTesting.newXooX3().setStatus(RuleStatus.DEPRECATED)); - session.commit(); - - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchAction.PARAM_STATUSES, "DEPRECATED"); - request.setParam(SearchAction.PARAM_FACETS, "statuses"); - request.execute().assertJson(this.getClass(), "statuses_facet_sticky.json"); - } - - @Test - public void sort_by_name() throws Exception { - ruleDao.insert(session, RuleTesting.newXooX1().setName("Dodgy - Consider returning a zero length array rather than null ")); - ruleDao.insert(session, RuleTesting.newXooX2().setName("Bad practice - Creates an empty zip file entry")); - ruleDao.insert(session, RuleTesting.newXooX3().setName("XPath rule")); - session.commit(); - - // 1. Sort Name Asc - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, ""); - request.setParam(SearchOptions.PARAM_SORT, "name"); - request.setParam(SearchOptions.PARAM_ASCENDING, "true"); - - WsTester.Result result = request.execute(); - result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x2\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x3\"}]}"); - - // 2. Sort Name DESC - request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, ""); - request.setParam(SearchOptions.PARAM_SORT, RuleNormalizer.RuleField.NAME.field()); - request.setParam(SearchOptions.PARAM_ASCENDING, "false"); - - result = request.execute(); - result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x3\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); - - } - - @Test - public void available_since() throws Exception { - ruleDao.insert(session, RuleTesting.newXooX1()); - ruleDao.insert(session, RuleTesting.newXooX2()); - session.commit(); - session.clearCache(); - - Date since = new Date(); - - // 1. find today's rules - WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, ""); - request.setParam(SearchAction.PARAM_AVAILABLE_SINCE, DateUtils.formatDate(since)); - request.setParam(SearchOptions.PARAM_SORT, RuleNormalizer.RuleField.KEY.field()); - WsTester.Result result = request.execute(); - result.assertJson("{\"total\":2,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); - - Calendar c = Calendar.getInstance(); - c.setTime(since); - c.add(Calendar.DATE, 1); // number of days to add - - // 2. no rules since tomorrow - request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); - request.setParam(SearchOptions.PARAM_FIELDS, ""); - request.setParam(SearchAction.PARAM_AVAILABLE_SINCE, DateUtils.formatDate(c.getTime())); - result = request.execute(); - result.assertJson("{\"total\":0,\"p\":1,\"ps\":100,\"rules\":[]}"); - } - - private ActiveRuleDto newActiveRule(QualityProfileDto profile, RuleDto rule) { - return ActiveRuleDto.createFor(profile, rule) - .setInheritance(null) - .setSeverity("BLOCKER"); - } - - private void insertDebtCharacteristics(DbSession dbSession) { - CharacteristicDto reliability = DebtTesting.newCharacteristicDto("RELIABILITY").setName("Reliability"); - db.debtCharacteristicDao().insert(reliability, dbSession); - - CharacteristicDto softReliability = DebtTesting.newCharacteristicDto("SOFT_RELIABILITY").setName("Soft Reliability") - .setParentId(reliability.getId()); - db.debtCharacteristicDao().insert(softReliability, dbSession); - softReliabilityId = softReliability.getId(); - - CharacteristicDto hardReliability = DebtTesting.newCharacteristicDto("HARD_RELIABILITY").setName("Hard Reliability") - .setParentId(reliability.getId()); - db.debtCharacteristicDao().insert(hardReliability, dbSession); - hardReliabilityId = hardReliability.getId(); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWsMediumTest.java new file mode 100644 index 00000000000..31e049b67fb --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWsMediumTest.java @@ -0,0 +1,657 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.rule.ws; + +import com.google.common.collect.ImmutableSet; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.RuleStatus; +import org.sonar.api.rule.Severity; +import org.sonar.api.server.debt.DebtRemediationFunction; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.DateUtils; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; +import org.sonar.core.qualityprofile.db.QualityProfileDao; +import org.sonar.core.qualityprofile.db.QualityProfileDto; +import org.sonar.core.rule.RuleDto; +import org.sonar.core.rule.RuleDto.Format; +import org.sonar.core.rule.RuleParamDto; +import org.sonar.core.technicaldebt.db.CharacteristicDto; +import org.sonar.server.db.DbClient; +import org.sonar.server.debt.DebtTesting; +import org.sonar.server.qualityprofile.QProfileTesting; +import org.sonar.server.qualityprofile.db.ActiveRuleDao; +import org.sonar.server.rule.RuleTesting; +import org.sonar.server.rule.db.RuleDao; +import org.sonar.server.rule.index.RuleNormalizer; +import org.sonar.server.search.ws.SearchOptions; +import org.sonar.server.tester.ServerTester; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RulesWsMediumTest { + + @ClassRule + public static ServerTester tester = new ServerTester(); + @Rule + public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); + + private static final String API_ENDPOINT = "api/rules"; + private static final String API_SEARCH_METHOD = "search"; + private static final String API_SHOW_METHOD = "show"; + private static final String API_TAGS_METHOD = "tags"; + + DbClient db; + RulesWs ws; + RuleDao ruleDao; + DbSession session; + int softReliabilityId, hardReliabilityId; + + @Before + public void setUp() { + tester.clearDbAndIndexes(); + db = tester.get(DbClient.class); + ruleDao = tester.get(RuleDao.class); + ws = tester.get(RulesWs.class); + session = tester.get(DbClient.class).openSession(false); + } + + @After + public void after() { + session.close(); + } + + @Test + public void define() { + WebService.Context context = new WebService.Context(); + ws.define(context); + + WebService.Controller controller = context.controller(API_ENDPOINT); + + assertThat(controller).isNotNull(); + assertThat(controller.actions()).hasSize(8); + assertThat(controller.action(API_SEARCH_METHOD)).isNotNull(); + assertThat(controller.action(API_SHOW_METHOD)).isNotNull(); + assertThat(controller.action(API_TAGS_METHOD)).isNotNull(); + assertThat(controller.action("update")).isNotNull(); + assertThat(controller.action("create")).isNotNull(); + assertThat(controller.action("delete")).isNotNull(); + assertThat(controller.action("repositories")).isNotNull(); + assertThat(controller.action("app")).isNotNull(); + } + + @Test + public void show_rule() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + + RuleDto rule = RuleTesting.newXooX1(); + ruleDao.insert(session, rule); + + ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(profile, rule).setSeverity("BLOCKER"); + tester.get(ActiveRuleDao.class).insert(session, activeRuleDto); + session.commit(); + session.clearCache(); + + // 1. With Activation + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SHOW_METHOD); + request.setParam(ShowAction.PARAM_KEY, rule.getKey().toString()); + request.setParam(ShowAction.PARAM_ACTIVES, "true"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "show_rule_active.json"); + + // 1. Default Activation (defaults to false) + request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SHOW_METHOD); + request.setParam(ShowAction.PARAM_KEY, rule.getKey().toString()); + result = request.execute(); + result.assertJson(this.getClass(), "show_rule_no_active.json"); + } + + @Test + public void search_no_rules() throws Exception { + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "actives"); + WsTester.Result result = request.execute(); + + result.assertJson(this.getClass(), "search_no_rules.json"); + } + + @Test + public void filter_by_key_rules() throws Exception { + ruleDao.insert(session, RuleTesting.newXooX1()); + ruleDao.insert(session, RuleTesting.newXooX2()); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchAction.PARAM_KEY, RuleTesting.XOO_X1.toString()); + request.setParam(SearchOptions.PARAM_FIELDS, "actives"); + WsTester.Result result = request.execute(); + result.assertJson("{\"total\":1,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x1\"}]}"); + + request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchAction.PARAM_KEY, RuleKey.of("xoo", "unknown").toString()); + request.setParam(SearchOptions.PARAM_FIELDS, "actives"); + result = request.execute(); + result.assertJson("{\"total\":0,\"p\":1,\"ps\":100,\"rules\":[],\"actives\":{}}"); + + } + + @Test + public void search_2_rules() throws Exception { + ruleDao.insert(session, RuleTesting.newXooX1()); + ruleDao.insert(session, RuleTesting.newXooX2()); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + WsTester.Result result = request.execute(); + + result.assertJson(getClass(), "search_2_rules.json"); + } + + @Test + public void search_2_rules_with_field_selection() throws Exception { + ruleDao.insert(session, RuleTesting.newXooX1()); + ruleDao.insert(session, RuleTesting.newXooX2().setDescription("A *Xoo* rule").setDescriptionFormat(Format.MARKDOWN)); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(SearchOptions.PARAM_FIELDS, "name,htmlDesc,mdDesc"); + WsTester.Result result = request.execute(); + + result.assertJson(getClass(), "search_2_rules_fields.json"); + } + + @Test + public void search_debt_rules() throws Exception { + insertDebtCharacteristics(session); + + ruleDao.insert(session, RuleTesting.newXooX1() + .setDefaultSubCharacteristicId(hardReliabilityId) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("1h") + .setDefaultRemediationOffset("15min") + + .setSubCharacteristicId(null) + .setRemediationFunction(null) + .setRemediationCoefficient(null) + .setRemediationOffset(null) + ); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); + request.setParam(SearchAction.PARAM_FACETS, "debt_characteristics"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_debt_rule.json"); + } + + @Test + public void search_debt_rules_with_default_and_overridden_debt_values() throws Exception { + insertDebtCharacteristics(session); + + ruleDao.insert(session, RuleTesting.newXooX1() + .setDefaultSubCharacteristicId(hardReliabilityId) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("1h") + .setDefaultRemediationOffset("15min") + + .setSubCharacteristicId(softReliabilityId) + .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setRemediationCoefficient("2h") + .setRemediationOffset("25min") + ); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_debt_rules_with_default_and_overridden_debt_values.json"); + } + + @Test + public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() throws Exception { + insertDebtCharacteristics(session); + + ruleDao.insert(session, RuleTesting.newXooX1() + .setDefaultSubCharacteristicId(hardReliabilityId) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("1h") + .setDefaultRemediationOffset("15min") + + .setSubCharacteristicId(softReliabilityId) + .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) + .setRemediationCoefficient(null) + .setRemediationOffset("5min") + ); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json"); + } + + @Test + public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() throws Exception { + insertDebtCharacteristics(session); + + ruleDao.insert(session, RuleTesting.newXooX1() + .setDefaultSubCharacteristicId(hardReliabilityId) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("1h") + .setDefaultRemediationOffset("15min") + + .setSubCharacteristicId(softReliabilityId) + .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) + .setRemediationCoefficient("1h") + .setRemediationOffset(null) + ); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json"); + } + + @Test + public void debt_characteristics_is_sticky_facet() throws Exception { + insertDebtCharacteristics(session); + + ruleDao.insert(session, RuleTesting.newXooX1() + .setDefaultSubCharacteristicId(hardReliabilityId) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("1h") + .setDefaultRemediationOffset("15min") + + .setSubCharacteristicId(null) + .setRemediationFunction(null) + .setRemediationCoefficient(null) + .setRemediationOffset(null) + ); + ruleDao.insert(session, RuleTesting.newXooX2() + .setDefaultSubCharacteristicId(hardReliabilityId) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("1h") + .setDefaultRemediationOffset("15min") + + .setSubCharacteristicId(softReliabilityId) + .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setRemediationCoefficient("30min") + .setRemediationOffset("5min") + ); + ruleDao.insert(session, RuleTesting.newXooX3() + .setDefaultSubCharacteristicId(null) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("2min") + .setDefaultRemediationOffset("1min") + + .setSubCharacteristicId(null) + .setRemediationFunction(null) + .setRemediationCoefficient(null) + .setRemediationOffset(null) + ); + ruleDao.insert(session, RuleTesting.newDto(RuleKey.of("xoo", "x4")).setLanguage("xoo") + .setDefaultSubCharacteristicId(softReliabilityId) + .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefaultRemediationCoefficient("2min") + .setDefaultRemediationOffset("1min") + + .setSubCharacteristicId(-1) + .setRemediationFunction(null) + .setRemediationCoefficient(null) + .setRemediationOffset(null) + ); + session.commit(); + + WsTester.Result result = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD) + .setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn") + .setParam("debt_characteristics", "SOFT_RELIABILITY") + .setParam(SearchAction.PARAM_FACETS, "debt_characteristics") + .execute(); + result.assertJson(this.getClass(), "search_debt_rules_sticky.json"); + } + + @Test + public void search_template_rules() throws Exception { + RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true); + ruleDao.insert(session, templateRule); + ruleDao.insert(session, RuleTesting.newXooX2()).setTemplateId(templateRule.getId()); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "isTemplate"); + request.setParam(SearchAction.PARAM_IS_TEMPLATE, "true"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_template_rules.json"); + } + + @Test + public void search_custom_rules_from_template_key() throws Exception { + RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true); + ruleDao.insert(session, templateRule); + ruleDao.insert(session, RuleTesting.newXooX2()).setTemplateId(templateRule.getId()); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "templateKey"); + request.setParam(SearchAction.PARAM_TEMPLATE_KEY, "xoo:x1"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_rules_from_template_key.json"); + } + + @Test + public void search_all_active_rules() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + + RuleDto rule = RuleTesting.newXooX1(); + ruleDao.insert(session, rule); + + ActiveRuleDto activeRule = newActiveRule(profile, rule); + tester.get(ActiveRuleDao.class).insert(session, activeRule); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); + request.setParam(SearchAction.PARAM_ACTIVATION, "true"); + request.setParam(SearchOptions.PARAM_FIELDS, ""); + WsTester.Result result = request.execute(); + + result.assertJson(this.getClass(), "search_active_rules.json"); + } + + @Test + public void search_profile_active_rules() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + + QualityProfileDto profile2 = QProfileTesting.newXooP2(); + tester.get(QualityProfileDao.class).insert(session, profile2); + + session.commit(); + + RuleDto rule = RuleTesting.newXooX1(); + ruleDao.insert(session, rule); + + ActiveRuleDto activeRule = newActiveRule(profile, rule); + tester.get(ActiveRuleDao.class).insert(session, activeRule); + ActiveRuleDto activeRule2 = newActiveRule(profile2, rule); + tester.get(ActiveRuleDao.class).insert(session, activeRule2); + + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); + request.setParam(SearchAction.PARAM_ACTIVATION, "true"); + request.setParam(SearchAction.PARAM_QPROFILE, profile2.getKey()); + request.setParam(SearchOptions.PARAM_FIELDS, "actives"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_profile_active_rules.json"); + + tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD) + .setParam(SearchAction.PARAM_ACTIVATION, "true") + .setParam(SearchAction.PARAM_QPROFILE, "unknown_profile") + .setParam(SearchOptions.PARAM_FIELDS, "actives") + .execute() + .assertJson(this.getClass(), "search_no_rules.json"); + } + + @Test + public void search_profile_active_rules_with_inheritance() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + + QualityProfileDto profile2 = QProfileTesting.newXooP2().setParentKee(profile.getKee()); + tester.get(QualityProfileDao.class).insert(session, profile2); + + session.commit(); + + RuleDto rule = RuleTesting.newXooX1(); + ruleDao.insert(session, rule); + + ActiveRuleDto activeRule = newActiveRule(profile, rule); + tester.get(ActiveRuleDao.class).insert(session, activeRule); + ActiveRuleDto activeRule2 = newActiveRule(profile2, rule).setInheritance(ActiveRuleDto.OVERRIDES).setSeverity(Severity.CRITICAL); + tester.get(ActiveRuleDao.class).insert(session, activeRule2); + + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); + request.setParam(SearchAction.PARAM_ACTIVATION, "true"); + request.setParam(SearchAction.PARAM_QPROFILE, profile2.getKey()); + request.setParam(SearchOptions.PARAM_FIELDS, "actives"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "search_profile_active_rules_inheritance.json"); + } + + @Test + public void search_all_active_rules_params() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + RuleDto rule = RuleTesting.newXooX1(); + ruleDao.insert(session, rule); + session.commit(); + + RuleParamDto param = RuleParamDto.createFor(rule) + .setDefaultValue("some value") + .setType("string") + .setDescription("My small description") + .setName("my_var"); + ruleDao.addRuleParam(session, rule, param); + + RuleParamDto param2 = RuleParamDto.createFor(rule) + .setDefaultValue("other value") + .setType("integer") + .setDescription("My small description") + .setName("the_var"); + ruleDao.addRuleParam(session, rule, param2); + + ActiveRuleDto activeRule = newActiveRule(profile, rule); + tester.get(ActiveRuleDao.class).insert(session, activeRule); + + ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param) + .setValue("The VALUE"); + tester.get(ActiveRuleDao.class).addParam(session, activeRule, activeRuleParam); + + ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2) + .setValue("The Other Value"); + tester.get(ActiveRuleDao.class).addParam(session, activeRule, activeRuleParam2); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_TEXT_QUERY, "x1"); + request.setParam(SearchAction.PARAM_ACTIVATION, "true"); + request.setParam(SearchOptions.PARAM_FIELDS, "params"); + WsTester.Result result = request.execute(); + + result.assertJson(this.getClass(), "search_active_rules_params.json"); + } + + @Test + public void get_tags() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + + RuleDto rule = RuleTesting.newXooX1(). + setTags(ImmutableSet.of("hello", "world")) + .setSystemTags(Collections.emptySet()); + ruleDao.insert(session, rule); + + RuleDto rule2 = RuleTesting.newXooX2() + .setTags(ImmutableSet.of("hello", "java")) + .setSystemTags(ImmutableSet.of("sys1")); + ruleDao.insert(session, rule2); + session.commit(); + + tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD).execute().assertJson(this.getClass(), "get_tags.json"); + tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD) + .setParam("ps", "1").execute().assertJson(this.getClass(), "get_tags_limited.json"); + tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD) + .setParam("q", "ll").execute().assertJson(this.getClass(), "get_tags_filtered.json"); + } + + @Test + public void get_note_as_markdown_and_html() throws Exception { + QualityProfileDto profile = QProfileTesting.newXooP1(); + tester.get(QualityProfileDao.class).insert(session, profile); + RuleDto rule = RuleTesting.newXooX1().setNoteData("this is *bold*"); + ruleDao.insert(session, rule); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, "htmlNote, mdNote"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "get_note_as_markdown_and_html.json"); + } + + @Test + public void filter_by_tags() throws Exception { + ruleDao.insert(session, RuleTesting.newXooX1() + .setTags(Collections.emptySet()) + .setSystemTags(ImmutableSet.of("tag1"))); + ruleDao.insert(session, RuleTesting.newXooX2() + .setTags(Collections.emptySet()) + .setSystemTags(ImmutableSet.of("tag2"))); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchAction.PARAM_TAGS, "tag1"); + request.setParam(SearchOptions.PARAM_FIELDS, "sysTags, tags"); + request.setParam(SearchAction.PARAM_FACETS, "tags"); + WsTester.Result result = request.execute(); + result.assertJson(this.getClass(), "filter_by_tags.json"); + } + + @Test + public void severities_facet_should_have_all_severities() throws Exception { + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchAction.PARAM_FACETS, "severities"); + request.execute().assertJson(this.getClass(), "severities_facet.json"); + } + + @Test + public void statuses_facet_should_have_all_statuses_except_removed() throws Exception { + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchAction.PARAM_FACETS, "statuses"); + request.execute().assertJson(this.getClass(), "statuses_facet.json"); + } + + @Test + public void statuses_facet_should_be_sticky() throws Exception { + ruleDao.insert(session, RuleTesting.newXooX1()); + ruleDao.insert(session, RuleTesting.newXooX2().setStatus(RuleStatus.BETA)); + ruleDao.insert(session, RuleTesting.newXooX3().setStatus(RuleStatus.DEPRECATED)); + session.commit(); + + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchAction.PARAM_STATUSES, "DEPRECATED"); + request.setParam(SearchAction.PARAM_FACETS, "statuses"); + request.execute().assertJson(this.getClass(), "statuses_facet_sticky.json"); + } + + @Test + public void sort_by_name() throws Exception { + ruleDao.insert(session, RuleTesting.newXooX1().setName("Dodgy - Consider returning a zero length array rather than null ")); + ruleDao.insert(session, RuleTesting.newXooX2().setName("Bad practice - Creates an empty zip file entry")); + ruleDao.insert(session, RuleTesting.newXooX3().setName("XPath rule")); + session.commit(); + + // 1. Sort Name Asc + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, ""); + request.setParam(SearchOptions.PARAM_SORT, "name"); + request.setParam(SearchOptions.PARAM_ASCENDING, "true"); + + WsTester.Result result = request.execute(); + result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x2\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x3\"}]}"); + + // 2. Sort Name DESC + request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, ""); + request.setParam(SearchOptions.PARAM_SORT, RuleNormalizer.RuleField.NAME.field()); + request.setParam(SearchOptions.PARAM_ASCENDING, "false"); + + result = request.execute(); + result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x3\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); + + } + + @Test + public void available_since() throws Exception { + ruleDao.insert(session, RuleTesting.newXooX1()); + ruleDao.insert(session, RuleTesting.newXooX2()); + session.commit(); + session.clearCache(); + + Date since = new Date(); + + // 1. find today's rules + WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, ""); + request.setParam(SearchAction.PARAM_AVAILABLE_SINCE, DateUtils.formatDate(since)); + request.setParam(SearchOptions.PARAM_SORT, RuleNormalizer.RuleField.KEY.field()); + WsTester.Result result = request.execute(); + result.assertJson("{\"total\":2,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); + + Calendar c = Calendar.getInstance(); + c.setTime(since); + c.add(Calendar.DATE, 1); // number of days to add + + // 2. no rules since tomorrow + request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + request.setParam(SearchOptions.PARAM_FIELDS, ""); + request.setParam(SearchAction.PARAM_AVAILABLE_SINCE, DateUtils.formatDate(c.getTime())); + result = request.execute(); + result.assertJson("{\"total\":0,\"p\":1,\"ps\":100,\"rules\":[]}"); + } + + private ActiveRuleDto newActiveRule(QualityProfileDto profile, RuleDto rule) { + return ActiveRuleDto.createFor(profile, rule) + .setInheritance(null) + .setSeverity("BLOCKER"); + } + + private void insertDebtCharacteristics(DbSession dbSession) { + CharacteristicDto reliability = DebtTesting.newCharacteristicDto("RELIABILITY").setName("Reliability"); + db.debtCharacteristicDao().insert(reliability, dbSession); + + CharacteristicDto softReliability = DebtTesting.newCharacteristicDto("SOFT_RELIABILITY").setName("Soft Reliability") + .setParentId(reliability.getId()); + db.debtCharacteristicDao().insert(softReliability, dbSession); + softReliabilityId = softReliability.getId(); + + CharacteristicDto hardReliability = DebtTesting.newCharacteristicDto("HARD_RELIABILITY").setName("Hard Reliability") + .setParentId(reliability.getId()); + db.debtCharacteristicDao().insert(hardReliability, dbSession); + hardReliabilityId = hardReliability.getId(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/ListingWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/ws/ListingWsTest.java index 04a290e4d2a..0243e8ed673 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ws/ListingWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ws/ListingWsTest.java @@ -55,13 +55,13 @@ public class ListingWsTest { @Test public void list() throws Exception { - WsTester tester = new WsTester(ws, new MetricWebService()); + WsTester tester = new WsTester(ws, new MetricWs()); tester.newGetRequest("api/webservices", "list").execute().assertJson(getClass(), "list.json"); } @Test public void list_including_internals() throws Exception { - WsTester tester = new WsTester(ws, new MetricWebService()); + WsTester tester = new WsTester(ws, new MetricWs()); tester.newGetRequest("api/webservices", "list") .setParam("include_internals", "true") .execute() @@ -70,7 +70,7 @@ public class ListingWsTest { @Test public void response_example() throws Exception { - WsTester tester = new WsTester(ws, new MetricWebService()); + WsTester tester = new WsTester(ws, new MetricWs()); tester .newGetRequest("api/webservices", "response_example") .setParam("controller", "api/metric") @@ -78,7 +78,7 @@ public class ListingWsTest { .execute().assertJson(getClass(), "response_example.json"); } - static class MetricWebService implements WebService { + static class MetricWs implements WebService { @Override public void define(Context context) { NewController newController = context.createController("api/metric") diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java b/server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java index 19ea3af700a..186feed789d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java @@ -20,11 +20,6 @@ package org.sonar.server.ws; import com.google.common.collect.Maps; -import java.io.IOException; -import java.io.InputStream; -import java.util.Locale; -import java.util.Map; -import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.Before; @@ -42,6 +37,12 @@ import org.sonar.server.exceptions.Message; import org.sonar.server.plugins.MimeTypes; import org.sonar.server.tester.UserSessionRule; +import javax.annotation.Nullable; +import java.io.IOException; +import java.io.InputStream; +import java.util.Locale; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -95,7 +96,7 @@ public class WebServiceEngineTest { @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); I18n i18n = mock(I18n.class); - WebServiceEngine engine = new WebServiceEngine(new WebService[] {new SystemWebService()}, i18n, userSessionRule); + WebServiceEngine engine = new WebServiceEngine(new WebService[] {new SystemWs()}, i18n, userSessionRule); @Before public void start() { @@ -290,7 +291,7 @@ public class WebServiceEngineTest { assertThat(response.stream().mediaType()).isEqualTo(MimeTypes.JSON); } - static class SystemWebService implements WebService { + static class SystemWs implements WebService { @Override public void define(Context context) { NewController newController = context.createController("api/system"); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/RubyRailsWebservice.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/RubyRailsWebservice.java index d61936f5d38..67be027294a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/RubyRailsWebservice.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/RubyRailsWebservice.java @@ -29,7 +29,7 @@ package org.sonar.api.web; * and will call the index controller method if not specified. * * @since 1.11 - * @deprecated in 4.2. Replaced by {@link org.sonar.api.web.ws.WebService} + * @deprecated in 4.2. Replaced by {@link org.sonar.api.server.ws.WebService} */ @Deprecated public interface RubyRailsWebservice extends Webservice { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java index e0ba0f40ef3..24a0f2a69fb 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java @@ -73,7 +73,7 @@ public class RequestTest { } } - private static class SimpleWebService implements WebService { + private static class SimpleWs implements WebService { @Override public void define(Context context) { @@ -114,7 +114,7 @@ public class RequestTest { @Before public void before() { WebService.Context context = new WebService.Context(); - new SimpleWebService().define(context); + new SimpleWs().define(context); request.setAction(context.controller("my_controller").action("my_action")); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java index 9226ea1af60..006aae7c064 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java @@ -35,7 +35,7 @@ import static org.mockito.Mockito.mock; public class WebServiceTest { - static class MetricWebService implements WebService { + static class MetricWs implements WebService { boolean showCalled = false, createCalled = false; @Override @@ -89,7 +89,7 @@ public class WebServiceTest { @Test public void define_web_service() { - MetricWebService metricWs = new MetricWebService(); + MetricWs metricWs = new MetricWs(); metricWs.define(context); @@ -125,7 +125,7 @@ public class WebServiceTest { @Test public void fail_if_duplicated_ws_keys() { - MetricWebService metricWs = new MetricWebService(); + MetricWs metricWs = new MetricWs(); metricWs.define(context); try { new WebService() { @@ -240,7 +240,7 @@ public class WebServiceTest { @Test public void handle_request() throws Exception { - MetricWebService metricWs = new MetricWebService(); + MetricWs metricWs = new MetricWs(); metricWs.define(context); assertThat(metricWs.showCalled).isFalse(); @@ -416,7 +416,7 @@ public class WebServiceTest { @Test public void response_example() { - MetricWebService metricWs = new MetricWebService(); + MetricWs metricWs = new MetricWs(); metricWs.define(context); WebService.Action action = context.controller("api/metric").action("create");