]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8857 make ws/qualityprofiles/inheritance organization aware
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Thu, 16 Mar 2017 15:33:32 +0000 (16:33 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:38:34 +0000 (17:38 +0100)
19 files changed:
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfile.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/InheritanceAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchData.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchDataLoader.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchDataLoaderTest.java
server/sonar-server/src/test/java/org/sonar/server/tester/UserSessionRule.java

index b9729f7476321362d04d12e3c3b2f7f1164c185f..7af73c840798ac6ff0fcc3e79a2fa8ccb53b61c2 100644 (file)
@@ -120,8 +120,8 @@ public class QualityProfileDao implements Dao {
     return executeLargeInputs(languageKeys, input -> mapper(session).selectByProjectAndLanguages(organization.getUuid(), projectKey, input));
   }
 
-  public List<QualityProfileDto> selectByLanguage(DbSession dbSession, String language) {
-    return mapper(dbSession).selectByLanguage(language);
+  public List<QualityProfileDto> selectByLanguage(DbSession dbSession, OrganizationDto organization, String language) {
+    return mapper(dbSession).selectByLanguage(organization.getUuid(), language);
   }
 
   @CheckForNull
index dae2b3ac66d86f42fbab995733d46f0e2eb0be20..520ae8db2d9676f7af4b00a45c3d078380eca68d 100644 (file)
@@ -50,7 +50,7 @@ public interface QualityProfileMapper {
   @CheckForNull
   QualityProfileDto selectByKey(String key);
 
-  List<QualityProfileDto> selectByLanguage(String language);
+  List<QualityProfileDto> selectByLanguage(@Param("organizationUuid") String organizationUuid, @Param("language") String language);
 
   List<QualityProfileDto> selectByKeys(@Param("keys") List<String> keys);
 
index eab697fe65c8089319603f93ad248d98b74e7cad..d3da8b3b4d23f07943822c5a512a9d95d6c99dd3 100644 (file)
     <include refid="profilesColumns"/>
     FROM rules_profiles p
     WHERE p.language=#{language}
+    AND p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
     ORDER BY p.name
   </select>
 
index 2b4882839740d9fc7847b2355987a39b445f8f25..98cee8ab22b5144b3c65483cc24ef23b6db25d98 100644 (file)
@@ -191,13 +191,40 @@ public class QualityProfileDaoTest {
   }
 
   @Test
-  public void find_by_language() {
-    dbTester.prepareDbUnit(getClass(), "select_by_language.xml");
+  public void should_find_by_language() {
+    QualityProfileDto profile = QualityProfileTesting.newQualityProfileDto()
+      .setOrganizationUuid(organization.getUuid());
+    underTest.insert(dbSession, profile);
+
+    List<QualityProfileDto> results = underTest.selectByLanguage(dbSession, organization, profile.getLanguage());
+    assertThat(results).hasSize(1);
+    QualityProfileDto result = results.get(0);
+
+    assertThat(result.getId()).isEqualTo(profile.getId());
+    assertThat(result.getName()).isEqualTo(profile.getName());
+    assertThat(result.getKey()).isEqualTo(profile.getKey());
+    assertThat(result.getLanguage()).isEqualTo(profile.getLanguage());
+    assertThat(result.getOrganizationUuid()).isEqualTo(profile.getOrganizationUuid());
+  }
+
+  @Test
+  public void should_not_find_by_language_in_wrong_organization() {
+    QualityProfileDto profile = QualityProfileTesting.newQualityProfileDto()
+      .setOrganizationUuid(organization.getUuid());
+    underTest.insert(dbSession, profile);
+
+    List<QualityProfileDto> results = underTest.selectByLanguage(dbSession, OrganizationTesting.newOrganizationDto(), profile.getLanguage());
+    assertThat(results).isEmpty();
+  }
+
+  @Test
+  public void should_not_find_by_language_with_wrong_language() {
+    QualityProfileDto profile = QualityProfileTesting.newQualityProfileDto()
+      .setOrganizationUuid(organization.getUuid());
+    underTest.insert(dbSession, profile);
 
-    List<QualityProfileDto> result = underTest.selectByLanguage(dbTester.getSession(), "java");
-    assertThat(result).hasSize(2);
-    assertThat(result.get(0).getName()).isEqualTo("Sonar Way 1");
-    assertThat(result.get(1).getName()).isEqualTo("Sonar Way 2");
+    List<QualityProfileDto> results = underTest.selectByLanguage(dbSession, organization, "another language");
+    assertThat(results).isEmpty();
   }
 
   @Test
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfile.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfile.java
deleted file mode 100644 (file)
index 85668c9..0000000
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info 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 javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.apache.commons.lang.builder.ReflectionToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualityprofile.QualityProfileDto;
-
-public class QProfile {
-
-  private int id;
-  private OrganizationDto organization;
-  private String key;
-  private String name;
-  private String language;
-  private String parent;
-  private boolean isDefault;
-  private String rulesUpdatedAt;
-  private Long lastUsed;
-  private Long userUpdatedAt;
-
-  /**
-   * @deprecated in 4.4
-   */
-  @Deprecated
-  public int id() {
-    return id;
-  }
-
-  /**
-   * @deprecated in 4.4
-   */
-  @Deprecated
-  QProfile setId(int id) {
-    this.id = id;
-    return this;
-  }
-
-  public OrganizationDto organization() {
-    return organization;
-  }
-
-  public QProfile setOrganization(OrganizationDto organization) {
-    this.organization = organization;
-    return this;
-  }
-
-  public String name() {
-    return name;
-  }
-
-  public QProfile setName(String name) {
-    this.name = name;
-    return this;
-  }
-
-  public String language() {
-    return language;
-  }
-
-  public QProfile setLanguage(String language) {
-    this.language = language;
-    return this;
-  }
-
-  public String key() {
-    return key;
-  }
-
-  public QProfile setKey(String s) {
-    this.key = s;
-    return this;
-  }
-
-  @CheckForNull
-  public String parent() {
-    return parent;
-  }
-
-  public QProfile setParent(@Nullable String parent) {
-    this.parent = parent;
-    return this;
-  }
-
-  public boolean isInherited() {
-    return parent != null;
-  }
-
-  public boolean isDefault() {
-    return isDefault;
-  }
-
-  public QProfile setDefault(boolean isDefault) {
-    this.isDefault = isDefault;
-    return this;
-  }
-
-  public String getRulesUpdatedAt() {
-    return rulesUpdatedAt;
-  }
-
-  public QProfile setRulesUpdatedAt(String rulesUpdatedAt) {
-    this.rulesUpdatedAt = rulesUpdatedAt;
-    return this;
-  }
-
-  @CheckForNull
-  public Long getLastUsed() {
-    return lastUsed;
-  }
-
-  public QProfile setLastUsed(@Nullable Long lastUsed) {
-    this.lastUsed = lastUsed;
-    return this;
-  }
-
-  @CheckForNull
-  public Long getUserUpdatedAt() {
-    return userUpdatedAt;
-  }
-
-  public QProfile setUserUpdatedAt(@Nullable Long userUpdatedAt) {
-    this.userUpdatedAt = userUpdatedAt;
-    return this;
-  }
-
-  public static QProfile from(QualityProfileDto dto, OrganizationDto organization) {
-    return new QProfile()
-      .setId(dto.getId())
-      .setOrganization(organization)
-      .setKey(dto.getKey())
-      .setName(dto.getName())
-      .setLanguage(dto.getLanguage())
-      .setParent(dto.getParentKee())
-      .setDefault(dto.isDefault())
-      .setRulesUpdatedAt(dto.getRulesUpdatedAt())
-      .setLastUsed(dto.getLastUsed())
-      .setUserUpdatedAt(dto.getUserUpdatedAt());
-  }
-
-  @Override
-  public String toString() {
-    return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
-  }
-}
index 2a10697aa5b858db218d5ac94964b20820bd2e09..6f3c333e066b2f5633115c47e9e93643fa58b996 100644 (file)
@@ -27,7 +27,6 @@ import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 
 import static com.google.common.collect.Lists.newArrayList;
-import static org.sonar.core.util.stream.Collectors.toList;
 
 @ServerSide
 public class QProfileLookup {
@@ -38,34 +37,29 @@ public class QProfileLookup {
     this.db = db;
   }
 
-  public List<QProfile> allProfiles(DbSession dbSession, OrganizationDto organization) {
-    return toQProfiles(db.qualityProfileDao().selectAll(dbSession, organization), organization);
+  public List<QualityProfileDto> allProfiles(DbSession dbSession, OrganizationDto organization) {
+    return db.qualityProfileDao().selectAll(dbSession, organization);
   }
 
-  public List<QProfile> profiles(DbSession dbSession, String language, OrganizationDto organization) {
-    return toQProfiles(db.qualityProfileDao().selectByLanguage(dbSession, language), organization);
+  public List<QualityProfileDto> profiles(DbSession dbSession, String language, OrganizationDto organization) {
+    return db.qualityProfileDao().selectByLanguage(dbSession, organization, language);
   }
 
-  public List<QProfile> ancestors(QualityProfileDto profile, DbSession session) {
-    List<QProfile> ancestors = newArrayList();
-    incrementAncestors(QProfile.from(profile, null), ancestors, session);
+  public List<QualityProfileDto> ancestors(QualityProfileDto profile, DbSession session) {
+    List<QualityProfileDto> ancestors = newArrayList();
+    incrementAncestors(profile, ancestors, session);
     return ancestors;
   }
 
-  private void incrementAncestors(QProfile profile, List<QProfile> ancestors, DbSession session) {
-    if (profile.parent() != null) {
-      QualityProfileDto parentDto = db.qualityProfileDao().selectParentById(session, profile.id());
+  private void incrementAncestors(QualityProfileDto profile, List<QualityProfileDto> ancestors, DbSession session) {
+    if (profile.getParentKee() != null) {
+      QualityProfileDto parentDto = db.qualityProfileDao().selectParentById(session, profile.getId());
       if (parentDto == null) {
-        throw new IllegalStateException("Cannot find parent of profile : " + profile.id());
+        throw new IllegalStateException("Cannot find parent of profile : " + profile.getId());
       }
-      QProfile parent = QProfile.from(parentDto, null);
-      ancestors.add(parent);
-      incrementAncestors(parent, ancestors, session);
+      ancestors.add(parentDto);
+      incrementAncestors(parentDto, ancestors, session);
     }
   }
 
-  private static List<QProfile> toQProfiles(List<QualityProfileDto> dtos, OrganizationDto organization) {
-    return dtos.stream().map(dto -> QProfile.from(dto, organization)).collect(toList(dtos.size()));
-  }
-
 }
index a82c3b0956b9d9e9d2df21bf1dd52eaee1b971ed..c0a9849cd0f53fa0cb2f78afe7e7c734eae4fca1 100644 (file)
@@ -87,6 +87,7 @@ public class QProfileService {
   }
 
   private void verifyAdminPermission() {
+    // FIXME check for the permission of the appropriate organization, not the default one
     userSession
       .checkLoggedIn()
       .checkPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganizationProvider.get().getUuid());
index 94454baebf38f14a12c169b987e6229f8e402b7f..fabf8f240661993ab41a16b5f10a7c1ca2c00eb7 100644 (file)
@@ -133,7 +133,7 @@ public class CreateAction implements QProfileWsAction {
         result.add(exporters.importXml(profile, importerKey, contentToImport, dbSession));
       }
     }
-    String organizationKey = qProfileWsSupport.getOrganizationKey(result.profile(), dbSession);
+    String organizationKey = qProfileWsSupport.getOrganizationKey(dbSession, result.profile());
     dbSession.commit();
     activeRuleIndexer.index(result.getChanges());
     return buildResponse(result, organizationKey);
index 8ea22e62acfd731b96f65f08c3ad6feeb5d118c0..5f25333ee859b44617cc281a7fe68a79064c9b07 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile.ws;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Multimap;
 import java.util.List;
 import java.util.Map;
@@ -34,11 +35,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfile;
-import org.sonar.server.qualityprofile.QProfileFactory;
 import org.sonar.server.qualityprofile.QProfileLookup;
-import org.sonar.server.qualityprofile.QProfileRef;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.search.FacetValue;
@@ -50,18 +47,15 @@ public class InheritanceAction implements QProfileWsAction {
   private final DbClient dbClient;
   private final QProfileLookup profileLookup;
   private final ActiveRuleIndex activeRuleIndex;
-  private final QProfileFactory profileFactory;
+  private final QProfileWsSupport wsSupport;
   private final Languages languages;
-  private final DefaultOrganizationProvider defaultOrganizationProvider;
 
-  public InheritanceAction(DbClient dbClient, QProfileLookup profileLookup, ActiveRuleIndex activeRuleIndex, QProfileFactory profileFactory, Languages languages,
-    DefaultOrganizationProvider defaultOrganizationProvider) {
+  public InheritanceAction(DbClient dbClient, QProfileLookup profileLookup, ActiveRuleIndex activeRuleIndex, QProfileWsSupport wsSupport, Languages languages) {
     this.dbClient = dbClient;
     this.profileLookup = profileLookup;
     this.activeRuleIndex = activeRuleIndex;
-    this.profileFactory = profileFactory;
+    this.wsSupport = wsSupport;
     this.languages = languages;
-    this.defaultOrganizationProvider = defaultOrganizationProvider;
   }
 
   @Override
@@ -72,32 +66,34 @@ public class InheritanceAction implements QProfileWsAction {
       .setHandler(this)
       .setResponseExample(getClass().getResource("example-inheritance.json"));
 
-    QProfileRef.defineParams(inheritance, languages);
+    QProfileWsSupport.createOrganizationParam(inheritance)
+      .setSince("6.4");
+    QProfileReference.defineParams(inheritance, languages);
   }
 
   @Override
   public void handle(Request request, Response response) throws Exception {
+    QProfileReference reference = QProfileReference.from(request);
     try (DbSession dbSession = dbClient.openSession(false)) {
-      QualityProfileDto profile = profileFactory.find(dbSession, QProfileRef.from(request));
-      List<QProfile> ancestors = profileLookup.ancestors(profile, dbSession);
+      QualityProfileDto profile = wsSupport.getProfile(dbSession, reference);
+      String organizationUuid = profile.getOrganizationUuid();
+      OrganizationDto organization = dbClient.organizationDao().selectByUuid(dbSession, organizationUuid)
+        .orElseThrow(() -> new IllegalStateException(String.format("Could not find organization with uuid '%s' for quality profile '%s'", organizationUuid, profile.getKee())));
+      List<QualityProfileDto> ancestors = profileLookup.ancestors(profile, dbSession);
       List<QualityProfileDto> children = dbClient.qualityProfileDao().selectChildren(dbSession, profile.getKey());
-      Map<String, Multimap<String, FacetValue>> profileStats = getAllProfileStats();
+      Map<String, Multimap<String, FacetValue>> profileStats = getAllProfileStats(dbSession, organization);
 
       writeResponse(response.newJsonWriter(), profile, ancestors, children, profileStats);
     }
   }
 
-  public Map<String, Multimap<String, FacetValue>> getAllProfileStats() {
-    try (DbSession dbSession = dbClient.openSession(false)) {
-      String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid();
-      OrganizationDto defaultOrganization = dbClient.organizationDao().selectByUuid(dbSession, defaultOrganizationUuid)
-        .orElseThrow(() -> new IllegalStateException(String.format("Cannot find default organization by uuid '%s'", defaultOrganizationUuid)));
-      List<String> keys = dbClient.qualityProfileDao().selectAll(dbSession, defaultOrganization).stream().map(QualityProfileDto::getKey).collect(Collectors.toList());
-      return activeRuleIndex.getStatsByProfileKeys(keys);
-    }
+  @VisibleForTesting
+  Map<String, Multimap<String, FacetValue>> getAllProfileStats(DbSession dbSession, OrganizationDto organization) {
+    List<String> keys = dbClient.qualityProfileDao().selectAll(dbSession, organization).stream().map(QualityProfileDto::getKey).collect(Collectors.toList());
+    return activeRuleIndex.getStatsByProfileKeys(keys);
   }
 
-  private void writeResponse(JsonWriter json, QualityProfileDto profile, List<QProfile> ancestors, List<QualityProfileDto> children,
+  private static void writeResponse(JsonWriter json, QualityProfileDto profile, List<QualityProfileDto> ancestors, List<QualityProfileDto> children,
     Map<String, Multimap<String, FacetValue>> profileStats) {
     json.beginObject();
     writeProfile(json, profile, profileStats);
@@ -106,22 +102,22 @@ public class InheritanceAction implements QProfileWsAction {
     json.endObject().close();
   }
 
-  private void writeProfile(JsonWriter json, QualityProfileDto profile, Map<String, Multimap<String, FacetValue>> profileStats) {
+  private static void writeProfile(JsonWriter json, QualityProfileDto profile, Map<String, Multimap<String, FacetValue>> profileStats) {
     String profileKey = profile.getKey();
     json.name("profile");
     writeProfileAttributes(json, profileKey, profile.getName(), profile.getParentKee(), profileStats);
   }
 
-  private void writeAncestors(JsonWriter json, List<QProfile> ancestors, Map<String, Multimap<String, FacetValue>> profileStats) {
+  private static void writeAncestors(JsonWriter json, List<QualityProfileDto> ancestors, Map<String, Multimap<String, FacetValue>> profileStats) {
     json.name("ancestors").beginArray();
-    for (QProfile ancestor : ancestors) {
-      String ancestorKey = ancestor.key();
-      writeProfileAttributes(json, ancestorKey, ancestor.name(), ancestor.parent(), profileStats);
+    for (QualityProfileDto ancestor : ancestors) {
+      String ancestorKey = ancestor.getKey();
+      writeProfileAttributes(json, ancestorKey, ancestor.getName(), ancestor.getParentKee(), profileStats);
     }
     json.endArray();
   }
 
-  private void writeChildren(JsonWriter json, List<QualityProfileDto> children, Map<String, Multimap<String, FacetValue>> profileStats) {
+  private static void writeChildren(JsonWriter json, List<QualityProfileDto> children, Map<String, Multimap<String, FacetValue>> profileStats) {
     json.name("children").beginArray();
     for (QualityProfileDto child : children) {
       String childKey = child.getKey();
@@ -130,7 +126,7 @@ public class InheritanceAction implements QProfileWsAction {
     json.endArray();
   }
 
-  private void writeProfileAttributes(JsonWriter json, String key, String name, @Nullable String parentKey, Map<String, Multimap<String, FacetValue>> profileStats) {
+  private static void writeProfileAttributes(JsonWriter json, String key, String name, @Nullable String parentKey, Map<String, Multimap<String, FacetValue>> profileStats) {
     json.beginObject();
     json.prop("key", key)
       .prop("name", name)
@@ -139,7 +135,7 @@ public class InheritanceAction implements QProfileWsAction {
     json.endObject();
   }
 
-  private void writeStats(JsonWriter json, String profileKey, Map<String, Multimap<String, FacetValue>> profileStats) {
+  private static void writeStats(JsonWriter json, String profileKey, Map<String, Multimap<String, FacetValue>> profileStats) {
     if (profileStats.containsKey(profileKey)) {
       Multimap<String, FacetValue> ancestorStats = profileStats.get(profileKey);
       json.prop("activeRuleCount", getActiveRuleCount(ancestorStats));
@@ -149,7 +145,7 @@ public class InheritanceAction implements QProfileWsAction {
     }
   }
 
-  private Long getActiveRuleCount(Multimap<String, FacetValue> profileStats) {
+  private static Long getActiveRuleCount(Multimap<String, FacetValue> profileStats) {
     Long result = null;
     if (profileStats.containsKey(COUNT_ACTIVE_RULES)) {
       result = profileStats.get(COUNT_ACTIVE_RULES).iterator().next().getValue();
@@ -157,7 +153,7 @@ public class InheritanceAction implements QProfileWsAction {
     return result;
   }
 
-  private Long getOverridingRuleCount(Multimap<String, FacetValue> profileStats) {
+  private static Long getOverridingRuleCount(Multimap<String, FacetValue> profileStats) {
     Long result = null;
     if (profileStats.containsKey(RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE)) {
       for (FacetValue value : profileStats.get(RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE)) {
index 27b728ea95913725b7c4a38bf2dfa047b9abedbd..0bbad55e5ccd48dcd5586ad7b6613958e3696826 100644 (file)
@@ -49,7 +49,7 @@ public class QProfileWsSupport {
     this.defaultOrganizationProvider = defaultOrganizationProvider;
   }
 
-  public String getOrganizationKey(QualityProfileDto profile, DbSession dbSession) {
+  public String getOrganizationKey(DbSession dbSession, QualityProfileDto profile) {
     String organizationUuid = profile.getOrganizationUuid();
     return dbClient.organizationDao().selectByUuid(dbSession, organizationUuid)
       .orElseThrow(() -> new IllegalStateException("Cannot load organization with uuid=" + organizationUuid))
index 0b5222a85352803bdda26ca9e337c89d7644668c..87517cb2d5933e6b95fef8aa9ee612859f8fb6f2 100644 (file)
@@ -30,8 +30,9 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.server.qualityprofile.QProfile;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.util.LanguageParamUtils;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
@@ -55,12 +56,14 @@ public class SearchAction implements QProfileWsAction {
   private final Languages languages;
   private final ActiveRuleIndex activeRuleIndex;
   private final DbClient dbClient;
+  private final QProfileWsSupport wsSupport;
 
-  public SearchAction(SearchDataLoader dataLoader, Languages languages, ActiveRuleIndex activeRuleIndex, DbClient dbClient) {
+  public SearchAction(SearchDataLoader dataLoader, Languages languages, ActiveRuleIndex activeRuleIndex, DbClient dbClient, QProfileWsSupport wsSupport) {
     this.dataLoader = dataLoader;
     this.languages = languages;
     this.activeRuleIndex = activeRuleIndex;
     this.dbClient = dbClient;
+    this.wsSupport = wsSupport;
   }
 
   @Override
@@ -125,8 +128,10 @@ public class SearchAction implements QProfileWsAction {
 
   private SearchData load(SearchWsRequest request) {
     try (DbSession dbSession = dbClient.openSession(false)) {
+      OrganizationDto organization = wsSupport.getOrganizationByKey(dbSession, request.getOrganizationKey());
       return new SearchData()
-        .setProfiles(dataLoader.findProfiles(dbSession, request))
+        .setOrganization(organization)
+        .setProfiles(dataLoader.findProfiles(dbSession, request, organization))
         .setActiveRuleCountByProfileKey(activeRuleIndex.countAllByQualityProfileKey())
         .setActiveDeprecatedRuleCountByProfileKey(activeRuleIndex.countAllDeprecatedByQualityProfileKey())
         .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey(dbSession));
@@ -146,21 +151,21 @@ public class SearchAction implements QProfileWsAction {
   }
 
   private SearchWsResponse buildResponse(SearchData data) {
-    List<QProfile> profiles = data.getProfiles();
-    Map<String, QProfile> profilesByKey = profiles.stream().collect(Collectors.toMap(QProfile::key, identity()));
+    List<QualityProfileDto> profiles = data.getProfiles();
+    Map<String, QualityProfileDto> profilesByKey = profiles.stream().collect(Collectors.toMap(QualityProfileDto::getKey, identity()));
 
     SearchWsResponse.Builder response = SearchWsResponse.newBuilder();
 
-    for (QProfile profile : profiles) {
+    for (QualityProfileDto profile : profiles) {
       QualityProfile.Builder profileBuilder = response.addProfilesBuilder();
 
-      String profileKey = profile.key();
-      if (profile.organization() != null) {
-        profileBuilder.setOrganization(profile.organization().getKey());
+      String profileKey = profile.getKey();
+      if (profile.getOrganizationUuid() != null) {
+        profileBuilder.setOrganization(data.getOrganization().getKey());
       }
       profileBuilder.setKey(profileKey);
-      if (profile.name() != null) {
-        profileBuilder.setName(profile.name());
+      if (profile.getName() != null) {
+        profileBuilder.setName(profile.getName());
       }
       if (profile.getRulesUpdatedAt() != null) {
         profileBuilder.setRulesUpdatedAt(profile.getRulesUpdatedAt());
@@ -179,15 +184,15 @@ public class SearchAction implements QProfileWsAction {
 
       writeLanguageFields(profileBuilder, profile);
       writeParentFields(profileBuilder, profile, profilesByKey);
-      profileBuilder.setIsInherited(profile.isInherited());
+      profileBuilder.setIsInherited(profile.getParentKee() != null);
       profileBuilder.setIsDefault(profile.isDefault());
     }
 
     return response.build();
   }
 
-  private void writeLanguageFields(QualityProfile.Builder profileBuilder, QProfile profile) {
-    String languageKey = profile.language();
+  private void writeLanguageFields(QualityProfile.Builder profileBuilder, QualityProfileDto profile) {
+    String languageKey = profile.getLanguage();
     if (languageKey == null) {
       return;
     }
@@ -199,16 +204,16 @@ public class SearchAction implements QProfileWsAction {
     }
   }
 
-  private static void writeParentFields(QualityProfile.Builder profileBuilder, QProfile profile, Map<String, QProfile> profilesByKey) {
-    String parentKey = profile.parent();
+  private static void writeParentFields(QualityProfile.Builder profileBuilder, QualityProfileDto profile, Map<String, QualityProfileDto> profilesByKey) {
+    String parentKey = profile.getParentKee();
     if (parentKey == null) {
       return;
     }
 
     profileBuilder.setParentKey(parentKey);
-    QProfile parent = profilesByKey.get(parentKey);
-    if (parent != null && parent.name() != null) {
-      profileBuilder.setParentName(parent.name());
+    QualityProfileDto parent = profilesByKey.get(parentKey);
+    if (parent != null && parent.getName() != null) {
+      profileBuilder.setParentName(parent.getName());
     }
   }
 }
index 2aac5d7e628163600d0a5d0d346a2d71eb47ccc0..3217b978676d46e606701cf69ba3e3e15689d65c 100644 (file)
@@ -21,23 +21,34 @@ package org.sonar.server.qualityprofile.ws;
 
 import java.util.List;
 import java.util.Map;
-import org.sonar.server.qualityprofile.QProfile;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
 
 import static com.google.common.base.MoreObjects.firstNonNull;
 import static com.google.common.collect.ImmutableList.copyOf;
 import static com.google.common.collect.ImmutableMap.copyOf;
 
 public class SearchData {
-  private List<QProfile> profiles;
+  private OrganizationDto organization;
+  private List<QualityProfileDto> profiles;
   private Map<String, Long> activeRuleCountByProfileKey;
   private Map<String, Long> activeDeprecatedRuleCountByProfileKey;
   private Map<String, Long> projectCountByProfileKey;
 
-  public List<QProfile> getProfiles() {
+  public SearchData setOrganization(OrganizationDto organization) {
+    this.organization = organization;
+    return this;
+  }
+
+  public OrganizationDto getOrganization() {
+    return organization;
+  }
+
+  public List<QualityProfileDto> getProfiles() {
     return profiles;
   }
 
-  public SearchData setProfiles(List<QProfile> profiles) {
+  public SearchData setProfiles(List<QualityProfileDto> profiles) {
     this.profiles = copyOf(profiles);
     return this;
   }
index d5909fd7f221301644e250d2e8febf584b186465..a7e5d9f4423f496219145e5eb49ce7566c44f57d 100644 (file)
@@ -36,8 +36,8 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.qualityprofile.QProfile;
 import org.sonar.server.qualityprofile.QProfileLookup;
 import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
 
@@ -45,29 +45,26 @@ import static java.lang.String.format;
 
 public class SearchDataLoader {
 
-  private static final Comparator<QProfile> Q_PROFILE_COMPARATOR = Comparator
-    .comparing(QProfile::language)
-    .thenComparing(QProfile::name);
+  private static final Comparator<QualityProfileDto> Q_PROFILE_COMPARATOR = Comparator
+    .comparing(QualityProfileDto::getLanguage)
+    .thenComparing(QualityProfileDto::getName);
 
   private final Languages languages;
   private final QProfileLookup profileLookup;
   private final DbClient dbClient;
   private final ComponentFinder componentFinder;
-  private final QProfileWsSupport qProfileWsSupport;
 
   public SearchDataLoader(Languages languages, QProfileLookup profileLookup, DbClient dbClient,
-    ComponentFinder componentFinder, QProfileWsSupport qProfileWsSupport) {
+    ComponentFinder componentFinder) {
     this.languages = languages;
     this.profileLookup = profileLookup;
     this.dbClient = dbClient;
     this.componentFinder = componentFinder;
-    this.qProfileWsSupport = qProfileWsSupport;
   }
 
   @VisibleForTesting
-  List<QProfile> findProfiles(DbSession dbSession, SearchWsRequest request) {
-    OrganizationDto organization = qProfileWsSupport.getOrganizationByKey(dbSession, request.getOrganizationKey());
-    Collection<QProfile> profiles;
+  List<QualityProfileDto> findProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
+    Collection<QualityProfileDto> profiles;
     if (askDefaultProfiles(request)) {
       profiles = findDefaultProfiles(dbSession, request, organization);
     } else if (hasComponentKey(request)) {
@@ -79,11 +76,11 @@ public class SearchDataLoader {
     return profiles.stream().sorted(Q_PROFILE_COMPARATOR).collect(Collectors.toList());
   }
 
-  private Collection<QProfile> findDefaultProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
+  private Collection<QualityProfileDto> findDefaultProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
     String profileName = request.getProfileName();
 
     Set<String> languageKeys = getLanguageKeys();
-    Map<String, QProfile> qualityProfiles = new HashMap<>(languageKeys.size());
+    Map<String, QualityProfileDto> qualityProfiles = new HashMap<>(languageKeys.size());
 
     Set<String> missingLanguageKeys = lookupByProfileName(dbSession, organization, qualityProfiles, languageKeys, profileName);
     Set<String> noDefaultProfileLanguageKeys = lookupDefaults(dbSession, organization, qualityProfiles, missingLanguageKeys);
@@ -95,12 +92,12 @@ public class SearchDataLoader {
     return qualityProfiles.values();
   }
 
-  private Collection<QProfile> findProjectProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
+  private Collection<QualityProfileDto> findProjectProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
     String componentKey = request.getProjectKey();
     String profileName = request.getProfileName();
 
     Set<String> languageKeys = getLanguageKeys();
-    Map<String, QProfile> qualityProfiles = new HashMap<>(languageKeys.size());
+    Map<String, QualityProfileDto> qualityProfiles = new HashMap<>(languageKeys.size());
 
     // look up profiles by profileName (if any) for each language
     Set<String> unresolvedLanguages = lookupByProfileName(dbSession, organization, qualityProfiles, languageKeys, profileName);
@@ -116,16 +113,16 @@ public class SearchDataLoader {
     return qualityProfiles.values();
   }
 
-  private List<QProfile> findAllProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
+  private List<QualityProfileDto> findAllProfiles(DbSession dbSession, SearchWsRequest request, OrganizationDto organization) {
     String language = request.getLanguage();
 
     if (language == null) {
-      return profileLookup.allProfiles(dbSession, organization).stream().filter(qProfile -> languages.get(qProfile.language()) != null).collect(Collectors.toList());
+      return profileLookup.allProfiles(dbSession, organization).stream().filter(qProfile -> languages.get(qProfile.getLanguage()) != null).collect(Collectors.toList());
     }
     return profileLookup.profiles(dbSession, language, organization);
   }
 
-  private Set<String> lookupByProfileName(DbSession dbSession, OrganizationDto organization, Map<String, QProfile> qualityProfiles, Set<String> languageKeys,
+  private Set<String> lookupByProfileName(DbSession dbSession, OrganizationDto organization, Map<String, QualityProfileDto> qualityProfiles, Set<String> languageKeys,
     @Nullable String profileName) {
     if (languageKeys.isEmpty() || profileName == null) {
       return languageKeys;
@@ -133,11 +130,11 @@ public class SearchDataLoader {
 
     dbClient.qualityProfileDao().selectByNameAndLanguages(organization, profileName, languageKeys, dbSession)
       .forEach(qualityProfile -> qualityProfiles
-        .put(qualityProfile.getLanguage(), QProfile.from(qualityProfile, organization)));
+        .put(qualityProfile.getLanguage(), qualityProfile));
     return difference(languageKeys, qualityProfiles.keySet());
   }
 
-  private Set<String> lookupByModuleKey(DbSession dbSession, OrganizationDto organization, Map<String, QProfile> qualityProfiles, Set<String> languageKeys,
+  private Set<String> lookupByModuleKey(DbSession dbSession, OrganizationDto organization, Map<String, QualityProfileDto> qualityProfiles, Set<String> languageKeys,
     @Nullable String moduleKey) {
     if (languageKeys.isEmpty() || moduleKey == null) {
       return languageKeys;
@@ -145,7 +142,7 @@ public class SearchDataLoader {
 
     ComponentDto project = getProject(moduleKey, dbSession);
     dbClient.qualityProfileDao().selectByProjectAndLanguages(dbSession, organization, project.getKey(), languageKeys)
-      .forEach(qualityProfile -> qualityProfiles.put(qualityProfile.getLanguage(), QProfile.from(qualityProfile, organization)));
+      .forEach(qualityProfile -> qualityProfiles.put(qualityProfile.getLanguage(), qualityProfile));
     return difference(languageKeys, qualityProfiles.keySet());
   }
 
@@ -157,7 +154,7 @@ public class SearchDataLoader {
     return dbClient.componentDao().selectOrFailByUuid(session, module.projectUuid());
   }
 
-  private Set<String> lookupDefaults(DbSession dbSession, OrganizationDto organization, Map<String, QProfile> qualityProfiles, Set<String> languageKeys) {
+  private Set<String> lookupDefaults(DbSession dbSession, OrganizationDto organization, Map<String, QualityProfileDto> qualityProfiles, Set<String> languageKeys) {
     if (languageKeys.isEmpty()) {
       return languageKeys;
     }
@@ -170,18 +167,16 @@ public class SearchDataLoader {
     return Sets.newHashSet(Sets.difference(languageKeys, set2));
   }
 
-  private static void addAll(Map<String, QProfile> qualityProfiles, Collection<QProfile> list) {
-    list.forEach(qualityProfile -> qualityProfiles.put(qualityProfile.language(), qualityProfile));
+  private static void addAll(Map<String, QualityProfileDto> qualityProfiles, Collection<QualityProfileDto> list) {
+    list.forEach(qualityProfile -> qualityProfiles.put(qualityProfile.getLanguage(), qualityProfile));
   }
 
   private Set<String> getLanguageKeys() {
     return Arrays.stream(languages.all()).map(Language::getKey).collect(Collectors.toSet());
   }
 
-  private List<QProfile> findDefaultProfiles(final DbSession dbSession, OrganizationDto organization, Set<String> languageKeys) {
-    return dbClient.qualityProfileDao().selectDefaultProfiles(dbSession, organization, languageKeys).stream()
-      .map(result -> QProfile.from(result, organization))
-      .collect(Collectors.toList());
+  private List<QualityProfileDto> findDefaultProfiles(final DbSession dbSession, OrganizationDto organization, Set<String> languageKeys) {
+    return dbClient.qualityProfileDao().selectDefaultProfiles(dbSession, organization, languageKeys);
   }
 
   private static boolean askDefaultProfiles(SearchWsRequest request) {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileTest.java
deleted file mode 100644 (file)
index bf65202..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info 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.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class QProfileTest {
-
-  @Test
-  public void test_getters_and_setters() {
-    QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java").setParent("Parent");
-
-    assertThat(profile.id()).isEqualTo(1);
-    assertThat(profile.name()).isEqualTo("Default");
-    assertThat(profile.language()).isEqualTo("java");
-    assertThat(profile.parent()).isEqualTo("Parent");
-  }
-
-  @Test
-  public void is_inherited() {
-    assertThat(new QProfile().setId(1).setName("Default").setLanguage("java").setParent("Parent").isInherited()).isTrue();
-    assertThat(new QProfile().setId(1).setName("Default").setLanguage("java").setParent(null).isInherited()).isFalse();
-  }
-}
index fd7d11f5323d292d3ff435d6dc116c5db86c7b61..6f79e5742831e3f0393ef08c1bdaa4fd45024307 100644 (file)
@@ -45,7 +45,6 @@ import org.sonar.server.es.EsClient;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfileFactory;
 import org.sonar.server.qualityprofile.QProfileLookup;
 import org.sonar.server.qualityprofile.QProfileName;
 import org.sonar.server.qualityprofile.QProfileService;
@@ -75,7 +74,7 @@ public class InheritanceActionTest {
   @Rule
   public EsTester esTester = new EsTester(new RuleIndexDefinition(new MapSettings()));
   @Rule
-  public UserSessionRule userSessionRule = UserSessionRule.standalone();
+  public UserSessionRule userSession = UserSessionRule.standalone();
 
   private DbClient dbClient;
   private DbSession dbSession;
@@ -100,9 +99,8 @@ public class InheritanceActionTest {
       dbClient,
       new QProfileLookup(dbClient),
       new ActiveRuleIndex(esClient),
-      new QProfileFactory(dbClient),
-      new Languages(),
-      defaultOrganizationProvider
+      new QProfileWsSupport(dbClient, userSession, defaultOrganizationProvider),
+      new Languages()
     );
     wsActionTester = new WsActionTester(underTest);
     ruleActivator = new RuleActivator(
@@ -112,16 +110,16 @@ public class InheritanceActionTest {
       new RuleActivatorContextFactory(dbClient),
       new TypeValidations(new ArrayList<>()),
       activeRuleIndexer,
-      userSessionRule
+      userSession
     );
     service = new QProfileService(
       dbClient,
       activeRuleIndexer,
       ruleActivator,
-      userSessionRule,
+      userSession,
       defaultOrganizationProvider
     );
-    organization = dbTester.getDefaultOrganization();
+    organization = dbTester.organizations().insert();
   }
 
   @Test
@@ -161,7 +159,6 @@ public class InheritanceActionTest {
     String response = wsActionTester.newRequest()
       .setMethod("GET")
       .setParam("profileKey", buWide.getKee())
-      .setParam("organization", organization.getKey())
       .execute()
       .getInput();
 
@@ -190,7 +187,7 @@ public class InheritanceActionTest {
 
   @Test
   public void stat_for_all_profiles() {
-    userSessionRule.logIn()
+    userSession.logIn()
       .addPermission(ADMINISTER_QUALITY_PROFILES, organization.getUuid());
 
     String language = randomAlphanumeric(20);
@@ -209,11 +206,17 @@ public class InheritanceActionTest {
     dbClient.ruleDao().insert(dbSession, rule);
     dbSession.commit();
 
+    userSession.logIn()
+      .addPermission(ADMINISTER_QUALITY_PROFILES, dbTester.getDefaultOrganization().getUuid());
+
     service.activate(profile1.getKey(), new RuleActivation(rule.getKey()).setSeverity("MINOR"));
     service.activate(profile2.getKey(), new RuleActivation(rule.getKey()).setSeverity("BLOCKER"));
     activeRuleIndexer.index();
 
-    Map<String, Multimap<String, FacetValue>> stats = underTest.getAllProfileStats();
+    userSession.logIn()
+      .addPermission(ADMINISTER_QUALITY_PROFILES, organization.getUuid());
+
+    Map<String, Multimap<String, FacetValue>> stats = underTest.getAllProfileStats(dbSession, organization);
 
     assertThat(stats.size()).isEqualTo(2);
     assertThat(stats.get(profile1.getKey()).size()).isEqualTo(3);
index 9f5cc82c197dec4bd3638d9403a075b2279b90c9..a1a483550505a7ca232e08d10ea40db63270c24f 100644 (file)
@@ -65,7 +65,7 @@ public class QProfilesWsTest {
       new BulkRuleActivationActions(profileService, null),
       new CreateAction(null, null, null, languages, wsSupport, userSessionRule, null, importers),
       new ImportersAction(importers),
-      new SearchAction(null, languages, null, dbClient),
+      new SearchAction(null, languages, null, dbClient, wsSupport ),
       new SetDefaultAction(languages, null, null, wsSupport),
       new ProjectsAction(null, userSessionRule),
       new ChangelogAction(null, wsSupport, languages, dbClient),
@@ -73,7 +73,7 @@ public class QProfilesWsTest {
       new CompareAction(null, null, languages),
       new DeleteAction(languages, null, null, userSessionRule, wsSupport),
       new ExportersAction(),
-      new InheritanceAction(null, null, null, null, languages, null),
+      new InheritanceAction(null, null, null, null, languages),
       new RenameAction(null, wsSupport, dbClient, userSessionRule))).controller(QProfilesWs.API_ENDPOINT);
   }
 
@@ -238,8 +238,8 @@ public class QProfilesWsTest {
     WebService.Action inheritance = controller.action("inheritance");
     assertThat(inheritance).isNotNull();
     assertThat(inheritance.isPost()).isFalse();
-    assertThat(inheritance.params()).hasSize(3).extracting("key").containsOnly(
-      "profileKey", "language", "profileName");
+    assertThat(inheritance.params()).hasSize(4).extracting("key").containsExactlyInAnyOrder(
+      "organization", "profileKey", "language", "profileName");
     assertThat(inheritance.responseExampleAsString()).isNotEmpty();
   }
 
index fccaa2af6f994b196ed928aa41f2c3cb14bdc118..95966919fd45e7b2b87539e7e096ec7e6b3ad447 100644 (file)
@@ -111,10 +111,11 @@ public class SearchActionTest {
         languages,
         new QProfileLookup(dbClient),
         dbClient,
-        new ComponentFinder(dbClient), qProfileWsSupport),
+        new ComponentFinder(dbClient)),
       languages,
       activeRuleIndex,
-      dbClient);
+      dbClient,
+      qProfileWsSupport);
     ws = new WsActionTester(underTest);
   }
 
index 78d35c3c47c42bc80ce72dbc810959e5be124e7e..4ea4b7984e66598bfddc2862516c323147a93cce 100644 (file)
@@ -39,9 +39,7 @@ import org.sonar.db.qualityprofile.QualityProfileTesting;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.language.LanguageTesting;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfile;
 import org.sonar.server.qualityprofile.QProfileLookup;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
 
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
@@ -62,7 +60,6 @@ public class SearchDataLoaderTest {
   private Languages languages;
   private QProfileLookup profileLookup;
   private ComponentFinder componentFinder;
-  private ActiveRuleIndex activeRuleIndex;
   private QProfileWsSupport qProfileWsSupport;
   private OrganizationDto organization;
 
@@ -80,59 +77,58 @@ public class SearchDataLoaderTest {
   @Test
   public void find_no_profiles_if_database_is_empty() throws Exception {
     assertThat(findProfiles(
-      new SearchWsRequest())).isEmpty();
-  }
-
-  @Test
-  public void findAll_in_default_organization() throws Exception {
-    insertQualityProfile(dbTester.getDefaultOrganization());
-    assertThat(findProfiles(
-      new SearchWsRequest())).hasSize(1);
+      new SearchWsRequest()
+    )).isEmpty();
   }
 
   @Test
-  public void findAll_in_specific_organization() throws Exception {
+  public void findAll() throws Exception {
     insertQualityProfile(organization);
     assertThat(findProfiles(
       new SearchWsRequest()
-        .setOrganizationKey(organization.getKey()))).hasSize(1);
+        .setOrganizationKey(organization.getKey())
+    )).hasSize(1);
   }
 
   @Test
-  public void findDefaults_in_specific_organization() throws Exception {
+  public void findDefaults() throws Exception {
     insertQualityProfile(organization, dto -> dto.setDefault(true));
     assertThat(findProfiles(
       new SearchWsRequest()
         .setOrganizationKey(organization.getKey())
-        .setDefaults(true))).hasSize(1);
+        .setDefaults(true)
+    )).hasSize(1);
   }
 
   @Test
-  public void findForProject_in_specific_organization() throws Exception {
+  public void findForProject() throws Exception {
     insertQualityProfile(organization, dto -> dto.setDefault(true));
     ComponentDto project1 = insertProject();
     assertThat(findProfiles(
       new SearchWsRequest()
         .setOrganizationKey(organization.getKey())
-        .setProjectKey(project1.getKey()))).hasSize(1);
+        .setProjectKey(project1.getKey())
+    )).hasSize(1);
   }
 
   @Test
-  public void findAllForLanguage_in_specific_organization() throws Exception {
+  public void findAllForLanguage() throws Exception {
     QualityProfileDto qualityProfile = insertQualityProfile(organization, dto -> dto.setDefault(true));
     assertThat(findProfiles(
       new SearchWsRequest()
         .setOrganizationKey(organization.getKey())
-        .setLanguage(qualityProfile.getLanguage()))).hasSize(1);
+        .setLanguage(qualityProfile.getLanguage())
+    )).hasSize(1);
     assertThat(findProfiles(
       new SearchWsRequest()
         .setOrganizationKey(organization.getKey())
-        .setLanguage("other language"))).hasSize(0);
+        .setLanguage("other language")
+    )).hasSize(0);
   }
 
-  private List<QProfile> findProfiles(SearchWsRequest request) {
-    return new SearchDataLoader(languages, profileLookup, dbTester.getDbClient(), componentFinder, qProfileWsSupport)
-      .findProfiles(dbTester.getSession(), request);
+  private List<QualityProfileDto> findProfiles(SearchWsRequest request) {
+    return new SearchDataLoader(languages, profileLookup, dbTester.getDbClient(), componentFinder)
+      .findProfiles(dbTester.getSession(), request, organization);
   }
 
   private QualityProfileDto insertQualityProfile(OrganizationDto organization, Consumer<QualityProfileDto>... specials) {
index 985cf91e12bbaca3d2e1963ae3804ab11bd9a628..6e58b4074b075164f9fc61f2674fdd02f4c7bd43 100644 (file)
@@ -53,7 +53,7 @@ import static com.google.common.base.Preconditions.checkState;
  * In both cases, one can define user session behavior which should apply on all tests directly on the property, eg.:
  * <pre>
  * {@literal @}Rule
- * public UserSessionRule userSessionRule = UserSessionRule.standalone().login("admin").setOrganizationPermissions(OrganizationPermissions.SYSTEM_ADMIN);
+ * public UserSessionRule userSession = UserSessionRule.standalone().login("admin").setOrganizationPermissions(OrganizationPermissions.SYSTEM_ADMIN);
  * </pre>
  * </p>
  * <p>
@@ -63,7 +63,7 @@ import static com.google.common.base.Preconditions.checkState;
  * <pre>
  * {@literal @}Test
  * public void test_method() {
- *   userSessionRule.standalone();
+ *   userSession.standalone();
  *   {@literal [...]}
  * }
  * </pre>