From adc4fa5bd0c179b6a16823987538fbdc0a0b47ce Mon Sep 17 00:00:00 2001 From: Jacek Date: Thu, 2 Jan 2020 16:37:33 +0100 Subject: [PATCH] SONAR-12778 add response example to 'api/hotspot/search' --- .../sonar/server/hotspot/ws/SearchAction.java | 5 +- .../server/hotspot/ws/search-example.json | 68 +++++++++++++++++++ .../server/hotspot/ws/SearchActionTest.java | 42 ++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/search-example.json diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java index 61f1baa53ec..f4c2793d228 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java @@ -157,8 +157,7 @@ public class SearchAction implements HotspotsWsAction { .setBooleanPossibleValues() .setRequired(false); - // FIXME add response example and test it - // action.setResponseExample() + action.setResponseExample(getClass().getResource("search-example.json")); } @Override @@ -275,7 +274,7 @@ public class SearchAction implements HotspotsWsAction { .types(singleton(RuleType.SECURITY_HOTSPOT.name())) .sort(IssueQuery.SORT_HOTSPOTS) .asc(true) - .statuses(wsRequest.getStatus().map(Collections::singletonList).orElse(STATUSES));; + .statuses(wsRequest.getStatus().map(Collections::singletonList).orElse(STATUSES)); project.ifPresent(p -> { builder.organizationUuid(p.getOrganizationUuid()); diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/search-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/search-example.json new file mode 100644 index 00000000000..c20a4e5c50e --- /dev/null +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/search-example.json @@ -0,0 +1,68 @@ +{ + "paging": { + "pageIndex": 1, + "pageSize": 100, + "total": 3 + }, + "hotspots": [ + { + "key": "hotspot-0", + "component": "com.sonarsource:test-project:src/main/java/com/sonarsource/FourthClass.java", + "project": "com.sonarsource:test-project", + "securityCategory": "others", + "vulnerabilityProbability": "LOW", + "status": "TO_REVIEW", + "line": 10, + "message": "message-0", + "assignee": "assignee-uuid", + "author": "joe", + "creationDate": "2020-01-02T15:43:10+0100", + "updateDate": "2020-01-02T15:43:10+0100" + }, + { + "key": "hotspot-1", + "component": "com.sonarsource:test-project:src/main/java/com/sonarsource/FourthClass.java", + "project": "com.sonarsource:test-project", + "securityCategory": "others", + "vulnerabilityProbability": "LOW", + "status": "TO_REVIEW", + "line": 11, + "message": "message-1", + "assignee": "assignee-uuid", + "author": "joe", + "creationDate": "2020-01-02T15:43:10+0100", + "updateDate": "2020-01-02T15:43:10+0100" + }, + { + "key": "hotspot-2", + "component": "com.sonarsource:test-project:src/main/java/com/sonarsource/FourthClass.java", + "project": "com.sonarsource:test-project", + "securityCategory": "others", + "vulnerabilityProbability": "LOW", + "status": "TO_REVIEW", + "line": 12, + "message": "message-2", + "assignee": "assignee-uuid", + "author": "joe", + "creationDate": "2020-01-02T15:43:10+0100", + "updateDate": "2020-01-02T15:43:10+0100" + } + ], + "components": [ + { + "organization": "default-organization", + "key": "com.sonarsource:test-project:src/main/java/com/sonarsource/FourthClass.java", + "qualifier": "FIL", + "name": "FourthClass.java", + "longName": "src/main/java/com/sonarsource/FourthClass.java", + "path": "src/main/java/com/sonarsource/FourthClass.java" + }, + { + "organization": "default-organization", + "key": "com.sonarsource:test-project", + "qualifier": "TRK", + "name": "test-project", + "longName": "test-project" + } + ] +} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java index d591ea0bf03..d7d8852fb72 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java @@ -21,6 +21,7 @@ package org.sonar.server.hotspot.ws; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Ordering; +import com.google.common.collect.Sets; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; @@ -49,6 +50,7 @@ import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; import org.sonar.db.issue.IssueDto; +import org.sonar.db.organization.OrganizationDto; import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.db.rule.RuleTesting; import org.sonar.server.es.EsTester; @@ -1352,6 +1354,46 @@ public class SearchActionTest { .toArray(String[]::new)); } + @Test + public void verify_response_example() { + ComponentDto project = dbTester.components().insertPublicProject(componentDto -> componentDto + .setName("test-project") + .setLongName("test-project") + .setDbKey("com.sonarsource:test-project") + ); + userSessionRule.registerComponents(project); + indexPermissions(); + ComponentDto fileWithHotspot = dbTester.components().insertComponent(newFileDto(project) + .setDbKey("com.sonarsource:test-project:src/main/java/com/sonarsource/FourthClass.java") + .setName("FourthClass.java") + .setLongName("src/main/java/com/sonarsource/FourthClass.java") + .setPath("src/main/java/com/sonarsource/FourthClass.java") + ); + + long time = 1577976190000L; + + IssueDto[] hotspots = IntStream.range(0, 3) + .mapToObj(i -> { + RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT) + .setSecurityStandards(Sets.newHashSet(SQCategory.WEAK_CRYPTOGRAPHY.getKey())); + return insertHotspot(rule, project, fileWithHotspot, issueDto -> issueDto.setKee("hotspot-" + i) + .setAssigneeUuid("assignee-uuid") + .setAuthorLogin("joe") + .setMessage("message-" +i) + .setLine(10 + i) + .setIssueCreationTime(time) + .setIssueUpdateTime(time) + ); + }) + .toArray(IssueDto[]::new); + indexIssues(); + + newRequest(project) + .execute() + .assertJson(actionTester.getDef().responseExampleAsString() + .replaceAll("default-organization", dbTester.getDefaultOrganization().getKey())); + } + private IssueDto insertHotspot(ComponentDto project, ComponentDto file, RuleDefinitionDto rule) { return insertHotspot(rule, project, file, t -> { }); -- 2.39.5