From 68776c0a9456b77f051baf474810e7a4e2c32694 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 26 Apr 2018 17:33:08 +0200 Subject: [PATCH] SONAR-10567 Compute Engine analysis do not fail if project name or description is too long --- .../org/sonar/db/component/ComponentDto.java | 3 +- .../db/component/ComponentValidator.java | 36 +++++++++- .../org/sonar/db/component/ResourceDto.java | 6 +- .../db/component/ComponentValidatorTest.java | 6 +- .../org/sonar/server/ce/ws/SubmitAction.java | 7 +- .../computation/queue/ReportSubmitter.java | 4 +- .../component/ComponentImpl.java | 7 +- .../component/ComponentTreeBuilder.java | 2 +- .../sonar/server/project/ws/CreateAction.java | 11 ++- .../sonar/server/ce/ws/SubmitActionTest.java | 22 ++++++ .../server/component/NewComponentTest.java | 4 +- .../component/ComponentImplTest.java | 41 ++++++++--- .../server/project/ws/CreateActionTest.java | 22 +++++- .../project/OrganizationProjectSuite.java | 1 + .../tests/project/ProjectInfoTest.java | 68 +++++++++++++++++++ 15 files changed, 206 insertions(+), 34 deletions(-) create mode 100644 tests/src/test/java/org/sonarqube/tests/project/ProjectInfoTest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java index ef402d4e48f..fad8823a8b9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java @@ -36,6 +36,7 @@ import static java.lang.String.format; import static org.apache.commons.lang.StringUtils.substringBeforeLast; import static org.sonar.db.DaoDatabaseUtils.buildLikeValue; import static org.sonar.db.component.ComponentValidator.checkComponentKey; +import static org.sonar.db.component.ComponentValidator.checkComponentLongName; import static org.sonar.db.component.ComponentValidator.checkComponentName; import static org.sonar.db.component.DbTagsReader.readDbTags; @@ -338,7 +339,7 @@ public class ComponentDto { } public ComponentDto setLongName(String longName) { - this.longName = longName; + this.longName = checkComponentLongName(longName); return this; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentValidator.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentValidator.java index 016842ce800..d50213f858a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentValidator.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentValidator.java @@ -19,12 +19,17 @@ */ package org.sonar.db.component; +import javax.annotation.Nullable; + import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH; public class ComponentValidator { - public static final int MAX_COMPONENT_NAME_LENGTH = 2000; + // b_name column is 500 characters wide + public static final int MAX_COMPONENT_NAME_LENGTH = 500; + public static final int MAX_COMPONENT_DESCRIPTION_LENGTH = 2_000; + private static final int MAX_COMPONENT_TAGS_LENGTH = 500; private static final int MAX_COMPONENT_QUALIFIER_LENGTH = 10; private ComponentValidator() { @@ -38,6 +43,35 @@ public class ComponentValidator { return name; } + public static String checkComponentLongName(@Nullable String value) { + if (value == null) { + return null; + } + checkArgument(value.length() <= MAX_COMPONENT_NAME_LENGTH, "Component name length (%s) is longer than the maximum authorized (%s). '%s' was provided.", + value.length(), MAX_COMPONENT_NAME_LENGTH, value); + return value; + } + + public static String checkDescription(@Nullable String value) { + if (value == null) { + return null; + } + + checkArgument(value.length() <= MAX_COMPONENT_NAME_LENGTH, "Component description length (%s) is longer than the maximum authorized (%s). '%s' was provided.", + value.length(), MAX_COMPONENT_DESCRIPTION_LENGTH, value); + return value; + } + + public static String checkTags(@Nullable String value) { + if (value == null) { + return null; + } + + checkArgument(value.length() <= MAX_COMPONENT_NAME_LENGTH, "Component tags length (%s) is longer than the maximum authorized (%s). '%s' was provided.", + value.length(), MAX_COMPONENT_TAGS_LENGTH, value); + return value; + } + public static String checkComponentKey(String key) { checkArgument(!isNullOrEmpty(key), "Component key can't be empty"); checkArgument(key.length() <= MAX_COMPONENT_KEY_LENGTH, "Component key length (%s) is longer than the maximum authorized (%s). '%s' was provided.", diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ResourceDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ResourceDto.java index 6a83bea9d37..759d65e753c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ResourceDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ResourceDto.java @@ -24,7 +24,9 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import static org.sonar.db.component.ComponentValidator.checkComponentKey; +import static org.sonar.db.component.ComponentValidator.checkComponentLongName; import static org.sonar.db.component.ComponentValidator.checkComponentName; +import static org.sonar.db.component.ComponentValidator.checkDescription; public class ResourceDto { @@ -142,7 +144,7 @@ public class ResourceDto { } public ResourceDto setLongName(String longName) { - this.longName = longName; + this.longName = checkComponentLongName(longName); return this; } @@ -178,7 +180,7 @@ public class ResourceDto { } public ResourceDto setDescription(String description) { - this.description = description; + this.description = checkDescription(description); return this; } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java index 668ca786031..c1db5835db1 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentValidatorTest.java @@ -33,17 +33,17 @@ public class ComponentValidatorTest { @Test public void check_name() { - String name = repeat("a", 2000); + String name = repeat("a", 500); assertThat(ComponentValidator.checkComponentName(name)).isEqualTo(name); } @Test - public void fail_when_name_longer_than_2000_characters() { + public void fail_when_name_longer_than_500_characters() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Component name length"); - ComponentValidator.checkComponentName(repeat("a", 2000 + 1)); + ComponentValidator.checkComponentName(repeat("a", 500 + 1)); } @Test diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/SubmitAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/SubmitAction.java index d2ddeb33cda..102565e3b14 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/SubmitAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/SubmitAction.java @@ -33,6 +33,8 @@ import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.ws.WsUtils; import org.sonarqube.ws.Ce; +import static org.apache.commons.lang.StringUtils.abbreviate; +import static org.apache.commons.lang.StringUtils.defaultIfBlank; import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH; import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH; import static org.sonar.server.ws.WsUtils.checkRequest; @@ -86,8 +88,7 @@ public class SubmitAction implements CeWsAction { action .createParam(PARAM_PROJECT_NAME) .setRequired(false) - .setMaximumLength(MAX_COMPONENT_NAME_LENGTH) - .setDescription("Optional name of the project, used only if the project does not exist yet.") + .setDescription("Optional name of the project, used only if the project does not exist yet. If name is longer than %d, it is abbreviated.", MAX_COMPONENT_NAME_LENGTH) .setExampleValue("My Project"); action @@ -110,7 +111,7 @@ public class SubmitAction implements CeWsAction { .or(defaultOrganizationProvider.get()::getKey); String projectKey = wsRequest.mandatoryParam(PARAM_PROJECT_KEY); String projectBranch = wsRequest.param(PARAM_PROJECT_BRANCH); - String projectName = StringUtils.defaultIfBlank(wsRequest.param(PARAM_PROJECT_NAME), projectKey); + String projectName = abbreviate(defaultIfBlank(wsRequest.param(PARAM_PROJECT_NAME), projectKey), MAX_COMPONENT_NAME_LENGTH); Map characteristics = parseTaskCharacteristics(wsRequest); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java index 22165c43d74..f3be61d393c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; import org.sonar.api.server.ServerSide; @@ -52,6 +51,7 @@ import org.sonar.server.user.UserSession; import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; +import static org.apache.commons.lang.StringUtils.defaultIfBlank; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; import static org.sonar.server.component.NewComponent.newComponentBuilder; import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; @@ -161,7 +161,7 @@ public class ReportSubmitter { NewComponent newProject = newComponentBuilder() .setOrganizationUuid(organization.getUuid()) .setKey(projectKey) - .setName(StringUtils.defaultIfBlank(projectName, projectKey)) + .setName(defaultIfBlank(projectName, projectKey)) .setBranch(deprecatedBranch) .setQualifier(Qualifiers.PROJECT) .setPrivate(newProjectPrivate) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java index d1bb10c3234..6bbbd309781 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImpl.java @@ -29,7 +29,10 @@ import javax.annotation.concurrent.Immutable; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang.StringUtils.abbreviate; import static org.apache.commons.lang.StringUtils.trimToNull; +import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_DESCRIPTION_LENGTH; +import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH; @Immutable public class ComponentImpl implements Component { @@ -186,12 +189,12 @@ public class ComponentImpl implements Component { } public Builder setName(String name) { - this.name = requireNonNull(name, NAME_CANNOT_BE_NULL); + this.name = abbreviate(requireNonNull(name, NAME_CANNOT_BE_NULL), MAX_COMPONENT_NAME_LENGTH); return this; } public Builder setDescription(@Nullable String description) { - this.description = trimToNull(description); + this.description = abbreviate(trimToNull(description), MAX_COMPONENT_DESCRIPTION_LENGTH); return this; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java index 50cdf2e082a..b6964fb1532 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/ComponentTreeBuilder.java @@ -149,7 +149,7 @@ public class ComponentTreeBuilder { if (branch.isMain()) { builder .setName(nameOfProject(component)) - .setDescription(trimToNull(component.getDescription())); + .setDescription(component.getDescription()); } else { builder .setName(project.getName()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/CreateAction.java index de8d06b77cf..6df58965ba6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/CreateAction.java @@ -19,6 +19,8 @@ */ package org.sonar.server.project.ws; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -32,9 +34,7 @@ import org.sonar.server.project.Visibility; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Projects.CreateWsResponse; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - +import static org.apache.commons.lang.StringUtils.abbreviate; import static org.sonar.api.resources.Qualifiers.PROJECT; import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH; import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH; @@ -88,9 +88,8 @@ public class CreateAction implements ProjectsWsAction { .setExampleValue(KEY_PROJECT_EXAMPLE_001); action.createParam(PARAM_NAME) - .setDescription("Name of the project") + .setDescription("Name of the project. If name is longer than %d, it is abbreviated.", MAX_COMPONENT_NAME_LENGTH) .setRequired(true) - .setMaximumLength(MAX_COMPONENT_NAME_LENGTH) .setExampleValue("SonarQube"); action.createParam(PARAM_BRANCH) @@ -138,7 +137,7 @@ public class CreateAction implements ProjectsWsAction { return CreateRequest.builder() .setOrganization(request.param(PARAM_ORGANIZATION)) .setKey(request.mandatoryParam(PARAM_PROJECT)) - .setName(request.mandatoryParam(PARAM_NAME)) + .setName(abbreviate(request.mandatoryParam(PARAM_NAME), MAX_COMPONENT_NAME_LENGTH)) .setBranch(request.param(PARAM_BRANCH)) .setVisibility(request.param(PARAM_VISIBILITY)) .build(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java index 969817d88a3..c35385b08ea 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.ce.ws; +import com.google.common.base.Strings; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Arrays; @@ -112,6 +113,27 @@ public class SubmitActionTest { assertThat(map.getValue()).containsOnly(entry("branch", "branch1"), entry("key", "value1=value2")); } + @Test + public void abbreviate_long_name() { + String longName = Strings.repeat("a", 1_000); + String expectedName = Strings.repeat("a", 497) + "..."; + when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), isNull(), eq(expectedName), + anyMap(), any())).thenReturn(A_CE_TASK); + + Ce.SubmitResponse submitResponse = tester.newRequest() + .setParam("projectKey", "my_project") + .setParam("projectName", longName) + .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar") + .setMethod("POST") + .executeProtobuf(Ce.SubmitResponse.class); + + verify(reportSubmitter).submit(eq(organizationKey), eq("my_project"), isNull(), eq(expectedName), + anyMap(), any()); + + assertThat(submitResponse.getTaskId()).isEqualTo("TASK_1"); + assertThat(submitResponse.getProjectId()).isEqualTo("PROJECT_1"); + } + @Test public void test_example_json_response() { when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), isNull(), eq("My Project"), diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/NewComponentTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/NewComponentTest.java index 0192fa8a256..d17576e2510 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/NewComponentTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/NewComponentTest.java @@ -90,11 +90,11 @@ public class NewComponentTest { public void build_fails_with_IAE_when_name_is_longer_than_2000_characters() { underTest.setOrganizationUuid(ORGANIZATION_UUID) .setKey(KEY) - .setName(repeat("a", 2001)); + .setName(repeat("a", 501)); expectBuildException( IllegalArgumentException.class, - "Component name length (2001) is longer than the maximum authorized (2000)"); + "Component name length (501) is longer than the maximum authorized (500)"); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java index 1754a9e261a..c4921b8e68e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ComponentImplTest.java @@ -26,6 +26,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.server.computation.task.projectanalysis.component.Component.Status; +import static com.google.common.base.Strings.repeat; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE; @@ -37,7 +38,7 @@ public class ComponentImplTest { static final String UUID = "UUID"; @Rule - public ExpectedException thrown = ExpectedException.none(); + public ExpectedException expectedException = ExpectedException.none(); @Test public void verify_key_uuid_and_name() { @@ -50,21 +51,21 @@ public class ComponentImplTest { @Test public void builder_throws_NPE_if_component_arg_is_Null() { - thrown.expect(NullPointerException.class); + expectedException.expect(NullPointerException.class); builder(null); } @Test public void builder_throws_NPE_if_status_arg_is_Null() { - thrown.expect(NullPointerException.class); + expectedException.expect(NullPointerException.class); builder(FILE).setStatus(null); } @Test public void builder_throws_NPE_if_status_is_Null() { - thrown.expect(NullPointerException.class); + expectedException.expect(NullPointerException.class); builder(Component.Type.DIRECTORY) .setName("DIR") @@ -76,28 +77,28 @@ public class ComponentImplTest { @Test public void set_key_throws_NPE_if_component_arg_is_Null() { - thrown.expect(NullPointerException.class); + expectedException.expect(NullPointerException.class); builder(FILE).setUuid(null); } @Test public void set_uuid_throws_NPE_if_component_arg_is_Null() { - thrown.expect(NullPointerException.class); + expectedException.expect(NullPointerException.class); builder(FILE).setKey(null); } @Test public void build_without_key_throws_NPE_if_component_arg_is_Null() { - thrown.expect(NullPointerException.class); + expectedException.expect(NullPointerException.class); builder(FILE).setUuid("ABCD").build(); } @Test public void build_without_uuid_throws_NPE_if_component_arg_is_Null() { - thrown.expect(NullPointerException.class); + expectedException.expect(NullPointerException.class); builder(FILE).setKey(KEY).build(); } @@ -169,6 +170,30 @@ public class ComponentImplTest { assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey); } + @Test + public void keep_500_first_characters_of_name() { + String veryLongString = repeat("a", 3_000); + + ComponentImpl underTest = buildSimpleComponent(FILE, "file") + .setName(veryLongString) + .build(); + + String expectedName = repeat("a", 500-3) + "..."; + assertThat(underTest.getName()).isEqualTo(expectedName); + } + + @Test + public void keep_2000_first_characters_of_description() { + String veryLongString = repeat("a", 3_000); + + ComponentImpl underTest = buildSimpleComponent(FILE, "file") + .setDescription(veryLongString) + .build(); + + String expectedDescription = repeat("a", 2_000-3) + "..."; + assertThat(underTest.getDescription()).isEqualTo(expectedDescription); + } + @Test public void build_with_child() { ComponentImpl child = builder(FILE) diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java index 31d3936a36c..a2be524f1a6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.project.ws; -import org.assertj.core.api.AssertionsForClassTypes; +import com.google.common.base.Strings; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -212,7 +212,23 @@ public class CreateActionTest { .setParam("visibility", "public") .executeProtobuf(CreateWsResponse.class); - AssertionsForClassTypes.assertThat(result.getProject().getVisibility()).isEqualTo("public"); + assertThat(result.getProject().getVisibility()).isEqualTo("public"); + } + + @Test + public void abbreviate_project_name_if_very_long() { + OrganizationDto organization = db.organizations().insert(); + userSession.addPermission(PROVISION_PROJECTS, organization); + String longName = Strings.repeat("a", 1_000); + + ws.newRequest() + .setParam("key", DEFAULT_PROJECT_KEY) + .setParam("name", longName) + .setParam("organization", organization.getKey()) + .executeProtobuf(CreateWsResponse.class); + + assertThat(db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get().name()) + .isEqualTo(Strings.repeat("a", 497) + "..."); } @Test @@ -335,7 +351,7 @@ public class CreateActionTest { WebService.Param name = definition.param(PARAM_NAME); assertThat(name.isRequired()).isTrue(); - assertThat(name.maximumLength()).isEqualTo(2000); + assertThat(name.description()).isEqualTo("Name of the project. If name is longer than 500, it is abbreviated."); } private CreateWsResponse call(CreateRequest request) { diff --git a/tests/src/test/java/org/sonarqube/tests/project/OrganizationProjectSuite.java b/tests/src/test/java/org/sonarqube/tests/project/OrganizationProjectSuite.java index c0675b8159f..75dc194bac2 100644 --- a/tests/src/test/java/org/sonarqube/tests/project/OrganizationProjectSuite.java +++ b/tests/src/test/java/org/sonarqube/tests/project/OrganizationProjectSuite.java @@ -36,6 +36,7 @@ import static util.ItUtils.xooPlugin; ProjectBulkDeletionPageTest.class, ProjectDeletionTest.class, ProjectFilterTest.class, + ProjectInfoTest.class, ProjectKeyUpdateTest.class, ProjectKeyUpdatePageTest.class, ProjectLeakPageTest.class, diff --git a/tests/src/test/java/org/sonarqube/tests/project/ProjectInfoTest.java b/tests/src/test/java/org/sonarqube/tests/project/ProjectInfoTest.java new file mode 100644 index 00000000000..407acd2478f --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/project/ProjectInfoTest.java @@ -0,0 +1,68 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.sonarqube.tests.project; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarScanner; +import java.io.File; +import java.io.IOException; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonarqube.qa.util.Tester; +import org.sonarqube.ws.Components.Component; +import org.sonarqube.ws.Organizations; +import org.sonarqube.ws.client.components.ShowRequest; +import util.XooProjectBuilder; + +import static org.apache.commons.lang.StringUtils.repeat; +import static org.assertj.core.api.Assertions.assertThat; + +public class ProjectInfoTest { + + @ClassRule + public static Orchestrator orchestrator = OrganizationProjectSuite.ORCHESTRATOR; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public Tester tester = new Tester(orchestrator); + + @Test + public void project_name_and_description_should_be_truncated_if_too_long() throws IOException { + Organizations.Organization organization = tester.organizations().generate(); + File projectDir = temp.newFolder(); + new XooProjectBuilder("sample").setFilesPerModule(0).build(projectDir); + String longName = repeat("x", 1_000); + String longDescription = repeat("y", 3_000); + + orchestrator.executeBuild(SonarScanner.create(projectDir, + "sonar.organization", organization.getKey(), + "sonar.login", "admin", + "sonar.password", "admin", + "sonar.projectDescription", longDescription, + "sonar.projectName", longName)); + + Component createdProject = tester.wsClient().components().show(new ShowRequest().setComponent("sample")).getComponent(); + assertThat(createdProject.getName()).isEqualTo(repeat("x", 497) + "..."); + assertThat(createdProject.getDescription()).isEqualTo(repeat("y", 1_997) + "..."); + } +} -- 2.39.5