]> source.dussan.org Git - sonarqube.git/blob
90f547b0fe0c44089d61f2df9956102b53e1bf2b
[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.security.SecureRandom;
23 import java.util.List;
24 import java.util.Map;
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;
35
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;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class FlowGeneratorTest {
45
46   private static final String COMPONENT_NAME = "test_comp";
47
48   private final Random random = new SecureRandom();
49
50
51   @Mock
52   private TreeRootHolder treeRootHolder;
53
54   @InjectMocks
55   private FlowGenerator flowGenerator;
56
57   @Test
58   public void convertFlows_withNullDbLocations_returnsEmptyList() {
59     assertThat(flowGenerator.convertFlows(COMPONENT_NAME, null)).isEmpty();
60   }
61
62   @Test
63   public void convertFlows_withEmptyDbLocations_returnsEmptyList() {
64     DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder().build();
65     assertThat(flowGenerator.convertFlows(COMPONENT_NAME, issueLocations)).isEmpty();
66   }
67
68   @Test
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))
73       .build();
74
75     List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
76
77     assertThat(flows).hasSize(1);
78     Flow singleFlow = flows.iterator().next();
79
80     assertThat(singleFlow.getLocations()).hasSize(1);
81     Location singleLocation = singleFlow.getLocations().iterator().next();
82
83     assertLocationMatches(singleLocation, location);
84   }
85
86   @Test
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))
93       .build();
94
95     List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
96
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);
101     flows.stream()
102       .map(actualFlow -> actualFlow.getLocations().iterator().next())
103       .forEach(l -> assertLocationMatches(l, toDbLocation.get(l.getFilePath())));
104   }
105
106   @Test
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))
112       .build();
113
114     List<Flow> flows = flowGenerator.convertFlows(COMPONENT_NAME, issueLocations);
115
116     assertThat(flows).hasSize(1);
117     Flow singleFlow = flows.iterator().next();
118
119     assertThat(singleFlow.getLocations()).hasSize(2);
120     Map<String, Location> pathToLocations = singleFlow.getLocations()
121       .stream()
122       .collect(toMap(Location::getFilePath, identity()));
123
124     assertLocationMatches(pathToLocations.get("file_path_comp_id_1"), location1);
125     assertLocationMatches(pathToLocations.get("file_path_comp_id_2"), location2);
126
127   }
128
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))
135       .build();
136
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))
145       .build();
146   }
147
148   private static DbIssues.Flow createFlow(DbIssues.Location ... locations) {
149     return DbIssues.Flow.newBuilder()
150       .addAllLocation(List.of(locations))
151       .build();
152   }
153
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());
162   }
163
164 }