From 8c7b10b6bf2712e1d7201ebd025aed36fad7ead6 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 18 Mar 2014 09:34:41 +0100 Subject: [PATCH] SONAR-5056 Refactoring of DebtModelService to have a clean dedicated server side API of debt --- .../widgets/technical_debt_pyramid.html.erb | 2 +- .../DefaultTechnicalDebtManagerTest.java | 107 ------------------ .../api/server/debt/DebtCharacteristic.java | 32 ++---- .../debt/DebtModel.java} | 17 +-- .../internal/DefaultDebtCharacteristic.java | 85 ++++++++++++++ .../server/debt/internal/package-info.java | 24 ++++ .../sonar/api/server/debt/package-info.java | 24 ++++ .../technicaldebt/server/Characteristic.java | 4 +- .../internal/DefaultCharacteristic.java | 2 + .../sonar/server/debt/DebtModelService.java | 64 +++++------ .../server/issue/ws/IssueShowWsHandler.java | 18 +-- .../server/platform/ServerComponents.java | 104 ++--------------- .../app/controllers/issue_controller.rb | 4 +- .../webapp/WEB-INF/app/models/internal.rb | 2 +- .../server/debt/DebtModelServiceTest.java | 88 ++++++++++++++ .../sonar/server/debt/DebtServiceTest.java | 65 ----------- .../issue/ws/IssueShowWsHandlerTest.java | 24 ++-- 17 files changed, 302 insertions(+), 364 deletions(-) delete mode 100644 sonar-core/src/test/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManagerTest.java rename sonar-server/src/main/java/org/sonar/server/debt/DebtService.java => sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtCharacteristic.java (55%) rename sonar-plugin-api/src/main/java/org/sonar/api/{technicaldebt/server/TechnicalDebtManager.java => server/debt/DebtModel.java} (71%) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtCharacteristic.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/package-info.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/server/debt/package-info.java rename sonar-core/src/main/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManager.java => sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java (50%) create mode 100644 sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java delete mode 100644 sonar-server/src/test/java/org/sonar/server/debt/DebtServiceTest.java diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb index a2ecdbd479f..cb4b9b3f8a3 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb @@ -1,7 +1,7 @@ <% technical_debt = measure('sqale_index') - root_characteristics = Internal.debt.findRootCharacteristics().to_a + root_characteristics = Internal.debt.rootCharacteristics().to_a should_display_diff_measures = dashboard_configuration.selected_period? && technical_debt.variation(dashboard_configuration.period_index)!=nil if technical_debt.nil? || root_characteristics.empty? diff --git a/sonar-core/src/test/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManagerTest.java b/sonar-core/src/test/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManagerTest.java deleted file mode 100644 index fb7b2a664a9..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManagerTest.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.core.technicaldebt; - -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.rules.Rule; -import org.sonar.api.technicaldebt.server.Characteristic; -import org.sonar.core.technicaldebt.db.CharacteristicDao; -import org.sonar.core.technicaldebt.db.CharacteristicDto; - -import java.util.List; - -import static com.google.common.collect.Lists.newArrayList; -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class DefaultTechnicalDebtManagerTest { - - @Mock - CharacteristicDao dao; - - DefaultTechnicalDebtManager finder; - - @Before - public void setUp() throws Exception { - finder = new DefaultTechnicalDebtManager(dao); - } - - @Test - public void find_root_characteristics() throws Exception { - CharacteristicDto rootCharacteristicDto = new CharacteristicDto() - .setId(1) - .setKey("MEMORY_EFFICIENCY") - .setName("Memory use"); - when(dao.selectEnabledRootCharacteristics()).thenReturn(newArrayList(rootCharacteristicDto)); - - List result = finder.findRootCharacteristics(); - assertThat(result).hasSize(1); - - Characteristic rootCharacteristic = result.get(0); - assertThat(rootCharacteristic.key()).isEqualTo("MEMORY_EFFICIENCY"); - assertThat(rootCharacteristic.name()).isEqualTo("Memory use"); - assertThat(rootCharacteristic.parentId()).isNull(); - assertThat(rootCharacteristic.rootId()).isNull(); - } - - @Test - public void find_characteristic() throws Exception { - Rule rule = Rule.create("repo", "key"); - rule.setId(1); - - when(dao.selectById(2)).thenReturn( - new CharacteristicDto().setId(2).setKey("COMPILER_RELATED_PORTABILITY").setName("Compiler").setParentId(1)); - - Characteristic result = finder.findCharacteristicById(2); - - assertThat(result.id()).isEqualTo(2); - assertThat(result.parentId()).isEqualTo(1); - assertThat(result.key()).isEqualTo("COMPILER_RELATED_PORTABILITY"); - assertThat(result.name()).isEqualTo("Compiler"); - } - - @Test - public void not_find_characteristic() throws Exception { - Rule rule = Rule.create("repo", "key"); - rule.setId(1); - - when(dao.selectById(rule.getId())).thenReturn(null); - - Characteristic result = finder.findCharacteristicById(2); - assertThat(result).isNull(); - } - - @Test - public void find_requirement_always_return_null() throws Exception { - assertThat(finder.findRequirementByRule(Rule.create("repo", "key"))).isNull(); - } - - @Test - public void find_requirement_by_rule_id_always_return_null() throws Exception { - assertThat(finder.findRequirementByRuleId(1)).isNull(); - } - -} diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtService.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtCharacteristic.java similarity index 55% rename from sonar-server/src/main/java/org/sonar/server/debt/DebtService.java rename to sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtCharacteristic.java index 4e5a84ce7d6..8f3dd602d7b 100644 --- a/sonar-server/src/main/java/org/sonar/server/debt/DebtService.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtCharacteristic.java @@ -18,39 +18,23 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.server.debt; - -import org.sonar.api.ServerComponent; -import org.sonar.api.technicaldebt.server.Characteristic; -import org.sonar.core.technicaldebt.DefaultTechnicalDebtManager; +package org.sonar.api.server.debt; import javax.annotation.CheckForNull; -import java.util.List; - /** - * Used through ruby code
Internal.debt
+ * @since 4.3 */ -public class DebtService implements ServerComponent { +public interface DebtCharacteristic { + Integer id(); - private final DefaultTechnicalDebtManager finder; + String key(); - public DebtService(DefaultTechnicalDebtManager finder) { - this.finder = finder; - } - - public List findRootCharacteristics() { - return finder.findRootCharacteristics(); - } + String name(); @CheckForNull - public Characteristic findRequirementByRuleId(int ruleId) { - return finder.findRequirementByRuleId(ruleId); - } + Integer order(); @CheckForNull - public Characteristic findCharacteristic(int id) { - return finder.findCharacteristicById(id); - } - + Integer parentId(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/TechnicalDebtManager.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtModel.java similarity index 71% rename from sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/TechnicalDebtManager.java rename to sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtModel.java index e3b1d8465d6..e2bd93cba6c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/TechnicalDebtManager.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtModel.java @@ -18,26 +18,21 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.api.technicaldebt.server; +package org.sonar.api.server.debt; import org.sonar.api.ServerComponent; -import org.sonar.api.rules.Rule; import java.util.List; /** - * @since 4.1 + * @since 4.3 */ +public interface DebtModel extends ServerComponent { -public interface TechnicalDebtManager extends ServerComponent { + List characteristics(); - List findRootCharacteristics(); + List rootCharacteristics(); - /** - * @deprecated since 4.3. Always return null - */ - @Deprecated - Characteristic findRequirementByRule(Rule rule); + DebtCharacteristic characteristicById(int id); - Characteristic findCharacteristicById(Integer id); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtCharacteristic.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtCharacteristic.java new file mode 100644 index 00000000000..6b6fce040f5 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtCharacteristic.java @@ -0,0 +1,85 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.api.server.debt.internal; + +import org.sonar.api.server.debt.DebtCharacteristic; + +/** + * @since 4.3 + */ +public class DefaultDebtCharacteristic implements DebtCharacteristic { + + private Integer id; + private String key; + private String name; + private Integer order; + private Integer parentId; + + @Override + public Integer id() { + return id; + } + + public DefaultDebtCharacteristic setId(Integer id) { + this.id = id; + return this; + } + + @Override + public String key() { + return key; + } + + public DefaultDebtCharacteristic setKey(String key) { + this.key = key; + return this; + } + + @Override + public String name() { + return name; + } + + public DefaultDebtCharacteristic setName(String name) { + this.name = name; + return this; + } + + @Override + public Integer order() { + return order; + } + + public DefaultDebtCharacteristic setOrder(Integer order) { + this.order = order; + return this; + } + + @Override + public Integer parentId() { + return parentId; + } + + public DefaultDebtCharacteristic setParentId(Integer parentId) { + this.parentId = parentId; + return this; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/package-info.java new file mode 100644 index 00000000000..fc333dadf16 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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. + */ + +@ParametersAreNonnullByDefault +package org.sonar.api.server.debt.internal; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/package-info.java new file mode 100644 index 00000000000..296802f21d5 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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. + */ + +@ParametersAreNonnullByDefault +package org.sonar.api.server.debt; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/Characteristic.java b/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/Characteristic.java index c39eba4108a..1e263a6fe20 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/Characteristic.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/Characteristic.java @@ -27,8 +27,10 @@ import org.sonar.api.utils.internal.WorkDuration; import javax.annotation.CheckForNull; /** - * @since 4.1 + * @since 4.1 * + * @deprecated since 4.3. */ +@Deprecated public interface Characteristic { Integer id(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristic.java b/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristic.java index 15a0e68dfd4..1c49df8a662 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristic.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/technicaldebt/server/internal/DefaultCharacteristic.java @@ -32,7 +32,9 @@ import javax.annotation.Nullable; /** * @since 4.1 + * @deprecated since 4.3. */ +@Deprecated public class DefaultCharacteristic implements Characteristic { private Integer id; diff --git a/sonar-core/src/main/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManager.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java similarity index 50% rename from sonar-core/src/main/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManager.java rename to sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java index b9d6d6b6d9a..a5a32807add 100644 --- a/sonar-core/src/main/java/org/sonar/core/technicaldebt/DefaultTechnicalDebtManager.java +++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java @@ -18,71 +18,59 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.core.technicaldebt; +package org.sonar.server.debt; - -import org.sonar.api.rules.Rule; -import org.sonar.api.technicaldebt.server.Characteristic; -import org.sonar.api.technicaldebt.server.TechnicalDebtManager; -import org.sonar.api.technicaldebt.server.internal.DefaultCharacteristic; +import com.google.common.base.Function; +import com.google.common.collect.Iterables; +import org.sonar.api.server.debt.DebtCharacteristic; +import org.sonar.api.server.debt.DebtModel; +import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic; import org.sonar.core.technicaldebt.db.CharacteristicDao; import org.sonar.core.technicaldebt.db.CharacteristicDto; import javax.annotation.CheckForNull; +import java.util.Collection; import java.util.List; import static com.google.common.collect.Lists.newArrayList; /** - * TODO This class should be replaced or created by a TechnicalDebtManagerBuilder + * Used through ruby code
Internal.debt
*/ -public class DefaultTechnicalDebtManager implements TechnicalDebtManager { +public class DebtModelService implements DebtModel { private final CharacteristicDao dao; - public DefaultTechnicalDebtManager(CharacteristicDao dao) { + public DebtModelService(CharacteristicDao dao) { this.dao = dao; } - public List findRootCharacteristics() { - List dtos = dao.selectEnabledRootCharacteristics(); - List characteristics = newArrayList(); - for (CharacteristicDto dto : dtos) { - characteristics.add(toCharacteristic(dto)); - } - return characteristics; + public List rootCharacteristics() { + return toCharacteristics(dao.selectEnabledRootCharacteristics()); } - @CheckForNull - public Characteristic findCharacteristicById(Integer id) { - CharacteristicDto dto = dao.selectById(id); - if (dto != null) { - return toCharacteristic(dto); - } - return null; + public List characteristics() { + return toCharacteristics(dao.selectEnabledCharacteristics()); } - /** - * @deprecated since 4.3. Always return null - */ - @Deprecated @CheckForNull - public Characteristic findRequirementByRuleId(int ruleId) { - return null; + public DebtCharacteristic characteristicById(int id) { + CharacteristicDto dto = dao.selectById(id); + return dto != null ? toCharacteristic(dto) : null; } - /** - * @deprecated since 4.3. Always return null - */ - @Deprecated - @CheckForNull - public Characteristic findRequirementByRule(Rule rule) { - return null; + private static List toCharacteristics(Collection dtos) { + return newArrayList(Iterables.transform(dtos, new Function() { + @Override + public DebtCharacteristic apply(CharacteristicDto input) { + return toCharacteristic(input); + } + })); } - private static Characteristic toCharacteristic(CharacteristicDto dto) { - return new DefaultCharacteristic() + private static DebtCharacteristic toCharacteristic(CharacteristicDto dto) { + return new DefaultDebtCharacteristic() .setId(dto.getId()) .setKey(dto.getKey()) .setName(dto.getName()) 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 b3a1316ae42..e41a035ea08 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 @@ -27,10 +27,11 @@ import org.sonar.api.issue.*; import org.sonar.api.issue.action.Action; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.issue.internal.FieldDiffs; +import org.sonar.api.server.debt.DebtCharacteristic; +import org.sonar.api.server.debt.DebtModel; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; -import org.sonar.api.technicaldebt.server.Characteristic; import org.sonar.api.user.User; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.Duration; @@ -39,7 +40,6 @@ import org.sonar.api.utils.text.JsonWriter; import org.sonar.api.web.UserRole; import org.sonar.core.component.ComponentDto; import org.sonar.core.issue.workflow.Transition; -import org.sonar.core.technicaldebt.DefaultTechnicalDebtManager; import org.sonar.markdown.Markdown; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.issue.ActionService; @@ -63,17 +63,17 @@ public class IssueShowWsHandler implements RequestHandler { private final IssueService issueService; private final IssueChangelogService issueChangelogService; private final ActionService actionService; - private final DefaultTechnicalDebtManager technicalDebtManager; + private final DebtModel debtModel; private final I18n i18n; private final Durations durations; public IssueShowWsHandler(IssueFinder issueFinder, IssueService issueService, IssueChangelogService issueChangelogService, ActionService actionService, - DefaultTechnicalDebtManager technicalDebtManager, I18n i18n, Durations durations) { + DebtModel debtModel, I18n i18n, Durations durations) { this.issueFinder = issueFinder; this.issueService = issueService; this.issueChangelogService = issueChangelogService; this.actionService = actionService; - this.technicalDebtManager = technicalDebtManager; + this.debtModel = debtModel; this.i18n = i18n; this.durations = durations; } @@ -190,18 +190,18 @@ public class IssueShowWsHandler implements RequestHandler { private void addCharacteristics(IssueQueryResult result, DefaultIssue issue, JsonWriter json) { Integer subCharacteristicId = result.rule(issue).getCharacteristicId() != null ? result.rule(issue).getCharacteristicId() : result.rule(issue).getDefaultCharacteristicId(); - Characteristic subCharacteristic = findCharacteristicById(subCharacteristicId); + DebtCharacteristic subCharacteristic = characteristicById(subCharacteristicId); if (subCharacteristic != null) { json.prop("subCharacteristic", subCharacteristic.name()); - Characteristic characteristic = findCharacteristicById(subCharacteristic.parentId()); + DebtCharacteristic characteristic = characteristicById(subCharacteristic.parentId()); json.prop("characteristic", characteristic != null ? characteristic.name() : null); } } @CheckForNull - private Characteristic findCharacteristicById(@Nullable Integer id) { + private DebtCharacteristic characteristicById(@Nullable Integer id) { if (id != null) { - return technicalDebtManager.findCharacteristicById(id); + return debtModel.characteristicById(id); } return null; } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index 4b208704a7e..87e242e4ae2 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -52,13 +52,7 @@ import org.sonar.core.measure.MeasureFilterFactory; import org.sonar.core.metric.DefaultMetricFinder; import org.sonar.core.notification.DefaultNotificationManager; import org.sonar.core.permission.PermissionFacade; -import org.sonar.core.persistence.DaoUtils; -import org.sonar.core.persistence.DatabaseVersion; -import org.sonar.core.persistence.DefaultDatabase; -import org.sonar.core.persistence.MyBatis; -import org.sonar.core.persistence.PreviewDatabaseFactory; -import org.sonar.core.persistence.SemaphoreUpdater; -import org.sonar.core.persistence.SemaphoresImpl; +import org.sonar.core.persistence.*; import org.sonar.core.preview.PreviewCache; import org.sonar.core.profiling.Profiling; import org.sonar.core.purge.PurgeProfiler; @@ -67,7 +61,6 @@ import org.sonar.core.qualitygate.db.QualityGateConditionDao; import org.sonar.core.qualitygate.db.QualityGateDao; import org.sonar.core.resource.DefaultResourcePermissions; import org.sonar.core.rule.DefaultRuleFinder; -import org.sonar.core.technicaldebt.DefaultTechnicalDebtManager; import org.sonar.core.technicaldebt.TechnicalDebtModelRepository; import org.sonar.core.technicaldebt.TechnicalDebtModelSynchronizer; import org.sonar.core.technicaldebt.TechnicalDebtXMLImporter; @@ -90,28 +83,12 @@ import org.sonar.server.db.EmbeddedDatabaseFactory; import org.sonar.server.db.migrations.DatabaseMigrations; import org.sonar.server.db.migrations.DatabaseMigrator; import org.sonar.server.debt.DebtCharacteristicsXMLImporter; +import org.sonar.server.debt.DebtModelService; import org.sonar.server.debt.DebtModelSynchronizer; import org.sonar.server.debt.DebtRulesXMLImporter; -import org.sonar.server.debt.DebtService; import org.sonar.server.es.ESIndex; import org.sonar.server.es.ESNode; -import org.sonar.server.issue.ActionPlanService; -import org.sonar.server.issue.ActionService; -import org.sonar.server.issue.AssignAction; -import org.sonar.server.issue.CommentAction; -import org.sonar.server.issue.DefaultIssueFinder; -import org.sonar.server.issue.InternalRubyIssueService; -import org.sonar.server.issue.IssueBulkChangeService; -import org.sonar.server.issue.IssueChangelogFormatter; -import org.sonar.server.issue.IssueChangelogService; -import org.sonar.server.issue.IssueCommentService; -import org.sonar.server.issue.IssueService; -import org.sonar.server.issue.IssueStatsFinder; -import org.sonar.server.issue.PlanAction; -import org.sonar.server.issue.PublicRubyIssueService; -import org.sonar.server.issue.ServerIssueStorage; -import org.sonar.server.issue.SetSeverityAction; -import org.sonar.server.issue.TransitionAction; +import org.sonar.server.issue.*; import org.sonar.server.issue.filter.IssueFilterService; import org.sonar.server.issue.filter.IssueFilterWs; import org.sonar.server.issue.ws.IssueShowWsHandler; @@ -123,87 +100,29 @@ import org.sonar.server.permission.InternalPermissionTemplateService; import org.sonar.server.permission.PermissionFinder; import org.sonar.server.platform.ws.PlatformWs; import org.sonar.server.platform.ws.RestartHandler; -import org.sonar.server.plugins.InstalledPluginReferentialFactory; -import org.sonar.server.plugins.PluginDownloader; -import org.sonar.server.plugins.ServerExtensionInstaller; -import org.sonar.server.plugins.ServerPluginJarInstaller; -import org.sonar.server.plugins.ServerPluginJarsInstaller; -import org.sonar.server.plugins.ServerPluginRepository; -import org.sonar.server.plugins.UpdateCenterClient; -import org.sonar.server.plugins.UpdateCenterMatrixFactory; +import org.sonar.server.plugins.*; import org.sonar.server.qualitygate.QgateProjectFinder; import org.sonar.server.qualitygate.QualityGates; import org.sonar.server.qualitygate.ws.QgateAppHandler; import org.sonar.server.qualitygate.ws.QualityGatesWs; -import org.sonar.server.qualityprofile.ESActiveRule; -import org.sonar.server.qualityprofile.ProfilesManager; -import org.sonar.server.qualityprofile.QProfileActiveRuleOperations; -import org.sonar.server.qualityprofile.QProfileBackup; -import org.sonar.server.qualityprofile.QProfileLookup; -import org.sonar.server.qualityprofile.QProfileOperations; -import org.sonar.server.qualityprofile.QProfileProjectLookup; -import org.sonar.server.qualityprofile.QProfileProjectOperations; -import org.sonar.server.qualityprofile.QProfileRepositoryExporter; -import org.sonar.server.qualityprofile.QProfileRuleLookup; -import org.sonar.server.qualityprofile.QProfiles; -import org.sonar.server.rule.DeprecatedRulesDefinition; -import org.sonar.server.rule.ESRuleTags; -import org.sonar.server.rule.RubyRuleService; -import org.sonar.server.rule.RuleDefinitionsLoader; -import org.sonar.server.rule.RuleOperations; -import org.sonar.server.rule.RuleRegistration; -import org.sonar.server.rule.RuleRegistry; -import org.sonar.server.rule.RuleRepositories; -import org.sonar.server.rule.RuleTagLookup; -import org.sonar.server.rule.RuleTagOperations; -import org.sonar.server.rule.RuleTags; -import org.sonar.server.rule.Rules; -import org.sonar.server.rule.ws.AddTagsWsHandler; -import org.sonar.server.rule.ws.RemoveTagsWsHandler; -import org.sonar.server.rule.ws.RuleSearchWsHandler; -import org.sonar.server.rule.ws.RuleShowWsHandler; -import org.sonar.server.rule.ws.RuleTagsWs; -import org.sonar.server.rule.ws.RulesWs; +import org.sonar.server.qualityprofile.*; +import org.sonar.server.rule.*; +import org.sonar.server.rule.ws.*; import org.sonar.server.source.CodeColorizers; import org.sonar.server.source.DeprecatedSourceDecorator; import org.sonar.server.source.HtmlSourceDecorator; import org.sonar.server.source.SourceService; import org.sonar.server.source.ws.SourcesShowWsHandler; import org.sonar.server.source.ws.SourcesWs; -import org.sonar.server.startup.CleanPreviewAnalysisCache; -import org.sonar.server.startup.CopyRequirementsFromCharacteristicsToRules; -import org.sonar.server.startup.GenerateBootstrapIndex; -import org.sonar.server.startup.GeneratePluginIndex; -import org.sonar.server.startup.GwtPublisher; -import org.sonar.server.startup.JdbcDriverDeployer; -import org.sonar.server.startup.LogServerId; -import org.sonar.server.startup.RegisterDebtModel; -import org.sonar.server.startup.RegisterMetrics; -import org.sonar.server.startup.RegisterNewDashboards; -import org.sonar.server.startup.RegisterNewMeasureFilters; -import org.sonar.server.startup.RegisterNewProfiles; -import org.sonar.server.startup.RegisterPermissionTemplates; -import org.sonar.server.startup.RegisterServletFilters; -import org.sonar.server.startup.RenameDeprecatedPropertyKeys; -import org.sonar.server.startup.ServerMetadataPersister; +import org.sonar.server.startup.*; import org.sonar.server.text.MacroInterpreter; import org.sonar.server.text.RubyTextService; import org.sonar.server.ui.JRubyI18n; import org.sonar.server.ui.JRubyProfiling; import org.sonar.server.ui.PageDecorations; import org.sonar.server.ui.Views; -import org.sonar.server.user.DefaultUserService; -import org.sonar.server.user.GroupMembershipFinder; -import org.sonar.server.user.GroupMembershipService; -import org.sonar.server.user.NewUserNotifier; -import org.sonar.server.user.SecurityRealmFactory; -import org.sonar.server.util.BooleanTypeValidation; -import org.sonar.server.util.FloatTypeValidation; -import org.sonar.server.util.IntegerTypeValidation; -import org.sonar.server.util.StringListTypeValidation; -import org.sonar.server.util.StringTypeValidation; -import org.sonar.server.util.TextTypeValidation; -import org.sonar.server.util.TypeValidations; +import org.sonar.server.user.*; +import org.sonar.server.util.*; import org.sonar.server.ws.ListingWs; import org.sonar.server.ws.WebServiceEngine; @@ -428,14 +347,13 @@ class ServerComponents { pico.addSingleton(TransitionAction.class); // technical debt - pico.addSingleton(DebtService.class); + pico.addSingleton(DebtModelService.class); pico.addSingleton(TechnicalDebtModelSynchronizer.class); pico.addSingleton(DebtModelSynchronizer.class); pico.addSingleton(TechnicalDebtModelRepository.class); pico.addSingleton(TechnicalDebtXMLImporter.class); pico.addSingleton(DebtRulesXMLImporter.class); pico.addSingleton(DebtCharacteristicsXMLImporter.class); - pico.addSingleton(DefaultTechnicalDebtManager.class); // source pico.addSingleton(HtmlSourceDecorator.class); diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/issue_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/issue_controller.rb index 2df6b11d23d..43542de7520 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/issue_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/issue_controller.rb @@ -189,8 +189,8 @@ class IssueController < ApplicationController @rule = Rule.first(:conditions => ['plugin_name=? and plugin_rule_key=?', rule_key[0], rule_key[1]]) characteristic_id = @rule.characteristic_id || @rule.default_characteristic_id if characteristic_id - @characteristic = Internal.debt.findCharacteristic(characteristic_id) - @root_characteristic = Internal.debt.findCharacteristic(@characteristic.parentId()) + @characteristic = Internal.debt.characteristicById(characteristic_id) + @root_characteristic = Internal.debt.characteristicById(@characteristic.parentId()) end render :partial => 'issue/rule' end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb index 15493435126..ae569edb0c1 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb @@ -55,7 +55,7 @@ class Internal end def self.debt - component(Java::OrgSonarServerDebt::DebtService.java_class) + component(Java::OrgSonarServerDebt::DebtModelService.java_class) end def self.profiling diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java new file mode 100644 index 00000000000..8a59e05b387 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java @@ -0,0 +1,88 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.debt; + +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.server.debt.DebtCharacteristic; +import org.sonar.core.technicaldebt.db.CharacteristicDao; +import org.sonar.core.technicaldebt.db.CharacteristicDto; + +import static com.google.common.collect.Lists.newArrayList; +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class DebtModelServiceTest { + + @Mock + CharacteristicDao dao; + + DebtModelService service; + + @Before + public void setUp() throws Exception { + service = new DebtModelService(dao); + } + + @Test + public void find_root_characteristics() { + CharacteristicDto dto = new CharacteristicDto() + .setId(1) + .setKey("MEMORY_EFFICIENCY") + .setName("Memory use"); + when(dao.selectEnabledRootCharacteristics()).thenReturn(newArrayList(dto)); + assertThat(service.rootCharacteristics()).hasSize(1); + } + + @Test + public void find_characteristics() { + CharacteristicDto dto = new CharacteristicDto() + .setId(1) + .setKey("MEMORY_EFFICIENCY") + .setName("Memory use"); + when(dao.selectEnabledCharacteristics()).thenReturn(newArrayList(dto)); + assertThat(service.characteristics()).hasSize(1); + } + + @Test + public void find_characteristic_by_id() { + CharacteristicDto dto = new CharacteristicDto() + .setId(1) + .setKey("MEMORY_EFFICIENCY") + .setName("Memory use") + .setParentId(2) + .setOrder(1); + when(dao.selectById(1)).thenReturn(dto); + + DebtCharacteristic characteristic = service.characteristicById(1); + assertThat(characteristic.id()).isEqualTo(1); + assertThat(characteristic.key()).isEqualTo("MEMORY_EFFICIENCY"); + assertThat(characteristic.name()).isEqualTo("Memory use"); + assertThat(characteristic.parentId()).isEqualTo(2); + assertThat(characteristic.order()).isEqualTo(1); + + assertThat(service.characteristicById(10)).isNull(); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtServiceTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtServiceTest.java deleted file mode 100644 index d9c3070b051..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtServiceTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.debt; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.technicaldebt.server.Characteristic; -import org.sonar.api.technicaldebt.server.internal.DefaultCharacteristic; -import org.sonar.core.technicaldebt.DefaultTechnicalDebtManager; - -import java.util.List; - -import static com.google.common.collect.Lists.newArrayList; -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.*; - -public class DebtServiceTest { - - DefaultTechnicalDebtManager finder = mock(DefaultTechnicalDebtManager.class); - - DebtService service; - - @Before - public void setUp() throws Exception { - service = new DebtService(finder); - } - - @Test - public void find_root_characteristics() { - List rootCharacteristics = newArrayList(); - when(finder.findRootCharacteristics()).thenReturn(rootCharacteristics); - assertThat(service.findRootCharacteristics()).isEqualTo(rootCharacteristics); - } - - @Test - public void find_requirement_by_rule_id() { - service.findRequirementByRuleId(1); - verify(finder).findRequirementByRuleId(1); - } - - @Test - public void find_characteristic() { - Characteristic characteristic = new DefaultCharacteristic(); - when(finder.findCharacteristicById(1)).thenReturn(characteristic); - assertThat(service.findCharacteristic(1)).isEqualTo(characteristic); - } - -} 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 b27ded9403b..1624b8704f0 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 @@ -37,8 +37,9 @@ import org.sonar.api.issue.internal.DefaultIssueComment; import org.sonar.api.issue.internal.FieldDiffs; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.Rule; +import org.sonar.api.server.debt.DebtModel; +import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic; import org.sonar.api.server.ws.WsTester; -import org.sonar.api.technicaldebt.server.internal.DefaultCharacteristic; import org.sonar.api.user.User; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.Duration; @@ -48,7 +49,6 @@ import org.sonar.core.component.ComponentDto; import org.sonar.core.issue.DefaultActionPlan; import org.sonar.core.issue.DefaultIssueQueryResult; import org.sonar.core.issue.workflow.Transition; -import org.sonar.core.technicaldebt.DefaultTechnicalDebtManager; import org.sonar.core.user.DefaultUser; import org.sonar.server.issue.ActionService; import org.sonar.server.issue.IssueChangelog; @@ -84,7 +84,7 @@ public class IssueShowWsHandlerTest { ActionService actionService; @Mock - DefaultTechnicalDebtManager technicalDebtManager; + DebtModel debtModel; @Mock I18n i18n; @@ -113,7 +113,7 @@ public class IssueShowWsHandlerTest { when(i18n.message(any(Locale.class), eq("created"), eq((String) null))).thenReturn("Created"); - tester = new WsTester(new IssuesWs(new IssueShowWsHandler(issueFinder, issueService, issueChangelogService, actionService, technicalDebtManager, i18n, durations))); + tester = new WsTester(new IssuesWs(new IssueShowWsHandler(issueFinder, issueService, issueChangelogService, actionService, debtModel, i18n, durations))); } @Test @@ -323,8 +323,8 @@ public class IssueShowWsHandlerTest { issues.add(issue); result.rule(issue).setCharacteristicId(2); - when(technicalDebtManager.findCharacteristicById(1)).thenReturn(new DefaultCharacteristic().setId(1).setName("Maintainability")); - when(technicalDebtManager.findCharacteristicById(2)).thenReturn(new DefaultCharacteristic().setId(2).setName("Readability").setParentId(1)); + when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setId(1).setName("Maintainability")); + when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setId(2).setName("Readability").setParentId(1)); MockUserSession.set(); WsTester.TestRequest request = tester.newRequest("show").setParam("key", issue.key()); @@ -337,8 +337,8 @@ public class IssueShowWsHandlerTest { issues.add(issue); result.rule(issue).setDefaultCharacteristicId(2); - when(technicalDebtManager.findCharacteristicById(1)).thenReturn(new DefaultCharacteristic().setId(1).setName("Maintainability")); - when(technicalDebtManager.findCharacteristicById(2)).thenReturn(new DefaultCharacteristic().setId(2).setName("Readability").setParentId(1)); + when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setId(1).setName("Maintainability")); + when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setId(2).setName("Readability").setParentId(1)); MockUserSession.set(); WsTester.TestRequest request = tester.newRequest("show").setParam("key", issue.key()); @@ -351,12 +351,12 @@ public class IssueShowWsHandlerTest { issues.add(issue); result.rule(issue).setCharacteristicId(2); - when(technicalDebtManager.findCharacteristicById(1)).thenReturn(new DefaultCharacteristic().setId(1).setName("Maintainability")); - when(technicalDebtManager.findCharacteristicById(2)).thenReturn(new DefaultCharacteristic().setId(2).setName("Readability").setParentId(1)); + when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setId(1).setName("Maintainability")); + when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setId(2).setName("Readability").setParentId(1)); result.rule(issue).setDefaultCharacteristicId(20); - when(technicalDebtManager.findCharacteristicById(10)).thenReturn(new DefaultCharacteristic().setId(10).setName("Default Maintainability")); - when(technicalDebtManager.findCharacteristicById(20)).thenReturn(new DefaultCharacteristic().setId(20).setName("Default Readability").setParentId(10)); + when(debtModel.characteristicById(10)).thenReturn(new DefaultDebtCharacteristic().setId(10).setName("Default Maintainability")); + when(debtModel.characteristicById(20)).thenReturn(new DefaultDebtCharacteristic().setId(20).setName("Default Readability").setParentId(10)); MockUserSession.set(); WsTester.TestRequest request = tester.newRequest("show").setParam("key", issue.key()); -- 2.39.5