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
@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);
<include refid="profilesColumns"/>
FROM rules_profiles p
WHERE p.language=#{language}
+ AND p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
ORDER BY p.name
</select>
}
@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
+++ /dev/null
-/*
- * 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();
- }
-}
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 {
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()));
- }
-
}
}
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());
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);
*/
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;
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;
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
.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);
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();
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)
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));
}
}
- 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();
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)) {
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))
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;
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
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));
}
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());
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;
}
}
}
- 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());
}
}
}
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;
}
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;
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)) {
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);
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);
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;
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;
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());
}
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;
}
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) {
+++ /dev/null
-/*
- * 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();
- }
-}
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;
@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;
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(
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
String response = wsActionTester.newRequest()
.setMethod("GET")
.setParam("profileKey", buWide.getKee())
- .setParam("organization", organization.getKey())
.execute()
.getInput();
@Test
public void stat_for_all_profiles() {
- userSessionRule.logIn()
+ userSession.logIn()
.addPermission(ADMINISTER_QUALITY_PROFILES, organization.getUuid());
String language = randomAlphanumeric(20);
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);
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),
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);
}
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();
}
languages,
new QProfileLookup(dbClient),
dbClient,
- new ComponentFinder(dbClient), qProfileWsSupport),
+ new ComponentFinder(dbClient)),
languages,
activeRuleIndex,
- dbClient);
+ dbClient,
+ qProfileWsSupport);
ws = new WsActionTester(underTest);
}
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;
private Languages languages;
private QProfileLookup profileLookup;
private ComponentFinder componentFinder;
- private ActiveRuleIndex activeRuleIndex;
private QProfileWsSupport qProfileWsSupport;
private OrganizationDto organization;
@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) {
* 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>
* <pre>
* {@literal @}Test
* public void test_method() {
- * userSessionRule.standalone();
+ * userSession.standalone();
* {@literal [...]}
* }
* </pre>