]> source.dussan.org Git - sonarqube.git/commitdiff
Add some unit tests
authorJulien Lancelot <julien.lancelot@gmail.com>
Thu, 22 Aug 2013 07:57:54 +0000 (09:57 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Thu, 22 Aug 2013 07:57:54 +0000 (09:57 +0200)
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java
sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java
sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java
sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java
sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java

index 54d54bc6b658e733a13f5b747e201deee26fe266..d6940cbb4833f4efc558a2f0a92cb8c7a268c304 100644 (file)
@@ -200,6 +200,35 @@ public class IssueTrackingDecoratorTest extends AbstractDaoTestCase {
     assertThat(issue.isOnDisabledRule()).isTrue();
   }
 
+  @Test
+  public void manual_issues_should_be_closed_if_manual_rule_is_not_found() throws Exception {
+    // "Unmatched" issues existed in previous scan but not in current one -> they have to be closed
+    Resource file = new File("Action.java").setEffectiveKey("struts:Action.java").setId(123);
+
+    // INPUT : one issue existing during previous scan
+    IssueDto unmatchedIssue = new IssueDto().setKee("ABCDE").setReporter("freddy").setStatus("OPEN").setRuleKey_unit_test_only("manual", "Performance");
+    when(ruleFinder.findByKey(RuleKey.of("manual", "Performance"))).thenReturn(null);
+
+    IssueTrackingResult trackingResult = new IssueTrackingResult();
+    trackingResult.addUnmatched(unmatchedIssue);
+
+    when(tracking.track(eq(file), anyCollection(), anyCollection())).thenReturn(trackingResult);
+
+    decorator.doDecorate(file);
+
+    verify(workflow, times(1)).doAutomaticTransition(any(DefaultIssue.class), any(IssueChangeContext.class));
+    verify(handlers, times(1)).execute(any(DefaultIssue.class), any(IssueChangeContext.class));
+
+    ArgumentCaptor<DefaultIssue> argument = ArgumentCaptor.forClass(DefaultIssue.class);
+    verify(issueCache).put(argument.capture());
+
+    DefaultIssue issue = argument.getValue();
+    assertThat(issue.key()).isEqualTo("ABCDE");
+    assertThat(issue.isNew()).isFalse();
+    assertThat(issue.isEndOfLife()).isTrue();
+    assertThat(issue.isOnDisabledRule()).isTrue();
+  }
+
   @Test
   public void should_register_issues_on_deleted_components() throws Exception {
     Project project = new Project("struts");
index 2440064362c636b193d54ce8d6807efca2ced364..8591a47ab8de2ce13a5dccac5ef027ab2fafd5f4 100644 (file)
@@ -47,8 +47,8 @@ public class PastSnapshotTest {
   }
 
   @Test
-  public void test_some_setters_and_getters2() {
-    PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION, new Date(), new Snapshot());
+  public void test_some_setters_and_getters_with_empty_snapshot() {
+    PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION);
 
     assertThat(pastSnapshot.getQualifier()).isNull();
     assertThat(pastSnapshot.getDate()).isNull();
index 825890ac7d2a8dc922f0bf69b795a09d16d0ddc7..9528e691d289e189a880cad3ac7be84662ec3bac 100644 (file)
@@ -31,8 +31,10 @@ import org.sonar.api.issue.internal.DefaultIssue;
 import org.sonar.api.issue.internal.IssueChangeContext;
 import org.sonar.api.notifications.Notification;
 import org.sonar.api.notifications.NotificationManager;
+import org.sonar.api.resources.File;
 import org.sonar.api.resources.Project;
 import org.sonar.api.utils.DateUtils;
+import org.sonar.core.component.ResourceComponent;
 import org.sonar.core.i18n.RuleI18nManager;
 
 import java.util.Arrays;
@@ -76,6 +78,7 @@ public class IssueNotificationsTest {
       .setAssignee("freddy")
       .setFieldChange(context, "resolution", null, "FIXED")
       .setFieldChange(context, "status", "OPEN", "RESOLVED")
+      .setFieldChange(context, "assignee", "simon", null)
       .setSendNotifications(true)
       .setComponentKey("struts:Action")
       .setProjectKey("struts");
@@ -86,10 +89,14 @@ public class IssueNotificationsTest {
 
     assertThat(notification.getFieldValue("message")).isEqualTo("the message");
     assertThat(notification.getFieldValue("key")).isEqualTo("ABCDE");
+    assertThat(notification.getFieldValue("componentKey")).isEqualTo("struts:Action");
+    assertThat(notification.getFieldValue("componentName")).isNull();
     assertThat(notification.getFieldValue("old.resolution")).isNull();
     assertThat(notification.getFieldValue("new.resolution")).isEqualTo("FIXED");
     assertThat(notification.getFieldValue("old.status")).isEqualTo("OPEN");
     assertThat(notification.getFieldValue("new.status")).isEqualTo("RESOLVED");
+    assertThat(notification.getFieldValue("old.assignee")).isEqualTo("simon");
+    assertThat(notification.getFieldValue("new.assignee")).isNull();
     Mockito.verify(manager).scheduleForSending(notification);
   }
 
@@ -113,6 +120,32 @@ public class IssueNotificationsTest {
     Mockito.verify(manager).scheduleForSending(notification);
   }
 
+  @Test
+  public void should_send_changes_with_component_name() throws Exception {
+    IssueChangeContext context = IssueChangeContext.createScan(new Date());
+    DefaultIssue issue = new DefaultIssue()
+      .setMessage("the message")
+      .setKey("ABCDE")
+      .setAssignee("freddy")
+      .setFieldChange(context, "resolution", null, "FIXED")
+      .setSendNotifications(true)
+      .setComponentKey("struts:Action")
+      .setProjectKey("struts");
+    DefaultIssueQueryResult queryResult = new DefaultIssueQueryResult(Arrays.<Issue>asList(issue));
+    queryResult.addProjects(Arrays.<Component>asList(new Project("struts")));
+    queryResult.addComponents(Arrays.<Component>asList(new ResourceComponent(new File("struts:Action").setEffectiveKey("struts:Action"))));
+
+    Notification notification = issueNotifications.sendChanges(issue, context, queryResult);
+
+    assertThat(notification.getFieldValue("message")).isEqualTo("the message");
+    assertThat(notification.getFieldValue("key")).isEqualTo("ABCDE");
+    assertThat(notification.getFieldValue("componentKey")).isEqualTo("struts:Action");
+    assertThat(notification.getFieldValue("componentName")).isEqualTo("struts:Action");
+    assertThat(notification.getFieldValue("old.resolution")).isNull();
+    assertThat(notification.getFieldValue("new.resolution")).isEqualTo("FIXED");
+    Mockito.verify(manager).scheduleForSending(notification);
+  }
+
   @Test
   public void should_not_send_changes_if_no_diffs() throws Exception {
     IssueChangeContext context = IssueChangeContext.createScan(new Date());
index 702816322240f7174dc2577213c369fa8cc41ecd..7bd22ea1711863b38f885b9cbf727cdc18a411d2 100644 (file)
@@ -207,6 +207,23 @@ public class IssueBulkChangeServiceTest {
     verifyZeroInteractions(issueNotifications);
   }
 
+  @Test
+  public void should_not_execute_bulk_if_action_is_not_verified() {
+    Map<String, Object> properties = newHashMap();
+    properties.put("issues", "ABCD");
+    properties.put("actions", "assign");
+    properties.put("assign.assignee", "fred");
+    actions.add(new MockAction("assign", false, true, true));
+
+    IssueBulkChangeQuery issueBulkChangeQuery = new IssueBulkChangeQuery(properties);
+    IssueBulkChangeResult result = service.execute(issueBulkChangeQuery, userSession);
+    assertThat(result.issuesChanged()).isEmpty();
+    assertThat(result.issuesNotChanged()).isEmpty();
+
+    verifyZeroInteractions(issueStorage);
+    verifyZeroInteractions(issueNotifications);
+  }
+
   @Test
   public void should_not_execute_bulk_if_action_could_not_be_executed_on_issue() {
     Map<String, Object> properties = newHashMap();
index dce2dd5eeac617e34a947a5f52ea25e98aca8801..4132c7a63ce4bcaccb0880074affa70e2e81c7a8 100644 (file)
@@ -201,6 +201,26 @@ public class IssueServiceTest {
     verify(issueNotifications).sendChanges(eq(issue), eq(issueChangeContext), eq(issueQueryResult));
   }
 
+
+  @Test
+  public void should_unassign() {
+    when(issueUpdater.assign(eq(issue), eq((User) null), any(IssueChangeContext.class))).thenReturn(true);
+
+    Issue result = issueService.assign("ABCD", null, userSession);
+    assertThat(result).isNotNull();
+
+    ArgumentCaptor<IssueChangeContext> measureCaptor = ArgumentCaptor.forClass(IssueChangeContext.class);
+    verify(issueUpdater).assign(eq(issue), eq((User) null), measureCaptor.capture());
+    verify(issueStorage).save(issue);
+
+    IssueChangeContext issueChangeContext = measureCaptor.getValue();
+    assertThat(issueChangeContext.login()).isEqualTo("arthur");
+    assertThat(issueChangeContext.date()).isNotNull();
+
+    verify(issueNotifications).sendChanges(eq(issue), eq(issueChangeContext), eq(issueQueryResult));
+    verify(userFinder, never()).findByLogin(anyString());
+  }
+
   @Test
   public void should_not_assign() {
     String assignee = "perceval";
@@ -258,6 +278,25 @@ public class IssueServiceTest {
     verify(issueNotifications).sendChanges(eq(issue), eq(issueChangeContext), eq(issueQueryResult));
   }
 
+  @Test
+  public void should_unplan() {
+    when(issueUpdater.plan(eq(issue), eq((ActionPlan) null), any(IssueChangeContext.class))).thenReturn(true);
+
+    Issue result = issueService.plan("ABCD", null, userSession);
+    assertThat(result).isNotNull();
+
+    ArgumentCaptor<IssueChangeContext> measureCaptor = ArgumentCaptor.forClass(IssueChangeContext.class);
+    verify(issueUpdater).plan(eq(issue), eq((ActionPlan) null), measureCaptor.capture());
+    verify(issueStorage).save(issue);
+
+    IssueChangeContext issueChangeContext = measureCaptor.getValue();
+    assertThat(issueChangeContext.login()).isEqualTo("arthur");
+    assertThat(issueChangeContext.date()).isNotNull();
+
+    verify(issueNotifications).sendChanges(eq(issue), eq(issueChangeContext), eq(issueQueryResult));
+    verify(actionPlanService, never()).findByKey(anyString(), any(UserSession.class));
+  }
+
   @Test
   public void should_not_plan() {
     String actionPlanKey = "EFGH";