3 * Copyright (C) 2009-2024 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.locations.flow;
22 import java.util.List;
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;
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;
42 @RunWith(MockitoJUnitRunner.class)
43 public class FlowGeneratorTest {
45 private static final String COMPONENT_NAME = "test_comp";
48 private TreeRootHolder treeRootHolder;
51 private FlowGenerator flowGenerator;
54 public void convertFlows_withNullDbLocations_returnsEmptyList() {
55 assertThat(flowGenerator.convertFlows(COMPONENT_NAME, null)).isEmpty();
59 public void convertFlows_withEmptyDbLocations_returnsEmptyList() {
60 DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder().build();
61 assertThat(flowGenerator.convertFlows(COMPONENT_NAME, issueLocations)).isEmpty();
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))
71 List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
73 assertThat(flows).hasSize(1);
74 Flow singleFlow = flows.iterator().next();
76 assertThat(singleFlow.getLocations()).hasSize(1);
77 Location singleLocation = singleFlow.getLocations().iterator().next();
79 assertLocationMatches(singleLocation, location);
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))
91 List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
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);
98 .map(actualFlow -> actualFlow.getLocations().iterator().next())
99 .forEach(l -> assertLocationMatches(l, toDbLocation.get(l.getFilePath())));
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))
110 List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
112 assertThat(flows).hasSize(1);
113 Flow singleFlow = flows.iterator().next();
115 assertThat(singleFlow.getLocations()).hasSize(2);
116 Map<String, Location> pathToLocations = singleFlow.getLocations()
118 .collect(toMap(Location::getFilePath, identity()));
120 assertLocationMatches(pathToLocations.get("file_path_comp_id_1"), location1);
121 assertLocationMatches(pathToLocations.get("file_path_comp_id_2"), location2);
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())
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))
144 private static DbIssues.Flow createFlow(DbIssues.Location ... locations) {
145 return DbIssues.Flow.newBuilder()
146 .addAllLocation(List.of(locations))
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());