]> source.dussan.org Git - sonarqube.git/blob
d4581c0ec461b82dcb18d0e73ccfe6f14da946d6
[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.locations.flow;
21
22 import java.util.List;
23 import java.util.Map;
24 import org.apache.commons.lang.math.RandomUtils;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.sonar.ce.task.projectanalysis.component.Component;
31 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
32 import org.sonar.db.protobuf.DbCommons;
33 import org.sonar.db.protobuf.DbIssues;
34
35 import static java.util.function.Function.identity;
36 import static java.util.stream.Collectors.toMap;
37 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class FlowGeneratorTest {
44
45   private static final String COMPONENT_NAME = "test_comp";
46
47   @Mock
48   private TreeRootHolder treeRootHolder;
49
50   @InjectMocks
51   private FlowGenerator flowGenerator;
52
53   @Test
54   public void convertFlows_withNullDbLocations_returnsEmptyList() {
55     assertThat(flowGenerator.convertFlows(COMPONENT_NAME, null)).isEmpty();
56   }
57
58   @Test
59   public void convertFlows_withEmptyDbLocations_returnsEmptyList() {
60     DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder().build();
61     assertThat(flowGenerator.convertFlows(COMPONENT_NAME, issueLocations)).isEmpty();
62   }
63
64   @Test
65   public void convertFlows_withSingleDbLocations_returnsCorrectFlow() {
66     DbIssues.Location location = createDbLocation("comp_id_1");
67     DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
68       .addFlow(createFlow(location))
69       .build();
70
71     List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
72
73     assertThat(flows).hasSize(1);
74     Flow singleFlow = flows.iterator().next();
75
76     assertThat(singleFlow.getLocations()).hasSize(1);
77     Location singleLocation = singleFlow.getLocations().iterator().next();
78
79     assertLocationMatches(singleLocation, location);
80   }
81
82   @Test
83   public void convertFlows_with2FlowsSingleDbLocations_returnsCorrectFlow() {
84     DbIssues.Location location1 = createDbLocation("comp_id_1");
85     DbIssues.Location location2 = createDbLocation("comp_id_2");
86     DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
87       .addFlow(createFlow(location1))
88       .addFlow(createFlow(location2))
89       .build();
90
91     List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
92
93     assertThat(flows).hasSize(2).extracting(f -> f.getLocations().size()).containsExactly(1, 1);
94     Map<String, DbIssues.Location> toDbLocation = Map.of(
95       "file_path_" + location1.getComponentId(), location1,
96       "file_path_" + location2.getComponentId(), location2);
97     flows.stream()
98       .map(actualFlow -> actualFlow.getLocations().iterator().next())
99       .forEach(l -> assertLocationMatches(l, toDbLocation.get(l.getFilePath())));
100   }
101
102   @Test
103   public void convertFlows_with2DbLocations_returns() {
104     DbIssues.Location location1 = createDbLocation("comp_id_1");
105     DbIssues.Location location2 = createDbLocation("comp_id_2");
106     DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
107       .addFlow(createFlow(location1, location2))
108       .build();
109
110     List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
111
112     assertThat(flows).hasSize(1);
113     Flow singleFlow = flows.iterator().next();
114
115     assertThat(singleFlow.getLocations()).hasSize(2);
116     Map<String, Location> pathToLocations = singleFlow.getLocations()
117       .stream()
118       .collect(toMap(Location::getFilePath, identity()));
119
120     assertLocationMatches(pathToLocations.get("file_path_comp_id_1"), location1);
121     assertLocationMatches(pathToLocations.get("file_path_comp_id_2"), location2);
122
123   }
124
125   private DbIssues.Location createDbLocation(String componentId) {
126     org.sonar.db.protobuf.DbCommons.TextRange textRange = org.sonar.db.protobuf.DbCommons.TextRange.newBuilder()
127       .setStartLine(RandomUtils.nextInt())
128       .setEndLine(RandomUtils.nextInt())
129       .setStartOffset(RandomUtils.nextInt())
130       .setEndOffset(RandomUtils.nextInt())
131       .build();
132
133     Component component = mock(Component.class);
134     when(component.getName()).thenReturn("file_path_" + componentId);
135     when(treeRootHolder.getComponentByUuid(componentId)).thenReturn(component);
136     return DbIssues.Location.newBuilder()
137       .setComponentId(componentId)
138       .setChecksum("hash" + randomAlphanumeric(10))
139       .setTextRange(textRange)
140       .setMsg("msg" + randomAlphanumeric(15))
141       .build();
142   }
143
144   private static DbIssues.Flow createFlow(DbIssues.Location ... locations) {
145     return DbIssues.Flow.newBuilder()
146       .addAllLocation(List.of(locations))
147       .build();
148   }
149
150   private static void assertLocationMatches(Location actualLocation, DbIssues.Location sourceLocation) {
151     assertThat(actualLocation.getMessage()).isEqualTo(sourceLocation.getMsg());
152     DbCommons.TextRange textRange = sourceLocation.getTextRange();
153     assertThat(actualLocation.getTextRange().getStartLine()).isEqualTo(textRange.getStartLine());
154     assertThat(actualLocation.getTextRange().getEndLine()).isEqualTo(textRange.getEndLine());
155     assertThat(actualLocation.getTextRange().getStartLineOffset()).isEqualTo(textRange.getStartOffset());
156     assertThat(actualLocation.getTextRange().getEndLineOffset()).isEqualTo(textRange.getEndOffset());
157     assertThat(actualLocation.getTextRange().getHash()).isEqualTo(sourceLocation.getChecksum());
158   }
159
160 }