123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 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.component;
-
- import org.junit.Rule;
- import org.junit.Test;
- import org.sonar.api.utils.System2;
- import org.sonar.api.web.UserRole;
- import org.sonar.db.DbClient;
- import org.sonar.db.DbSession;
- import org.sonar.db.DbTester;
- import org.sonar.db.component.ComponentDbTester;
- import org.sonar.db.component.ComponentDto;
- import org.sonar.db.component.ComponentTesting;
- import org.sonar.db.project.ProjectDto;
- import org.sonar.server.es.ProjectIndexer;
- import org.sonar.server.es.TestProjectIndexers;
- import org.sonar.server.exceptions.ForbiddenException;
- import org.sonar.server.project.ProjectLifeCycleListeners;
- import org.sonar.server.tester.UserSessionRule;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.assertj.core.api.Assertions.assertThatThrownBy;
- import static org.mockito.Mockito.mock;
-
- public class ComponentServiceUpdateKeyTest {
-
- private final System2 system2 = System2.INSTANCE;
-
- @Rule
- public final UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public final DbTester db = DbTester.create(system2);
-
- private ComponentDbTester componentDb = new ComponentDbTester(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private TestProjectIndexers projectIndexers = new TestProjectIndexers();
- private ProjectLifeCycleListeners projectLifeCycleListeners = mock(ProjectLifeCycleListeners.class);
- private ComponentService underTest = new ComponentService(dbClient, userSession, projectIndexers, projectLifeCycleListeners);
-
- @Test
- public void update_project_key() {
- ComponentDto project = insertSampleProject();
- ComponentDto file = componentDb.insertComponent(ComponentTesting.newFileDto(project, null).setDbKey("sample:root:src/File.xoo"));
- ComponentDto inactiveFile = componentDb.insertComponent(ComponentTesting.newFileDto(project, null).setDbKey("sample:root:src/InactiveFile.xoo").setEnabled(false));
-
- dbSession.commit();
-
- logInAsProjectAdministrator(project);
- underTest.updateKey(dbSession, componentDb.getProjectDto(project), "sample2:root");
- dbSession.commit();
-
- // Check project key has been updated
- assertThat(db.getDbClient().componentDao().selectByKey(dbSession, project.getDbKey())).isEmpty();
- assertThat(db.getDbClient().componentDao().selectByKey(dbSession, "sample2:root")).isNotNull();
-
- // Check file key has been updated
- assertThat(db.getDbClient().componentDao().selectByKey(dbSession, file.getDbKey())).isEmpty();
- assertThat(db.getDbClient().componentDao().selectByKey(dbSession, "sample2:root:src/File.xoo")).isNotNull();
- assertThat(db.getDbClient().componentDao().selectByKey(dbSession, "sample2:root:src/InactiveFile.xoo")).isNotNull();
-
- assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getDbKey())).isEmpty();
-
- assertThat(projectIndexers.hasBeenCalled(project.uuid(), ProjectIndexer.Cause.PROJECT_KEY_UPDATE)).isTrue();
- }
-
- @Test
- public void update_provisioned_project_key() {
- ComponentDto provisionedProject = insertProject("provisionedProject");
-
- dbSession.commit();
-
- logInAsProjectAdministrator(provisionedProject);
- underTest.updateKey(dbSession, componentDb.getProjectDto(provisionedProject), "provisionedProject2");
- dbSession.commit();
-
- assertComponentKeyHasBeenUpdated(provisionedProject.getDbKey(), "provisionedProject2");
- assertThat(projectIndexers.hasBeenCalled(provisionedProject.uuid(), ProjectIndexer.Cause.PROJECT_KEY_UPDATE)).isTrue();
- }
-
- @Test
- public void fail_to_update_project_key_without_admin_permission() {
- ComponentDto project = insertSampleProject();
- userSession.logIn("john").addProjectPermission(UserRole.USER, project);
-
- ProjectDto projectDto = componentDb.getProjectDto(project);
- assertThatThrownBy(() -> underTest.updateKey(dbSession, projectDto, "sample2:root"))
- .isInstanceOf(ForbiddenException.class);
- }
-
- @Test
- public void fail_if_old_key_and_new_key_are_the_same() {
- ComponentDto project = insertSampleProject();
- ComponentDto anotherProject = componentDb.insertPrivateProject();
- logInAsProjectAdministrator(project);
-
- ProjectDto projectDto = componentDb.getProjectDto(project);
- String anotherProjectDbKey = anotherProject.getDbKey();
- assertThatThrownBy(() -> underTest.updateKey(dbSession, projectDto,
- anotherProjectDbKey))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage("Impossible to update key: a component with key \"" + anotherProjectDbKey + "\" already exists.");
- }
-
- @Test
- public void fail_if_new_key_is_empty() {
- ComponentDto project = insertSampleProject();
- logInAsProjectAdministrator(project);
-
- ProjectDto projectDto = componentDb.getProjectDto(project);
- assertThatThrownBy(() -> underTest.updateKey(dbSession, projectDto, ""))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage("Malformed key for ''. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
- }
-
- @Test
- public void fail_if_new_key_is_not_formatted_correctly() {
- ComponentDto project = insertSampleProject();
- logInAsProjectAdministrator(project);
-
- ProjectDto projectDto = componentDb.getProjectDto(project);
- assertThatThrownBy(() -> underTest.updateKey(dbSession, projectDto, "sample?root"))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage("Malformed key for 'sample?root'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
- }
-
- private ComponentDto insertSampleProject() {
- return insertProject("sample:root");
- }
-
- private ComponentDto insertProject(String key) {
- return componentDb.insertPrivateProject(c -> c.setDbKey(key));
- }
-
- private void assertComponentKeyHasBeenUpdated(String oldKey, String newKey) {
- assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isEmpty();
- assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent();
- }
-
- private void logInAsProjectAdministrator(ComponentDto provisionedProject) {
- userSession.logIn("john").addProjectPermission(UserRole.ADMIN, provisionedProject);
- }
- }
|