From 172403a9eadc6bd4e34fbb7e3e5108db26a984b5 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 22 Aug 2013 09:57:54 +0200 Subject: [PATCH] Add some unit tests --- .../issue/IssueTrackingDecoratorTest.java | 29 ++++++++++++++ .../batch/components/PastSnapshotTest.java | 4 +- .../core/issue/IssueNotificationsTest.java | 33 ++++++++++++++++ .../issue/IssueBulkChangeServiceTest.java | 17 ++++++++ .../sonar/server/issue/IssueServiceTest.java | 39 +++++++++++++++++++ 5 files changed, 120 insertions(+), 2 deletions(-) diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java index 54d54bc6b65..d6940cbb483 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/IssueTrackingDecoratorTest.java @@ -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 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"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java index 2440064362c..8591a47ab8d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java @@ -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(); diff --git a/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java b/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java index 825890ac7d2..9528e691d28 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java @@ -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.asList(issue)); + queryResult.addProjects(Arrays.asList(new Project("struts"))); + queryResult.addComponents(Arrays.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()); diff --git a/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java b/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java index 70281632224..7bd22ea1711 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java @@ -207,6 +207,23 @@ public class IssueBulkChangeServiceTest { verifyZeroInteractions(issueNotifications); } + @Test + public void should_not_execute_bulk_if_action_is_not_verified() { + Map 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 properties = newHashMap(); diff --git a/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java b/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java index dce2dd5eeac..4132c7a63ce 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java @@ -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 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 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"; -- 2.39.5