diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-28 17:18:14 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-28 17:18:24 +0100 |
commit | a743db9c4104a4ab1f56dd061314f4dc96d24e6c (patch) | |
tree | ccbf198c1db6ed2096076d1f840cca9ae3f1b36e | |
parent | ebf80da0c5c70d9ecc26d7d987d67fa529458fb9 (diff) | |
download | sonarqube-a743db9c4104a4ab1f56dd061314f4dc96d24e6c.tar.gz sonarqube-a743db9c4104a4ab1f56dd061314f4dc96d24e6c.zip |
Fix issues when accessing to an issue on removed components / projects
4 files changed, 116 insertions, 23 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java index 9f5c0c6c97d..fa631bc7881 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java @@ -19,6 +19,7 @@ */ package org.sonar.server.issue.ws; +import org.sonar.api.component.Component; import org.sonar.api.i18n.I18n; import org.sonar.api.issue.*; import org.sonar.api.issue.action.Action; @@ -91,6 +92,8 @@ public class IssueShowWsHandler implements RequestHandler { } private void writeIssue(IssueQueryResult result, DefaultIssue issue, JsonWriter json) { + Component component = result.component(issue); + Component project = result.project(issue); String actionPlanKey = issue.actionPlanKey(); WorkDayDuration technicalDebt = issue.technicalDebt(); Date updateDate = issue.updateDate(); @@ -99,10 +102,10 @@ public class IssueShowWsHandler implements RequestHandler { json .prop("key", issue.key()) .prop("component", issue.componentKey()) - .prop("componentLongName", result.component(issue).longName()) - .prop("componentQualifier", result.component(issue).qualifier()) - .prop("project", result.project(issue).key()) - .prop("projectLongName", result.project(issue).longName()) + .prop("componentLongName", component != null ? component.longName() : null) + .prop("componentQualifier", component != null ? component.qualifier() : null) + .prop("project", issue.projectKey()) + .prop("projectLongName", project != null ? project.longName() : null) .prop("rule", issue.ruleKey().toString()) .prop("ruleName", result.rule(issue).getName()) .prop("line", issue.line()) diff --git a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java index 1959e1babf1..d610d85ad04 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java @@ -89,28 +89,24 @@ public class IssueShowWsHandlerTest { List<Issue> issues; DefaultIssueQueryResult result; + private Date issue_creation_date; + WsTester tester; @Before public void setUp() throws Exception { issues = new ArrayList<Issue>(); result = new DefaultIssueQueryResult(issues); - - Component component = mock(Component.class); - when(component.key()).thenReturn("org.sonar.server.issue.IssueClient"); - when(component.longName()).thenReturn("SonarQube :: Issue Client"); - when(component.qualifier()).thenReturn("FIL"); - result.addComponents(newArrayList(component)); - Component project = mock(Component.class); - when(project.key()).thenReturn("org.sonar.Sonar"); - when(project.longName()).thenReturn("SonarQube"); - result.addProjects(newArrayList(project)); - result.addRules(newArrayList(Rule.create("squid", "AvoidCycle").setName("Avoid cycle"))); when(issueFinder.find(any(IssueQuery.class))).thenReturn(result); when(issueChangelogService.changelog(any(Issue.class))).thenReturn(mock(IssueChangelog.class)); + issue_creation_date = DateUtils.parseDateTime("2014-01-22T19:10:03+0100"); + when(i18n.formatDateTime(any(Locale.class), eq(issue_creation_date))).thenReturn("Jan 22, 2014 10:03 AM"); + + when(i18n.message(any(Locale.class), eq("created"), eq((String) null))).thenReturn("Created"); + tester = new WsTester(new IssuesWs(new IssueShowWsHandler(issueFinder, issueService, issueChangelogService, actionService, technicalDebtFormatter, i18n))); } @@ -128,11 +124,19 @@ public class IssueShowWsHandlerTest { .setResolution("FIXED") .setStatus("CLOSED") .setSeverity("MAJOR") - .setCreationDate(DateUtils.parseDateTime("2014-01-22T19:10:03+0100")); + .setCreationDate(issue_creation_date); issues.add(issue); - when(i18n.formatDateTime(any(Locale.class), eq(issue.creationDate()))).thenReturn("Jan 22, 2014 10:03 AM"); - when(i18n.message(any(Locale.class), eq("created"), eq((String) null))).thenReturn("Created"); + Component component = mock(Component.class); + when(component.key()).thenReturn("org.sonar.server.issue.IssueClient"); + when(component.longName()).thenReturn("SonarQube :: Issue Client"); + when(component.qualifier()).thenReturn("FIL"); + result.addComponents(newArrayList(component)); + + Component project = mock(Component.class); + when(project.key()).thenReturn("org.sonar.Sonar"); + when(project.longName()).thenReturn("SonarQube"); + result.addProjects(newArrayList(project)); MockUserSession.set(); WsTester.TestRequest request = tester.newRequest("show").setParam("key", issueKey); @@ -140,6 +144,33 @@ public class IssueShowWsHandlerTest { } @Test + public void show_issue_on_removed_component() throws Exception { + String issueKey = "ABCD"; + Issue issue = createIssue(); + issues.add(issue); + + Component project = mock(Component.class); + when(project.key()).thenReturn("org.sonar.Sonar"); + when(project.longName()).thenReturn("SonarQube"); + result.addProjects(newArrayList(project)); + + MockUserSession.set(); + WsTester.TestRequest request = tester.newRequest("show").setParam("key", issueKey); + request.execute().assertJson(getClass(), "show_issue_on_removed_component.json"); + } + + @Test + public void show_issue_on_removed_project_and_component() throws Exception { + String issueKey = "ABCD"; + Issue issue = createIssue(); + issues.add(issue); + + MockUserSession.set(); + WsTester.TestRequest request = tester.newRequest("show").setParam("key", issueKey); + request.execute().assertJson(getClass(), "show_issue_on_removed_project_and_component.json"); + } + + @Test public void show_issue_with_action_plan() throws Exception { Issue issue = createStandardIssue() .setActionPlanKey("AP-ABCD"); @@ -347,14 +378,30 @@ public class IssueShowWsHandlerTest { } private DefaultIssue createStandardIssue() { - DefaultIssue issue = new DefaultIssue() + DefaultIssue issue = createIssue(); + addComponentAndProject(); + return issue; + } + + private DefaultIssue createIssue() { + return new DefaultIssue() .setKey("ABCD") .setComponentKey("org.sonar.server.issue.IssueClient") .setProjectKey("org.sonar.Sonar") .setRuleKey(RuleKey.of("squid", "AvoidCycle")) - .setCreationDate(DateUtils.parseDateTime("2014-01-22T19:10:03+0100")); - when(i18n.formatDateTime(any(Locale.class), eq(issue.creationDate()))).thenReturn("Jan 22, 2014 10:03 AM"); - when(i18n.message(any(Locale.class), eq("created"), eq((String) null))).thenReturn("Created"); - return issue; + .setCreationDate(issue_creation_date); + } + + private void addComponentAndProject(){ + Component component = mock(Component.class); + when(component.key()).thenReturn("org.sonar.server.issue.IssueClient"); + when(component.longName()).thenReturn("SonarQube :: Issue Client"); + when(component.qualifier()).thenReturn("FIL"); + result.addComponents(newArrayList(component)); + + Component project = mock(Component.class); + when(project.key()).thenReturn("org.sonar.Sonar"); + when(project.longName()).thenReturn("SonarQube"); + result.addProjects(newArrayList(project)); } } diff --git a/sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_component.json b/sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_component.json new file mode 100644 index 00000000000..1f6563e1eac --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_component.json @@ -0,0 +1,22 @@ +{ + "issue": { + "key": "ABCD", + "component": "org.sonar.server.issue.IssueClient", + "project": "org.sonar.Sonar", + "projectLongName": "SonarQube", + "rule": "squid:AvoidCycle", + "ruleName": "Avoid cycle", + "creationDate": "2014-01-22T19:10:03+0100", + "fCreationDate": "Jan 22, 2014 10:03 AM", + "transitions": [], + "actions": [], + "comments": [], + "changelog": [ + { + "creationDate": "2014-01-22T19:10:03+0100", + "fCreationDate": "Jan 22, 2014 10:03 AM", + "diffs": ["Created"] + } + ] + } +} diff --git a/sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_project_and_component.json b/sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_project_and_component.json new file mode 100644 index 00000000000..74542d5ed0e --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_project_and_component.json @@ -0,0 +1,21 @@ +{ + "issue": { + "key": "ABCD", + "component": "org.sonar.server.issue.IssueClient", + "project": "org.sonar.Sonar", + "rule": "squid:AvoidCycle", + "ruleName": "Avoid cycle", + "creationDate": "2014-01-22T19:10:03+0100", + "fCreationDate": "Jan 22, 2014 10:03 AM", + "transitions": [], + "actions": [], + "comments": [], + "changelog": [ + { + "creationDate": "2014-01-22T19:10:03+0100", + "fCreationDate": "Jan 22, 2014 10:03 AM", + "diffs": ["Created"] + } + ] + } +} |