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.

ProjectPersisterTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.component;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.impl.utils.TestSystem2;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  27. import org.sonar.db.DbTester;
  28. import org.sonar.db.project.ProjectDto;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.verify;
  32. import static org.mockito.Mockito.verifyNoMoreInteractions;
  33. import static org.mockito.Mockito.when;
  34. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  35. import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
  36. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  37. public class ProjectPersisterTest {
  38. private final static Component ROOT = builder(PROJECT, 1)
  39. .setUuid("PROJECT_UUID")
  40. .setKey("PROJECT_KEY")
  41. .setDescription("PROJECT_DESC")
  42. .setName("PROJECT_NAME")
  43. .build();
  44. @Rule
  45. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  46. @Rule
  47. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  48. @Rule
  49. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  50. public TestSystem2 system2 = new TestSystem2();
  51. private ProjectPersister underTest = new ProjectPersister(dbTester.getDbClient(), treeRootHolder, system2);
  52. @Before
  53. public void prepare() {
  54. treeRootHolder.setRoot(ROOT);
  55. system2.setNow(1000L);
  56. }
  57. @Test
  58. public void skip_portfolios() {
  59. Component root = ViewsComponent.builder(VIEW, 1).build();
  60. TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
  61. when(treeRootHolder.getRoot()).thenReturn(root);
  62. new ProjectPersister(dbTester.getDbClient(), treeRootHolder, system2).persist(dbTester.getSession());
  63. verify(treeRootHolder).getRoot();
  64. verifyNoMoreInteractions(treeRootHolder);
  65. }
  66. @Test
  67. public void update_description() {
  68. ProjectDto p1 = dbTester.components().insertPublicProjectDto(
  69. c -> c.setUuid("PROJECT_UUID").setDbKey(ROOT.getKey()).setName(ROOT.getName()).setDescription("OLD_DESC"));
  70. assertProject("OLD_DESC", ROOT.getName(), p1.getUpdatedAt());
  71. underTest.persist(dbTester.getSession());
  72. assertProject(ROOT.getDescription(), ROOT.getName(), 1000L);
  73. }
  74. @Test
  75. public void update_name() {
  76. ProjectDto p1 = dbTester.components().insertPublicProjectDto(
  77. c -> c.setUuid("PROJECT_UUID").setDbKey(ROOT.getKey()).setName("OLD_NAME").setDescription(ROOT.getDescription()));
  78. assertProject(ROOT.getDescription(), "OLD_NAME", p1.getUpdatedAt());
  79. underTest.persist(dbTester.getSession());
  80. assertProject(ROOT.getDescription(), ROOT.getName(), 1000L);
  81. }
  82. @Test
  83. public void dont_update() {
  84. ProjectDto p1 = dbTester.components().insertPublicProjectDto(
  85. c -> c.setUuid("PROJECT_UUID").setDbKey(ROOT.getKey()).setName(ROOT.getName()).setDescription(ROOT.getDescription()));
  86. assertProject(ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt());
  87. underTest.persist(dbTester.getSession());
  88. assertProject(ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt());
  89. }
  90. private void assertProject(String description, String name, long updated) {
  91. assertThat(dbTester.getDbClient().projectDao().selectProjectByKey(dbTester.getSession(), ROOT.getKey()).get())
  92. .extracting(ProjectDto::getName, ProjectDto::getDescription, ProjectDto::getUpdatedAt)
  93. .containsExactly(name, description, updated);
  94. }
  95. }