]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12778 add response example to 'api/hotspot/search'
authorJacek <jacek.poreda@sonarsource.com>
Thu, 2 Jan 2020 15:37:33 +0000 (16:37 +0100)
committerSonarTech <sonartech@sonarsource.com>
Mon, 13 Jan 2020 19:46:33 +0000 (20:46 +0100)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java
server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/search-example.json [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java

index 61f1baa53ece3a756c281e91348bb3930e44960d..f4c2793d2287b7642baa4ffcad09c584cb4c7cdd 100644 (file)
@@ -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 (file)
index 0000000..c20a4e5
--- /dev/null
@@ -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"
+    }
+  ]
+}
index d591ea0bf039a0efbe5cc87e89d8913bdcf61ef7..d7d8852fb72eae3d66d91200656a7ce6dff3388b 100644 (file)
@@ -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 -> {
     });