aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJacek <jacek.poreda@sonarsource.com>2022-02-09 10:11:21 +0100
committersonartech <sonartech@sonarsource.com>2022-02-25 20:02:54 +0000
commit4e70110df370f2454a81cffe706603e98025ac84 (patch)
tree211b12f7ec8437186220b7a5632a54ed841346cb /server
parente30eb01b20e62a3365fb66d39e9b88ceb87bb4af (diff)
downloadsonarqube-4e70110df370f2454a81cffe706603e98025ac84.tar.gz
sonarqube-4e70110df370f2454a81cffe706603e98025ac84.zip
SONAR-16007 Update `api/sources/issue_snippet` WS description
Diffstat (limited to 'server')
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java12
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IssueSnippetsActionTest.java31
2 files changed, 28 insertions, 15 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java
index 51cb9eda7a1..d64836787e1 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java
@@ -19,7 +19,6 @@
*/
package org.sonar.server.source.ws;
-import com.google.common.io.Resources;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@@ -46,6 +45,7 @@ import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.source.SourceService;
import org.sonar.server.user.UserSession;
+import static com.google.common.io.Resources.getResource;
import static java.lang.String.format;
public class IssueSnippetsAction implements SourcesWsAction {
@@ -67,16 +67,16 @@ public class IssueSnippetsAction implements SourcesWsAction {
@Override
public void define(WebService.NewController controller) {
WebService.NewAction action = controller.createAction("issue_snippets")
- .setDescription("Get code snipets involved in an issue. Requires 'See Source Code permission' permission on the project<br/>")
+ .setDescription("Get code snippets involved in an issue or hotspot. Requires 'See Source Code permission' permission on the project<br/>")
.setSince("7.8")
.setInternal(true)
- .setResponseExample(Resources.getResource(getClass(), "example-show.json"))
+ .setResponseExample(getResource(getClass(), "example-show.json"))
.setHandler(this);
action
.createParam("issueKey")
.setRequired(true)
- .setDescription("Issue key")
+ .setDescription("Issue or hotspot key")
.setExampleValue("AU-Tpxb--iU5OvuD2FLy");
}
@@ -116,7 +116,7 @@ public class IssueSnippetsAction implements SourcesWsAction {
private void writeSnippet(DbSession dbSession, JsonWriter writer, ComponentDto fileDto, Set<Integer> lines) {
Optional<Iterable<DbFileSources.Line>> lineSourcesOpt = sourceService.getLines(dbSession, fileDto.uuid(), lines);
- if (!lineSourcesOpt.isPresent()) {
+ if (lineSourcesOpt.isEmpty()) {
return;
}
@@ -165,7 +165,7 @@ public class IssueSnippetsAction implements SourcesWsAction {
TreeSet<Integer> lines = linesPerComponent.computeIfAbsent(componentUuid, c -> new TreeSet<>());
IntStream.rangeClosed(start, end).forEach(lines::add);
- // If two snippets in the same component are 10 lines apart of each other, include those 10 lines.
+ // If two snippets in the same component are 10 lines apart from each other, include those 10 lines.
Integer closestToStart = lines.lower(start);
if (closestToStart != null && closestToStart >= start - 11) {
IntStream.range(closestToStart + 1, start).forEach(lines::add);
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IssueSnippetsActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IssueSnippetsActionTest.java
index 06a94dd5785..bb1133faed1 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IssueSnippetsActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/IssueSnippetsActionTest.java
@@ -25,6 +25,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.stubbing.Answer;
+import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
@@ -47,6 +48,7 @@ import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import org.sonar.test.JsonAssert;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
@@ -62,15 +64,14 @@ import static org.sonar.api.web.UserRole.USER;
import static org.sonar.db.component.ComponentTesting.newFileDto;
public class IssueSnippetsActionTest {
- private static final String SCM_AUTHOR_JSON_FIELD = "scmAuthor";
@Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
+ public final DbTester db = DbTester.create(System2.INSTANCE);
@Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
+ public final UserSessionRule userSession = UserSessionRule.standalone();
- private DbClient dbClient = db.getDbClient();
- private FileSourceTester fileSourceTester = new FileSourceTester(db);
+ private final DbClient dbClient = db.getDbClient();
+ private final FileSourceTester fileSourceTester = new FileSourceTester(db);
private ComponentDto project;
private WsActionTester actionTester;
@@ -88,6 +89,16 @@ public class IssueSnippetsActionTest {
}
@Test
+ public void verify_definition() {
+ var def = actionTester.getDef();
+ assertThat(def.isInternal()).isTrue();
+ assertThat(def.since()).isEqualTo("7.8");
+
+ assertThat(def.param("issueKey")).extracting(Param::isRequired, Param::description)
+ .containsExactly(true, "Issue or hotspot key");
+ }
+
+ @Test
public void should_display_single_location_single_file() {
ComponentDto file = insertFile(project, "file");
DbFileSources.Data fileSources = FileSourceTesting.newFakeData(10).build();
@@ -159,8 +170,8 @@ public class IssueSnippetsActionTest {
userSession.logIn().addProjectPermission(USER, project, file);
String issueKey = insertIssue(file, newLocation(file.uuid(), 5, 5));
-
- assertThatThrownBy(() -> actionTester.newRequest().setParam("issueKey", issueKey).execute())
+ var request = actionTester.newRequest().setParam("issueKey", issueKey);
+ assertThatThrownBy(request::execute)
.isInstanceOf(ForbiddenException.class);
}
@@ -170,7 +181,8 @@ public class IssueSnippetsActionTest {
insertIssue(file, newLocation(file.uuid(), 5, 5));
userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
- assertThatThrownBy(() -> actionTester.newRequest().setParam("issueKey", "invalid").execute())
+ var request = actionTester.newRequest().setParam("issueKey", "invalid");
+ assertThatThrownBy(request::execute)
.isInstanceOf(NotFoundException.class)
.hasMessageContaining("Issue with key 'invalid' does not exist");
}
@@ -180,7 +192,8 @@ public class IssueSnippetsActionTest {
ComponentDto file = insertFile(project, "file");
userSession.logIn().addProjectPermission(CODEVIEWER, project, file);
- assertThatThrownBy(() -> actionTester.newRequest().execute())
+ var request = actionTester.newRequest();
+ assertThatThrownBy(request::execute)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("The 'issueKey' parameter is missing");
}