]> source.dussan.org Git - sonarqube.git/commitdiff
Remove unused class BuiltInProfiles
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 14 Sep 2016 08:38:21 +0000 (10:38 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 14 Sep 2016 09:36:50 +0000 (11:36 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInProfiles.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInProfilesTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInActionTest.java

index 7ada98d442660b24fafe3065d0e7fc7fee53f101..cd45ee8682a28898a6ef83f8f92ac77569293bf7 100644 (file)
@@ -112,7 +112,6 @@ import org.sonar.server.plugins.ServerExtensionInstaller;
 import org.sonar.server.plugins.privileged.PrivilegedPluginsBootstraper;
 import org.sonar.server.plugins.privileged.PrivilegedPluginsStopper;
 import org.sonar.server.property.InternalPropertiesImpl;
-import org.sonar.server.qualityprofile.BuiltInProfiles;
 import org.sonar.server.qualityprofile.QProfileComparison;
 import org.sonar.server.qualityprofile.QProfileLookup;
 import org.sonar.server.qualityprofile.QProfileProjectLookup;
@@ -293,8 +292,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer {
       QProfileProjectOperations.class,
       QProfileProjectLookup.class,
       QProfileComparison.class,
-      BuiltInProfiles.class,
-
+      
       // rule
       RuleIndexer.class,
       AnnotationRuleParser.class,
index a8fe38c29a556847cc44a124e51b1892d3e75918..539cea63231cee744369259d426dec0bce173d59 100644 (file)
@@ -88,7 +88,7 @@ public class ComputeEngineContainerImplTest {
     assertThat(picoContainer.getComponentAdapters())
       .hasSize(
         CONTAINER_ITSELF
-          + 77 // level 4
+          + 76 // level 4
           + 4 // content of CeConfigurationModule
           + 3 // content of CeHttpModule
           + 5 // content of CeQueueModule
index 392a091ed085960afd15b97a094a5e32a39263a9..6c0e078a468cd7aef1ffdb2a4235bff81e91f0d0 100644 (file)
@@ -178,7 +178,6 @@ import org.sonar.server.project.ws.ProjectsWsModule;
 import org.sonar.server.projectlink.ws.ProjectLinksModule;
 import org.sonar.server.property.InternalPropertiesImpl;
 import org.sonar.server.qualitygate.QualityGateModule;
-import org.sonar.server.qualityprofile.BuiltInProfiles;
 import org.sonar.server.qualityprofile.QProfileBackuper;
 import org.sonar.server.qualityprofile.QProfileComparison;
 import org.sonar.server.qualityprofile.QProfileCopier;
@@ -336,7 +335,6 @@ public class PlatformLevel4 extends PlatformLevel {
       QProfileProjectOperations.class,
       QProfileProjectLookup.class,
       QProfileComparison.class,
-      BuiltInProfiles.class,
       SearchDataLoader.class,
       ProfilesWs.class,
       OldRestoreAction.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInProfiles.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInProfiles.java
deleted file mode 100644 (file)
index f8b265a..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.qualityprofile;
-
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
-import java.util.Collection;
-import org.sonar.api.ce.ComputeEngineSide;
-import org.sonar.api.server.ServerSide;
-
-/**
- * Used to list default profile names.
- *
- * It should be removed as soon as a new API to define quality profiles is created. Currently loading all definitions
- * just to get the profile names is too slow (see {@link org.sonar.api.profiles.ProfileDefinition}).
- */
-@ServerSide
-@ComputeEngineSide
-public class BuiltInProfiles {
-
-  // built-in profile names grouped by language
-  private final Multimap<String, String> namesByLang;
-
-  public BuiltInProfiles() {
-    this.namesByLang = ArrayListMultimap.create();
-  }
-
-  Collection<String> byLanguage(String language) {
-    return namesByLang.get(language);
-  }
-
-  BuiltInProfiles put(String language, String name) {
-    namesByLang.put(language, name);
-    return this;
-  }
-
-}
index 5503ea0f5a2e4eee034d2a69a6422feb5531855d..d6c9fbdccc843f090206892f44b416dabeb821de 100644 (file)
@@ -52,32 +52,26 @@ public class QProfileReset {
   private final QProfileFactory factory;
   private final RuleActivator activator;
   private final ActiveRuleIndexer activeRuleIndexer;
-  private final BuiltInProfiles builtInProfiles;
   private final ProfileDefinition[] definitions;
 
-  public QProfileReset(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, BuiltInProfiles builtInProfiles, QProfileFactory factory,
+  public QProfileReset(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, QProfileFactory factory,
     ProfileDefinition[] definitions) {
     this.db = db;
     this.activator = activator;
     this.activeRuleIndexer = activeRuleIndexer;
-    this.builtInProfiles = builtInProfiles;
     this.factory = factory;
     this.definitions = definitions;
   }
 
-  public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ActiveRuleIndexer activeRuleIndexer) {
-    this(db, activator, activeRuleIndexer, builtInProfiles, factory, new ProfileDefinition[0]);
-  }
-
-  public Collection<String> builtInProfileNamesForLanguage(String language) {
-    return builtInProfiles.byLanguage(language);
+  public QProfileReset(DbClient db, RuleActivator activator, QProfileFactory factory, ActiveRuleIndexer activeRuleIndexer) {
+    this(db, activator, activeRuleIndexer, factory, new ProfileDefinition[0]);
   }
 
   /**
    * Reset built-in profiles for the given language. Missing profiles are created and
    * existing ones are updated.
    */
-  void resetLanguage(String language) {
+  public void resetLanguage(String language) {
     DbSession dbSession = db.openSession(false);
     try {
       ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language);
index 14bb7914bb6e020591bd0695d46aebce9c01f893..991ae55a8819742959ffc3c50539c3b3a6616916 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.server.qualityprofile;
 
-import java.io.Reader;
-import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.List;
@@ -44,17 +42,15 @@ public class QProfileService {
   private final RuleActivator ruleActivator;
   private final QProfileFactory factory;
   private final QProfileBackuper backuper;
-  private final QProfileReset reset;
   private final UserSession userSession;
 
   public QProfileService(DbClient db, ActiveRuleIndexer activeRuleIndexer, RuleActivator ruleActivator, QProfileFactory factory,
-    QProfileBackuper backuper, QProfileReset reset, UserSession userSession) {
+    QProfileBackuper backuper, UserSession userSession) {
     this.db = db;
     this.activeRuleIndexer = activeRuleIndexer;
     this.ruleActivator = ruleActivator;
     this.factory = factory;
     this.backuper = backuper;
-    this.reset = reset;
     this.userSession = userSession;
   }
 
@@ -109,49 +105,6 @@ public class QProfileService {
     return output.toString();
   }
 
-  public void restore(Reader backup) {
-    verifyAdminPermission();
-    backuper.restore(backup, null);
-  }
-
-  /**
-   * @deprecated used only by Ruby on Rails. Use {@link #restore(java.io.Reader)}
-   */
-  @Deprecated
-  public void restore(String backup) {
-    restore(new StringReader(backup));
-  }
-
-  public void restoreBuiltInProfilesForLanguage(String lang) {
-    verifyAdminPermission();
-    reset.resetLanguage(lang);
-  }
-
-  public void delete(String key) {
-    verifyAdminPermission();
-    DbSession session = db.openSession(false);
-    try {
-      List<ActiveRuleChange> changes = factory.delete(session, key, false);
-      session.commit();
-      activeRuleIndexer.index(changes);
-    } finally {
-      db.closeSession(session);
-    }
-  }
-
-  public void rename(String key, String newName) {
-    verifyAdminPermission();
-    factory.rename(key, newName);
-  }
-
-  /**
-   * Set the given quality profile as default for the related language
-   */
-  public void setDefault(String key) {
-    verifyAdminPermission();
-    factory.setDefault(key);
-  }
-
   /**
    * Used in /api/profiles and in /profiles/export
    */
index dae95a37cd1886fe25be0be6a57eebcab06dabfc..38c8d8c21f0e1483c082699c89b4df6e253e3468 100644 (file)
@@ -58,7 +58,6 @@ public class RegisterQualityProfiles {
   private static final String DEFAULT_PROFILE_NAME = "Sonar way";
 
   private final List<ProfileDefinition> definitions;
-  private final BuiltInProfiles builtInProfiles;
   private final DbClient dbClient;
   private final QProfileFactory profileFactory;
   private final RuleActivator ruleActivator;
@@ -68,15 +67,14 @@ public class RegisterQualityProfiles {
   /**
    * To be kept when no ProfileDefinition are injected
    */
-  public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
-    DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
-    this(builtInProfiles, dbClient, profileFactory, ruleActivator, Collections.<ProfileDefinition>emptyList(), languages, activeRuleIndexer);
+  public RegisterQualityProfiles(DbClient dbClient,
+    QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
+    this(dbClient, profileFactory, ruleActivator, Collections.emptyList(), languages, activeRuleIndexer);
   }
 
-  public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
-    DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator,
+  public RegisterQualityProfiles(DbClient dbClient,
+    QProfileFactory profileFactory, RuleActivator ruleActivator,
     List<ProfileDefinition> definitions, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
-    this.builtInProfiles = builtInProfiles;
     this.dbClient = dbClient;
     this.profileFactory = profileFactory;
     this.ruleActivator = ruleActivator;
@@ -129,7 +127,6 @@ public class RegisterQualityProfiles {
       if (shouldRegister(profileName, session)) {
         changes.addAll(register(profileName, entry.getValue(), session));
       }
-      builtInProfiles.put(language, name);
     }
     setDefault(language, defs, session);
     session.commit();
index f6873d5ce5657ef9bf6507d0419fb0d4221937c8..61ebd6686bdcb917bfaf1015532c2d916277430b 100644 (file)
@@ -23,19 +23,23 @@ import org.sonar.api.resources.Languages;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.qualityprofile.QProfileReset;
+import org.sonar.server.user.UserSession;
 
 import static org.sonar.server.util.LanguageParamUtils.getExampleValue;
 import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys;
 
 public class RestoreBuiltInAction implements QProfileWsAction {
 
-  private final QProfileService service;
+  private final QProfileReset reset;
   private final Languages languages;
+  private final UserSession userSession;
 
-  public RestoreBuiltInAction(QProfileService service, Languages languages) {
-    this.service = service;
+  public RestoreBuiltInAction(QProfileReset reset, Languages languages, UserSession userSession) {
+    this.reset = reset;
     this.languages = languages;
+    this.userSession = userSession;
   }
 
   @Override
@@ -55,9 +59,15 @@ public class RestoreBuiltInAction implements QProfileWsAction {
 
   @Override
   public void handle(Request request, Response response) {
+    verifyAdminPermission();
+
     String language = request.mandatoryParam("language");
-    service.restoreBuiltInProfilesForLanguage(language);
+    reset.resetLanguage(language);
     response.noContent();
   }
 
+  private void verifyAdminPermission() {
+    userSession.checkLoggedIn();
+    userSession.checkPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+  }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInProfilesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInProfilesTest.java
deleted file mode 100644 (file)
index 9014368..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.qualityprofile;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-
-public class BuiltInProfilesTest {
-
-  BuiltInProfiles cache;
-
-  @Before
-  public void setUp() {
-    cache = new BuiltInProfiles();
-  }
-
-  @Test
-  public void add_profiles() {
-    cache.put("java", "Default");
-    cache.put("java", "Sonar Way");
-    cache.put("js", "Default");
-
-    assertThat(cache.byLanguage("java")).containsOnly("Default", "Sonar Way");
-    assertThat(cache.byLanguage("js")).containsOnly("Default");
-  }
-
-  @Test
-  public void not_add_same_profile_name() {
-    cache.put("java", "Default");
-    cache.put("java", "Default");
-
-    assertThat(cache.byLanguage("java")).containsOnly("Default");
-  }
-}
index 0ff190f5a8aafec8a1c8f485d7f3f9e442ac02a6..1591fea91ac2a94430888d6cf7d42f8240e28f95 100644 (file)
@@ -68,7 +68,7 @@ public class QProfilesWsTest {
       new RemoveProjectAction(projectAssociationParameters, null, null, null),
       new CreateAction(null, null, null, languages, importers, userSessionRule, null),
       new ImportersAction(importers),
-      new RestoreBuiltInAction(null, languages),
+      new RestoreBuiltInAction(null, languages, userSessionRule),
       new SearchAction(null, languages),
       new SetDefaultAction(languages, null, null, userSessionRule),
       new ProjectsAction(null, userSessionRule),
index d4cd38c53cd308fba4008342841695345a691bc8..7112747cabde571ba2d5a9f3781d9a57ee7ea76b 100644 (file)
@@ -23,36 +23,50 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.resources.Languages;
+import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.language.LanguageTesting;
-import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.server.qualityprofile.QProfileReset;
+import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 
 import static org.assertj.core.api.Java6Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
 
 public class RestoreBuiltInActionTest {
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  QProfileService profileService = mock(QProfileService.class);
-  Languages languages = LanguageTesting.newLanguages("xoo");
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+
+  private QProfileReset reset = mock(QProfileReset.class);
+  private Languages languages = LanguageTesting.newLanguages("xoo");
 
-  WsActionTester tester = new WsActionTester(new RestoreBuiltInAction(profileService, languages));
+  private WsActionTester tester = new WsActionTester(new RestoreBuiltInAction(reset, languages, userSession));
 
   @Test
   public void return_empty_result_when_no_info_or_warning() {
+    userSession.login("himself").setGlobalPermissions(QUALITY_PROFILE_ADMIN);
     TestResponse response = tester.newRequest().setParam("language", "xoo").execute();
 
-    verify(profileService).restoreBuiltInProfilesForLanguage("xoo");
+    verify(reset).resetLanguage("xoo");
     assertThat(response.getStatus()).isEqualTo(204);
   }
 
   @Test
   public void fail_on_unknown_language() throws Exception {
+    userSession.login("himself").setGlobalPermissions(QUALITY_PROFILE_ADMIN);
     expectedException.expect(IllegalArgumentException.class);
     tester.newRequest().setParam("language", "unknown").execute();
   }
+
+  @Test
+  public void fail_if_not_profile_administrator() throws Exception {
+    expectedException.expect(UnauthorizedException.class);
+    tester.newRequest().setParam("language", "xoo").execute();
+  }
 }