diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-06-22 09:49:47 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-06-22 10:49:09 +0200 |
commit | 58909c760e9fb30aef671a3d637a4a63d34dd013 (patch) | |
tree | 55febecd51bb0002fe83b885f30a11cd07f8f2b2 /server | |
parent | 71e022071f6b244422f74580ec3c04179695a3ea (diff) | |
download | sonarqube-58909c760e9fb30aef671a3d637a4a63d34dd013.tar.gz sonarqube-58909c760e9fb30aef671a3d637a4a63d34dd013.zip |
SONAR-6582 Extract class to serialize components
Diffstat (limited to 'server')
6 files changed, 78 insertions, 25 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentJsonWriter.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentJsonWriter.java new file mode 100644 index 00000000000..04ca8b22699 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentJsonWriter.java @@ -0,0 +1,43 @@ +/* + * 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.component.ws; + +import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.component.ComponentDto; + +public class ComponentJsonWriter { + + public void write(JsonWriter json, ComponentDto component, ComponentDto project) { + json.beginObject() + .prop("uuid", component.uuid()) + .prop("key", component.key()) + .prop("id", component.getId()) + .prop("qualifier", component.qualifier()) + .prop("name", component.name()) + .prop("longName", component.longName()) + .prop("enabled", component.isEnabled()) + .prop("path", component.path()) + // On a root project, parentProjectId is null but projectId is equal to itself, which make no sense. + .prop("projectId", (component.projectUuid() != null && component.parentProjectId() != null) ? project.getId() : null) + // TODO replace with parentProjectId when sonar-ws-client is not used anymore by tests... + .prop("subProjectId", component.parentProjectId()) + .endObject(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java index d2b57cbd80e..ad8e534fdf2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java @@ -64,6 +64,7 @@ import org.sonar.core.persistence.MyBatis; import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceDto; import org.sonar.core.resource.ResourceQuery; +import org.sonar.server.component.ws.ComponentJsonWriter; import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; import org.sonar.server.exceptions.BadRequestException; @@ -110,6 +111,7 @@ public class InternalRubyIssueService { private final IssueBulkChangeService issueBulkChangeService; private final IssueJsonWriter issueWriter; private final IssueComponentHelper issueComponentHelper; + private final ComponentJsonWriter componentWriter; private final UserIndex userIndex; private final DbClient dbClient; private final UserSession userSession; @@ -122,7 +124,7 @@ public class InternalRubyIssueService { IssueChangelogService changelogService, ActionPlanService actionPlanService, ResourceDao resourceDao, ActionService actionService, IssueFilterService issueFilterService, IssueBulkChangeService issueBulkChangeService, - IssueJsonWriter issueWriter, IssueComponentHelper issueComponentHelper, UserIndex userIndex, DbClient dbClient, + IssueJsonWriter issueWriter, IssueComponentHelper issueComponentHelper, ComponentJsonWriter componentWriter, UserIndex userIndex, DbClient dbClient, UserSession userSession, UserJsonWriter userWriter) { this.issueService = issueService; this.issueQueryService = issueQueryService; @@ -135,6 +137,7 @@ public class InternalRubyIssueService { this.issueBulkChangeService = issueBulkChangeService; this.issueWriter = issueWriter; this.issueComponentHelper = issueComponentHelper; + this.componentWriter = componentWriter; this.userIndex = userIndex; this.dbClient = dbClient; this.userSession = userSession; @@ -755,6 +758,18 @@ public class InternalRubyIssueService { } json.endArray(); + json.name("projects").beginArray(); + for (ComponentDto project : projectDtos) { + componentWriter.write(json, project, project); + } + json.endArray(); + + json.name("components").beginArray(); + for (ComponentDto component : componentDtos) { + componentWriter.write(json, component, projectsByComponentUuid.get(component.uuid())); + } + json.endArray(); + json.endObject().close(); } finally { MyBatis.closeQuietly(dbSession); diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java index 2851efd96a5..103fa37e612 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java @@ -52,6 +52,7 @@ import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.component.ComponentDto; import org.sonar.core.persistence.DbSession; +import org.sonar.server.component.ws.ComponentJsonWriter; import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; import org.sonar.server.es.SearchResult; @@ -65,6 +66,8 @@ import org.sonar.server.issue.index.IssueIndex; import org.sonar.server.rule.Rule; import org.sonar.server.rule.RuleService; import org.sonar.server.user.UserSession; +import org.sonar.server.user.ws.UserJsonWriter; + import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; import static com.google.common.collect.Sets.newHashSet; @@ -88,11 +91,13 @@ public class SearchAction implements IssuesWsAction { private final Languages languages; private final UserSession userSession; private final IssueJsonWriter issueWriter; + private final UserJsonWriter userWriter; private final IssueComponentHelper issueComponentHelper; + private final ComponentJsonWriter componentWriter; public SearchAction(DbClient dbClient, IssueService service, IssueQueryService issueQueryService, RuleService ruleService, ActionPlanService actionPlanService, UserFinder userFinder, I18n i18n, Languages languages, - UserSession userSession, IssueJsonWriter issueWriter, IssueComponentHelper issueComponentHelper) { + UserSession userSession, IssueJsonWriter issueWriter, IssueComponentHelper issueComponentHelper, ComponentJsonWriter componentWriter, UserJsonWriter userWriter) { this.dbClient = dbClient; this.service = service; this.issueQueryService = issueQueryService; @@ -103,7 +108,9 @@ public class SearchAction implements IssuesWsAction { this.languages = languages; this.userSession = userSession; this.issueWriter = issueWriter; + this.userWriter = userWriter; this.issueComponentHelper = issueComponentHelper; + this.componentWriter = componentWriter; } @Override @@ -499,20 +506,7 @@ public class SearchAction implements IssuesWsAction { json.name("components").beginArray(); for (ComponentDto component : components) { ComponentDto project = projectsByComponentUuid.get(component.uuid()); - json.beginObject() - .prop("uuid", component.uuid()) - .prop("key", component.key()) - .prop("id", component.getId()) - .prop("enabled", component.isEnabled()) - .prop("qualifier", component.qualifier()) - .prop("name", component.name()) - .prop("longName", component.longName()) - .prop("path", component.path()) - // On a root project, parentProjectId is null but projectId is equal to itself, which make no sense. - .prop("projectId", (component.projectUuid() != null && component.parentProjectId() != null) ? project.getId() : null) - // TODO should be renamed to parentProjectId - .prop("subProjectId", component.parentProjectId()) - .endObject(); + componentWriter.write(json, component, project); } json.endArray(); } @@ -532,15 +526,10 @@ public class SearchAction implements IssuesWsAction { json.endArray(); } - private static void writeUsers(JsonWriter json, Map<String, User> usersByLogin) { + private void writeUsers(JsonWriter json, Map<String, User> usersByLogin) { json.name("users").beginArray(); for (User user : usersByLogin.values()) { - json.beginObject() - .prop("login", user.login()) - .prop("name", user.name()) - .prop("active", user.active()) - .prop("email", user.email()) - .endObject(); + userWriter.write(json, user); } json.endArray(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 85f0d0495e9..7a82967845a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -61,6 +61,7 @@ import org.sonar.server.component.ComponentCleanerService; import org.sonar.server.component.ComponentService; import org.sonar.server.component.DefaultComponentFinder; import org.sonar.server.component.DefaultRubyComponentService; +import org.sonar.server.component.ws.ComponentJsonWriter; import org.sonar.server.component.ws.ComponentsWs; import org.sonar.server.component.ws.EventsWs; import org.sonar.server.component.ws.ResourcesWs; @@ -579,6 +580,7 @@ public class PlatformLevel4 extends PlatformLevel { NewAlerts.class, NewAlerts.newMetadata(), ComponentCleanerService.class, + ComponentJsonWriter.class, // views ViewIndexDefinition.class, diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java index d1d30b3a074..93d9c9d3696 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java @@ -43,6 +43,7 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceDto; import org.sonar.core.resource.ResourceQuery; +import org.sonar.server.component.ws.ComponentJsonWriter; import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; import org.sonar.server.exceptions.BadRequestException; @@ -104,6 +105,8 @@ public class InternalRubyIssueServiceTest { UserJsonWriter userWriter; + ComponentJsonWriter componentWriter; + @Before public void setUp() { issueService = mock(IssueService.class); @@ -121,12 +124,13 @@ public class InternalRubyIssueServiceTest { dbClient = mock(DbClient.class); dbSession = mock(DbSession.class); userWriter = mock(UserJsonWriter.class); + userWriter = mock(UserJsonWriter.class); ResourceDto project = new ResourceDto().setKey("org.sonar.Sample"); when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(project); service = new InternalRubyIssueService(issueService, issueQueryService, commentService, changelogService, actionPlanService, resourceDao, actionService, - issueFilterService, issueBulkChangeService, issueWriter, issueComponentHelper, userIndex, dbClient, userSessionRule, userWriter); + issueFilterService, issueBulkChangeService, issueWriter, issueComponentHelper, componentWriter, userIndex, dbClient, userSessionRule, userWriter); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionComponentsMediumTest/display_module_facet.json b/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionComponentsMediumTest/display_module_facet.json index 4ae5c790a6e..8b86025ab6f 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionComponentsMediumTest/display_module_facet.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/ws/SearchActionComponentsMediumTest/display_module_facet.json @@ -10,7 +10,7 @@ { "key": "MySubModule1" }, { "key": "MySubModule2" }, { "key": "MySubModule3" }, - { "key": "MyComponent1" }, + { "key": "MyComponent1" } ], "issues": [ { |