Browse Source

SONAR-12890 add response example to 'api/hotspot/show'

tags/8.2.0.32929
Jacek 4 years ago
parent
commit
995d27cb6f

+ 2
- 2
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java View 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

+ 105
- 0
server/sonar-webserver-webapi/src/main/resources/org/sonar/server/hotspot/ws/show-example.json View File

@@ -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
}
]
}

+ 65
- 0
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java View 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);

Loading…
Cancel
Save