From d9280761c4e45c6fce77ef507ea50e2c2d0c4f8e Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 10 Jan 2018 15:04:43 +0100 Subject: [PATCH] SONAR-10266 Create stub for api/project_badges WS --- .../sonar/server/badge/ws/MeasureAction.java | 58 ++++++++++++++++++ .../server/badge/ws/ProjectBadgesWs.java | 42 +++++++++++++ .../badge/ws/ProjectBadgesWsAction.java | 26 ++++++++ .../badge/ws/ProjectBadgesWsModule.java | 45 ++++++++++++++ .../server/badge/ws/QualityGateAction.java | 59 +++++++++++++++++++ .../sonar/server/badge/ws/package-info.java | 23 ++++++++ .../platformlevel/PlatformLevel4.java | 4 ++ .../sonar/server/badge/ws/measure-example.svg | 22 +++++++ .../server/badge/ws/quality_gate-example.svg | 22 +++++++ .../server/badge/ws/MeasureActionTest.java | 56 ++++++++++++++++++ .../badge/ws/ProjectBadgesWsModuleTest.java | 53 +++++++++++++++++ .../badge/ws/QualityGateActionTest.java | 56 ++++++++++++++++++ 12 files changed, 466 insertions(+) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/badge/ws/MeasureAction.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWs.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsAction.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsModule.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/badge/ws/QualityGateAction.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/badge/ws/package-info.java create mode 100644 server/sonar-server/src/main/resources/org/sonar/server/badge/ws/measure-example.svg create mode 100644 server/sonar-server/src/main/resources/org/sonar/server/badge/ws/quality_gate-example.svg create mode 100644 server/sonar-server/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/badge/ws/MeasureAction.java b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/MeasureAction.java new file mode 100644 index 00000000000..d0fceddd9eb --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/MeasureAction.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import com.google.common.io.Resources; +import org.apache.commons.io.IOUtils; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.server.ws.WebService.NewAction; + +import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; + +public class MeasureAction implements ProjectBadgesWsAction { + + public static final String PARAM_COMPONENT = "component"; + public static final String PARAM_METRIC = "metric"; + + @Override + public void define(WebService.NewController controller) { + NewAction action = controller.createAction("measure") + .setHandler(this) + .setDescription("Generate badge for measure as an SVG") + .setResponseExample(Resources.getResource(getClass(), "measure-example.svg")); + action.createParam(PARAM_COMPONENT) + .setDescription("Project key") + .setRequired(true) + .setExampleValue(KEY_PROJECT_EXAMPLE_001); + action.createParam(PARAM_METRIC) + .setDescription("Metric key") + .setRequired(true) + .setExampleValue(KEY_PROJECT_EXAMPLE_001); + } + + @Override + public void handle(Request request, Response response) throws Exception { + response.stream().setMediaType("image/svg+xml"); + IOUtils.copy(Resources.getResource(getClass(), "measure-example.svg").openStream(), response.stream().output()); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWs.java b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWs.java new file mode 100644 index 00000000000..2b95f7b8ded --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWs.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import java.util.List; +import org.sonar.api.server.ws.WebService; + +public class ProjectBadgesWs implements WebService { + + private final List actions; + + public ProjectBadgesWs(List actions) { + this.actions = actions; + } + + @Override + public void define(Context context) { + NewController controller = context.createController("api/project_badges"); + controller.setDescription("Generate badges based on quality gates or measures"); + controller.setSince("7.1"); + actions.forEach(action -> action.define(controller)); + controller.done(); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsAction.java new file mode 100644 index 00000000000..5e8381b1299 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsAction.java @@ -0,0 +1,26 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import org.sonar.server.ws.WsAction; + +interface ProjectBadgesWsAction extends WsAction { + // Marker interface +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsModule.java new file mode 100644 index 00000000000..085fdc36291 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWsModule.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import org.sonar.api.config.Configuration; +import org.sonar.core.config.WebConstants; +import org.sonar.core.platform.Module; + +public class ProjectBadgesWsModule extends Module { + + private final Configuration config; + + public ProjectBadgesWsModule(Configuration config) { + this.config = config; + } + + @Override + protected void configureModule() { + if (!config.getBoolean(WebConstants.SONARCLOUD_ENABLED).orElse(false)) { + return; + } + add( + ProjectBadgesWs.class, + QualityGateAction.class, + MeasureAction.class + ); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/badge/ws/QualityGateAction.java b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/QualityGateAction.java new file mode 100644 index 00000000000..6f605191db2 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/QualityGateAction.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import com.google.common.io.Resources; +import org.apache.commons.io.IOUtils; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.server.ws.WebService.NewAction; + +import static java.util.Arrays.asList; +import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; + +public class QualityGateAction implements ProjectBadgesWsAction { + + public static final String PARAM_COMPONENT = "component"; + public static final String PARAM_TYPE = "type"; + + @Override + public void define(WebService.NewController controller) { + NewAction action = controller.createAction("quality_gate") + .setHandler(this) + .setDescription("Generate badge for quality gate as an SVG") + .setResponseExample(Resources.getResource(getClass(), "quality_gate-example.svg")); + action.createParam(PARAM_COMPONENT) + .setDescription("Project key") + .setRequired(true) + .setExampleValue(KEY_PROJECT_EXAMPLE_001); + action.createParam(PARAM_TYPE) + .setDescription("Type of badge.") + .setRequired(false) + .setPossibleValues(asList("BADGE", "CARD")); + } + + @Override + public void handle(Request request, Response response) throws Exception { + response.stream().setMediaType("image/svg+xml"); + IOUtils.copy(Resources.getResource(getClass(), "quality_gate-example.svg").openStream(), response.stream().output()); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/badge/ws/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/package-info.java new file mode 100644 index 00000000000..747c157c316 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/badge/ws/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.server.badge.ws; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index db08d7499b5..89cf7930d08 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -35,6 +35,7 @@ import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.timemachine.Periods; import org.sonar.server.authentication.AuthenticationModule; import org.sonar.server.authentication.LogOAuthWarning; +import org.sonar.server.badge.ws.ProjectBadgesWsModule; import org.sonar.server.batch.BatchWsModule; import org.sonar.server.branch.BranchFeatureProxyImpl; import org.sonar.server.branch.ws.BranchWsModule; @@ -535,6 +536,9 @@ public class PlatformLevel4 extends PlatformLevel { // Branch BranchFeatureProxyImpl.class, + // Project badges + ProjectBadgesWsModule.class, + // privileged plugins PrivilegedPluginsBootstraper.class, PrivilegedPluginsStopper.class, diff --git a/server/sonar-server/src/main/resources/org/sonar/server/badge/ws/measure-example.svg b/server/sonar-server/src/main/resources/org/sonar/server/badge/ws/measure-example.svg new file mode 100644 index 00000000000..8934ac3cfc2 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/badge/ws/measure-example.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + Coverage + Coverage + 100.0% + 100.0% + + \ No newline at end of file diff --git a/server/sonar-server/src/main/resources/org/sonar/server/badge/ws/quality_gate-example.svg b/server/sonar-server/src/main/resources/org/sonar/server/badge/ws/quality_gate-example.svg new file mode 100644 index 00000000000..038231f8b52 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/badge/ws/quality_gate-example.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + Quality Gate + Quality Gate + failing + failing + + \ No newline at end of file diff --git a/server/sonar-server/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java new file mode 100644 index 00000000000..51b619e5f92 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import org.junit.Test; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.server.ws.WebService.Param; +import org.sonar.server.ws.WsActionTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class MeasureActionTest { + + private WsActionTester ws = new WsActionTester(new MeasureAction()); + + @Test + public void test_definition() { + WebService.Action def = ws.getDef(); + assertThat(def.key()).isEqualTo("measure"); + assertThat(def.isInternal()).isFalse(); + assertThat(def.isPost()).isFalse(); + assertThat(def.since()).isNull(); + assertThat(def.responseExampleAsString()).isNotEmpty(); + + assertThat(def.params()) + .extracting(Param::key, Param::isRequired) + .containsExactlyInAnyOrder( + tuple("component", true), + tuple("metric", true)); + } + + @Test + public void test_example() { + String response = ws.newRequest().execute().getInput(); + + assertThat(response).isEqualTo(ws.getDef().responseExampleAsString()); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java new file mode 100644 index 00000000000..69574191e11 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/badge/ws/ProjectBadgesWsModuleTest.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import org.junit.Test; +import org.sonar.api.config.internal.MapSettings; +import org.sonar.core.platform.ComponentContainer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; + +public class ProjectBadgesWsModuleTest { + + private ComponentContainer container = new ComponentContainer(); + private MapSettings mapSettings = new MapSettings(); + private ProjectBadgesWsModule underTest = new ProjectBadgesWsModule(mapSettings.asConfig()); + + @Test + public void verify_count_of_added_components() { + mapSettings.setProperty("sonar.sonarcloud.enabled", true); + + underTest.configure(container); + + assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 3); + } + + @Test + public void no_component_when_not_on_sonar_cloud() { + mapSettings.setProperty("sonar.sonarcloud.enabled", false); + + underTest.configure(container); + + assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java new file mode 100644 index 00000000000..cfecf96178b --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.badge.ws; + +import org.junit.Test; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.server.ws.WebService.Param; +import org.sonar.server.ws.WsActionTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class QualityGateActionTest { + + private WsActionTester ws = new WsActionTester(new QualityGateAction()); + + @Test + public void test_definition() { + WebService.Action def = ws.getDef(); + assertThat(def.key()).isEqualTo("quality_gate"); + assertThat(def.isInternal()).isFalse(); + assertThat(def.isPost()).isFalse(); + assertThat(def.since()).isNull(); + assertThat(def.responseExampleAsString()).isNotEmpty(); + + assertThat(def.params()) + .extracting(Param::key, Param::isRequired) + .containsExactlyInAnyOrder( + tuple("component", true), + tuple("type", false)); + } + + @Test + public void test_example() { + String response = ws.newRequest().execute().getInput(); + + assertThat(response).isEqualTo(ws.getDef().responseExampleAsString()); + } +} -- 2.39.5