123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- /*
- * SonarQube
- * Copyright (C) 2009-2023 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.sonar.server.projectlink.ws;
-
- import org.apache.commons.lang.StringUtils;
- import org.junit.Rule;
- import org.junit.Test;
- import org.sonar.api.server.ws.WebService;
- import org.sonar.api.utils.System2;
- import org.sonar.api.web.UserRole;
- import org.sonar.core.util.UuidFactoryFast;
- import org.sonar.db.DbClient;
- import org.sonar.db.DbSession;
- import org.sonar.db.DbTester;
- import org.sonar.db.component.BranchDto;
- import org.sonar.db.component.ComponentDto;
- import org.sonar.db.component.ComponentTesting;
- import org.sonar.db.component.PortfolioData;
- import org.sonar.db.component.ProjectData;
- import org.sonar.db.component.ProjectLinkDto;
- import org.sonar.db.project.ProjectDto;
- import org.sonar.server.component.TestComponentFinder;
- import org.sonar.server.exceptions.ForbiddenException;
- import org.sonar.server.exceptions.NotFoundException;
- import org.sonar.server.tester.UserSessionRule;
- import org.sonar.server.ws.WsActionTester;
- import org.sonarqube.ws.ProjectLinks;
-
- import static java.lang.String.format;
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.assertj.core.api.Assertions.assertThatThrownBy;
- import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_NAME;
- import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_PROJECT_ID;
- import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_PROJECT_KEY;
- import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_URL;
- import static org.sonar.test.JsonAssert.assertJson;
-
- public class CreateActionIT {
-
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private final DbClient dbClient = db.getDbClient();
- private final DbSession dbSession = db.getSession();
- private final WsActionTester ws = new WsActionTester(new CreateAction(dbClient, userSession, TestComponentFinder.from(db), UuidFactoryFast.getInstance()));
-
- @Test
- public void example_with_key() {
- ProjectDto project = db.components().insertPrivateProject().getProjectDto();
- logInAsProjectAdministrator(project);
-
- String result = ws.newRequest()
- .setMethod("POST")
- .setParam(PARAM_PROJECT_KEY, project.getKey())
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .execute().getInput();
-
- assertJson(result).ignoreFields("id").isSimilarTo(getClass().getResource("create-example.json"));
- }
-
- @Test
- public void example_with_id() {
- ProjectDto project = db.components().insertPrivateProject().getProjectDto();
- logInAsProjectAdministrator(project);
-
- String result = ws.newRequest()
- .setMethod("POST")
- .setParam(PARAM_PROJECT_ID, project.getUuid())
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .execute().getInput();
-
- assertJson(result).ignoreFields("id").isSimilarTo(getClass().getResource("create-example.json"));
- }
-
- @Test
- public void require_project_admin() {
- ProjectDto project = db.components().insertPrivateProject().getProjectDto();
- logInAsProjectAdministrator(project);
-
- createAndTest(project);
- }
-
- @Test
- public void with_long_name() {
- ProjectDto project = db.components().insertPrivateProject().getProjectDto();
- logInAsProjectAdministrator(project);
- String longName = StringUtils.leftPad("", 60, "a");
- String expectedType = StringUtils.leftPad("", 20, "a");
-
- createAndTest(project, longName, "http://example.org", expectedType);
- }
-
- @Test
- public void fail_if_no_name() {
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_KEY, "unknown")
- .setParam(PARAM_URL, "http://example.org")
- .execute())
- .isInstanceOf(IllegalArgumentException.class);
- }
-
- @Test
- public void fail_if_long_name() {
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_KEY, "unknown")
- .setParam(PARAM_NAME, StringUtils.leftPad("", 129, "*"))
- .setParam(PARAM_URL, "http://example.org")
- .execute())
- .isInstanceOf(IllegalArgumentException.class);
- }
-
- @Test
- public void fail_if_no_url() {
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_KEY, "unknown")
- .setParam(PARAM_NAME, "Custom")
- .execute())
- .isInstanceOf(IllegalArgumentException.class);
- }
-
- @Test
- public void fail_if_long_url() {
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_KEY, "unknown")
- .setParam(PARAM_NAME, "random")
- .setParam(PARAM_URL, StringUtils.leftPad("", 2049, "*"))
- .execute())
- .isInstanceOf(IllegalArgumentException.class);
- }
-
- @Test
- public void fail_when_no_project() {
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_KEY, "unknown")
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .execute())
- .isInstanceOf(NotFoundException.class);
- }
-
- @Test
- public void fail_if_anonymous() {
- userSession.anonymous();
-
- ProjectData projectData = db.components().insertPublicProject();
- ComponentDto project = projectData.getMainBranchComponent();
-
- userSession.registerProjects(projectData.getProjectDto());
-
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_KEY, project.getKey())
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .execute())
- .isInstanceOf(ForbiddenException.class);
- }
-
- @Test
- public void fail_if_not_project_admin() {
- userSession.logIn();
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_KEY, project.getKey())
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .execute())
- .isInstanceOf(ForbiddenException.class);
- }
-
- @Test
- public void fail_if_directory() {
- ProjectData project = db.components().insertPrivateProject();
- ComponentDto directory = db.components().insertComponent(ComponentTesting.newDirectory(project.getMainBranchComponent(), "A/B"));
- failIfNotAProjectWithKey(project.getProjectDto(), directory);
- failIfNotAProjectWithUuid(project.getProjectDto(), directory);
- }
-
- @Test
- public void fail_if_file() {
- ProjectData project = db.components().insertPrivateProject();
- ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project.getMainBranchComponent()));
- failIfNotAProjectWithKey(project.getProjectDto(), file);
- failIfNotAProjectWithUuid(project.getProjectDto(), file);
- }
-
- @Test
- public void fail_if_view() {
- PortfolioData view = db.components().insertPrivatePortfolioData();
-
- userSession.logIn().addProjectPermission(UserRole.ADMIN, view.getRootComponent());
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .setParam(PARAM_PROJECT_KEY, view.getPortfolioDto().getKey())
- .execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining("Project '" + view.getPortfolioDto().getKey() + "' not found");
-
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .setParam(PARAM_PROJECT_ID, view.getPortfolioDto().getUuid())
- .execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining("Project '" + view.getPortfolioDto().getUuid() + "' not found");
-
- }
-
- @Test
- public void fail_when_using_branch_db_uuid() {
- ProjectData project = db.components().insertPrivateProject();
- userSession.logIn().addProjectPermission(UserRole.USER, project.getProjectDto());
- BranchDto branch = db.components().insertProjectBranch(project.getProjectDto());
-
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_PROJECT_ID, branch.getUuid())
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining(format("Project '%s' not found", branch.getUuid()));
- }
-
- @Test
- public void define_create_action() {
- WebService.Action action = ws.getDef();
- assertThat(action).isNotNull();
- assertThat(action.isPost()).isTrue();
- assertThat(action.handler()).isNotNull();
- assertThat(action.responseExampleAsString()).isNotEmpty();
- assertThat(action.params()).hasSize(4);
- }
-
- private void failIfNotAProjectWithKey(ProjectDto project, ComponentDto component) {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
-
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .setParam(PARAM_PROJECT_KEY, component.getKey())
- .execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining("Project '" + component.getKey() + "' not found");
- }
-
- private void failIfNotAProjectWithUuid(ProjectDto project, ComponentDto component) {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
-
- assertThatThrownBy(() -> ws.newRequest()
- .setParam(PARAM_NAME, "Custom")
- .setParam(PARAM_URL, "http://example.org")
- .setParam(PARAM_PROJECT_ID, component.uuid())
- .execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining("Project '" + component.uuid() + "' not found");
- }
-
- private void createAndTest(ProjectDto project, String name, String url, String type) {
- ProjectLinks.CreateWsResponse response = ws.newRequest()
- .setMethod("POST")
- .setParam(PARAM_PROJECT_KEY, project.getKey())
- .setParam(PARAM_NAME, name)
- .setParam(PARAM_URL, url)
- .executeProtobuf(ProjectLinks.CreateWsResponse.class);
-
- String newId = response.getLink().getId();
-
- ProjectLinkDto link = dbClient.projectLinkDao().selectByUuid(dbSession, newId);
- assertThat(link.getName()).isEqualTo(name);
- assertThat(link.getHref()).isEqualTo(url);
- assertThat(link.getType()).isEqualTo(type);
- }
-
- private void createAndTest(ProjectDto project) {
- createAndTest(project, "Custom", "http://example.org", "custom");
- }
-
- private void logInAsProjectAdministrator(ProjectDto project) {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- }
- }
|