]> source.dussan.org Git - sonarqube.git/blob
49d5b8850d250b56adfbeb20a00668c61db0db14
[sonarqube.git] /
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.issue;
21
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
27 import org.sonar.core.issue.DefaultIssue;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.mockito.Mockito.mock;
31 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
32
33 public class IssuesRepositoryVisitorTest {
34   static final String FILE_UUID = "FILE_UUID";
35   static final String FILE_KEY = "FILE_KEY";
36   static final int FILE_REF = 2;
37   static final Component FILE = builder(Component.Type.FILE, FILE_REF)
38     .setKey(FILE_KEY)
39     .setUuid(FILE_UUID)
40     .build();
41
42   static final String PROJECT_KEY = "PROJECT_KEY";
43   static final String PROJECT_UUID = "PROJECT_UUID";
44   static final int PROJECT_REF = 1;
45   static final Component PROJECT = builder(Component.Type.PROJECT, PROJECT_REF)
46     .setKey(PROJECT_KEY)
47     .setUuid(PROJECT_UUID)
48     .addChildren(FILE)
49     .build();
50
51   @Rule
52   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
53
54   @Rule
55   public ComponentIssuesRepositoryRule componentIssuesRepository = new ComponentIssuesRepositoryRule(treeRootHolder);
56
57   IssuesRepositoryVisitor underTest = new IssuesRepositoryVisitor(componentIssuesRepository);
58
59   @Before
60   public void setUp() {
61     treeRootHolder.setRoot(PROJECT);
62   }
63
64   @Test
65   public void feed_component_issues_repo() {
66     DefaultIssue i1 = mock(DefaultIssue.class);
67     DefaultIssue i2 = mock(DefaultIssue.class);
68
69     underTest.beforeComponent(FILE);
70     underTest.onIssue(FILE, i1);
71     underTest.onIssue(FILE, i2);
72     underTest.afterComponent(FILE);
73
74     assertThat(componentIssuesRepository.getIssues(FILE_REF)).hasSize(2);
75   }
76
77   @Test
78   public void empty_component_issues_repo_when_no_issue() {
79     DefaultIssue i1 = mock(DefaultIssue.class);
80     DefaultIssue i2 = mock(DefaultIssue.class);
81
82     underTest.beforeComponent(FILE);
83     underTest.onIssue(FILE, i1);
84     underTest.onIssue(FILE, i2);
85     underTest.afterComponent(FILE);
86     assertThat(componentIssuesRepository.getIssues(FILE)).hasSize(2);
87
88     underTest.beforeComponent(PROJECT);
89     underTest.afterComponent(PROJECT);
90     assertThat(componentIssuesRepository.getIssues(PROJECT)).isEmpty();
91   }
92 }