]> source.dussan.org Git - sonarqube.git/blob
4ee0dd0aef954d6ff38c53236afd3602828849cc
[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.server.duplication.ws;
21
22 import java.io.StringWriter;
23 import java.util.Collections;
24 import java.util.List;
25 import javax.annotation.Nullable;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.utils.text.JsonWriter;
29 import org.sonar.core.util.ProtobufJsonFormat;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.test.JsonAssert;
33
34 import static com.google.common.collect.Lists.newArrayList;
35 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
36 import static org.sonar.db.component.BranchType.PULL_REQUEST;
37 import static org.sonar.db.component.ComponentTesting.newDirectory;
38 import static org.sonar.db.component.ComponentTesting.newFileDto;
39
40 public class ShowResponseBuilderIT {
41
42   @Rule
43   public DbTester db = DbTester.create();
44
45   private ShowResponseBuilder underTest = new ShowResponseBuilder(db.getDbClient().componentDao());
46
47   @Test
48   public void write_duplications() {
49     ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
50     ComponentDto dir = db.components().insertComponent(newDirectory(project, "path"));
51     ComponentDto file1 = db.components().insertComponent(newFileDto(project, dir));
52     ComponentDto file2 = db.components().insertComponent(newFileDto(project, dir));
53     List<DuplicationsParser.Block> blocks = newArrayList();
54     blocks.add(new DuplicationsParser.Block(newArrayList(
55       Duplication.newComponent(file1, 57, 12),
56       Duplication.newComponent(file2, 73, 12))));
57
58     test(blocks, null, null,
59       "{\n" +
60         "  \"duplications\": [\n" +
61         "    {\n" +
62         "      \"blocks\": [\n" +
63         "        {\n" +
64         "          \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
65         "        },\n" +
66         "        {\n" +
67         "          \"from\": 73, \"size\": 12, \"_ref\": \"2\"\n" +
68         "        }\n" +
69         "      ]\n" +
70         "    }," +
71         "  ],\n" +
72         "  \"files\": {\n" +
73         "    \"1\": {\n" +
74         "      \"key\": \"" + file1.getKey() + "\",\n" +
75         "      \"name\": \"" + file1.longName() + "\",\n" +
76         "      \"project\": \"" + project.getKey() + "\",\n" +
77         "      \"projectName\": \"" + project.longName() + "\",\n" +
78         "    },\n" +
79         "    \"2\": {\n" +
80         "      \"key\": \"" + file2.getKey() + "\",\n" +
81         "      \"name\": \"" + file2.longName() + "\",\n" +
82         "      \"project\": \"" + project.getKey() + "\",\n" +
83         "      \"projectName\": \"" + project.longName() + "\",\n" +
84         "    }\n" +
85         "  }" +
86         "}");
87   }
88
89   @Test
90   public void write_duplications_without_sub_project() {
91     ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
92     ComponentDto file1 = db.components().insertComponent(newFileDto(project));
93     ComponentDto file2 = db.components().insertComponent(newFileDto(project));
94     List<DuplicationsParser.Block> blocks = newArrayList();
95     blocks.add(new DuplicationsParser.Block(newArrayList(
96       Duplication.newComponent(file1, 57, 12),
97       Duplication.newComponent(file2, 73, 12))));
98
99     test(blocks, null, null,
100       "{\n" +
101         "  \"duplications\": [\n" +
102         "    {\n" +
103         "      \"blocks\": [\n" +
104         "        {\n" +
105         "          \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
106         "        },\n" +
107         "        {\n" +
108         "          \"from\": 73, \"size\": 12, \"_ref\": \"2\"\n" +
109         "        }\n" +
110         "      ]\n" +
111         "    }," +
112         "  ],\n" +
113         "  \"files\": {\n" +
114         "    \"1\": {\n" +
115         "      \"key\": \"" + file1.getKey() + "\",\n" +
116         "      \"name\": \"" + file1.longName() + "\",\n" +
117         "      \"project\": \"" + project.getKey() + "\",\n" +
118         "      \"projectName\": \"" + project.longName() + "\",\n" +
119         "    },\n" +
120         "    \"2\": {\n" +
121         "      \"key\": \"" + file2.getKey() + "\",\n" +
122         "      \"name\": \"" + file2.longName() + "\",\n" +
123         "      \"project\": \"" + project.getKey() + "\",\n" +
124         "      \"projectName\": \"" + project.longName() + "\",\n" +
125         "    }\n" +
126         "  }" +
127         "}");
128   }
129
130   @Test
131   public void write_duplications_with_a_removed_component() {
132     ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
133     ComponentDto file = db.components().insertComponent(newFileDto(project));
134     List<DuplicationsParser.Block> blocks = newArrayList();
135     blocks.add(new DuplicationsParser.Block(newArrayList(
136       Duplication.newComponent(file, 57, 12),
137       // Duplication on a removed file
138       Duplication.newRemovedComponent("key", 73, 12))));
139
140     test(blocks, null, null,
141       "{\n" +
142         "  \"duplications\": [\n" +
143         "    {\n" +
144         "      \"blocks\": [\n" +
145         "        {\n" +
146         "          \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
147         "        },\n" +
148         "        {\n" +
149         "          \"from\": 73, \"size\": 12\n" +
150         "        }\n" +
151         "      ]\n" +
152         "    }," +
153         "  ],\n" +
154         "  \"files\": {\n" +
155         "    \"1\": {\n" +
156         "      \"key\": \"" + file.getKey() + "\",\n" +
157         "      \"name\": \"" + file.longName() + "\",\n" +
158         "      \"project\": \"" + project.getKey() + "\",\n" +
159         "      \"projectName\": \"" + project.longName() + "\",\n" +
160         "    }\n" +
161         "  }" +
162         "}");
163   }
164
165   @Test
166   public void write_duplications_with_a_component_without_details() {
167     ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
168     ComponentDto file = db.components().insertComponent(newFileDto(project));
169     List<DuplicationsParser.Block> blocks = newArrayList();
170     blocks.add(new DuplicationsParser.Block(newArrayList(
171       Duplication.newComponent(file, 57, 12),
172       // Duplication on a file without details
173       Duplication.newTextComponent("project:path/to/file", 73, 12))));
174
175     test(blocks, null, null,
176       "{\n" +
177         "  \"duplications\": [\n" +
178         "    {\n" +
179         "      \"blocks\": [\n" +
180         "        {\n" +
181         "          \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
182         "        },\n" +
183         "        {\n" +
184         "          \"from\": 73, \"size\": 12\n" +
185         "        }\n" +
186         "      ]\n" +
187         "    }," +
188         "  ],\n" +
189         "  \"files\": {\n" +
190         "    \"1\": {\n" +
191         "      \"key\": \"" + file.getKey() + "\",\n" +
192         "      \"name\": \"" + file.longName() + "\",\n" +
193         "      \"project\": \"" + project.getKey() + "\",\n" +
194         "      \"projectName\": \"" + project.longName() + "\",\n" +
195         "    }\n" +
196         "    \"2\": {\n" +
197         "      \"key\": \"project:path/to/file\",\n" +
198         "      \"name\": \"path/to/file\",\n" +
199         "    }\n" +
200         "  }" +
201         "}");
202   }
203
204   @Test
205   public void write_duplications_on_branch() {
206     ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
207     String branchName = randomAlphanumeric(248);
208     ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
209     ComponentDto file1 = db.components().insertComponent(newFileDto(branch, project.uuid()));
210     ComponentDto file2 = db.components().insertComponent(newFileDto(branch, project.uuid()));
211     List<DuplicationsParser.Block> blocks = newArrayList();
212     blocks.add(new DuplicationsParser.Block(newArrayList(
213       Duplication.newComponent(file1, 57, 12),
214       Duplication.newComponent(file2, 73, 12))));
215
216     test(blocks, branchName, null,
217       "{\n" +
218         "  \"duplications\": [\n" +
219         "    {\n" +
220         "      \"blocks\": [\n" +
221         "        {\n" +
222         "          \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
223         "        },\n" +
224         "        {\n" +
225         "          \"from\": 73, \"size\": 12, \"_ref\": \"2\"\n" +
226         "        }\n" +
227         "      ]\n" +
228         "    }," +
229         "  ],\n" +
230         "  \"files\": {\n" +
231         "    \"1\": {\n" +
232         "      \"key\": \"" + file1.getKey() + "\",\n" +
233         "      \"name\": \"" + file1.longName() + "\",\n" +
234         "      \"project\": \"" + branch.getKey() + "\",\n" +
235         "      \"projectName\": \"" + branch.longName() + "\",\n" +
236         "      \"branch\": \"" + branchName + "\",\n" +
237         "    },\n" +
238         "    \"2\": {\n" +
239         "      \"key\": \"" + file2.getKey() + "\",\n" +
240         "      \"name\": \"" + file2.longName() + "\",\n" +
241         "      \"project\": \"" + branch.getKey() + "\",\n" +
242         "      \"projectName\": \"" + branch.longName() + "\",\n" +
243         "      \"branch\": \"" + branchName + "\",\n" +
244         "    }\n" +
245         "  }" +
246         "}");
247   }
248
249   @Test
250   public void write_duplications_on_pull_request() {
251     ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
252     String pullRequestKey = randomAlphanumeric(100);
253     ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey));
254     ComponentDto file1 = db.components().insertComponent(newFileDto(pullRequest));
255     ComponentDto file2 = db.components().insertComponent(newFileDto(pullRequest));
256     List<DuplicationsParser.Block> blocks = newArrayList();
257     blocks.add(new DuplicationsParser.Block(newArrayList(
258       Duplication.newComponent(file1, 57, 12),
259       Duplication.newComponent(file2, 73, 12))));
260
261     test(blocks, null, pullRequestKey,
262       "{\n" +
263         "  \"duplications\": [\n" +
264         "    {\n" +
265         "      \"blocks\": [\n" +
266         "        {\n" +
267         "          \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
268         "        },\n" +
269         "        {\n" +
270         "          \"from\": 73, \"size\": 12, \"_ref\": \"2\"\n" +
271         "        }\n" +
272         "      ]\n" +
273         "    }," +
274         "  ],\n" +
275         "  \"files\": {\n" +
276         "    \"1\": {\n" +
277         "      \"key\": \"" + file1.getKey() + "\",\n" +
278         "      \"name\": \"" + file1.longName() + "\",\n" +
279         "      \"project\": \"" + pullRequest.getKey() + "\",\n" +
280         "      \"projectName\": \"" + pullRequest.longName() + "\",\n" +
281         "      \"pullRequest\": \"" + pullRequestKey + "\",\n" +
282         "    },\n" +
283         "    \"2\": {\n" +
284         "      \"key\": \"" + file2.getKey() + "\",\n" +
285         "      \"name\": \"" + file2.longName() + "\",\n" +
286         "      \"project\": \"" + pullRequest.getKey() + "\",\n" +
287         "      \"projectName\": \"" + pullRequest.longName() + "\",\n" +
288         "      \"pullRequest\": \"" + pullRequestKey + "\",\n" +
289         "    }\n" +
290         "  }" +
291         "}");
292   }
293
294   @Test
295   public void write_nothing_when_no_data() {
296     test(Collections.emptyList(), null, null, "{\"duplications\": [], \"files\": {}}");
297   }
298
299   private void test(List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest, String expected) {
300     StringWriter output = new StringWriter();
301     JsonWriter jsonWriter = JsonWriter.of(output);
302     ProtobufJsonFormat.write(underTest.build(db.getSession(), blocks, branch, pullRequest), jsonWriter);
303     JsonAssert.assertJson(output.toString()).isSimilarTo(expected);
304   }
305
306 }