From b94a2e555ec5392412e46d59fbf40be6d4cb07ca Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 5 Jun 2015 16:59:56 +0200 Subject: [PATCH] SONAR-6616 WS custom_measures/delete --- .../persistence/CustomMeasureDao.java | 15 +- .../custommeasure/ws/CustomMeasuresWs.java | 46 +++++++ .../ws/CustomMeasuresWsAction.java | 27 ++++ .../ws/CustomMeasuresWsModule.java | 32 +++++ .../server/custommeasure/ws/DeleteAction.java | 87 ++++++++++++ .../server/custommeasure/ws/package-info.java | 24 ++++ .../platformlevel/PlatformLevel4.java | 7 +- .../persistence/CustomMeasureDaoTest.java | 5 +- .../ws/CustomMeasuresWsModuleTest.java | 35 +++++ .../ws/CustomMeasuresWsTest.java | 58 ++++++++ .../custommeasure/ws/DeleteActionTest.java | 129 ++++++++++++++++++ .../custommeasure/db/CustomMeasureDto.java | 8 +- .../custommeasure/db/CustomMeasureMapper.java | 3 +- .../custommeasure/db/CustomMeasureMapper.xml | 5 + 14 files changed, 469 insertions(+), 12 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWs.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsAction.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModule.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/DeleteAction.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/package-info.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModuleTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/DeleteActionTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/custommeasure/persistence/CustomMeasureDao.java b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/persistence/CustomMeasureDao.java index a4aa7139392..ff92ecd4004 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/custommeasure/persistence/CustomMeasureDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/persistence/CustomMeasureDao.java @@ -30,6 +30,7 @@ import org.sonar.core.custommeasure.db.CustomMeasureMapper; import org.sonar.core.persistence.DaoComponent; import org.sonar.core.persistence.DaoUtils; import org.sonar.core.persistence.DbSession; +import org.sonar.server.exceptions.NotFoundException; @ServerSide public class CustomMeasureDao implements DaoComponent { @@ -37,6 +38,10 @@ public class CustomMeasureDao implements DaoComponent { mapper(session).insert(customMeasureDto); } + public void delete(DbSession session, long id) { + mapper(session).delete(id); + } + public void deleteByMetricIds(final DbSession session, final List metricIds) { DaoUtils.executeLargeInputsWithoutOutput(metricIds, new Function, Void>() { @Override @@ -52,11 +57,19 @@ public class CustomMeasureDao implements DaoComponent { return mapper(session).selectById(id); } + public CustomMeasureDto selectById(DbSession session, long id) { + CustomMeasureDto customMeasure = selectNullableById(session, id); + if (customMeasure == null) { + throw new NotFoundException(String.format("CustomMeasure '%d' not found", id)); + } + return customMeasure; + } + public List selectByMetricId(DbSession session, int id) { return mapper(session).selectByMetricId(id); } - public List selectByComponentId(DbSession session, int id) { + public List selectByComponentId(DbSession session, long id) { return mapper(session).selectByComponentId(id); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWs.java b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWs.java new file mode 100644 index 00000000000..f67a62ff1e4 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWs.java @@ -0,0 +1,46 @@ +/* + * 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.custommeasure.ws; + +import org.sonar.api.server.ws.WebService; + +public class CustomMeasuresWs implements WebService { + public static final String ENDPOINT = "api/custom_measures"; + + private final CustomMeasuresWsAction[] actions; + + public CustomMeasuresWs(CustomMeasuresWsAction... actions) { + this.actions = actions; + } + + @Override + public void define(Context context) { + NewController controller = context.createController(ENDPOINT) + .setDescription("Custom measures management") + .setSince("5.2"); + + for (CustomMeasuresWsAction action : actions) { + action.define(controller); + } + + controller.done(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsAction.java new file mode 100644 index 00000000000..2fa1a82ddf2 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsAction.java @@ -0,0 +1,27 @@ +/* + * 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.custommeasure.ws; + +import org.sonar.server.ws.WsAction; + +public interface CustomMeasuresWsAction extends WsAction { + // marker interface +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModule.java new file mode 100644 index 00000000000..108cc8c6e09 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModule.java @@ -0,0 +1,32 @@ +/* + * 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.custommeasure.ws; + +import org.sonar.core.component.Module; + +public class CustomMeasuresWsModule extends Module { + @Override + protected void configureModule() { + add( + CustomMeasuresWs.class, + DeleteAction.class); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/DeleteAction.java new file mode 100644 index 00000000000..56349d0cef2 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/DeleteAction.java @@ -0,0 +1,87 @@ +/* + * 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.custommeasure.ws; + +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.web.UserRole; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.custommeasure.db.CustomMeasureDto; +import org.sonar.core.permission.GlobalPermissions; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.MyBatis; +import org.sonar.server.db.DbClient; +import org.sonar.server.user.UserSession; + +public class DeleteAction implements CustomMeasuresWsAction { + + private static final String ACTION = "delete"; + public static final String PARAM_ID = "id"; + + private final DbClient dbClient; + private final UserSession userSession; + + public DeleteAction(DbClient dbClient, UserSession userSession) { + this.dbClient = dbClient; + this.userSession = userSession; + } + + @Override + public void define(WebService.NewController context) { + WebService.NewAction action = context.createAction(ACTION) + .setPost(true) + .setHandler(this) + .setSince("5.2") + .setDescription("Delete a custom measure.
Requires 'Administer System' permission or 'Administer' permission on the project."); + + action.createParam(PARAM_ID) + .setDescription("Id") + .setExampleValue("24") + .setRequired(true); + } + + @Override + public void handle(Request request, Response response) throws Exception { + long id = request.mandatoryParamAsLong(PARAM_ID); + + DbSession dbSession = dbClient.openSession(false); + try { + CustomMeasureDto customMeasure = dbClient.customMeasureDao().selectById(dbSession, id); + checkPermissions(dbSession, customMeasure); + dbClient.customMeasureDao().delete(dbSession, id); + dbSession.commit(); + } finally { + MyBatis.closeQuietly(dbSession); + } + + response.noContent(); + } + + private void checkPermissions(DbSession dbSession, CustomMeasureDto customMeasure) { + if (userSession.hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN)) { + return; + } + + ComponentDto component = dbClient.componentDao().selectById(customMeasure.getComponentId(), dbSession); + userSession.checkLoggedIn().checkProjectUuidPermission(UserRole.ADMIN, component.projectUuid()); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/package-info.java new file mode 100644 index 00000000000..29a6629de4c --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/package-info.java @@ -0,0 +1,24 @@ +/* + * 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. + */ + +@ParametersAreNonnullByDefault +package org.sonar.server.custommeasure.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 eb78b0ffb89..ca0260e5dea 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 @@ -75,6 +75,7 @@ import org.sonar.server.computation.ws.HistoryAction; import org.sonar.server.computation.ws.IsQueueEmptyWs; import org.sonar.server.computation.ws.QueueAction; import org.sonar.server.config.ws.PropertiesWs; +import org.sonar.server.custommeasure.ws.CustomMeasuresWsModule; import org.sonar.server.dashboard.template.GlobalDefaultDashboard; import org.sonar.server.dashboard.template.ProjectDefaultDashboard; import org.sonar.server.dashboard.template.ProjectIssuesDashboard; @@ -489,14 +490,14 @@ public class PlatformLevel4 extends PlatformLevel { MeasureFilterFactory.class, MeasureFilterExecutor.class, MeasureFilterEngine.class, + ManualMeasuresWs.class, + MetricsWsModule.class, + CustomMeasuresWsModule.class, ProjectFilter.class, MyFavouritesFilter.class, CoreCustomMetrics.class, DefaultMetricFinder.class, TimeMachineWs.class, - ManualMeasuresWs.class, - - MetricsWsModule.class, // quality gates QualityGateDao.class, diff --git a/server/sonar-server/src/test/java/org/sonar/server/custommeasure/persistence/CustomMeasureDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/persistence/CustomMeasureDaoTest.java index a44a6074de0..60c6a0232d5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/custommeasure/persistence/CustomMeasureDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/persistence/CustomMeasureDaoTest.java @@ -94,10 +94,9 @@ public class CustomMeasureDaoTest { sut.insert(session, newCustomMeasureDto().setComponentId(2)); session.commit(); - List result = sut.selectByComponentId(session, 1); + List result = sut.selectByComponentId(session, 1L); assertThat(result).hasSize(2); - assertThat(result).extracting("componentId").containsOnly(1); - + assertThat(result).extracting("componentId").containsOnly(1L); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModuleTest.java new file mode 100644 index 00000000000..caff479e7e6 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModuleTest.java @@ -0,0 +1,35 @@ +/* + * 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.custommeasure.ws; + +import org.junit.Test; +import org.sonar.core.platform.ComponentContainer; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CustomMeasuresWsModuleTest { + @Test + public void verify_count_of_added_components() { + ComponentContainer container = new ComponentContainer(); + new CustomMeasuresWsModule().configure(container); + assertThat(container.size()).isEqualTo(4); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsTest.java new file mode 100644 index 00000000000..085ef9b8e61 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsTest.java @@ -0,0 +1,58 @@ +/* + * 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.custommeasure.ws; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.server.ws.WebService; +import org.sonar.server.db.DbClient; +import org.sonar.server.user.UserSession; +import org.sonar.server.ws.WsTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class CustomMeasuresWsTest { + WsTester ws; + + @Before + public void setUp() { + DbClient dbClient = mock(DbClient.class); + UserSession userSession = mock(UserSession.class); + ws = new WsTester(new CustomMeasuresWs( + new DeleteAction(dbClient, userSession) + )); + } + + @Test + public void define_ws() { + WebService.Controller controller = ws.controller("api/custom_measures"); + assertThat(controller).isNotNull(); + assertThat(controller.description()).isNotEmpty(); + assertThat(controller.actions()).hasSize(1); + } + + @Test + public void delete_action_properties() { + WebService.Action deleteAction = ws.controller("api/custom_measures").action("delete"); + assertThat(deleteAction.isPost()).isTrue(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/DeleteActionTest.java new file mode 100644 index 00000000000..b31a733124b --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/DeleteActionTest.java @@ -0,0 +1,129 @@ +/* + * 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.custommeasure.ws; + +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.web.UserRole; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.custommeasure.db.CustomMeasureDto; +import org.sonar.core.permission.GlobalPermissions; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.DbTester; +import org.sonar.server.component.ComponentTesting; +import org.sonar.server.component.db.ComponentDao; +import org.sonar.server.custommeasure.persistence.CustomMeasureDao; +import org.sonar.server.db.DbClient; +import org.sonar.server.exceptions.ForbiddenException; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.server.custommeasure.persistence.CustomMeasureTesting.newCustomMeasureDto; +import static org.sonar.server.custommeasure.ws.DeleteAction.PARAM_ID; + +public class DeleteActionTest { + + public static final String ACTION = "delete"; + + @ClassRule + public static DbTester db = new DbTester(); + @Rule + public UserSessionRule userSessionRule = UserSessionRule.standalone(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + WsTester ws; + DbClient dbClient; + DbSession dbSession; + + @Before + public void setUp() { + dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new ComponentDao()); + dbSession = dbClient.openSession(false); + ws = new WsTester(new CustomMeasuresWs(new DeleteAction(dbClient, userSessionRule))); + userSessionRule.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); + db.truncateTables(); + } + + @After + public void tearDown() { + dbSession.close(); + } + + @Test + public void delete_in_db() throws Exception { + long id = insertCustomMeasure(newCustomMeasureDto()); + long anotherId = insertCustomMeasure(newCustomMeasureDto()); + assertThat(dbClient.customMeasureDao().selectNullableById(dbSession, id)).isNotNull(); + + WsTester.Result response = newRequest().setParam(PARAM_ID, String.valueOf(id)).execute(); + dbSession.commit(); + + assertThat(dbClient.customMeasureDao().selectNullableById(dbSession, id)).isNull(); + assertThat(dbClient.customMeasureDao().selectNullableById(dbSession, anotherId)).isNotNull(); + response.assertNoContent(); + } + + @Test + public void delete_in_db_when_admin_on_project() throws Exception { + ComponentDto project = ComponentTesting.newProjectDto("project-uuid"); + dbClient.componentDao().insert(dbSession, project); + userSessionRule.login("login").addProjectUuidPermissions(UserRole.ADMIN, "project-uuid"); + long id = insertCustomMeasure(newCustomMeasureDto().setComponentId(project.getId())); + + newRequest().setParam(PARAM_ID, String.valueOf(id)).execute(); + + assertThat(dbClient.customMeasureDao().selectNullableById(dbSession, id)).isNull(); + } + + @Test + public void fail_when_not_found_in_db() throws Exception { + expectedException.expect(NotFoundException.class); + + newRequest().setParam(PARAM_ID, "42").execute(); + } + + @Test + public void fail_when_insufficient_permissions() throws Exception { + expectedException.expect(ForbiddenException.class); + userSessionRule.login("login"); + ComponentDto project = ComponentTesting.newProjectDto("any-uuid"); + dbClient.componentDao().insert(dbSession, project); + long id = insertCustomMeasure(newCustomMeasureDto().setComponentId(project.getId())); + + newRequest().setParam(PARAM_ID, String.valueOf(id)).execute(); + } + + private long insertCustomMeasure(CustomMeasureDto customMeasure) { + dbClient.customMeasureDao().insert(dbSession, customMeasure); + dbSession.commit(); + return customMeasure.getId(); + } + + private WsTester.TestRequest newRequest() { + return ws.newPostRequest(CustomMeasuresWs.ENDPOINT, ACTION); + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureDto.java b/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureDto.java index 8a978370727..20f139252af 100644 --- a/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureDto.java +++ b/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureDto.java @@ -23,7 +23,7 @@ package org.sonar.core.custommeasure.db; public class CustomMeasureDto { private long id; private int metricId; - private int componentId; + private long componentId; private double value; private String textValue; private String userLogin; @@ -67,12 +67,12 @@ public class CustomMeasureDto { return this; } - public int getComponentId() { + public long getComponentId() { return componentId; } - public CustomMeasureDto setComponentId(int resourceId) { - this.componentId = resourceId; + public CustomMeasureDto setComponentId(long componentId) { + this.componentId = componentId; return this; } diff --git a/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureMapper.java b/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureMapper.java index 94f8a4908a7..b2f3d4b9d79 100644 --- a/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureMapper.java @@ -32,6 +32,7 @@ public interface CustomMeasureMapper { List selectByMetricId(int id); - List selectByComponentId(int id); + List selectByComponentId(long id); + void delete(long id); } diff --git a/sonar-core/src/main/resources/org/sonar/core/custommeasure/db/CustomMeasureMapper.xml b/sonar-core/src/main/resources/org/sonar/core/custommeasure/db/CustomMeasureMapper.xml index 65226d1c369..b796a7ac0bf 100644 --- a/sonar-core/src/main/resources/org/sonar/core/custommeasure/db/CustomMeasureMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/custommeasure/db/CustomMeasureMapper.xml @@ -53,4 +53,9 @@ #{metricId} + + + delete from manual_measures + where id=#{id} + -- 2.39.5