From 3bca2f3c7e9324a9ba2a9910398c291fc4da298e Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 5 Jun 2017 17:09:30 +0200 Subject: [PATCH] SONAR-8061 speed up add issue comment removing duplicate SQL --- .../server/issue/ws/AddCommentAction.java | 4 ++-- .../server/issue/ws/AddCommentActionTest.java | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AddCommentAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AddCommentAction.java index 61bae87ac54..9eed23186c1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AddCommentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AddCommentAction.java @@ -98,8 +98,8 @@ public class AddCommentAction implements IssuesWsAction { IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getLogin()); DefaultIssue defaultIssue = issueDto.toDefaultIssue(); issueFieldsSetter.addComment(defaultIssue, wsRequest.getText(), context); - issueUpdater.saveIssue(dbSession, defaultIssue, context, wsRequest.getText()); - responseWriter.write(defaultIssue.key(), request, response); + SearchResponseData preloadedSearchResponseData = issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, defaultIssue, context, wsRequest.getText()); + responseWriter.write(defaultIssue.key(), preloadedSearchResponseData, request, response); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java index 8efac57d7be..34d29e77107 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; import org.sonar.api.config.MapSettings; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -31,9 +32,11 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDto; import org.sonar.db.issue.IssueChangeDto; import org.sonar.db.issue.IssueDbTester; import org.sonar.db.issue.IssueDto; +import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -93,6 +96,7 @@ public class AddCommentActionTest { private ServerIssueStorage serverIssueStorage = new ServerIssueStorage(system2, new DefaultRuleFinder(dbClient, defaultOrganizationProvider), dbClient, issueIndexer); private IssueUpdater issueUpdater = new IssueUpdater(dbClient, serverIssueStorage, mock(NotificationManager.class)); private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class); + private ArgumentCaptor preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class); private WsActionTester tester = new WsActionTester( new AddCommentAction(system2, userSession, dbClient, new IssueFinder(dbClient, userSession), issueUpdater, new IssueFieldsSetter(), responseWriter)); @@ -109,7 +113,9 @@ public class AddCommentActionTest { call(issueDto.getKey(), "please fix it"); - verify(responseWriter).write(eq(issueDto.getKey()), any(Request.class), any(Response.class)); + verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); + verifyContentOfPreloadedSearchResponseData(issueDto); + IssueChangeDto issueComment = dbClient.issueChangeDao().selectByTypeAndIssueKeys(dbTester.getSession(), singletonList(issueDto.getKey()), TYPE_COMMENT).get(0); assertThat(issueComment.getKey()).isNotNull(); assertThat(issueComment.getUserLogin()).isEqualTo("john"); @@ -182,6 +188,19 @@ public class AddCommentActionTest { assertThat(action.responseExampleAsString()).isNotEmpty(); } + private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) { + SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue(); + assertThat(preloadedSearchResponseData.getIssues()) + .extracting(IssueDto::getKey) + .containsOnly(issue.getKey()); + assertThat(preloadedSearchResponseData.getRules()) + .extracting(RuleDefinitionDto::getKey) + .containsOnly(issue.getRuleKey()); + assertThat(preloadedSearchResponseData.getComponents()) + .extracting(ComponentDto::uuid) + .containsOnly(issue.getComponentUuid(), issue.getProjectUuid()); + } + private TestResponse call(@Nullable String issueKey, @Nullable String commentText) { TestRequest request = tester.newRequest(); setNullable(issueKey, issue -> request.setParam("issue", issue)); -- 2.39.5