aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-06-24 14:21:16 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-06-24 14:21:16 +0200
commit20589b0579adb0c86f74aad19962b84b4a8901bd (patch)
tree1a105ddc3b6a553e35183acd63e401d13d5ca4b5 /server
parente8bff4270a78e6468a348c87901212dfa6e54f66 (diff)
downloadsonarqube-20589b0579adb0c86f74aad19962b84b4a8901bd.tar.gz
sonarqube-20589b0579adb0c86f74aad19962b84b4a8901bd.zip
SONAR-6610 ws custom_measures/create add user and correct date in response
- only project and module are allowed - add user in response - set created_at and updated_at field in db and response - update several error message to make them clearer
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java17
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureValidator.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java24
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasureValidatorTest.java4
4 files changed, 38 insertions, 9 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java
index af5c91f22ef..68d28fb9401 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java
@@ -21,6 +21,7 @@
package org.sonar.server.measure.custom.ws;
import java.net.HttpURLConnection;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
@@ -73,7 +74,7 @@ public class CreateAction implements CustomMeasuresWsAction {
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction(ACTION)
.setDescription("Create a custom measure.<br /> " +
- "The project id or the project key must be provided. The metric id or the metric key must be provided.<br/>" +
+ "The project id or the project key must be provided (only project and module custom measures can be created). The metric id or the metric key must be provided.<br/>" +
"Requires 'Administer System' permission or 'Administer' permission on the project.")
.setSince("5.2")
.setPost(true)
@@ -116,6 +117,7 @@ public class CreateAction implements CustomMeasuresWsAction {
ComponentDto component = searchProject(dbSession, request);
MetricDto metric = searchMetric(dbSession, request);
checkPermissions(component);
+ checkIsProjectOrModule(component);
checkMeasureDoesNotExistAlready(dbSession, component, metric);
UserDoc user = userIndex.getByLogin(userSession.getLogin());
CustomMeasureDto measure = new CustomMeasureDto()
@@ -124,7 +126,8 @@ public class CreateAction implements CustomMeasuresWsAction {
.setMetricId(metric.getId())
.setDescription(description)
.setUserLogin(user.login())
- .setCreatedAt(now);
+ .setCreatedAt(now)
+ .setUpdatedAt(now);
validator.setMeasureValue(measure, valueAsString, metric);
dbClient.customMeasureDao().insert(dbSession, measure);
dbSession.commit();
@@ -137,6 +140,12 @@ public class CreateAction implements CustomMeasuresWsAction {
}
}
+ private static void checkIsProjectOrModule(ComponentDto component) {
+ if (!Qualifiers.PROJECT.equals(component.qualifier()) && !Qualifiers.MODULE.equals(component.qualifier())) {
+ throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid()));
+ }
+ }
+
private void checkPermissions(ComponentDto component) {
if (userSession.hasGlobalPermission(GlobalPermissions.SYSTEM_ADMIN)) {
return;
@@ -148,8 +157,8 @@ public class CreateAction implements CustomMeasuresWsAction {
private void checkMeasureDoesNotExistAlready(DbSession dbSession, ComponentDto component, MetricDto metric) {
int nbMeasuresOnSameMetricAndMeasure = dbClient.customMeasureDao().countByComponentIdAndMetricId(dbSession, component.uuid(), metric.getId());
if (nbMeasuresOnSameMetricAndMeasure > 0) {
- throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("A measure already exists for project id '%s' and metric id '%d'",
- component.uuid(), metric.getId()));
+ throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("A measure already exists for project '%s' (id: %s) and metric '%s' (id: '%d')",
+ component.key(), component.uuid(), metric.getKey(), metric.getId()));
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureValidator.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureValidator.java
index 2d38586da01..c7d76c7d1bc 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureValidator.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureValidator.java
@@ -66,7 +66,7 @@ public class CustomMeasureValidator {
throw new IllegalArgumentException("Unsupported metric type:" + metricType.description());
}
} catch (Exception e) {
- throw new IllegalArgumentException(String.format("Ill formatted value '%s' for metric type '%s'", valueAsString, metricType.description()), e);
+ throw new IllegalArgumentException(String.format("Incorrect value '%s' for metric type '%s'", valueAsString, metricType.description()), e);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
index fdc8581c9f0..c16cdc30736 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
@@ -35,6 +35,7 @@ import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metric.ValueType;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
+import org.sonar.core.component.ComponentDto;
import org.sonar.core.measure.custom.db.CustomMeasureDto;
import org.sonar.core.metric.db.MetricDto;
import org.sonar.core.permission.GlobalPermissions;
@@ -367,7 +368,7 @@ public class CreateActionTest {
MetricDto metric = insertMetricAndProject(ValueType.STRING, DEFAULT_PROJECT_UUID);
expectedException.expect(ServerException.class);
- expectedException.expectMessage(String.format("A measure already exists for project id 'project-uuid' and metric id '%d'", metric.getId()));
+ expectedException.expectMessage(String.format("A measure already exists for project 'project-key' (id: project-uuid) and metric 'metric-key' (id: '%d')", metric.getId()));
newRequest()
.setParam(CreateAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID)
@@ -386,7 +387,7 @@ public class CreateActionTest {
MetricDto metric = insertMetricAndProject(ValueType.BOOL, DEFAULT_PROJECT_UUID);
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Ill formatted value 'non-correct-boolean-value' for metric type 'Yes/No'");
+ expectedException.expectMessage("Incorrect value 'non-correct-boolean-value' for metric type 'Yes/No'");
newRequest()
.setParam(CreateAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID)
@@ -408,6 +409,25 @@ public class CreateActionTest {
.execute();
}
+ @Test
+ public void fail_when_not_a_project() throws Exception {
+ MetricDto metric = MetricTesting.newMetricDto().setEnabled(true).setValueType(ValueType.STRING.name()).setKey("metric-key");
+ dbClient.metricDao().insert(dbSession, metric);
+ ComponentDto project = ComponentTesting.newProjectDto(DEFAULT_PROJECT_UUID).setKey(DEFAULT_PROJECT_KEY);
+ dbClient.componentDao().insert(dbSession, project);
+ dbClient.componentDao().insert(dbSession, ComponentTesting.newDirectory(project, "directory-uuid", "path/to/directory").setKey("directory-key"));
+ dbSession.commit();
+
+ expectedException.expect(ServerException.class);
+ expectedException.expectMessage("Component 'directory-key' (id: directory-uuid) must be a project or a module.");
+
+ newRequest()
+ .setParam(CreateAction.PARAM_PROJECT_ID, "directory-uuid")
+ .setParam(CreateAction.PARAM_METRIC_ID, metric.getId().toString())
+ .setParam(CreateAction.PARAM_VALUE, "whatever-value")
+ .execute();
+ }
+
private WsTester.TestRequest newRequest() {
return ws.newPostRequest(CustomMeasuresWs.ENDPOINT, CreateAction.ACTION);
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasureValidatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasureValidatorTest.java
index c546022eee8..456769345bc 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasureValidatorTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasureValidatorTest.java
@@ -97,7 +97,7 @@ public class CustomMeasureValidatorTest {
@Test
public void fail_when_non_compliant_value() {
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Ill formatted value 'non-compliant-boolean-value' for metric type 'Yes/No'");
+ expectedException.expectMessage("Incorrect value 'non-compliant-boolean-value' for metric type 'Yes/No'");
sut.setMeasureValue(customMeasure, "non-compliant-boolean-value", newMetricDto().setValueType(BOOL.name()));
}
@@ -105,7 +105,7 @@ public class CustomMeasureValidatorTest {
@Test
public void fail_when_non_compliant_level_value() {
expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Ill formatted value 'non-compliant-level-value' for metric type 'Level'");
+ expectedException.expectMessage("Incorrect value 'non-compliant-level-value' for metric type 'Level'");
sut.setMeasureValue(customMeasure, "non-compliant-level-value", newMetricDto().setValueType(LEVEL.name()));
}