From 2c4f57d9c64d8ddd614a2d171cf7807f41471252 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Tue, 20 May 2014 15:55:16 +0200 Subject: [PATCH] SONAR-5007 - Updated WS App for deprecated QProfiles --- .../server/platform/ServerComponents.java | 4 +- .../qualityprofile/QualityProfileService.java | 43 +++++++++++++++++++ .../org/sonar/server/rule2/ws/AppAction.java | 16 ++++--- .../sonar/server/rule2/ws/AppActionTest.java | 14 +++--- 4 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/qualityprofile/QualityProfileService.java 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 9013b1a447c..6173d0bee2c 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 @@ -295,7 +295,9 @@ class ServerComponents { pico.addComponent(ProfilesManager.class, false); pico.addSingleton(AnnotationProfileParser.class); pico.addSingleton(QProfileRuleLookup.class); - pico.addSingleton(QProfiles.class); + + pico.addSingleton(QualityProfileService.class); + //pico.addSingleton(QProfiles.class); pico.addSingleton(QProfileLookup.class); pico.addSingleton(QProfileOperations.class); pico.addSingleton(QProfileActiveRuleOperations.class); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QualityProfileService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QualityProfileService.java new file mode 100644 index 00000000000..c4c470c340d --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QualityProfileService.java @@ -0,0 +1,43 @@ +/* + * 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.qualityprofile; + +import org.sonar.api.ServerComponent; +import org.sonar.core.qualityprofile.db.QualityProfileDao; +import org.sonar.core.qualityprofile.db.QualityProfileDto; +import org.sonar.server.db.DbClient; + +import java.util.List; + +/** + * @since 4.4 + */ +public class QualityProfileService implements ServerComponent { + + private final DbClient dbClient; + + public QualityProfileService(DbClient dbClient){ + this.dbClient = dbClient; + } + + public List findAll() { + return dbClient.getDao(QualityProfileDao.class).selectAll(); + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ws/AppAction.java b/sonar-server/src/main/java/org/sonar/server/rule2/ws/AppAction.java index cfdbb5148a3..aa23f71bdcc 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/ws/AppAction.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ws/AppAction.java @@ -32,8 +32,10 @@ import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.server.qualityprofile.QProfile; import org.sonar.server.qualityprofile.QProfiles; +import org.sonar.server.qualityprofile.QualityProfileService; import org.sonar.server.rule.RuleRepositories; import org.sonar.server.rule.RuleRepositories.Repository; @@ -136,14 +138,14 @@ public class AppAction implements RequestHandler { private final RuleRepositories ruleRepositories; private final I18n i18n; private final DebtModel debtModel; - private final QProfiles qProfiles; + private final QualityProfileService qualityProfileService; - public AppAction(Languages languages, RuleRepositories ruleRepositories, I18n i18n, DebtModel debtModel, QProfiles qProfiles) { + public AppAction(Languages languages, RuleRepositories ruleRepositories, I18n i18n, DebtModel debtModel, QualityProfileService qualityProfileService) { this.languages = languages; this.ruleRepositories = ruleRepositories; this.i18n = i18n; this.debtModel = debtModel; - this.qProfiles = qProfiles; + this.qualityProfileService = qualityProfileService; } @Override @@ -161,11 +163,11 @@ public class AppAction implements RequestHandler { private void addProfiles(JsonWriter json) { json.name("qualityprofiles").beginArray(); - for (QProfile profile: qProfiles.allProfiles()) { + for (QualityProfileDto profile: qualityProfileService.findAll()) { json.beginObject() - .prop("name", profile.name()) - .prop("lang", profile.language()) - .prop("parent", profile.parent()) + .prop("name", profile.getName()) + .prop("lang", profile.getLanguage()) + .prop("parent", profile.getParent()) .endObject(); } json.endArray(); diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/ws/AppActionTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/ws/AppActionTest.java index 3a40f2b2a62..ba7f26e0d39 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule2/ws/AppActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule2/ws/AppActionTest.java @@ -32,8 +32,8 @@ import org.sonar.api.resources.Languages; 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.server.qualityprofile.QProfile; -import org.sonar.server.qualityprofile.QProfiles; +import org.sonar.core.qualityprofile.db.QualityProfileDto; +import org.sonar.server.qualityprofile.QualityProfileService; import org.sonar.server.rule.RuleRepositories; import org.sonar.server.ws.WsTester; @@ -60,18 +60,18 @@ public class AppActionTest { DebtModel debtModel; @Mock - QProfiles qProfiles; + QualityProfileService qualityProfileService; @Test public void should_generate_app_init_info() throws Exception { - AppAction app = new AppAction(languages, ruleRepositories, i18n, debtModel, qProfiles); + AppAction app = new AppAction(languages, ruleRepositories, i18n, debtModel, qualityProfileService); WsTester tester = new WsTester(new RulesWebService( mock(SearchAction.class), mock(ShowAction.class), mock(TagsAction.class), mock(SetTagsAction.class), mock(SetNoteAction.class), app)); - QProfile profile1 = new QProfile().setName("Profile One").setLanguage("bf"); - QProfile profile2 = new QProfile().setName("Profile Two").setLanguage("bf").setParent("Profile One"); - when(qProfiles.allProfiles()).thenReturn(ImmutableList.of(profile1, profile2)); + QualityProfileDto profile1 = QualityProfileDto.createFor("Profile One","bf"); + QualityProfileDto profile2 = QualityProfileDto.createFor("Profile Two","bf").setParent("Profile One"); + when(qualityProfileService.findAll()).thenReturn(ImmutableList.of(profile1, profile2)); Language brainfsck = mock(Language.class); when(brainfsck.getKey()).thenReturn("bf"); -- 2.39.5