import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.Date;
-import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
+import org.sonar.db.KeyLongValue;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.organization.OrganizationDto;
return mapper(dbSession).selectUuidsOfAssociatedProjects(profileKey);
}
- /**
- * @deprecated provide organization
- */
- @Deprecated
- public Map<String, Long> countProjectsByProfileKey(DbSession dbSession) {
- Map<String, Long> countByKey = new HashMap<>();
- QualityProfileMapper mapper = mapper(dbSession);
- for (QualityProfileProjectCount count : mapper.countProjectsByProfile()) {
- countByKey.put(count.getProfileKey(), count.getProjectCount());
- }
- return countByKey;
+ public Map<String, Long> countProjectsByProfileKey(DbSession dbSession, OrganizationDto organization) {
+ return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileKey(organization.getUuid()));
}
public void insertProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) {
import java.util.List;
import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param;
+import org.sonar.db.KeyLongValue;
public interface QualityProfileMapper {
List<String> selectUuidsOfAssociatedProjects(@Param("profileKey") String profileKey);
- List<QualityProfileProjectCount> countProjectsByProfile();
+ List<KeyLongValue> countProjectsByProfileKey(@Param("organizationUuid") String organizationUuid);
QualityProfileDto selectByProjectAndLanguage(@Param("projectKey") String projectKey, @Param("language") String language);
+++ /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.db.qualityprofile;
-
-public class QualityProfileProjectCount {
-
- private String profileKey;
- private Long projectCount;
-
- public String getProfileKey() {
- return profileKey;
- }
-
- public Long getProjectCount() {
- return projectCount;
- }
-}
ORDER BY pj.name ASC
</select>
- <select id="countProjectsByProfile" resultType="org.sonar.db.qualityprofile.QualityProfileProjectCount">
- SELECT pp.profile_key as profileKey, count(projects.id) as projectCount
- FROM projects projects
- INNER JOIN project_qprofiles pp ON pp.project_uuid=projects.uuid
- INNER JOIN rules_profiles prof ON pp.profile_key=prof.kee
- WHERE projects.enabled=${_true}
- GROUP BY pp.profile_key
+ <select id="countProjectsByProfileKey" resultType="KeyLongValue" parameterType="map">
+ select pp.profile_key as "key", count(projects.id) as "value"
+ from projects projects
+ inner join project_qprofiles pp ON pp.project_uuid = projects.uuid
+ inner join rules_profiles prof ON pp.profile_key = prof.kee
+ where projects.enabled = ${_true}
+ and prof.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
+ group by pp.profile_key
</select>
<select id="selectByProjectAndLanguage" parameterType="map" resultType="QualityProfile">
assertThat(dto.getLanguage()).isEqualTo("java");
assertThat(dto.getParentKee()).isNull();
- assertThat(underTest.selectById(dbTester.getSession(),555)).isNull();
+ assertThat(underTest.selectById(dbTester.getSession(), 555)).isNull();
}
@Test
}
@Test
- public void count_projects_by_profile() {
- dbTester.prepareDbUnit(getClass(), "projects.xml");
-
- assertThat(underTest.countProjectsByProfileKey(dbTester.getSession())).containsOnly(
- MapEntry.entry("java_sonar_way", 2L),
- MapEntry.entry("js_sonar_way", 2L));
+ public void countProjectsByProfileKey() {
+ QualityProfileDto profileWithoutProjects = dbTester.qualityProfiles().insert(organization);
+ QualityProfileDto profileWithProjects = dbTester.qualityProfiles().insert(organization);
+ ComponentDto project1 = dbTester.components().insertProject(organization);
+ ComponentDto project2 = dbTester.components().insertProject(organization);
+ dbTester.qualityProfiles().associateProjectWithQualityProfile(project1, profileWithProjects);
+ dbTester.qualityProfiles().associateProjectWithQualityProfile(project2, profileWithProjects);
+
+ OrganizationDto otherOrg = dbTester.organizations().insert();
+ QualityProfileDto profileInOtherOrg = dbTester.qualityProfiles().insert(otherOrg);
+ ComponentDto projectInOtherOrg = dbTester.components().insertProject(otherOrg);
+ dbTester.qualityProfiles().associateProjectWithQualityProfile(projectInOtherOrg, profileInOtherOrg);
+
+ assertThat(underTest.countProjectsByProfileKey(dbTester.getSession(), organization)).containsOnly(
+ MapEntry.entry(profileWithProjects.getKey(), 2L));
}
@Test
.setProfiles(dataLoader.findProfiles(dbSession, request, organization))
.setActiveRuleCountByProfileKey(dbClient.activeRuleDao().countActiveRulesByProfileKey(dbSession, organization))
.setActiveDeprecatedRuleCountByProfileKey(dbClient.activeRuleDao().countActiveRulesForRuleStatusByProfileKey(dbSession, organization, RuleStatus.DEPRECATED))
- .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey(dbSession));
+ .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey(dbSession, organization));
}
}