From 995d27cb6feb9f44a0de8ced30241d3f107e31da Mon Sep 17 00:00:00 2001 From: Jacek Date: Fri, 3 Jan 2020 14:36:31 +0100 Subject: [PATCH] SONAR-12890 add response example to 'api/hotspot/show' --- .../sonar/server/hotspot/ws/ShowAction.java | 4 +- .../sonar/server/hotspot/ws/show-example.json | 105 ++++++++++++++++++ .../server/hotspot/ws/ShowActionTest.java | 65 +++++++++++ 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/show-example.json diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java index 68ca028099e..262499adf32 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java @@ -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 index 00000000000..613bae9642f --- /dev/null +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/show-example.json @@ -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 + } + ] +} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java index 2fe9922ef8f..9b0f6beb0d0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java @@ -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 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 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); -- 2.39.5