]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12890 add response example to 'api/hotspot/show'
authorJacek <jacek.poreda@sonarsource.com>
Fri, 3 Jan 2020 13:36:31 +0000 (14:36 +0100)
committerSonarTech <sonartech@sonarsource.com>
Mon, 13 Jan 2020 19:46:35 +0000 (20:46 +0100)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java
server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/show-example.json [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java

index 68ca028099e38c01924f8a901d781a5227f017a2..262499adf32a07aefc39103fb320eb820e595a99 100644 (file)
@@ -94,8 +94,8 @@ public class ShowAction implements HotspotsWsAction {
       .setDescription("Key of the Security Hotspot")
       .setExampleValue(Uuids.UUID_EXAMPLE_03)
       .setRequired(true);
-    // FIXME add response example and test it
-    // action.setResponseExample()
+
+    action.setResponseExample(getClass().getResource("show-example.json"));
   }
 
   @Override
diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/show-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/show-example.json
new file mode 100644 (file)
index 0000000..613bae9
--- /dev/null
@@ -0,0 +1,105 @@
+{
+  "key": "AW9mgJw6eFC3pGl94Wrf",
+  "component": {
+    "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"
+  },
+  "project": {
+    "organization": "default-organization",
+    "key": "com.sonarsource:test-project",
+    "qualifier": "TRK",
+    "name": "test-project",
+    "longName": "test-project"
+  },
+  "rule": {
+    "key": "java:S4787",
+    "name": "rule-name",
+    "securityCategory": "others",
+    "vulnerabilityProbability": "LOW"
+  },
+  "status": "TO_REVIEW",
+  "line": 10,
+  "message": "message",
+  "assignee": "joe",
+  "author": "joe",
+  "creationDate": "2020-01-02T15:43:10+0100",
+  "updateDate": "2020-01-02T15:43:10+0100",
+  "changelog": [
+    {
+      "user": "joe",
+      "userName": "Joe",
+      "creationDate": "2020-01-02T14:44:55+0100",
+      "diffs": [
+        {
+          "key": "diff-key-0",
+          "newValue": "new-value-0",
+          "oldValue": "old-value-0"
+        }
+      ],
+      "avatar": "my-avatar",
+      "isUserActive": true
+    },
+    {
+      "user": "joe",
+      "userName": "Joe",
+      "creationDate": "2020-01-02T14:44:55+0100",
+      "diffs": [
+        {
+          "key": "diff-key-1",
+          "newValue": "new-value-1",
+          "oldValue": "old-value-1"
+        }
+      ],
+      "avatar": "my-avatar",
+      "isUserActive": true
+    },
+    {
+      "user": "joe",
+      "userName": "Joe",
+      "creationDate": "2020-01-02T14:44:55+0100",
+      "diffs": [
+        {
+          "key": "diff-key-2",
+          "newValue": "new-value-2",
+          "oldValue": "old-value-2"
+        }
+      ],
+      "avatar": "my-avatar",
+      "isUserActive": true
+    }
+  ],
+  "comment": [
+    {
+      "key": "comment-0",
+      "login": "Joe",
+      "htmlText": "html text 0",
+      "markdown": "markdown 0",
+      "createdAt": "2020-01-02T14:47:47+0100"
+    },
+    {
+      "key": "comment-1",
+      "login": "Joe",
+      "htmlText": "html text 1",
+      "markdown": "markdown 1",
+      "createdAt": "2020-01-02T14:47:47+0100"
+    },
+    {
+      "key": "comment-2",
+      "login": "Joe",
+      "htmlText": "html text 2",
+      "markdown": "markdown 2",
+      "createdAt": "2020-01-02T14:47:47+0100"
+    }
+  ],
+  "users": [
+    {
+      "login": "joe",
+      "name": "Joe",
+      "active": true
+    }
+  ]
+}
index 2fe9922ef8f876cb2b97227447eab08b2894999c..9b0f6beb0d0dcc6616218478d97206f0d7692b2a 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.hotspot.ws;
 
 import com.google.common.collect.ImmutableSet;
+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;
@@ -72,6 +73,7 @@ import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.WsActionTester;
 import org.sonarqube.ws.Common;
+import org.sonarqube.ws.Common.Changelog.Diff;
 import org.sonarqube.ws.Common.User;
 import org.sonarqube.ws.Hotspots;
 
@@ -698,6 +700,69 @@ public class ShowActionTest {
       .containsOnly(tuple(author.getLogin(), author.getName(), author.isActive()));
   }
 
+  @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);
+
+    ComponentDto file = 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"));
+    UserDto author = dbTester.users().insertUser(u -> u.setLogin("joe")
+      .setName("Joe"));
+
+    long time = 1577976190000L;
+    RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT, r -> r.setRuleKey("S4787")
+      .setRepositoryKey("java")
+      .setName("rule-name")
+      .setSecurityStandards(Sets.newHashSet(SQCategory.WEAK_CRYPTOGRAPHY.getKey())));
+    IssueDto hotspot = dbTester.issues().insertHotspot(rule, project, file, h -> h
+      .setAssigneeUuid("assignee-uuid")
+      .setAuthorLogin("joe")
+      .setMessage("message")
+      .setLine(10)
+      .setIssueCreationTime(time)
+      .setIssueUpdateTime(time)
+      .setAuthorLogin(author.getLogin())
+      .setAssigneeUuid(author.getUuid())
+      .setKee("AW9mgJw6eFC3pGl94Wrf"));
+
+    List<Common.Changelog> changelog = IntStream.range(0, 3)
+      .mapToObj(i -> Common.Changelog.newBuilder()
+        .setUser("joe")
+        .setCreationDate("2020-01-02T14:44:55+0100")
+        .addDiffs(Diff.newBuilder().setKey("diff-key-" + i).setNewValue("new-value-" + i).setOldValue("old-value-" + i))
+        .setIsUserActive(true)
+        .setUserName("Joe")
+        .setAvatar("my-avatar")
+        .build())
+      .collect(Collectors.toList());
+    List<Common.Comment> comments = IntStream.range(0, 3)
+      .mapToObj(i -> Common.Comment.newBuilder()
+        .setKey("comment-" + i)
+        .setHtmlText("html text " + i)
+        .setLogin("Joe")
+        .setMarkdown("markdown " + i)
+        .setCreatedAt("2020-01-02T14:47:47+0100")
+        .build())
+      .collect(Collectors.toList());
+
+    mockChangelogAndCommentsFormattingContext();
+    when(issueChangeSupport.formatChangelog(any(), any())).thenReturn(changelog.stream());
+    when(issueChangeSupport.formatComments(any(), any(), any())).thenReturn(comments.stream());
+
+    newRequest(hotspot)
+      .execute()
+      .assertJson(actionTester.getDef().responseExampleAsString()
+        .replaceAll("default-organization", dbTester.getDefaultOrganization().getKey()));
+  }
+
   private FormattingContext mockChangelogAndCommentsFormattingContext() {
     FormattingContext formattingContext = Mockito.mock(FormattingContext.class);
     when(issueChangeSupport.newFormattingContext(any(), any(), any(), anySet(), anySet())).thenReturn(formattingContext);