}
}
- public Map<String, Long> countAllActiveRules() {
- return activeRuleIndex.countAllByQualityProfileKey();
- }
-
public Map<String, Multimap<String, FacetValue>> getAllProfileStats() {
List<String> keys = findAll().stream().map(QualityProfileDto::getKey).collect(Collectors.toList());
return activeRuleIndex.getStatsByProfileKeys(keys);
return countByField(FIELD_ACTIVE_RULE_PROFILE_KEY,
QueryBuilders.hasParentQuery(TYPE_RULE,
QueryBuilders.boolQuery().mustNot(
- QueryBuilders.termQuery(FIELD_RULE_STATUS, "REMOVED"))));
+ QueryBuilders.termQuery(FIELD_RULE_STATUS, RuleStatus.REMOVED.name()))));
+ }
+
+ public Map<String, Long> countAllDeprecatedByQualityProfileKey() {
+ return countByField(FIELD_ACTIVE_RULE_PROFILE_KEY,
+ QueryBuilders.hasParentQuery(TYPE_RULE,
+ QueryBuilders.boolQuery().must(
+ QueryBuilders.termQuery(FIELD_RULE_STATUS, RuleStatus.DEPRECATED.name()))));
}
private Map<String, Long> countByField(String indexField, QueryBuilder filter) {
.setSince("5.2")
.setDescription("List quality profiles.")
.setHandler(this)
- .setResponseExample(getClass().getResource("example-search.json"));
+ .setResponseExample(getClass().getResource("search-example.json"));
action
.createParam(PARAM_LANGUAGE)
private static SearchWsRequest toSearchWsRequest(Request request) {
return new SearchWsRequest()
- .setProjectKey(request.param(PARAM_PROJECT_KEY))
- .setProfileName(request.param(PARAM_PROFILE_NAME))
- .setDefaults(request.paramAsBoolean(PARAM_DEFAULTS))
- .setLanguage(request.param(PARAM_LANGUAGE));
+ .setProjectKey(request.param(PARAM_PROJECT_KEY))
+ .setProfileName(request.param(PARAM_PROFILE_NAME))
+ .setDefaults(request.paramAsBoolean(PARAM_DEFAULTS))
+ .setLanguage(request.param(PARAM_LANGUAGE));
}
private SearchWsResponse doHandle(SearchWsRequest request) {
profileBuilder.setRulesUpdatedAt(profile.getRulesUpdatedAt());
}
profileBuilder.setActiveRuleCount(data.getActiveRuleCount(profileKey));
+ profileBuilder.setActiveDeprecatedRuleCount(data.getActiveDeprecatedRuleCount(profileKey));
if (!profile.isDefault()) {
profileBuilder.setProjectCount(data.getProjectCount(profileKey));
}
public class SearchData {
private List<QProfile> profiles;
private Map<String, Long> activeRuleCountByProfileKey;
+ private Map<String, Long> activeDeprecatedRuleCountByProfileKey;
private Map<String, Long> projectCountByProfileKey;
public List<QProfile> getProfiles() {
return this;
}
+ public SearchData setActiveDeprecatedRuleCountByProfileKey(Map<String, Long> activeDeprecatedRuleCountByProfileKey) {
+ this.activeDeprecatedRuleCountByProfileKey = activeDeprecatedRuleCountByProfileKey;
+ return this;
+ }
+
public SearchData setProjectCountByProfileKey(Map<String, Long> projectCountByProfileKey) {
this.projectCountByProfileKey = copyOf(projectCountByProfileKey);
return this;
public long getProjectCount(String profileKey) {
return firstNonNull(projectCountByProfileKey.get(profileKey), 0L);
}
+
+ public long getActiveDeprecatedRuleCount(String profileKey) {
+ return firstNonNull(activeDeprecatedRuleCountByProfileKey.get(profileKey), 0L);
+ }
}
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.qualityprofile.QProfile;
import org.sonar.server.qualityprofile.QProfileFactory;
-import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.qualityprofile.QProfileLookup;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
import static java.lang.String.format;
public class SearchDataLoader {
private final Languages languages;
private final QProfileLookup profileLookup;
- private final QProfileLoader profileLoader;
private final QProfileFactory profileFactory;
private final DbClient dbClient;
private final ComponentFinder componentFinder;
+ private final ActiveRuleIndex activeRuleIndex;
- public SearchDataLoader(Languages languages, QProfileLookup profileLookup, QProfileLoader profileLoader, QProfileFactory profileFactory, DbClient dbClient,
- ComponentFinder componentFinder) {
+ public SearchDataLoader(Languages languages, QProfileLookup profileLookup, QProfileFactory profileFactory, DbClient dbClient,
+ ComponentFinder componentFinder, ActiveRuleIndex activeRuleIndex) {
this.languages = languages;
this.profileLookup = profileLookup;
- this.profileLoader = profileLoader;
this.profileFactory = profileFactory;
this.dbClient = dbClient;
this.componentFinder = componentFinder;
+ this.activeRuleIndex = activeRuleIndex;
}
SearchData load(SearchWsRequest request) {
return new SearchData()
.setProfiles(findProfiles(request))
- .setActiveRuleCountByProfileKey(profileLoader.countAllActiveRules())
+ .setActiveRuleCountByProfileKey(activeRuleIndex.countAllByQualityProfileKey())
+ .setActiveDeprecatedRuleCountByProfileKey(activeRuleIndex.countAllDeprecatedByQualityProfileKey())
.setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey());
}
+++ /dev/null
-{
- "profiles": [
- {
- "key": "sonar-way-cs-12345",
- "name": "Sonar way",
- "language": "cs",
- "languageName": "C#",
- "isInherited": false,
- "activeRuleCount": 37,
- "isDefault": true
- },
- {
- "key": "my-bu-profile-java-34567",
- "name": "My BU Profile",
- "language": "java",
- "languageName": "Java",
- "isInherited": true,
- "parentKey": "my-company-profile-java-23456",
- "parentName": "My Company Profile",
- "activeRuleCount": 72,
- "isDefault": false,
- "projectCount": 13
- },
- {
- "key": "my-company-profile-java-23456",
- "name": "My Company Profile",
- "language": "java",
- "languageName": "Java",
- "isInherited": false,
- "isDefault": true,
- "activeRuleCount": 42
- },
- {
- "key": "sonar-way-python-01234",
- "name": "Sonar way",
- "language": "py",
- "languageName": "Python",
- "isInherited": false,
- "activeRuleCount": 125,
- "isDefault": true
- }
- ]
-}
--- /dev/null
+{
+ "profiles": [
+ {
+ "key": "sonar-way-cs-12345",
+ "name": "Sonar way",
+ "language": "cs",
+ "languageName": "C#",
+ "isInherited": false,
+ "activeRuleCount": 37,
+ "activeDeprecatedRuleCount": 0,
+ "isDefault": true
+ },
+ {
+ "key": "my-bu-profile-java-34567",
+ "name": "My BU Profile",
+ "language": "java",
+ "languageName": "Java",
+ "isInherited": true,
+ "parentKey": "my-company-profile-java-23456",
+ "parentName": "My Company Profile",
+ "activeRuleCount": 72,
+ "activeDeprecatedRuleCount": 5,
+ "isDefault": false,
+ "projectCount": 13
+ },
+ {
+ "key": "my-company-profile-java-23456",
+ "name": "My Company Profile",
+ "language": "java",
+ "languageName": "Java",
+ "isInherited": false,
+ "isDefault": true,
+ "activeRuleCount": 42,
+ "activeDeprecatedRuleCount": 3
+ },
+ {
+ "key": "sonar-way-python-01234",
+ "name": "Sonar way",
+ "language": "py",
+ "languageName": "Python",
+ "isInherited": false,
+ "activeRuleCount": 125,
+ "activeDeprecatedRuleCount": 0,
+ "isDefault": true
+ }
+ ]
+}
import org.sonar.server.activity.Activity;
import org.sonar.server.activity.ActivityService;
import org.sonar.server.es.SearchOptions;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.tester.UserSessionRule;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.rule.RuleTesting.newXooX1;
+import static org.sonar.db.rule.RuleTesting.newXooX2;
+import static org.sonar.db.rule.RuleTesting.newXooX3;
import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY;
import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_KEY;
@org.junit.Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
- DbClient db;
+ DbClient dbClient;
DbSession dbSession;
QProfileService service;
QProfileLoader loader;
RuleActivator activator;
+ ActiveRuleIndex activeRuleIndex;
RuleIndexer ruleIndexer;
ActiveRuleIndexer activeRuleIndexer;
+ RuleDto xooRule1 = newXooX1().setSeverity("MINOR");
+
@Before
public void before() {
tester.clearDbAndIndexes();
- db = tester.get(DbClient.class);
- dbSession = db.openSession(false);
+ dbClient = tester.get(DbClient.class);
+ dbSession = dbClient.openSession(false);
service = tester.get(QProfileService.class);
loader = tester.get(QProfileLoader.class);
activator = tester.get(RuleActivator.class);
ruleIndexer = tester.get(RuleIndexer.class);
activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
+ activeRuleIndex = tester.get(ActiveRuleIndex.class);
- // create pre-defined rules
- RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR");
- db.ruleDao().insert(dbSession, xooRule1);
+ dbClient.ruleDao().insert(dbSession, xooRule1);
// create pre-defined profiles P1 and P2
- db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2());
+ dbClient.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2());
dbSession.commit();
dbSession.clearCache();
public void create_profile_with_xml() {
userSessionRule.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- db.ruleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "R1")).setLanguage("xoo").setSeverity("MINOR"));
+ dbClient.ruleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "R1")).setLanguage("xoo").setSeverity("MINOR"));
dbSession.commit();
ruleIndexer.index();
assertThat(loader.getByKey(profile.getKey()).getLanguage()).isEqualTo("xoo");
assertThat(loader.getByKey(profile.getKey()).getName()).isEqualTo("New Profile");
- assertThat(db.activeRuleDao().selectByProfileKey(dbSession, profile.getKey())).hasSize(1);
- assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey( profile.getKey()).setActivation(true))).hasSize(1);
+ assertThat(dbClient.activeRuleDao().selectByProfileKey(dbSession, profile.getKey())).hasSize(1);
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profile.getKey()).setActivation(true))).hasSize(1);
}
@Test
dbSession.clearCache();
activeRuleIndexer.index();
- Map<String, Long> counts = loader.countAllActiveRules();
+ Map<String, Long> counts = activeRuleIndex.countAllByQualityProfileKey();
assertThat(counts).hasSize(2);
assertThat(counts.keySet()).containsOnly(XOO_P1_KEY, XOO_P2_KEY);
assertThat(counts.values()).containsOnly(1L, 1L);
}
+ @Test
+ public void count_by_all_deprecated_profiles() {
+ userSessionRule.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+ RuleDto xooRule2 = newXooX2().setStatus(RuleStatus.DEPRECATED);
+ RuleDto xooRule3 = newXooX3().setStatus(RuleStatus.DEPRECATED);
+ dbClient.ruleDao().insert(dbSession, xooRule2);
+ dbClient.ruleDao().insert(dbSession, xooRule3);
+ dbSession.commit();
+ ruleIndexer.index();
+
+ // active some rules
+ service.activate(XOO_P1_KEY, new RuleActivation(xooRule1.getKey()));
+ service.activate(XOO_P1_KEY, new RuleActivation(xooRule2.getKey()));
+ service.activate(XOO_P1_KEY, new RuleActivation(xooRule3.getKey()));
+ service.activate(XOO_P2_KEY, new RuleActivation(xooRule1.getKey()));
+ service.activate(XOO_P2_KEY, new RuleActivation(xooRule3.getKey()));
+ dbSession.commit();
+
+ Map<String, Long> counts = activeRuleIndex.countAllDeprecatedByQualityProfileKey();
+ assertThat(counts)
+ .hasSize(2)
+ .containsEntry(XOO_P1_KEY, 2L)
+ .containsEntry(XOO_P2_KEY, 1L);
+ }
+
@Test
public void stat_for_all_profiles() {
userSessionRule.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
// create deprecated rule
RuleDto deprecatedXooRule = RuleTesting.newDto(RuleKey.of("xoo", "deprecated1"))
.setSeverity("MINOR").setLanguage("xoo").setStatus(RuleStatus.DEPRECATED);
- db.ruleDao().insert(dbSession, deprecatedXooRule);
+ dbClient.ruleDao().insert(dbSession, deprecatedXooRule);
dbSession.commit();
ruleIndexer.index();
userSessionRule.login("david").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis());
- db.userDao().insert(dbSession, user);
+ dbClient.userDao().insert(dbSession, user);
dbSession.commit();
// We need an actual rule in DB to test RuleName in Activity
- RuleDto rule = db.ruleDao().selectOrFailByKey(dbSession, RuleTesting.XOO_X1);
+ RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbSession, RuleTesting.XOO_X1);
tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))
.setSeverity(Severity.MAJOR)
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.language.LanguageTesting;
import org.sonar.server.qualityprofile.QProfileFactory;
-import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.qualityprofile.QProfileLookup;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.ws.WsActionTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.server.qualityprofile.ws.SearchAction.PARAM_PROJECT_KEY;
import static org.sonar.test.JsonAssert.assertJson;
-
public class SearchActionTest {
@Rule
@Rule
public ExpectedException expectedException = ExpectedException.none();
- // TODO remove mock
- private QProfileLoader profileLoader = mock(QProfileLoader.class);
+ private ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class);
final DbClient dbClient = db.getDbClient();
final DbSession dbSession = db.getSession();
new SearchDataLoader(
languages,
new QProfileLookup(dbClient),
- profileLoader,
new QProfileFactory(dbClient),
dbClient,
- new ComponentFinder(dbClient)),
+ new ComponentFinder(dbClient), activeRuleIndex),
languages));
}
@Test
public void search_nominal() throws Exception {
- when(profileLoader.countAllActiveRules()).thenReturn(ImmutableMap.of(
+ when(activeRuleIndex.countAllByQualityProfileKey()).thenReturn(ImmutableMap.of(
"sonar-way-xoo1-12345", 11L,
- "my-sonar-way-xoo2-34567", 33L
- ));
+ "my-sonar-way-xoo2-34567", 33L));
+ when(activeRuleIndex.countAllDeprecatedByQualityProfileKey()).thenReturn(ImmutableMap.of(
+ "sonar-way-xoo1-12345", 1L,
+ "my-sonar-way-xoo2-34567", 2L));
qualityProfileDao.insert(dbSession,
QualityProfileDto.createFor("sonar-way-xoo1-12345").setLanguage(xoo1.getKey()).setName("Sonar way").setDefault(true),
QualityProfileDto.createFor("sonar-way-xoo2-23456").setLanguage(xoo2.getKey()).setName("Sonar way"),
QualityProfileDto.createFor("my-sonar-way-xoo2-34567").setLanguage(xoo2.getKey()).setName("My Sonar way").setParentKee("sonar-way-xoo2-23456"),
- QualityProfileDto.createFor("sonar-way-other-666").setLanguage("other").setName("Sonar way").setDefault(true)
- );
+ QualityProfileDto.createFor("sonar-way-other-666").setLanguage("other").setName("Sonar way").setDefault(true));
new ComponentDao().insert(dbSession,
newProjectDto("project-uuid1"),
newProjectDto("project-uuid2"));
@Test
public void search_for_language() throws Exception {
qualityProfileDao.insert(dbSession,
- QualityProfileDto.createFor("sonar-way-xoo1-12345").setLanguage(xoo1.getKey()).setName("Sonar way")
- );
+ QualityProfileDto.createFor("sonar-way-xoo1-12345").setLanguage(xoo1.getKey()).setName("Sonar way"));
commit();
String result = ws.newRequest().setParam("language", xoo1.getKey()).execute().getInput();
"languageName": "Xoo1",
"isInherited": false,
"isDefault": true,
- "activeRuleCount": 11
+ "activeRuleCount": 11,
+ "activeDeprecatedRuleCount":1
},
{
"key": "my-sonar-way-xoo2-34567",
"parentKey": "sonar-way-xoo2-23456",
"parentName": "Sonar way",
"activeRuleCount": 33,
+ "activeDeprecatedRuleCount": 2,
"projectCount": 0
},
{
"isInherited": false,
"isDefault": false,
"activeRuleCount": 0,
+ "activeDeprecatedRuleCount": 0,
"projectCount": 2
}
]
-}
\ No newline at end of file
+}
optional string parentName = 7;
optional bool isDefault = 8;
optional int64 activeRuleCount = 9;
+ optional int64 activeDeprecatedRuleCount = 12;
optional int64 projectCount = 10;
optional string rulesUpdatedAt = 11;
}