From a743db9c4104a4ab1f56dd061314f4dc96d24e6c Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 28 Jan 2014 17:18:14 +0100 Subject: [PATCH] Fix issues when accessing to an issue on removed components / projects --- .../server/issue/ws/IssueShowWsHandler.java | 11 ++- .../issue/ws/IssueShowWsHandlerTest.java | 85 ++++++++++++++----- .../show_issue_on_removed_component.json | 22 +++++ ...ssue_on_removed_project_and_component.json | 21 +++++ 4 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_component.json create mode 100644 sonar-server/src/test/resources/org/sonar/server/issue/ws/IssueShowWsHandlerTest/show_issue_on_removed_project_and_component.json 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 issues; DefaultIssueQueryResult result; + private Date issue_creation_date; + WsTester tester; @Before public void setUp() throws Exception { issues = new ArrayList(); 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,17 +124,52 @@ 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); request.execute().assertJson(getClass(), "show_issue.json"); } + @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() @@ -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"] + } + ] + } +} -- 2.39.5