You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentServiceTest.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.component;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.db.component.ComponentDbTester;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.server.es.TestProjectIndexers;
  30. import org.sonar.server.project.ProjectLifeCycleListeners;
  31. import org.sonar.server.tester.UserSessionRule;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.mockito.Mockito.mock;
  34. import static org.sonar.db.component.ComponentTesting.newFileDto;
  35. import static org.sonar.db.component.ComponentTesting.newModuleDto;
  36. public class ComponentServiceTest {
  37. @Rule
  38. public final UserSessionRule userSession = UserSessionRule.standalone();
  39. @Rule
  40. public final DbTester dbTester = DbTester.create(System2.INSTANCE);
  41. private final ComponentDbTester componentDb = new ComponentDbTester(dbTester);
  42. private final DbClient dbClient = dbTester.getDbClient();
  43. private final DbSession dbSession = dbTester.getSession();
  44. private final TestProjectIndexers projectIndexers = new TestProjectIndexers();
  45. private final ProjectLifeCycleListeners projectLifeCycleListeners = mock(ProjectLifeCycleListeners.class);
  46. private final ComponentService underTest = new ComponentService(dbClient, userSession, projectIndexers, projectLifeCycleListeners);
  47. @Test
  48. public void bulk_update() {
  49. ComponentDto project = componentDb.insertPublicProject(c -> c.setDbKey("my_project"));
  50. ComponentDto module = componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:root:module"));
  51. ComponentDto inactiveModule = componentDb.insertComponent(newModuleDto(project).setDbKey("my_project:root:inactive_module").setEnabled(false));
  52. ComponentDto file = componentDb.insertComponent(newFileDto(module, null).setDbKey("my_project:root:module:src/File.xoo"));
  53. ComponentDto inactiveFile = componentDb.insertComponent(newFileDto(module, null).setDbKey("my_project:root:module:src/InactiveFile.xoo").setEnabled(false));
  54. underTest.bulkUpdateKey(dbSession, componentDb.getProjectDto(project), "my_", "your_");
  55. assertComponentKeyUpdated(project.getDbKey(), "your_project");
  56. assertComponentKeyUpdated(module.getDbKey(), "your_project:root:module");
  57. assertComponentKeyUpdated(file.getDbKey(), "your_project:root:module:src/File.xoo");
  58. assertComponentKeyUpdated(inactiveModule.getDbKey(), "your_project:root:inactive_module");
  59. assertComponentKeyUpdated(inactiveFile.getDbKey(), "your_project:root:module:src/InactiveFile.xoo");
  60. }
  61. private void assertComponentKeyUpdated(String oldKey, String newKey) {
  62. assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isEmpty();
  63. assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent();
  64. }
  65. }