]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8618 don't use ComponentDto.getOrganizationKey
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 13 Jan 2017 10:18:48 +0000 (11:18 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 16 Jan 2017 10:38:43 +0000 (11:38 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/ShowAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentDtoToWsComponentTest.java [new file with mode: 0644]

index 030017c18b482515d0b2961dff29fb3d38717a77..46b3b3225ddc263349d44251b99ee5559d1e8a5f 100644 (file)
 package org.sonar.server.component.ws;
 
 import java.util.Map;
+import java.util.Objects;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonarqube.ws.WsComponents;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Strings.emptyToNull;
 import static org.sonar.core.util.Protobuf.setNullable;
 
@@ -32,9 +35,25 @@ class ComponentDtoToWsComponent {
     // prevent instantiation
   }
 
+  /**
+   * @deprecated use {@link #componentDtoToWsComponent(ComponentDto, OrganizationDto)} instead
+   */
+  @Deprecated
   static WsComponents.Component.Builder componentDtoToWsComponent(ComponentDto dto) {
+    return componentDtoToWsComponent(dto, dto.getOrganizationKey());
+  }
+
+  static WsComponents.Component.Builder componentDtoToWsComponent(ComponentDto dto, OrganizationDto organizationDto) {
+    checkArgument(
+      Objects.equals(dto.getOrganizationUuid(), organizationDto.getUuid()),
+      "OrganizationUuid (%s) of ComponentDto to convert to Ws Component is not the same as the one (%s) of the specified OrganizationDto",
+      dto.getOrganizationUuid(), organizationDto.getUuid());
+    return componentDtoToWsComponent(dto, organizationDto.getKey());
+  }
+
+  private static WsComponents.Component.Builder componentDtoToWsComponent(ComponentDto dto, String organizationDtoKey) {
     WsComponents.Component.Builder wsComponent = WsComponents.Component.newBuilder()
-      .setOrganization(dto.getOrganizationKey())
+      .setOrganization(organizationDtoKey)
       .setId(dto.uuid())
       .setKey(dto.key())
       .setName(dto.name())
index 0aa142e7fbe64fdde27f248756ac54e08d6e0d82..3a07a553174e7cd3e127eb14a6c0927999fc0218 100644 (file)
@@ -28,8 +28,10 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.component.ComponentFinder.ParamNames;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.WsComponents.ShowWsResponse;
 import org.sonarqube.ws.client.component.ShowWsRequest;
@@ -95,21 +97,27 @@ public class ShowAction implements ComponentsWsAction {
     try {
       ComponentDto component = getComponentByUuidOrKey(dbSession, request);
       List<ComponentDto> ancestors = dbClient.componentDao().selectAncestors(dbSession, component);
-      return buildResponse(component, ancestors);
+      OrganizationDto organizationDto = getOrganization(dbSession, component.getOrganizationUuid());
+      return buildResponse(component, organizationDto, ancestors);
     } finally {
       dbClient.closeSession(dbSession);
     }
   }
 
-  private static ShowWsResponse buildResponse(ComponentDto component, List<ComponentDto> orderedAncestors) {
+  private OrganizationDto getOrganization(DbSession dbSession, String organizationUuid) {
+    return dbClient.organizationDao().selectByUuid(dbSession, organizationUuid)
+          .orElseThrow(() -> new NotFoundException(String.format("Organization with uuid '%s' not found", organizationUuid)));
+  }
+
+  private static ShowWsResponse buildResponse(ComponentDto component, OrganizationDto organizationDto, List<ComponentDto> orderedAncestors) {
     ShowWsResponse.Builder response = ShowWsResponse.newBuilder();
-    response.setComponent(componentDtoToWsComponent(component));
+    response.setComponent(componentDtoToWsComponent(component, organizationDto));
 
     // ancestors are ordered from root to leaf, whereas it's the opposite
     // in WS response
     for (int i = orderedAncestors.size() - 1; i >= 0; i--) {
       ComponentDto ancestor = orderedAncestors.get(i);
-      response.addAncestors(componentDtoToWsComponent(ancestor));
+      response.addAncestors(componentDtoToWsComponent(ancestor, organizationDto));
     }
 
     return response.build();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentDtoToWsComponentTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentDtoToWsComponentTest.java
new file mode 100644 (file)
index 0000000..4606d84
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.organization.OrganizationTesting;
+
+import static org.sonar.server.component.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
+
+public class ComponentDtoToWsComponentTest {
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void componentDtoToWsComponent_throws_IAE_if_organization_uuid_of_component_does_not_match_organizationDto_uuid() {
+    OrganizationDto organizationDto1 = OrganizationTesting.newOrganizationDto();
+    OrganizationDto organizationDto2 = OrganizationTesting.newOrganizationDto();
+    ComponentDto componentDto = ComponentTesting.newProjectDto(organizationDto1);
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("OrganizationUuid (" + organizationDto1.getUuid() + ") of ComponentDto to convert " +
+      "to Ws Component is not the same as the one (" + organizationDto2.getUuid() + ") of the specified OrganizationDto");
+
+    componentDtoToWsComponent(componentDto, organizationDto2);
+  }
+
+}