12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- /*
- * 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.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.server.es.TestProjectIndexers;
- import org.sonar.server.project.ProjectLifeCycleListeners;
- import org.sonar.server.tester.UserSessionRule;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.mockito.Mockito.mock;
- import static org.sonar.db.component.ComponentTesting.newFileDto;
- import static org.sonar.db.component.ComponentTesting.newModuleDto;
-
- public class ComponentServiceTest {
-
- @Rule
- public final UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public final DbTester dbTester = DbTester.create(System2.INSTANCE);
-
- private final ComponentDbTester componentDb = new ComponentDbTester(dbTester);
- private final DbClient dbClient = dbTester.getDbClient();
- private final DbSession dbSession = dbTester.getSession();
- private final TestProjectIndexers projectIndexers = new TestProjectIndexers();
- private final ProjectLifeCycleListeners projectLifeCycleListeners = mock(ProjectLifeCycleListeners.class);
-
- private final ComponentService underTest = new ComponentService(dbClient, userSession, projectIndexers, projectLifeCycleListeners);
-
- @Test
- public void bulk_update() {
- ComponentDto project = componentDb.insertPublicProject(c -> c.setDbKey("my_project"));
- ComponentDto module = componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:root:module"));
- ComponentDto inactiveModule = componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:root:inactive_module").setEnabled(false));
- ComponentDto file = componentDb.insertComponent(newFileDto(module, null).setDbKey("my_project:root:module:src/File.xoo"));
- ComponentDto inactiveFile = componentDb.insertComponent(newFileDto(module, null).setDbKey("my_project:root:module:src/InactiveFile.xoo").setEnabled(false));
-
- underTest.bulkUpdateKey(dbSession, componentDb.getProjectDto(project), "my_", "your_");
-
- assertComponentKeyUpdated(project.getDbKey(), "your_project");
- assertComponentKeyUpdated(module.getDbKey(), "your_project:root:module");
- assertComponentKeyUpdated(file.getDbKey(), "your_project:root:module:src/File.xoo");
- assertComponentKeyUpdated(inactiveModule.getDbKey(), "your_project:root:inactive_module");
- assertComponentKeyUpdated(inactiveFile.getDbKey(), "your_project:root:module:src/InactiveFile.xoo");
- }
-
- private void assertComponentKeyUpdated(String oldKey, String newKey) {
- assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isEmpty();
- assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent();
- }
-
- }
|