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 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.ce.task.projectanalysis.step;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  26. import org.sonar.ce.task.projectanalysis.component.BranchPersister;
  27. import org.sonar.ce.task.projectanalysis.component.Component;
  28. import org.sonar.ce.task.projectanalysis.component.MutableDisabledComponentsHolder;
  29. import org.sonar.ce.task.projectanalysis.component.ProjectPersister;
  30. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  31. import org.sonar.ce.task.step.TestComputationStepContext;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.component.ComponentDao;
  35. import static java.util.Collections.emptyList;
  36. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.ArgumentMatchers.eq;
  39. import static org.mockito.Mockito.doReturn;
  40. import static org.mockito.Mockito.mock;
  41. public class PersistComponentsStepTest {
  42. @Rule
  43. public ExpectedException thrown = ExpectedException.none();
  44. @Test
  45. public void should_fail_if_project_is_not_stored_in_database_yet() {
  46. TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
  47. Component component = mock(Component.class);
  48. DbClient dbClient = mock(DbClient.class);
  49. ComponentDao componentDao = mock(ComponentDao.class);
  50. String projectKey = randomAlphabetic(20);
  51. doReturn(component).when(treeRootHolder).getRoot();
  52. doReturn(projectKey).when(component).getDbKey();
  53. doReturn(componentDao).when(dbClient).componentDao();
  54. doReturn(emptyList()).when(componentDao).selectAllComponentsFromProjectKey(any(DbSession.class), eq(projectKey));
  55. thrown.expect(IllegalStateException.class);
  56. thrown.expectMessage("The project '" + projectKey + "' is not stored in the database, during a project analysis");
  57. new PersistComponentsStep(
  58. dbClient,
  59. treeRootHolder,
  60. System2.INSTANCE,
  61. mock(MutableDisabledComponentsHolder.class),
  62. mock(AnalysisMetadataHolder.class),
  63. mock(BranchPersister.class),
  64. mock(ProjectPersister.class)).execute(new TestComputationStepContext());
  65. }
  66. }