diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-06-26 16:35:04 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-06-26 16:46:24 +0200 |
commit | 99897bc10286b663aaed0e28465dd35d6d9c6cbc (patch) | |
tree | 27b75fdffbecbb4562ba48aa81edbba5971aaf70 /server/sonar-server | |
parent | f6850ec96a98738b2b99f347926c105790b6c137 (diff) | |
download | sonarqube-99897bc10286b663aaed0e28465dd35d6d9c6cbc.tar.gz sonarqube-99897bc10286b663aaed0e28465dd35d6d9c6cbc.zip |
SONAR-6615 ws custom_measures/* add the pending field
Diffstat (limited to 'server/sonar-server')
8 files changed, 61 insertions, 17 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 a809a623028..129bf9d24ab 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 @@ -132,7 +132,7 @@ public class CreateAction implements CustomMeasuresWsAction { dbSession.commit(); JsonWriter json = response.newJsonWriter(); - customMeasureJsonWriter.write(json, measure, metric, component, user, CustomMeasureJsonWriter.OPTIONAL_FIELDS); + customMeasureJsonWriter.write(json, measure, metric, component, user, true, CustomMeasureJsonWriter.OPTIONAL_FIELDS); json.close(); } finally { MyBatis.closeQuietly(dbSession); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java index 026d0cb9fa0..5abf7bb598e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java @@ -26,6 +26,7 @@ import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.Nullable; import org.sonar.api.measures.Metric; import org.sonar.api.user.User; import org.sonar.api.utils.text.JsonWriter; @@ -48,9 +49,10 @@ public class CustomMeasureJsonWriter { private static final String FIELD_CREATED_AT = "createdAt"; private static final String FIELD_UPDATED_AT = "updatedAt"; private static final String FIELD_USER = "user"; + private static final String FIELD_PENDING = "pending"; public static final Set<String> OPTIONAL_FIELDS = ImmutableSet.of(FIELD_PROJECT_ID, FIELD_PROJECT_KEY, FIELD_VALUE, FIELD_DESCRIPTION, FIELD_METRIC, FIELD_CREATED_AT, - FIELD_UPDATED_AT, FIELD_USER); + FIELD_UPDATED_AT, FIELD_USER, FIELD_PENDING); private final UserJsonWriter userJsonWriter; @@ -58,7 +60,7 @@ public class CustomMeasureJsonWriter { this.userJsonWriter = userJsonWriter; } - public void write(JsonWriter json, CustomMeasureDto measure, MetricDto metric, ComponentDto component, User user, Collection<String> fieldsToReturn) { + public void write(JsonWriter json, CustomMeasureDto measure, MetricDto metric, ComponentDto component, User user, boolean isPending, Collection<String> fieldsToReturn) { json.beginObject(); json.prop(FIELD_ID, String.valueOf(measure.getId())); writeIfNeeded(json, measureValue(measure, metric), FIELD_VALUE, fieldsToReturn); @@ -71,6 +73,7 @@ public class CustomMeasureJsonWriter { writeIfNeeded(json, component.key(), FIELD_PROJECT_KEY, fieldsToReturn); writeIfNeeded(json, new Date(measure.getCreatedAt()), FIELD_CREATED_AT, fieldsToReturn); writeIfNeeded(json, new Date(measure.getUpdatedAt()), FIELD_UPDATED_AT, fieldsToReturn); + writeIfNeeded(json, isPending, FIELD_PENDING, fieldsToReturn); if (isFieldNeeded(FIELD_USER, fieldsToReturn)) { json.name(FIELD_USER); @@ -108,11 +111,12 @@ public class CustomMeasureJsonWriter { } public void write(JsonWriter json, List<CustomMeasureDto> customMeasures, ComponentDto project, Map<Integer, MetricDto> metricsById, Map<String, User> usersByLogin, - Collection<String> fieldsToReturn) { + @Nullable Long lastAnalysisTimestamp, Collection<String> fieldsToReturn) { json.name("customMeasures"); json.beginArray(); for (CustomMeasureDto customMeasure : customMeasures) { - write(json, customMeasure, metricsById.get(customMeasure.getMetricId()), project, usersByLogin.get(customMeasure.getUserLogin()), fieldsToReturn); + boolean pending = lastAnalysisTimestamp == null || lastAnalysisTimestamp < customMeasure.getUpdatedAt(); + write(json, customMeasure, metricsById.get(customMeasure.getMetricId()), project, usersByLogin.get(customMeasure.getUserLogin()), pending, fieldsToReturn); } json.endArray(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java index 02f33429d72..b27e2534291 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/SearchAction.java @@ -26,6 +26,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; +import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; @@ -34,6 +35,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.user.User; import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.component.ComponentDto; +import org.sonar.core.component.SnapshotDto; import org.sonar.core.measure.custom.db.CustomMeasureDto; import org.sonar.core.metric.db.MetricDto; import org.sonar.core.persistence.DbSession; @@ -92,17 +94,25 @@ public class SearchAction implements CustomMeasuresWsAction { DbSession dbSession = dbClient.openSession(false); try { ComponentDto project = searchProject(dbSession, projectUuid, projectKey); + Long lastAnalysisDateMs = searchLastSnapshot(dbSession, project); List<CustomMeasureDto> customMeasures = searchCustomMeasures(dbSession, project, searchOptions); int nbCustomMeasures = countTotalOfCustomMeasures(dbSession, project); Map<String, User> usersByLogin = usersByLogin(customMeasures); Map<Integer, MetricDto> metricsById = metricsById(dbSession, customMeasures); - writeResponse(response, customMeasures, nbCustomMeasures, project, metricsById, usersByLogin, searchOptions, fieldsToReturn); + writeResponse(response, customMeasures, nbCustomMeasures, project, metricsById, usersByLogin, lastAnalysisDateMs, searchOptions, fieldsToReturn); } finally { MyBatis.closeQuietly(dbSession); } } + @CheckForNull + private Long searchLastSnapshot(DbSession dbSession, ComponentDto project) { + SnapshotDto lastSnapshot = dbClient.snapshotDao().selectLastSnapshotByComponentId(dbSession, project.getId()); + + return lastSnapshot == null ? null : lastSnapshot.getBuildDate(); + } + private int countTotalOfCustomMeasures(DbSession dbSession, ComponentDto project) { return dbClient.customMeasureDao().countByComponentUuid(dbSession, project.uuid()); } @@ -112,10 +122,10 @@ public class SearchAction implements CustomMeasuresWsAction { } private void writeResponse(Response response, List<CustomMeasureDto> customMeasures, int nbCustomMeasures, ComponentDto project, Map<Integer, MetricDto> metricsById, - Map<String, User> usersByLogin, SearchOptions searchOptions, List<String> fieldsToReturn) { + Map<String, User> usersByLogin, @Nullable Long lastAnalysisDate, SearchOptions searchOptions, List<String> fieldsToReturn) { JsonWriter json = response.newJsonWriter(); json.beginObject(); - customMeasureJsonWriter.write(json, customMeasures, project, metricsById, usersByLogin, fieldsToReturn); + customMeasureJsonWriter.write(json, customMeasures, project, metricsById, usersByLogin, lastAnalysisDate, fieldsToReturn); searchOptions.writeJson(json, nbCustomMeasures); json.endObject(); json.close(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java index 60ef4a642cd..94ffe2e6167 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java @@ -108,7 +108,7 @@ public class UpdateAction implements CustomMeasuresWsAction { dbSession.commit(); JsonWriter json = response.newJsonWriter(); - customMeasureJsonWriter.write(json, customMeasure, metric, component, user, CustomMeasureJsonWriter.OPTIONAL_FIELDS); + customMeasureJsonWriter.write(json, customMeasure, metric, component, user, true, CustomMeasureJsonWriter.OPTIONAL_FIELDS ); json.close(); } finally { MyBatis.closeQuietly(dbSession); diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java index be3a25e4c0b..bff6d7a27f0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java @@ -20,6 +20,7 @@ package org.sonar.server.measure.custom.ws; +import java.util.Date; import org.apache.commons.lang.StringUtils; import org.junit.After; import org.junit.Before; @@ -31,13 +32,16 @@ import org.junit.rules.ExpectedException; import org.sonar.api.config.Settings; import org.sonar.api.measures.Metric.ValueType; import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.DateUtils; 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.persistence.DbSession; import org.sonar.core.persistence.DbTester; import org.sonar.server.component.ComponentTesting; +import org.sonar.server.component.SnapshotTesting; import org.sonar.server.component.db.ComponentDao; +import org.sonar.server.component.db.SnapshotDao; import org.sonar.server.db.DbClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.NotFoundException; @@ -83,7 +87,7 @@ public class SearchActionTest { @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new ComponentDao(), new MetricDao()); + dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new ComponentDao(), new MetricDao(), new SnapshotDao()); dbSession = dbClient.openSession(false); db.truncateTables(); CustomMeasureJsonWriter customMeasureJsonWriter = new CustomMeasureJsonWriter(new UserJsonWriter(userSessionRule)); @@ -112,12 +116,12 @@ public class SearchActionTest { response.assertJson(getClass(), "metrics.json"); String responseAsString = response.outputAsString(); - assertThat(responseAsString).matches(nameValuePattern("id", metric1.getId().toString())); - assertThat(responseAsString).matches(nameValuePattern("id", metric2.getId().toString())); - assertThat(responseAsString).matches(nameValuePattern("id", metric3.getId().toString())); - assertThat(responseAsString).matches(nameValuePattern("id", String.valueOf(customMeasure1.getId()))); - assertThat(responseAsString).matches(nameValuePattern("id", String.valueOf(customMeasure2.getId()))); - assertThat(responseAsString).matches(nameValuePattern("id", String.valueOf(customMeasure3.getId()))); + assertThat(responseAsString).matches(nameStringValuePattern("id", metric1.getId().toString())); + assertThat(responseAsString).matches(nameStringValuePattern("id", metric2.getId().toString())); + assertThat(responseAsString).matches(nameStringValuePattern("id", metric3.getId().toString())); + assertThat(responseAsString).matches(nameStringValuePattern("id", String.valueOf(customMeasure1.getId()))); + assertThat(responseAsString).matches(nameStringValuePattern("id", String.valueOf(customMeasure2.getId()))); + assertThat(responseAsString).matches(nameStringValuePattern("id", String.valueOf(customMeasure3.getId()))); assertThat(responseAsString).contains("createdAt", "updatedAt"); } @@ -187,6 +191,23 @@ public class SearchActionTest { } @Test + public void search_with_more_recent_analysis() throws Exception { + long yesterday = DateUtils.addDays(new Date(), -1).getTime(); + MetricDto metric = insertCustomMetric(1); + dbClient.customMeasureDao().insert(dbSession, newCustomMeasure(1, metric) + .setCreatedAt(yesterday) + .setUpdatedAt(yesterday)); + dbClient.snapshotDao().insert(dbSession, SnapshotTesting.createForProject(defaultProject)); + dbSession.commit(); + + String response = newRequest() + .setParam(SearchAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID) + .execute().outputAsString(); + + assertThat(response).matches(nameValuePattern("pending", "false")); + } + + @Test public void empty_json_when_no_measure() throws Exception { WsTester.Result response = newRequest() .setParam(SearchAction.PARAM_PROJECT_KEY, DEFAULT_PROJECT_KEY) @@ -268,7 +289,11 @@ public class SearchActionTest { return ws.newGetRequest(CustomMeasuresWs.ENDPOINT, SearchAction.ACTION); } - private static String nameValuePattern(String name, String value) { + private static String nameStringValuePattern(String name, String value) { return String.format(".*\"%s\"\\s*:\\s*\"%s\".*", name, value); } + + private static String nameValuePattern(String name, String value) { + return String.format(".*\"%s\"\\s*:\\s*%s.*", name, value); + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/CreateActionTest/custom-measure.json b/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/CreateActionTest/custom-measure.json index ab5e1c12003..939b57e5f02 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/CreateActionTest/custom-measure.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/CreateActionTest/custom-measure.json @@ -7,6 +7,7 @@ }, "value": "custom-measure-free-text", "description": "custom-measure-description", + "pending":true, "user": { "active": true, "email": "login@login.com", diff --git a/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/SearchActionTest/metrics.json b/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/SearchActionTest/metrics.json index bf3bd9f8083..5b1cb2a5153 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/SearchActionTest/metrics.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/SearchActionTest/metrics.json @@ -8,6 +8,7 @@ }, "projectId": "project-uuid", "projectKey": "project-key", + "pending":true, "user": { "active": true, "email": "login@login.com", @@ -24,6 +25,7 @@ }, "projectId": "project-uuid", "projectKey": "project-key", + "pending":true, "user": { "active": true, "email": "login@login.com", @@ -40,6 +42,7 @@ }, "projectId": "project-uuid", "projectKey": "project-key", + "pending":true, "user": { "active": true, "email": "login@login.com", diff --git a/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/UpdateActionTest/custom-measure.json b/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/UpdateActionTest/custom-measure.json index a66612bb9b0..d91f151cefa 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/UpdateActionTest/custom-measure.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/measure/custom/ws/UpdateActionTest/custom-measure.json @@ -7,6 +7,7 @@ }, "value": "new-text-measure-value", "description": "new-custom-measure-description", + "pending":true, "user": { "active": true, "email": "login@login.com", |