]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3714 add bulk change in ruby service
authorJulien Lancelot <julien.lancelot@gmail.com>
Fri, 21 Jun 2013 14:56:06 +0000 (16:56 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Fri, 21 Jun 2013 14:56:06 +0000 (16:56 +0200)
sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java
sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java
sonar-server/src/test/java/org/sonar/server/issue/InternalRubyIssueServiceTest.java
sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java

index 3da16a249bb48b92d0a16305d4987c9f7c8b955d..d3b9ca5d0cbb173edc371eeb562ef9f717d3638d 100644 (file)
@@ -68,12 +68,13 @@ public class InternalRubyIssueService implements ServerComponent {
   private final ResourceDao resourceDao;
   private final ActionService actionService;
   private final IssueFilterService issueFilterService;
+  private final IssueBulkChangeService issueBulkChangeService;
 
   public InternalRubyIssueService(IssueService issueService,
                                   IssueCommentService commentService,
                                   IssueChangelogService changelogService, ActionPlanService actionPlanService,
                                   IssueStatsFinder issueStatsFinder, ResourceDao resourceDao, ActionService actionService,
-                                  IssueFilterService issueFilterService) {
+                                  IssueFilterService issueFilterService, IssueBulkChangeService issueBulkChangeService) {
     this.issueService = issueService;
     this.commentService = commentService;
     this.changelogService = changelogService;
@@ -82,6 +83,7 @@ public class InternalRubyIssueService implements ServerComponent {
     this.resourceDao = resourceDao;
     this.actionService = actionService;
     this.issueFilterService = issueFilterService;
+    this.issueBulkChangeService = issueBulkChangeService;
   }
 
   public IssueStatsFinder.IssueStatsResult findIssueAssignees(Map<String, Object> params) {
@@ -403,7 +405,7 @@ public class InternalRubyIssueService implements ServerComponent {
     }
   }
 
-  public String serializeFilterQuery(Map<String, Object> filterQuery){
+  public String serializeFilterQuery(Map<String, Object> filterQuery) {
     return issueFilterService.serializeFilterQuery(filterQuery);
   }
 
@@ -426,12 +428,12 @@ public class InternalRubyIssueService implements ServerComponent {
     return issueFilterService.execute(issueQuery);
   }
 
-  private void overrideProps(Map<String, Object> props, Map<String, Object> overrideProps){
+  private void overrideProps(Map<String, Object> props, Map<String, Object> overrideProps) {
     overrideProp(props, overrideProps, "pageSize");
     overrideProp(props, overrideProps, "pageIndex");
   }
 
-  private void overrideProp(Map<String, Object> props, Map<String, Object> overrideProps, String key){
+  private void overrideProp(Map<String, Object> props, Map<String, Object> overrideProps, String key) {
     if (overrideProps.containsKey(key)) {
       props.put(key, overrideProps.get(key));
     }
@@ -583,4 +585,29 @@ public class InternalRubyIssueService implements ServerComponent {
     }
   }
 
+  /**
+   * Execute a bulk change
+   */
+  public Result<List<Issue>> executebulkChange(Map<String, Object> props) {
+    Result<List<Issue>> result = Result.of();
+    try {
+      IssueBulkChangeQuery issueBulkChangeQuery = toIssueBulkChangeQuery(props);
+      result.set(issueBulkChangeService.execute(issueBulkChangeQuery, UserSession.get()));
+    } catch (Exception e) {
+      result.addError(e.getMessage());
+    }
+    return result;
+  }
+
+  private IssueBulkChangeQuery toIssueBulkChangeQuery(Map<String, Object> props) {
+    return IssueBulkChangeQuery.builder()
+      .issueKeys(RubyUtils.toStrings(props.get("issues")))
+      .assignee((String) props.get("assignee"))
+      .plan((String) props.get("plan"))
+      .severity((String) props.get("severity"))
+      .transition((String) props.get("transition"))
+      .comment((String) props.get("comment"))
+      .build();
+  }
+
 }
\ No newline at end of file
index d399748e7b513d6585f01b81b362fcc22bad29be..8480c44eff1c0384e4d5c047de71f0a7f505ab18 100644 (file)
@@ -60,8 +60,7 @@ public class IssueBulkChangeService {
     this.userFinder = userFinder;
   }
 
-  public Result<List<Issue>> execute(IssueBulkChangeQuery issueBulkChangeQuery, UserSession userSession) {
-    Result<List<Issue>> result = Result.of();
+  public List<Issue> execute(IssueBulkChangeQuery issueBulkChangeQuery, UserSession userSession) {
     List<Issue> issues = newArrayList();
     verifyLoggedIn(userSession);
 
@@ -103,11 +102,10 @@ public class IssueBulkChangeService {
         issueNotifications.sendChanges(defaultIssue, context, issueQueryResult);
         issues.add(defaultIssue);
       } catch (Exception e) {
-        result.addError(e.getMessage());
+        // Do nothing, just go to the next issue
       }
     }
-    result.set(issues);
-    return result;
+    return issues;
   }
 
   private void verifyLoggedIn(UserSession userSession) {
index 54d592260e8d51b7428fa6edcf1822675d91d401..bd53b68fce5ae1343fee7c64f70c26cbc77bd2fa 100644 (file)
@@ -54,12 +54,14 @@ public class InternalRubyIssueServiceTest {
   IssueStatsFinder issueStatsFinder = mock(IssueStatsFinder.class);
   ActionService actionService = mock(ActionService.class);
   IssueFilterService issueFilterService = mock(IssueFilterService.class);
+  IssueBulkChangeService issueBulkChangeService = mock(IssueBulkChangeService.class);
 
   @Before
   public void setUp() {
     ResourceDto project = new ResourceDto().setKey("org.sonar.Sample");
     when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(project);
-    service = new InternalRubyIssueService(issueService, commentService, changelogService, actionPlanService, issueStatsFinder, resourceDao, actionService, issueFilterService);
+    service = new InternalRubyIssueService(issueService, commentService, changelogService, actionPlanService, issueStatsFinder, resourceDao, actionService,
+      issueFilterService, issueBulkChangeService);
   }
 
   @Test
@@ -530,6 +532,21 @@ public class InternalRubyIssueServiceTest {
     verify(issueFilterService).verifyCurrentUserCanReadFilter(eq(issueFilter), any(UserSession.class));
   }
 
+  @Test
+  public void should_execute_bulk_change() {
+    service.executebulkChange(Maps.<String, Object>newHashMap());
+    verify(issueBulkChangeService).execute(any(IssueBulkChangeQuery.class), any(UserSession.class));
+  }
+
+  @Test
+  public void should_no_execute_bulk_change_if_unexpected_error() {
+    doThrow(new RuntimeException("Error")).when(issueBulkChangeService).execute(any(IssueBulkChangeQuery.class), any(UserSession.class));
+
+    Result result = service.executebulkChange(Maps.<String, Object>newHashMap());
+    assertThat(result.ok()).isFalse();
+    assertThat(((Result.Message) result.errors().get(0)).text()).contains("Error");
+  }
+
   private String createLongString(int size) {
     String result = "";
     for (int i = 0; i < size; i++) {
index eac959c0105b3f812041e4fab858bda2fc6fbb5f..5237982fcd1ebb11d0b20d67b5095ee99a3c9e9a 100644 (file)
@@ -78,9 +78,8 @@ public class IssueBulkChangeServiceTest {
     when(userFinder.findByLogin(assignee)).thenReturn(new DefaultUser());
 
     IssueBulkChangeQuery issueBulkChangeQuery = IssueBulkChangeQuery.builder().issueKeys(newArrayList(issue.key())).assignee(assignee).build();
-    Result result = service.execute(issueBulkChangeQuery, userSession);
-    assertThat(result.ok()).isTrue();
-    assertThat((List)result.get()).hasSize(1);
+    List<Issue> result = service.execute(issueBulkChangeQuery, userSession);
+    assertThat(result).hasSize(1);
 
     verify(issueUpdater).assign(eq(issue), eq(assignee), any(IssueChangeContext.class));
     verifyNoMoreInteractions(issueUpdater);
@@ -96,9 +95,8 @@ public class IssueBulkChangeServiceTest {
     when(actionPlanService.findByKey(actionPlanKey, userSession)).thenReturn(new DefaultActionPlan());
 
     IssueBulkChangeQuery issueBulkChangeQuery = IssueBulkChangeQuery.builder().issueKeys(newArrayList(issue.key())).plan(actionPlanKey).build();
-    Result result = service.execute(issueBulkChangeQuery, userSession);
-    assertThat(result.ok()).isTrue();
-    assertThat((List)result.get()).hasSize(1);
+    List<Issue> result = service.execute(issueBulkChangeQuery, userSession);
+    assertThat(result).hasSize(1);
 
     verify(issueUpdater).plan(eq(issue), eq(actionPlanKey), any(IssueChangeContext.class));
     verifyNoMoreInteractions(issueUpdater);
@@ -113,9 +111,8 @@ public class IssueBulkChangeServiceTest {
     String severity = "MINOR";
 
     IssueBulkChangeQuery issueBulkChangeQuery = IssueBulkChangeQuery.builder().issueKeys(newArrayList(issue.key())).severity(severity).build();
-    Result result = service.execute(issueBulkChangeQuery, userSession);
-    assertThat(result.ok()).isTrue();
-    assertThat((List)result.get()).hasSize(1);
+    List<Issue> result = service.execute(issueBulkChangeQuery, userSession);
+    assertThat(result).hasSize(1);
 
     verify(issueUpdater).setManualSeverity(eq(issue), eq(severity), any(IssueChangeContext.class));
     verifyNoMoreInteractions(issueUpdater);
@@ -132,9 +129,8 @@ public class IssueBulkChangeServiceTest {
     when(workflow.doTransition(eq(issue), eq(transition), any(IssueChangeContext.class))).thenReturn(true);
 
     IssueBulkChangeQuery issueBulkChangeQuery = IssueBulkChangeQuery.builder().issueKeys(newArrayList(issue.key())).transition(transition).build();
-    Result result = service.execute(issueBulkChangeQuery, userSession);
-    assertThat(result.ok()).isTrue();
-    assertThat((List)result.get()).hasSize(1);
+    List<Issue> result = service.execute(issueBulkChangeQuery, userSession);
+    assertThat(result).hasSize(1);
 
     verify(workflow).doTransition(eq(issue), eq(transition), any(IssueChangeContext.class));
     verifyNoMoreInteractions(issueUpdater);
@@ -149,9 +145,8 @@ public class IssueBulkChangeServiceTest {
     String comment = "Bulk change comment";
 
     IssueBulkChangeQuery issueBulkChangeQuery = IssueBulkChangeQuery.builder().issueKeys(newArrayList(issue.key())).comment(comment).build();
-    Result result = service.execute(issueBulkChangeQuery, userSession);
-    assertThat(result.ok()).isTrue();
-    assertThat((List)result.get()).hasSize(1);
+    List<Issue> result = service.execute(issueBulkChangeQuery, userSession);
+    assertThat(result).hasSize(1);
 
     verify(issueUpdater).addComment(eq(issue), eq(comment), any(IssueChangeContext.class));
     verifyNoMoreInteractions(issueUpdater);
@@ -171,13 +166,9 @@ public class IssueBulkChangeServiceTest {
     // The first call the change severity is ok, the second will fail
     when(issueUpdater.setManualSeverity(any(DefaultIssue.class), eq("MAJOR"),any(IssueChangeContext.class))).thenReturn(true).thenThrow(new RuntimeException("Cant change severity"));
 
-    Result result = service.execute(issueBulkChangeQuery, userSession);
-    assertThat(result.ok()).isFalse();
-    assertThat(((Result.Message) result.errors().get(0)).text()).isEqualTo("Cant change severity");
-
-    List<Issue> issues = (List) result.get();
-    assertThat(issues).hasSize(1);
-    assertThat(issues.get(0).key()).isEqualTo("ABCD");
+    List<Issue> result = service.execute(issueBulkChangeQuery, userSession);
+    assertThat(result).hasSize(1);
+    assertThat(result.get(0).key()).isEqualTo("ABCD");
 
     verify(issueStorage).save(eq(issue));
     verifyNoMoreInteractions(issueStorage);