]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6616 WS custom_measures/delete 358/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 5 Jun 2015 14:59:56 +0000 (16:59 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 8 Jun 2015 10:02:25 +0000 (12:02 +0200)
14 files changed:
server/sonar-server/src/main/java/org/sonar/server/custommeasure/persistence/CustomMeasureDao.java
server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWs.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModule.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/DeleteAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/custommeasure/ws/package-info.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/test/java/org/sonar/server/custommeasure/persistence/CustomMeasureDaoTest.java
server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsModuleTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/CustomMeasuresWsTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/custommeasure/ws/DeleteActionTest.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureDto.java
sonar-core/src/main/java/org/sonar/core/custommeasure/db/CustomMeasureMapper.java
sonar-core/src/main/resources/org/sonar/core/custommeasure/db/CustomMeasureMapper.xml

index a4aa7139392104743e3fb236663ca921487931f6..ff92ecd400412abdb6e20d0138ed9f2b0df3d5da 100644 (file)
@@ -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<Integer> metricIds) {
     DaoUtils.executeLargeInputsWithoutOutput(metricIds, new Function<List<Integer>, 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<CustomMeasureDto> selectByMetricId(DbSession session, int id) {
     return mapper(session).selectByMetricId(id);
   }
 
-  public List<CustomMeasureDto> selectByComponentId(DbSession session, int id) {
+  public List<CustomMeasureDto> 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 (file)
index 0000000..f67a62f
--- /dev/null
@@ -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 (file)
index 0000000..2fa1a82
--- /dev/null
@@ -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 (file)
index 0000000..108cc8c
--- /dev/null
@@ -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 (file)
index 0000000..56349d0
--- /dev/null
@@ -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.<br /> 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 (file)
index 0000000..29a6629
--- /dev/null
@@ -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;
index eb78b0ffb895793da1b063c610b28c0374721a62..ca0260e5dea60e57ffceb1270b7501c25004df88 100644 (file)
@@ -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,
index a44a6074de069e11c62ecdb647c763c313ce4ccf..60c6a0232d5a148cfb2e9e3fa508a76cfad492b3 100644 (file)
@@ -94,10 +94,9 @@ public class CustomMeasureDaoTest {
     sut.insert(session, newCustomMeasureDto().setComponentId(2));
     session.commit();
 
-    List<CustomMeasureDto> result = sut.selectByComponentId(session, 1);
+    List<CustomMeasureDto> 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 (file)
index 0000000..caff479
--- /dev/null
@@ -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 (file)
index 0000000..085ef9b
--- /dev/null
@@ -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 (file)
index 0000000..b31a733
--- /dev/null
@@ -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);
+  }
+}
index 8a9783707276d11ba33a271714d064a53754a71e..20f139252aff61a2338ce6c11aa963628cd7ce4f 100644 (file)
@@ -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;
   }
 
index 94f8a4908a75341b9932caf7709c0ae540ed07bb..b2f3d4b9d79fd068709a655d3283eff200e22d7f 100644 (file)
@@ -32,6 +32,7 @@ public interface CustomMeasureMapper {
 
   List<CustomMeasureDto> selectByMetricId(int id);
 
-  List<CustomMeasureDto> selectByComponentId(int id);
+  List<CustomMeasureDto> selectByComponentId(long id);
 
+  void delete(long id);
 }
index 65226d1c3691ff51b3c8fc69cc2944f59ca2f84b..b796a7ac0bfa732fa19e375b96db4fee2d376b40 100644 (file)
@@ -53,4 +53,9 @@
       #{metricId}
     </foreach>
   </delete>
+
+  <delete id="delete">
+    delete from manual_measures
+    where id=#{id}
+  </delete>
 </mapper>