Skip to content

Commit

Permalink
SONAR-6572 WS metrics/update clean error message when trying to updat…
Browse files Browse the repository at this point in the history
…e with an existing metric
  • Loading branch information
teryk committed Jun 8, 2015
1 parent 749ca1a commit 8f20957
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Expand Up @@ -169,10 +169,10 @@ private void updateMetricInDb(DbSession dbSession, MetricDto metricInDb, MetricD
}

private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) {
if (isMetricFoundInDb(metricInDb) || isMetricDisabled(metricInDb) || isMetricNonCustom(metricInDb)) {
throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("No active custom metric has been found for id '%d'. Maybe you want to create a metric ?",
template.getId()));
if (!isMetricFoundInDb(metricInDb) || isMetricDisabled(metricInDb) || !isMetricCustom(metricInDb)) {
throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("No active custom metric has been found for id '%d'.", template.getId()));
}
checkNoOtherMetricWithTargetKey(dbSession, metricInDb, template);
if (haveMetricTypeChanged(metricInDb, template)) {
List<CustomMeasureDto> customMeasures = dbClient.customMeasureDao().selectByMetricId(dbSession, metricInDb.getId());
if (haveAssociatedCustomMeasures(customMeasures)) {
Expand All @@ -182,6 +182,13 @@ private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto
}
}

private void checkNoOtherMetricWithTargetKey(DbSession dbSession, MetricDto metricInDb, MetricDto template) {
MetricDto metricWithTargetKey = dbClient.metricDao().selectNullableByKey(dbSession, template.getKey());
if (isMetricFoundInDb(metricWithTargetKey) && !metricInDb.getId().equals(metricWithTargetKey.getId())) {
throw new ServerException(HttpURLConnection.HTTP_CONFLICT, "A me metric exists with the key: " + metricInDb.getKey());
}
}

private static void writeMetric(JsonWriter json, MetricDto metric) {
json.beginObject();
json.prop(FIELD_ID, String.valueOf(metric.getId()));
Expand All @@ -193,16 +200,16 @@ private static void writeMetric(JsonWriter json, MetricDto metric) {
json.endObject();
}

private static boolean isMetricNonCustom(MetricDto metricInDb) {
return !metricInDb.isUserManaged();
private static boolean isMetricCustom(MetricDto metricInDb) {
return metricInDb.isUserManaged();
}

private static boolean isMetricDisabled(MetricDto metricInDb) {
return !metricInDb.isEnabled();
}

private static boolean isMetricFoundInDb(@Nullable MetricDto metricInDb) {
return metricInDb == null;
return metricInDb != null;
}

private static boolean haveAssociatedCustomMeasures(List<CustomMeasureDto> customMeasures) {
Expand Down
Expand Up @@ -140,6 +140,18 @@ public void update_return_the_full_object_with_id() throws Exception {
assertThat(requestResult.outputAsString()).matches(".*\"id\"\\s*:\\s*\"" + id + "\".*");
}

@Test
public void fail_when_changing_key_for_an_existing_one() throws Exception {
expectedException.expect(ServerException.class);
insertMetric(newDefaultMetric().setKey("metric-key"));
int id = insertMetric(newDefaultMetric().setKey("another-key"));

newRequest()
.setParam(PARAM_ID, String.valueOf(id))
.setParam(PARAM_KEY, "metric-key")
.execute();
}

@Test
public void fail_when_metric_not_in_db() throws Exception {
expectedException.expect(ServerException.class);
Expand Down

0 comments on commit 8f20957

Please sign in to comment.