3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.component;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.impl.utils.TestSystem2;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.project.ProjectDto;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.verifyNoMoreInteractions;
35 import static org.mockito.Mockito.when;
36 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
37 import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
38 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
40 public class ProjectPersisterIT {
41 private final static Component ROOT = builder(PROJECT, 1)
42 .setUuid("PROJECT_UUID")
43 .setKey("PROJECT_KEY")
44 .setDescription("PROJECT_DESC")
45 .setName("PROJECT_NAME")
49 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
51 public DbTester dbTester = DbTester.create(System2.INSTANCE);
53 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
54 public TestSystem2 system2 = new TestSystem2();
56 private ProjectPersister underTest = new ProjectPersister(dbTester.getDbClient(), treeRootHolder, system2);
59 public void prepare() {
60 treeRootHolder.setRoot(ROOT);
61 system2.setNow(1000L);
65 public void skip_portfolios() {
66 Component root = ViewsComponent.builder(VIEW, 1).build();
67 TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
68 when(treeRootHolder.getRoot()).thenReturn(root);
69 new ProjectPersister(dbTester.getDbClient(), treeRootHolder, system2).persist(dbTester.getSession());
70 verify(treeRootHolder).getRoot();
71 verifyNoMoreInteractions(treeRootHolder);
76 public void update_description() {
77 ProjectDto p1 = dbTester.components().insertPublicProject("PROJECT_UUID",
78 p -> p.setKey(ROOT.getKey()).setName(ROOT.getName()).setDescription("OLD_DESC")).getProjectDto();
80 assertProject("OLD_DESC", ROOT.getName(), p1.getUpdatedAt());
81 underTest.persist(dbTester.getSession());
82 assertProject(ROOT.getDescription(), ROOT.getName(), 1000L);
86 public void update_name() {
87 ProjectDto p1 = dbTester.components().insertPublicProject("PROJECT_UUID",
88 p -> p.setKey(ROOT.getKey()).setName("OLD_NAME").setDescription(ROOT.getDescription())).getProjectDto();
90 assertProject(ROOT.getDescription(), "OLD_NAME", p1.getUpdatedAt());
91 underTest.persist(dbTester.getSession());
92 assertProject(ROOT.getDescription(), ROOT.getName(), 1000L);
96 public void dont_update() {
97 ProjectDto p1 = dbTester.components().insertPublicProject(
98 c -> c.setUuid("PROJECT_UUID").setKey(ROOT.getKey()).setName(ROOT.getName()).setDescription(ROOT.getDescription())).getProjectDto();
100 assertProject(ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt());
101 underTest.persist(dbTester.getSession());
102 assertProject(ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt());
105 private void assertProject(String description, String name, long updated) {
106 assertThat(dbTester.getDbClient().projectDao().selectProjectByKey(dbTester.getSession(), ROOT.getKey()).get())
107 .extracting(ProjectDto::getName, ProjectDto::getDescription, ProjectDto::getUpdatedAt)
108 .containsExactly(name, description, updated);