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;
// 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())
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;
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();
--- /dev/null
+/*
+ * 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);
+ }
+
+}