+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.issue.ws;
-
-import org.sonar.api.issue.Issue;
-import org.sonar.api.server.ServerSide;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.server.issue.workflow.Transition;
-import org.sonar.server.issue.ActionService;
-import org.sonar.server.issue.IssueService;
-import org.sonar.server.user.UserSession;
-
-@ServerSide
-public class IssueActionsWriter {
-
- private final IssueService issueService;
- private final ActionService actionService;
- private final UserSession userSession;
-
- public IssueActionsWriter(IssueService issueService, ActionService actionService, UserSession userSession) {
- this.issueService = issueService;
- this.actionService = actionService;
- this.userSession = userSession;
- }
-
- public void writeTransitions(Issue issue, JsonWriter json) {
- json.name("transitions").beginArray();
- if (userSession.isLoggedIn()) {
- for (Transition transition : issueService.listTransitions(issue)) {
- json.value(transition.key());
- }
- }
- json.endArray();
- }
-
- public void writeActions(Issue issue, JsonWriter json) {
- json.name("actions").beginArray();
- for (String action : actionService.listAvailableActions(issue)) {
- json.value(action);
- }
- json.endArray();
- }
-
-}
CreateAction.class,
DoTransitionAction.class,
PlanAction.class,
- ShowAction.class,
SearchAction.class,
SetSeverityAction.class,
TagsAction.class,
SetTagsAction.class,
ComponentTagsAction.class,
- IssueActionsWriter.class,
AuthorsAction.class);
}
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.issue.ws;
-
-import com.google.common.base.Optional;
-import com.google.common.io.Resources;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.sonar.api.i18n.I18n;
-import org.sonar.api.issue.ActionPlan;
-import org.sonar.api.issue.Issue;
-import org.sonar.api.issue.IssueComment;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.user.User;
-import org.sonar.api.user.UserFinder;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.api.utils.Duration;
-import org.sonar.api.utils.Durations;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.issue.DefaultIssueComment;
-import org.sonar.core.issue.FieldDiffs;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.markdown.Markdown;
-import org.sonar.server.issue.IssueChangelog;
-import org.sonar.server.issue.IssueChangelogService;
-import org.sonar.server.issue.IssueCommentService;
-import org.sonar.server.issue.IssueService;
-import org.sonar.server.issue.actionplan.ActionPlanService;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.RuleService;
-import org.sonar.server.user.UserSession;
-
-import static com.google.common.collect.Maps.newHashMap;
-
-public class ShowAction implements IssuesWsAction {
-
- public static final String SHOW_ACTION = "show";
-
- private final DbClient dbClient;
-
- private final IssueService issueService;
- private final IssueChangelogService issueChangelogService;
- private final IssueCommentService commentService;
- private final IssueActionsWriter actionsWriter;
- private final ActionPlanService actionPlanService;
- private final UserFinder userFinder;
- private final RuleService ruleService;
- private final I18n i18n;
- private final Durations durations;
- private final UserSession userSession;
-
- public ShowAction(DbClient dbClient, IssueService issueService, IssueChangelogService issueChangelogService, IssueCommentService commentService,
- IssueActionsWriter actionsWriter, ActionPlanService actionPlanService, UserFinder userFinder, RuleService ruleService,
- I18n i18n, Durations durations, UserSession userSession) {
- this.dbClient = dbClient;
- this.issueService = issueService;
- this.issueChangelogService = issueChangelogService;
- this.commentService = commentService;
- this.actionsWriter = actionsWriter;
- this.actionPlanService = actionPlanService;
- this.userFinder = userFinder;
- this.ruleService = ruleService;
- this.i18n = i18n;
- this.durations = durations;
- this.userSession = userSession;
- }
-
- @Override
- public void define(WebService.NewController controller) {
- WebService.NewAction action = controller.createAction(SHOW_ACTION)
- .setDescription("Detail of issue")
- .setSince("4.2")
- .setInternal(true)
- .setHandler(this)
- .setResponseExample(Resources.getResource(this.getClass(), "example-show.json"));
- action.createParam("key")
- .setDescription("Issue key")
- .setExampleValue("5bccd6e8-f525-43a2-8d76-fcb13dde79ef");
- }
-
- @Override
- public void handle(Request request, Response response) {
- String issueKey = request.mandatoryParam("key");
-
- JsonWriter json = response.newJsonWriter();
- json.beginObject().name("issue").beginObject();
-
- DbSession session = dbClient.openSession(false);
- try {
- Issue issue = issueService.getByKey(issueKey);
-
- writeIssue(session, issue, json);
- actionsWriter.writeActions(issue, json);
- actionsWriter.writeTransitions(issue, json);
- writeComments(issue, json);
- writeChangelog(issue, json);
-
- } finally {
- session.close();
- }
-
- json.endObject().endObject().close();
- }
-
- private void writeIssue(DbSession session, Issue issue, JsonWriter json) {
- String actionPlanKey = issue.actionPlanKey();
- ActionPlan actionPlan = actionPlanKey == null ? null : actionPlanService.findByKey(actionPlanKey, userSession);
- Duration debt = issue.debt();
- Rule rule = ruleService.getNonNullByKey(issue.ruleKey());
- String actionPlanName = actionPlan != null ? actionPlan.name() : null;
- String debtValue = debt != null ? durations.encode(debt) : null;
- Date updateDate = issue.updateDate();
- Date closeDate = issue.closeDate();
-
- json
- .prop("key", issue.key())
- .prop("rule", issue.ruleKey().toString())
- .prop("ruleName", rule.name())
- .prop("line", issue.line())
- .prop("message", issue.message())
- .prop("resolution", issue.resolution())
- .prop("status", issue.status())
- .prop("severity", issue.severity())
- .prop("author", issue.authorLogin())
- .prop("actionPlan", actionPlanKey)
- .prop("actionPlanName", actionPlanName)
- .prop("debt", debtValue)
- .prop("creationDate", DateUtils.formatDateTime(issue.creationDate()))
- .prop("fCreationDate", formatDate(issue.creationDate()))
- .prop("updateDate", DateUtils.formatDateTimeNullSafe(updateDate))
- .prop("fUpdateDate", formatDate(updateDate))
- .prop("fUpdateAge", formatAgeDate(updateDate))
- .prop("closeDate", DateUtils.formatDateTimeNullSafe(closeDate))
- .prop("fCloseDate", formatDate(issue.closeDate()));
-
- addComponents(session, issue, json);
- addUserWithLabel(issue.assignee(), "assignee", json);
- addUserWithLabel(issue.reporter(), "reporter", json);
- }
-
- private void addComponents(DbSession session, Issue issue, JsonWriter json) {
- ComponentDto component = dbClient.componentDao().selectOrFailByUuid(session, issue.componentUuid());
- Long parentProjectId = component.parentProjectId();
- Optional<ComponentDto> parentProject = parentProjectId != null ? dbClient.componentDao().selectById(session, parentProjectId) : Optional.<ComponentDto>absent();
- ComponentDto project = dbClient.componentDao().selectOrFailByUuid(session, component.projectUuid());
-
- String projectName = project.longName() != null ? project.longName() : project.name();
- // Do not display sub project long name if sub project and project are the same
- boolean displayParentProjectName = parentProject.isPresent() && !parentProject.get().getId().equals(project.getId());
- String parentProjectKey = displayParentProjectName ? parentProject.get().key() : null;
- String parentProjectName = displayParentProjectName ? projectName(parentProject.get()) : null;
-
- json
- .prop("component", component.key())
- .prop("componentLongName", component.longName())
- .prop("componentQualifier", component.qualifier())
- .prop("componentEnabled", component.isEnabled())
- .prop("project", project.key())
- .prop("projectName", projectName)
- // TODO replace subProject names by parentProject
- .prop("subProject", parentProjectKey)
- .prop("subProjectName", parentProjectName);
- }
-
- private static String projectName(ComponentDto project) {
- return project.longName() != null ? project.longName() : project.name();
- }
-
- private void writeComments(Issue issue, JsonWriter json) {
- json.name("comments").beginArray();
- String login = userSession.getLogin();
-
- Map<String, User> usersByLogin = newHashMap();
- List<DefaultIssueComment> comments = commentService.findComments(issue.key());
- for (IssueComment comment : comments) {
- String userLogin = comment.userLogin();
- User user = usersByLogin.get(userLogin);
- if (user == null) {
- user = userFinder.findByLogin(userLogin);
- if (user != null) {
- usersByLogin.put(userLogin, user);
- }
- }
- json
- .beginObject()
- .prop("key", comment.key())
- .prop("userName", user != null ? user.name() : null)
- .prop("raw", comment.markdownText())
- .prop("html", Markdown.convertToHtml(comment.markdownText()))
- .prop("createdAt", DateUtils.formatDateTime(comment.createdAt()))
- .prop("fCreatedAge", formatAgeDate(comment.createdAt()))
- .prop("updatable", login != null && login.equals(userLogin))
- .endObject();
- }
- json.endArray();
- }
-
- private void writeChangelog(Issue issue, JsonWriter json) {
- json.name("changelog").beginArray()
- .beginObject()
- .prop("creationDate", DateUtils.formatDateTime(issue.creationDate()))
- .prop("fCreationDate", formatDate(issue.creationDate()))
- .name("diffs").beginArray()
- .value(i18n.message(userSession.locale(), "created", null))
- .endArray()
- .endObject();
-
- IssueChangelog changelog = issueChangelogService.changelog(issue);
- for (FieldDiffs diffs : changelog.changes()) {
- User user = changelog.user(diffs);
- json
- .beginObject()
- .prop("userName", user != null ? user.name() : null)
- .prop("creationDate", DateUtils.formatDateTime(diffs.creationDate()))
- .prop("fCreationDate", formatDate(diffs.creationDate()));
- json.name("diffs").beginArray();
- List<String> diffsFormatted = issueChangelogService.formatDiffs(diffs);
- for (String diff : diffsFormatted) {
- json.value(diff);
- }
- json.endArray();
- json.endObject();
- }
- json.endArray();
- }
-
- private void addUserWithLabel(@Nullable String value, String field, JsonWriter json) {
- if (value != null) {
- User user = userFinder.findByLogin(value);
- json
- .prop(field, value)
- .prop(field + "Name", user != null ? user.name() : null);
- }
- }
-
- @CheckForNull
- private String formatDate(@Nullable Date date) {
- if (date != null) {
- return i18n.formatDateTime(userSession.locale(), date);
- }
- return null;
- }
-
- @CheckForNull
- private String formatAgeDate(@Nullable Date date) {
- if (date != null) {
- return i18n.ageFromNow(userSession.locale(), date);
- }
- return null;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.issue.ws;
-
-import java.io.StringWriter;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.issue.Issue;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.issue.DefaultIssue;
-import org.sonar.server.issue.workflow.Transition;
-import org.sonar.server.issue.ActionService;
-import org.sonar.server.issue.IssueService;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.test.JsonAssert;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public class IssueActionsWriterTest {
- @Rule
- public UserSessionRule userSessionRule = UserSessionRule.standalone();
-
- @Mock
- IssueService issueService;
-
- @Mock
- ActionService actionService;
-
- IssueActionsWriter writer;
-
- @Before
- public void setUp() {
- writer = new IssueActionsWriter(issueService, actionService, userSessionRule);
- }
-
- @Test
- public void write_actions() {
- Issue issue = new DefaultIssue()
- .setKey("ABCD")
- .setComponentKey("sample:src/main/xoo/sample/Sample.xoo")
- .setProjectKey("sample")
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"))
- .setAssignee("john");
- when(actionService.listAvailableActions(issue)).thenReturn(newArrayList("comment"));
-
- userSessionRule.login("john");
-
- testActions(issue,
- "{\"actions\": " +
- "[" +
- "\"comment\"\n" +
- "]}");
- }
-
- @Test
- public void write_transitions() {
- Issue issue = new DefaultIssue()
- .setKey("ABCD")
- .setComponentKey("sample:src/main/xoo/sample/Sample.xoo")
- .setProjectKey("sample")
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"));
-
- when(issueService.listTransitions(eq(issue))).thenReturn(newArrayList(Transition.create("reopen", "RESOLVED", "REOPEN")));
- userSessionRule.login("john");
-
- testTransitions(issue,
- "{\"transitions\": [\n" +
- " \"reopen\"\n" +
- " ]}");
- }
-
- @Test
- public void write_no_transitions() {
- Issue issue = new DefaultIssue()
- .setKey("ABCD")
- .setComponentKey("sample:src/main/xoo/sample/Sample.xoo")
- .setProjectKey("sample")
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"));
-
- userSessionRule.login("john");
-
- testTransitions(issue,
- "{\"transitions\": []}");
- }
-
- @Test
- public void write_no_transitions_if_not_logged() {
- Issue issue = new DefaultIssue()
- .setKey("ABCD")
- .setComponentKey("sample:src/main/xoo/sample/Sample.xoo")
- .setProjectKey("sample")
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"));
-
- testTransitions(issue,
- "{\"transitions\": []}");
- }
-
- private void testActions(Issue issue, String expected) {
- StringWriter output = new StringWriter();
- JsonWriter jsonWriter = JsonWriter.of(output);
- jsonWriter.beginObject();
- writer.writeActions(issue, jsonWriter);
- jsonWriter.endObject();
- JsonAssert.assertJson(output.toString()).isSimilarTo(expected);
- }
-
- private void testTransitions(Issue issue, String expected) {
- StringWriter output = new StringWriter();
- JsonWriter jsonWriter = JsonWriter.of(output);
- jsonWriter.beginObject();
- writer.writeTransitions(issue, jsonWriter);
- jsonWriter.endObject();
- JsonAssert.assertJson(output.toString()).isSimilarTo(expected);
- }
-
-}
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new IssueWsModule().configure(container);
- assertThat(container.size()).isEqualTo(18);
+ assertThat(container.size()).isEqualTo(16);
}
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.issue.ws;
-
-import com.google.common.base.Optional;
-import com.google.common.collect.Lists;
-import java.util.Date;
-import java.util.List;
-import java.util.Locale;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.i18n.I18n;
-import org.sonar.api.issue.Issue;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.user.User;
-import org.sonar.api.user.UserFinder;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.api.utils.Duration;
-import org.sonar.api.utils.Durations;
-import org.sonar.core.issue.DefaultActionPlan;
-import org.sonar.core.issue.DefaultIssue;
-import org.sonar.core.issue.DefaultIssueComment;
-import org.sonar.core.issue.FieldDiffs;
-import org.sonar.server.issue.workflow.Transition;
-import org.sonar.core.user.DefaultUser;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDao;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.issue.ActionService;
-import org.sonar.server.issue.IssueChangelog;
-import org.sonar.server.issue.IssueChangelogService;
-import org.sonar.server.issue.IssueCommentService;
-import org.sonar.server.issue.IssueService;
-import org.sonar.server.issue.actionplan.ActionPlanService;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.RuleService;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.user.ThreadLocalUserSession;
-import org.sonar.server.ws.WsTester;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-/**
- * TODO Should be replaced by medium tests, as there are too many dependencies
- */
-@RunWith(MockitoJUnitRunner.class)
-public class ShowActionTest {
-
- @org.junit.Rule
- public UserSessionRule userSessionRule = UserSessionRule.standalone();
-
- @Mock
- DbClient dbClient;
-
- @Mock
- DbSession session;
-
- @Mock
- ComponentDao componentDao;
-
- @Mock
- IssueService issueService;
-
- @Mock
- ActionService actionService;
-
- @Mock
- IssueChangelogService issueChangelogService;
-
- @Mock
- IssueCommentService commentService;
-
- @Mock
- ActionPlanService actionPlanService;
-
- @Mock
- UserFinder userFinder;
-
- @Mock
- RuleService ruleService;
-
- @Mock
- I18n i18n;
-
- @Mock
- Durations durations;
-
- Date issueCreationDate;
-
- Rule rule;
-
- WsTester tester;
-
- @Before
- public void setUp() {
- when(dbClient.openSession(false)).thenReturn(session);
- when(dbClient.componentDao()).thenReturn(componentDao);
-
- rule = mock(Rule.class);
- when(rule.key()).thenReturn(RuleKey.of("squid", "AvoidCycle"));
- when(rule.name()).thenReturn("Avoid cycle");
- when(ruleService.getNonNullByKey(rule.key())).thenReturn(rule);
-
- when(issueChangelogService.changelog(any(Issue.class))).thenReturn(mock(IssueChangelog.class));
-
- issueCreationDate = DateUtils.parseDateTime("2014-01-22T19:10:03+0100");
- when(i18n.formatDateTime(any(Locale.class), eq(issueCreationDate))).thenReturn("Jan 22, 2014 10:03 AM");
-
- when(i18n.message(any(Locale.class), eq("created"), eq((String) null))).thenReturn("Created");
-
- when(componentDao.selectById(eq(session), anyLong())).thenReturn(Optional.<ComponentDto>absent());
-
- tester = new WsTester(new IssuesWs(
- new ShowAction(
- dbClient, issueService, issueChangelogService, commentService,
- new IssueActionsWriter(issueService, actionService, userSessionRule),
- actionPlanService, userFinder, ruleService, i18n, durations, userSessionRule)));
- }
-
- @Test
- public void show_issue() throws Exception {
- String issueKey = "ABCD";
-
- ComponentDto project = ComponentTesting.newProjectDto()
- .setId(1L)
- .setKey("org.sonar.Sonar")
- .setLongName("SonarQube")
- .setName("SonarQube");
- when(componentDao.selectOrFailByUuid(session, project.uuid())).thenReturn(project);
-
- ComponentDto file = ComponentTesting.newFileDto(project)
- .setId(10L)
- .setKey("org.sonar.server.issue.IssueClient")
- .setLongName("SonarQube :: Issue Client")
- .setName("SonarQube :: Issue Client")
- .setQualifier("FIL")
- .setParentProjectId(1L);
- when(componentDao.selectOrFailByUuid(session, file.uuid())).thenReturn(file);
-
- DefaultIssue issue = new DefaultIssue()
- .setKey(issueKey)
- .setComponentKey("org.sonar.server.issue.IssueClient")
- .setComponentUuid(file.uuid())
- .setProjectKey("org.sonar.Sonar")
- .setProjectUuid(project.uuid())
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"))
- .setLine(12)
- .setMessage("Fix it")
- .setResolution("FIXED")
- .setStatus("CLOSED")
- .setSeverity("MAJOR")
- .setCreationDate(issueCreationDate);
- when(issueService.getByKey(issueKey)).thenReturn(issue);
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issueKey);
- request.execute().assertJson(getClass(), "show_issue.json");
- }
-
- @Test
- public void show_issue_with_sub_project() throws Exception {
- String issueKey = "ABCD";
-
- // Project
- ComponentDto project = ComponentTesting.newProjectDto()
- .setId(1L)
- .setKey("org.sonar.Sonar")
- .setLongName("SonarQube");
- when(componentDao.selectOrFailByUuid(session, project.uuid())).thenReturn(project);
-
- // Module
- ComponentDto module = ComponentTesting.newModuleDto(project)
- .setId(2L)
- .setKey("org.sonar.server.Server")
- .setLongName("SonarQube :: Server")
- .setQualifier("BRC")
- .setParentProjectId(1L);
- when(componentDao.selectById(session, module.getId())).thenReturn(Optional.of(module));
-
- // File
- ComponentDto file = ComponentTesting.newFileDto(module)
- .setId(10L)
- .setKey("org.sonar.server.issue.IssueClient")
- .setLongName("SonarQube :: Issue Client")
- .setQualifier("FIL")
- .setParentProjectId(2L);
- when(componentDao.selectOrFailByUuid(session, file.uuid())).thenReturn(file);
-
- DefaultIssue issue = new DefaultIssue()
- .setKey(issueKey)
- .setComponentKey("org.sonar.server.issue.IssueClient")
- .setComponentUuid(file.uuid())
- .setProjectKey("org.sonar.Sonar")
- .setProjectUuid(project.uuid())
- .setModuleUuid(module.uuid())
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"))
- .setLine(12)
- .setMessage("Fix it")
- .setResolution("FIXED")
- .setStatus("CLOSED")
- .setSeverity("MAJOR")
- .setCreationDate(issueCreationDate);
- when(issueService.getByKey(issueKey)).thenReturn(issue);
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issueKey);
- request.execute().assertJson(getClass(), "show_issue_with_sub_project.json");
- }
-
- @Test
- public void use_project_and_sub_project_names_if_no_long_name() throws Exception {
- String issueKey = "ABCD";
-
- // Project
- ComponentDto project = ComponentTesting.newProjectDto()
- .setId(1L)
- .setKey("org.sonar.Sonar")
- .setName("SonarQube")
- .setLongName(null);
- when(componentDao.selectOrFailByUuid(session, project.uuid())).thenReturn(project);
-
- // Module
- ComponentDto module = ComponentTesting.newModuleDto(project)
- .setId(2L)
- .setKey("org.sonar.server.Server")
- .setName("SonarQube :: Server")
- .setLongName(null)
- .setQualifier("BRC")
- .setParentProjectId(1L);
- when(componentDao.selectById(session, module.getId())).thenReturn(Optional.of(module));
-
- // File
- ComponentDto file = ComponentTesting.newFileDto(module)
- .setId(10L)
- .setKey("org.sonar.server.issue.IssueClient")
- .setLongName("SonarQube :: Issue Client")
- .setQualifier("FIL")
- .setParentProjectId(2L);
- when(componentDao.selectOrFailByUuid(session, file.uuid())).thenReturn(file);
-
- DefaultIssue issue = new DefaultIssue()
- .setKey(issueKey)
- .setComponentKey("org.sonar.server.issue.IssueClient")
- .setComponentUuid(file.uuid())
- .setProjectKey("org.sonar.Sonar")
- .setProjectUuid(project.uuid())
- .setModuleUuid(module.uuid())
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"))
- .setLine(12)
- .setEffortToFix(2.0)
- .setMessage("Fix it")
- .setResolution("FIXED")
- .setStatus("CLOSED")
- .setSeverity("MAJOR")
- .setCreationDate(issueCreationDate);
- when(issueService.getByKey(issueKey)).thenReturn(issue);
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issueKey);
- request.execute().assertJson(getClass(), "show_issue_with_sub_project.json");
- }
-
- @Test
- public void show_issue_on_removed_component() throws Exception {
- String issueKey = "ABCD";
-
- ComponentDto project = ComponentTesting.newProjectDto()
- .setId(1L)
- .setKey("org.sonar.Sonar")
- .setLongName("SonarQube")
- .setName("SonarQube");
- when(componentDao.selectOrFailByUuid(session, project.uuid())).thenReturn(project);
-
- ComponentDto file = ComponentTesting.newFileDto(project)
- .setId(10L)
- .setEnabled(false)
- .setKey("org.sonar.server.issue.IssueClient")
- .setLongName("SonarQube :: Issue Client")
- .setName("SonarQube :: Issue Client")
- .setQualifier("FIL")
- .setParentProjectId(1L);
- when(componentDao.selectOrFailByUuid(session, file.uuid())).thenReturn(file);
-
- DefaultIssue issue = createIssue()
- .setComponentUuid(file.uuid())
- .setProjectUuid(project.uuid());
- when(issueService.getByKey(issueKey)).thenReturn(issue);
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issueKey);
- request.execute().assertJson(getClass(), "show_issue_on_removed_component.json");
- }
-
- @Test
- public void show_issue_with_action_plan() throws Exception {
- DefaultIssue issue = createStandardIssue()
- .setActionPlanKey("AP-ABCD");
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- when(actionPlanService.findByKey(eq(issue.actionPlanKey()), any(ThreadLocalUserSession.class))).thenReturn(new DefaultActionPlan().setKey("AP-ABCD").setName("Version 4.2"));
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_action_plan.json");
- }
-
- @Test
- public void show_issue_with_users() throws Exception {
- DefaultIssue issue = createStandardIssue()
- .setAssignee("john")
- .setReporter("steven")
- .setAuthorLogin("Henry");
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- when(userFinder.findByLogin("john")).thenReturn(new DefaultUser().setLogin("john").setName("John"));
- when(userFinder.findByLogin("steven")).thenReturn(new DefaultUser().setLogin("steven").setName("Steven"));
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_users.json");
- }
-
- @Test
- public void show_issue_with_technical_debt() throws Exception {
- Duration debt = (Duration.create(7260L));
- DefaultIssue issue = createStandardIssue().setDebt(debt);
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- when(durations.encode(debt)).thenReturn("2h1min");
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_technical_debt.json");
- }
-
- @Test
- public void show_issue_with_dates() throws Exception {
- Date creationDate = DateUtils.parseDateTime("2014-01-22T19:10:03+0100");
- Date updateDate = DateUtils.parseDateTime("2014-01-23T19:10:03+0100");
- Date closedDate = DateUtils.parseDateTime("2014-01-24T19:10:03+0100");
-
- DefaultIssue issue = createStandardIssue()
- .setCreationDate(creationDate)
- .setUpdateDate(updateDate)
- .setCloseDate(closedDate);
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- when(i18n.formatDateTime(any(Locale.class), eq(creationDate))).thenReturn("Jan 22, 2014 10:03 AM");
- when(i18n.formatDateTime(any(Locale.class), eq(updateDate))).thenReturn("Jan 23, 2014 10:03 AM");
- when(i18n.ageFromNow(any(Locale.class), eq(updateDate))).thenReturn("9 days");
- when(i18n.formatDateTime(any(Locale.class), eq(closedDate))).thenReturn("Jan 24, 2014 10:03 AM");
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_dates.json");
- }
-
- @Test
- public void show_issue_with_comments() throws Exception {
- Date date1 = DateUtils.parseDateTime("2014-02-22T19:10:03+0100");
- Date date2 = DateUtils.parseDateTime("2014-02-23T19:10:03+0100");
-
- DefaultIssue issue = createStandardIssue();
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- when(commentService.findComments(issue.key())).thenReturn(newArrayList(
- new DefaultIssueComment()
- .setKey("COMMENT-ABCD")
- .setMarkdownText("*My comment*")
- .setUserLogin("john")
- .setCreatedAt(date1),
- new DefaultIssueComment()
- .setKey("COMMENT-ABCE")
- .setMarkdownText("Another comment")
- .setUserLogin("arthur")
- .setCreatedAt(date2)));
-
- when(userFinder.findByLogin("john")).thenReturn(new DefaultUser().setLogin("john").setName("John"));
- when(userFinder.findByLogin("arthur")).thenReturn(new DefaultUser().setLogin("arthur").setName("Arthur"));
-
- when(i18n.ageFromNow(any(Locale.class), eq(date1))).thenReturn("9 days");
- when(i18n.ageFromNow(any(Locale.class), eq(date2))).thenReturn("10 days");
-
- userSessionRule.login("arthur");
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_comments.json");
- }
-
- @Test
- public void show_issue_with_transitions() throws Exception {
- DefaultIssue issue = createStandardIssue()
- .setStatus("RESOLVED")
- .setResolution("FIXED");
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- when(issueService.listTransitions(eq(issue))).thenReturn(newArrayList(Transition.create("reopen", "RESOLVED", "REOPEN")));
-
- userSessionRule.login("john");
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_transitions.json");
- }
-
- @Test
- public void show_issue_with_actions() throws Exception {
- DefaultIssue issue = createStandardIssue()
- .setStatus("OPEN");
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- when(actionService.listAvailableActions(issue)).thenReturn(newArrayList( "comment", "assign", "set_tags", "assign_to_me", "plan"));
-
- userSessionRule.login("john");
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_actions.json");
- }
-
- @Test
- public void show_issue_with_changelog() throws Exception {
- DefaultIssue issue = createStandardIssue();
- when(issueService.getByKey(issue.key())).thenReturn(issue);
-
- Date date1 = DateUtils.parseDateTime("2014-02-22T19:10:03+0100");
- Date date2 = DateUtils.parseDateTime("2014-02-23T19:10:03+0100");
-
- List<User> users = Lists.<User>newArrayList(new DefaultUser().setLogin("john").setName("John"));
- FieldDiffs userChange = new FieldDiffs()
- .setUserLogin("john")
- .setDiff("actionPlan", null, "1.0")
- .setCreationDate(date1);
- FieldDiffs scanChange = new FieldDiffs()
- .setDiff("severity", "INFO", "BLOCKER")
- .setDiff("status", "REOPEN", "RESOLVED")
- .setCreationDate(date2);
- when(issueChangelogService.changelog(issue)).thenReturn(new IssueChangelog(newArrayList(userChange, scanChange), users));
- when(issueChangelogService.formatDiffs(userChange)).thenReturn(newArrayList("Action plan updated to 1.0"));
- when(issueChangelogService.formatDiffs(scanChange)).thenReturn(newArrayList("Severity updated from Info to Blocker", "Status updated from Reopen to Resolved"));
-
- when(i18n.formatDateTime(any(Locale.class), eq(date1))).thenReturn("Fev 22, 2014 10:03 AM");
- when(i18n.formatDateTime(any(Locale.class), eq(date2))).thenReturn("Fev 23, 2014 10:03 AM");
-
- WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
- request.execute().assertJson(getClass(), "show_issue_with_changelog.json");
- }
-
- private DefaultIssue createStandardIssue() {
- ComponentDto project = ComponentTesting.newProjectDto()
- .setId(1L)
- .setKey("org.sonar.Sonar")
- .setLongName("SonarQube")
- .setName("SonarQube");
- when(componentDao.selectOrFailByUuid(session, project.uuid())).thenReturn(project);
-
- ComponentDto file = ComponentTesting.newFileDto(project)
- .setId(10L)
- .setKey("org.sonar.server.issue.IssueClient")
- .setLongName("SonarQube :: Issue Client")
- .setName("SonarQube :: Issue Client")
- .setQualifier("FIL")
- .setParentProjectId(1L);
- when(componentDao.selectOrFailByUuid(session, file.uuid())).thenReturn(file);
-
- return createIssue()
- .setComponentUuid(file.uuid())
- .setProjectUuid(project.uuid());
- }
-
- private DefaultIssue createIssue() {
- return new DefaultIssue()
- .setKey("ABCD")
- .setComponentKey("org.sonar.server.issue.IssueClient")
- .setProjectKey("org.sonar.Sonar")
- .setRuleKey(RuleKey.of("squid", "AvoidCycle"))
- .setCreationDate(issueCreationDate);
- }
-
-}