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.security.SecureRandom;
23 import java.util.List;
25 import java.util.Random;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.sonar.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
33 import org.sonar.db.protobuf.DbCommons;
34 import org.sonar.db.protobuf.DbIssues;
36 import static java.util.function.Function.identity;
37 import static java.util.stream.Collectors.toMap;
38 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
43 @RunWith(MockitoJUnitRunner.class)
44 public class FlowGeneratorTest {
46 private static final String COMPONENT_NAME = "test_comp";
48 private final Random random = new SecureRandom();
52 private TreeRootHolder treeRootHolder;
55 private FlowGenerator flowGenerator;
58 public void convertFlows_withNullDbLocations_returnsEmptyList() {
59 assertThat(flowGenerator.convertFlows(COMPONENT_NAME, null)).isEmpty();
63 public void convertFlows_withEmptyDbLocations_returnsEmptyList() {
64 DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder().build();
65 assertThat(flowGenerator.convertFlows(COMPONENT_NAME, issueLocations)).isEmpty();
69 public void convertFlows_withSingleDbLocations_returnsCorrectFlow() {
70 DbIssues.Location location = createDbLocation("comp_id_1");
71 DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
72 .addFlow(createFlow(location))
75 List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
77 assertThat(flows).hasSize(1);
78 Flow singleFlow = flows.iterator().next();
80 assertThat(singleFlow.getLocations()).hasSize(1);
81 Location singleLocation = singleFlow.getLocations().iterator().next();
83 assertLocationMatches(singleLocation, location);
87 public void convertFlows_with2FlowsSingleDbLocations_returnsCorrectFlow() {
88 DbIssues.Location location1 = createDbLocation("comp_id_1");
89 DbIssues.Location location2 = createDbLocation("comp_id_2");
90 DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
91 .addFlow(createFlow(location1))
92 .addFlow(createFlow(location2))
95 List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
97 assertThat(flows).hasSize(2).extracting(f -> f.getLocations().size()).containsExactly(1, 1);
98 Map<String, DbIssues.Location> toDbLocation = Map.of(
99 "file_path_" + location1.getComponentId(), location1,
100 "file_path_" + location2.getComponentId(), location2);
102 .map(actualFlow -> actualFlow.getLocations().iterator().next())
103 .forEach(l -> assertLocationMatches(l, toDbLocation.get(l.getFilePath())));
107 public void convertFlows_with2DbLocations_returns() {
108 DbIssues.Location location1 = createDbLocation("comp_id_1");
109 DbIssues.Location location2 = createDbLocation("comp_id_2");
110 DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
111 .addFlow(createFlow(location1, location2))
114 List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
116 assertThat(flows).hasSize(1);
117 Flow singleFlow = flows.iterator().next();
119 assertThat(singleFlow.getLocations()).hasSize(2);
120 Map<String, Location> pathToLocations = singleFlow.getLocations()
122 .collect(toMap(Location::getFilePath, identity()));
124 assertLocationMatches(pathToLocations.get("file_path_comp_id_1"), location1);
125 assertLocationMatches(pathToLocations.get("file_path_comp_id_2"), location2);
129 private DbIssues.Location createDbLocation(String componentId) {
130 org.sonar.db.protobuf.DbCommons.TextRange textRange = org.sonar.db.protobuf.DbCommons.TextRange.newBuilder()
131 .setStartLine(random.nextInt(Integer.MAX_VALUE))
132 .setEndLine(random.nextInt(Integer.MAX_VALUE))
133 .setStartOffset(random.nextInt(Integer.MAX_VALUE))
134 .setEndOffset(random.nextInt(Integer.MAX_VALUE))
137 Component component = mock(Component.class);
138 when(component.getName()).thenReturn("file_path_" + componentId);
139 when(treeRootHolder.getComponentByUuid(componentId)).thenReturn(component);
140 return DbIssues.Location.newBuilder()
141 .setComponentId(componentId)
142 .setChecksum("hash" + randomAlphanumeric(10))
143 .setTextRange(textRange)
144 .setMsg("msg" + randomAlphanumeric(15))
148 private static DbIssues.Flow createFlow(DbIssues.Location ... locations) {
149 return DbIssues.Flow.newBuilder()
150 .addAllLocation(List.of(locations))
154 private static void assertLocationMatches(Location actualLocation, DbIssues.Location sourceLocation) {
155 assertThat(actualLocation.getMessage()).isEqualTo(sourceLocation.getMsg());
156 DbCommons.TextRange textRange = sourceLocation.getTextRange();
157 assertThat(actualLocation.getTextRange().getStartLine()).isEqualTo(textRange.getStartLine());
158 assertThat(actualLocation.getTextRange().getEndLine()).isEqualTo(textRange.getEndLine());
159 assertThat(actualLocation.getTextRange().getStartLineOffset()).isEqualTo(textRange.getStartOffset());
160 assertThat(actualLocation.getTextRange().getEndLineOffset()).isEqualTo(textRange.getEndOffset());
161 assertThat(actualLocation.getTextRange().getHash()).isEqualTo(sourceLocation.getChecksum());