]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8061 speed up set issue type removing duplicate SQ
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 5 Jun 2017 15:22:37 +0000 (17:22 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 9 Jun 2017 08:09:44 +0000 (10:09 +0200)
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SetTypeAction.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SetTypeActionTest.java

index 4fde4097328c85cd2bf0ce2a0ac6995411fa1d60..0c68784f0348c57a96258bb403069a2874e15328 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.core.issue.IssueChangeContext;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.issue.IssueDto;
 import org.sonar.server.issue.IssueFieldsSetter;
 import org.sonar.server.issue.IssueFinder;
 import org.sonar.server.issue.IssueUpdater;
@@ -94,19 +95,21 @@ public class SetTypeAction implements IssuesWsAction {
     String issueKey = request.mandatoryParam(PARAM_ISSUE);
     RuleType ruleType = RuleType.valueOf(request.mandatoryParam(PARAM_TYPE));
     try (DbSession session = dbClient.openSession(false)) {
-      setType(session, issueKey, ruleType);
+      SearchResponseData preloadedSearchResponseData = setType(session, issueKey, ruleType);
+      responseWriter.write(issueKey, preloadedSearchResponseData, request, response);
     }
-    responseWriter.write(issueKey, request, response);
   }
 
-  private void setType(DbSession session, String issueKey, RuleType ruleType) {
-    DefaultIssue issue = issueFinder.getByKey(session, issueKey).toDefaultIssue();
+  private SearchResponseData setType(DbSession session, String issueKey, RuleType ruleType) {
+    IssueDto issueDto = issueFinder.getByKey(session, issueKey);
+    DefaultIssue issue = issueDto.toDefaultIssue();
     userSession.checkComponentUuidPermission(ISSUE_ADMIN, issue.projectUuid());
 
     IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getLogin());
     if (issueFieldsSetter.setType(issue, ruleType, context)) {
-      issueUpdater.saveIssue(session, issue, context, null);
+      return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, null);
     }
+    return new SearchResponseData(issueDto);
   }
 
 }
index ea5b11160a9211303af58df51e08e89928241cbf..bc492ea7be52c952186c5b60694d90a605c2b53c 100644 (file)
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
 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;
@@ -35,6 +36,7 @@ import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.issue.IssueDbTester;
 import org.sonar.db.issue.IssueDto;
+import org.sonar.db.rule.RuleDefinitionDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.ForbiddenException;
@@ -87,6 +89,7 @@ public class SetTypeActionTest {
   private IssueDbTester issueDbTester = new IssueDbTester(dbTester);
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
   private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class);
+  private ArgumentCaptor<SearchResponseData> preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class);
 
   private IssueIndexer issueIndexer = new IssueIndexer(esTester.client(), new IssueIteratorFactory(dbClient));
   private WsActionTester tester = new WsActionTester(new SetTypeAction(userSession, dbClient, new IssueFinder(dbClient, userSession), new IssueFieldsSetter(),
@@ -101,7 +104,8 @@ public class SetTypeActionTest {
 
     call(issueDto.getKey(), BUG.name());
 
-    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);
     IssueDto issueReloaded = dbClient.issueDao().selectByKey(dbTester.getSession(), issueDto.getKey()).get();
     assertThat(issueReloaded.getType()).isEqualTo(BUG.getDbConstant());
   }
@@ -190,4 +194,17 @@ public class SetTypeActionTest {
   private void logInAndAddProjectPermission(String login, IssueDto issueDto, String permission) {
     userSession.logIn(login).addProjectPermission(permission, dbClient.componentDao().selectByUuid(dbTester.getSession(), issueDto.getProjectUuid()).get());
   }
+
+  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());
+  }
 }