From ec96c3e2c8e33bc40e519a7b9566cfb2e9338b52 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Wed, 20 Jan 2016 12:38:33 +0100 Subject: [PATCH] SONAR-7135 WS api/measures/component_tree refKey in the response --- .../measure/ws/ComponentDtoToWsComponent.java | 8 +++++--- .../server/measure/ws/ComponentTreeAction.java | 4 ++-- .../sonar/server/measure/ws/ComponentTreeData.java | 14 +++++++------- .../server/measure/ws/ComponentTreeDataLoader.java | 10 +++++----- .../server/measure/ws/ComponentTreeActionTest.java | 3 ++- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java index 5734963a43d..319148d89ea 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java @@ -33,11 +33,13 @@ class ComponentDtoToWsComponent { } static WsMeasures.Component.Builder componentDtoToWsComponent(ComponentDto component, Map measuresByMetric, - Map referenceComponentUuidsById) { + Map referenceComponentsById) { WsMeasures.Component.Builder wsComponent = componentDtoToWsComponent(component); - if (!referenceComponentUuidsById.isEmpty() && referenceComponentUuidsById.get(component.getCopyResourceId()) != null) { - wsComponent.setRefId(referenceComponentUuidsById.get(component.getCopyResourceId())); + ComponentDto referenceComponent = referenceComponentsById.get(component.getCopyResourceId()); + if (!referenceComponentsById.isEmpty() && referenceComponent != null) { + wsComponent.setRefId(referenceComponent.uuid()); + wsComponent.setRefKey(referenceComponent.key()); } for (Map.Entry entry : measuresByMetric.entrySet()) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java index 8d4d970807f..ca904c05765 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java @@ -194,13 +194,13 @@ public class ComponentTreeAction implements MeasuresWsAction { componentDtoToWsComponent( data.getBaseComponent(), data.getMeasuresByComponentUuidAndMetric().row(data.getBaseComponent().uuid()), - data.getReferenceComponentUuidsById())); + data.getReferenceComponentsById())); for (ComponentDto componentDto : data.getComponents()) { response.addComponents(componentDtoToWsComponent( componentDto, data.getMeasuresByComponentUuidAndMetric().row(componentDto.uuid()), - data.getReferenceComponentUuidsById())); + data.getReferenceComponentsById())); } if (areMetricsInResponse(request)) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java index 79d6fb116ca..7c4505c61bf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java @@ -35,7 +35,7 @@ class ComponentTreeData { private final ComponentDto baseComponent; private final List components; private final int componentCount; - private final Map referenceComponentUuidsById; + private final Map referenceComponentsById; private final List metrics; private final List periods; private final Table measuresByComponentUuidAndMetric; @@ -44,7 +44,7 @@ class ComponentTreeData { this.baseComponent = builder.baseComponent; this.components = builder.componentsFromDb; this.componentCount = builder.componentCount; - this.referenceComponentUuidsById = builder.referenceComponentUuidsById; + this.referenceComponentsById = builder.referenceComponentsById; this.metrics = builder.metrics; this.measuresByComponentUuidAndMetric = builder.measuresByComponentUuidAndMetric; this.periods = builder.periods; @@ -65,8 +65,8 @@ class ComponentTreeData { } @CheckForNull - public Map getReferenceComponentUuidsById() { - return referenceComponentUuidsById; + public Map getReferenceComponentsById() { + return referenceComponentsById; } @CheckForNull @@ -91,7 +91,7 @@ class ComponentTreeData { static class Builder { private ComponentDto baseComponent; private List componentsFromDb; - private Map referenceComponentUuidsById; + private Map referenceComponentsById; private int componentCount; private List metrics; private List periods; @@ -131,8 +131,8 @@ class ComponentTreeData { return this; } - public Builder setReferenceComponentUuidsById(Map referenceComponentsById) { - this.referenceComponentUuidsById = referenceComponentsById; + public Builder setReferenceComponentsById(Map referenceComponentsById) { + this.referenceComponentsById = referenceComponentsById; return this; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java index 16154108dfa..e73cb8cc1a5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java @@ -111,7 +111,7 @@ public class ComponentTreeDataLoader { components = sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric); components = paginateComponents(components, componentCount, wsRequest); - Map referenceComponentUuidsById = searchReferenceComponentUuidsById(dbSession, components); + Map referenceComponentsById = searchReferenceComponentsById(dbSession, components); return ComponentTreeData.builder() .setBaseComponent(baseComponent) @@ -120,14 +120,14 @@ public class ComponentTreeDataLoader { .setMeasuresByComponentUuidAndMetric(measuresByComponentUuidAndMetric) .setMetrics(metrics) .setPeriods(periods) - .setReferenceComponentUuidsById(referenceComponentUuidsById) + .setReferenceComponentsById(referenceComponentsById) .build(); } finally { dbClient.closeSession(dbSession); } } - private Map searchReferenceComponentUuidsById(DbSession dbSession, List components) { + private Map searchReferenceComponentsById(DbSession dbSession, List components) { List referenceComponentIds = from(components) .transform(ComponentDtoWithSnapshotIdToCopyResourceIdFunction.INSTANCE) .filter(Predicates.notNull()) @@ -137,9 +137,9 @@ public class ComponentTreeDataLoader { } List referenceComponents = dbClient.componentDao().selectByIds(dbSession, referenceComponentIds); - Map referenceComponentUuidsById = new HashMap<>(); + Map referenceComponentUuidsById = new HashMap<>(); for (ComponentDto referenceComponent : referenceComponents) { - referenceComponentUuidsById.put(referenceComponent.getId(), referenceComponent.uuid()); + referenceComponentUuidsById.put(referenceComponent.getId(), referenceComponent); } return referenceComponentUuidsById; diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java index 51ffab6228c..f8d35db0e7c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java @@ -270,7 +270,7 @@ public class ComponentTreeActionTest { @Test public void load_developer_descendants() { ComponentDto developer = newDeveloper("developer").setUuid("developer-uuid"); - ComponentDto project = newProjectDto("project-uuid"); + ComponentDto project = newProjectDto("project-uuid").setKey("project-key"); SnapshotDto developerSnapshot = componentDb.insertDeveloperAndSnapshot(developer); componentDb.insertProjectAndSnapshot(project); componentDb.insertComponentAndSnapshot(newDevProjectCopy("project-uuid-copy", project, developer), developerSnapshot); @@ -284,6 +284,7 @@ public class ComponentTreeActionTest { WsMeasures.Component projectCopy = response.getComponents(0); assertThat(projectCopy.getId()).isEqualTo("project-uuid-copy"); assertThat(projectCopy.getRefId()).isEqualTo("project-uuid"); + assertThat(projectCopy.getRefKey()).isEqualTo("project-key"); } @Test -- 2.39.5