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.

PersistComponentsStepTest.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.ce.task.projectanalysis.step;
  21. import org.junit.Test;
  22. import org.sonar.api.utils.System2;
  23. import org.sonar.ce.task.projectanalysis.component.BranchPersister;
  24. import org.sonar.ce.task.projectanalysis.component.Component;
  25. import org.sonar.ce.task.projectanalysis.component.MutableDisabledComponentsHolder;
  26. import org.sonar.ce.task.projectanalysis.component.ProjectPersister;
  27. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  28. import org.sonar.ce.task.step.TestComputationStepContext;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.component.ComponentDao;
  32. import static java.util.Collections.emptyList;
  33. import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
  34. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  35. import static org.mockito.ArgumentMatchers.any;
  36. import static org.mockito.ArgumentMatchers.eq;
  37. import static org.mockito.Mockito.doReturn;
  38. import static org.mockito.Mockito.mock;
  39. public class PersistComponentsStepTest {
  40. @Test
  41. public void should_fail_if_project_is_not_stored_in_database_yet() {
  42. TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
  43. Component component = mock(Component.class);
  44. DbClient dbClient = mock(DbClient.class);
  45. ComponentDao componentDao = mock(ComponentDao.class);
  46. String projectKey = randomAlphabetic(20);
  47. doReturn(component).when(treeRootHolder).getRoot();
  48. doReturn(projectKey).when(component).getKey();
  49. doReturn(componentDao).when(dbClient).componentDao();
  50. doReturn(emptyList()).when(componentDao).selectByBranchUuid(eq(projectKey), any(DbSession.class));
  51. assertThatThrownBy(() -> new PersistComponentsStep(
  52. dbClient,
  53. treeRootHolder,
  54. System2.INSTANCE,
  55. mock(MutableDisabledComponentsHolder.class),
  56. mock(BranchPersister.class),
  57. mock(ProjectPersister.class)).execute(new TestComputationStepContext()))
  58. .isInstanceOf(IllegalStateException.class)
  59. .hasMessageContaining("The project '" + projectKey + "' is not stored in the database, during a project analysis");
  60. }
  61. }