import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.tuple;
@Test
public void delete_does_not_fail_when_active_rule_does_not_exist() {
- underTest.delete(dbSession, ActiveRuleKey.of(profile1, rule1.getKey()));
+ assertThatNoException().isThrownBy(() -> underTest.delete(dbSession, ActiveRuleKey.of(profile1, rule1.getKey())));
}
@Test
tuple(ar3.getUuid(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity()));
}
+ @Test
+ public void countMissingRules() {
+ db.qualityProfiles().activateRule(profile1, rule1);
+ db.qualityProfiles().activateRule(profile1, rule3);
+ db.qualityProfiles().activateRule(profile2, rule1);
+ db.qualityProfiles().activateRule(profile2, rule2);
+
+ int result = underTest.countMissingRules(dbSession, profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid());
+
+ assertThat(result).isOne();
+ }
+
+ @Test
+ public void countMissingRules_whenNoRulesInCommon_shouldReturnNumberOfRulesInComparedToProfile() {
+ db.qualityProfiles().activateRule(profile1, rule1);
+ db.qualityProfiles().activateRule(profile2, rule2);
+ db.qualityProfiles().activateRule(profile2, rule3);
+
+ int result = underTest.countMissingRules(dbSession, profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid());
+
+ assertThat(result).isEqualTo(2);
+ }
+
+ @Test
+ public void countMissingRules_whenSomeRulesRemoved_shouldNotCountRemovedRules() {
+ db.qualityProfiles().activateRule(profile1, rule1);
+ db.qualityProfiles().activateRule(profile2, rule2);
+ db.qualityProfiles().activateRule(profile2, rule3);
+ db.qualityProfiles().activateRule(profile2, removedRule);
+
+ int result = underTest.countMissingRules(dbSession, profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid());
+
+ assertThat(result).isEqualTo(2);
+ }
+
private static class Accumulator implements Consumer<IndexedActiveRuleDto> {
private final List<IndexedActiveRuleDto> list = new ArrayList<>();
delete from active_rule_parameters
where
active_rule_uuid in
- <foreach collection="activeRuleUuids" open="(" close=")" item="activeRuleUuid" separator=",">#{activeRuleUuid, jdbcType=VARCHAR}</foreach>
+ <foreach collection="activeRuleUuids" open="(" close=")" item="activeRuleUuid"
+ separator=",">#{activeRuleUuid, jdbcType=VARCHAR}</foreach>
</delete>
<select id="selectParamsByActiveRuleUuid" parameterType="String" resultType="ActiveRuleParam">
group by oqp.uuid
</select>
- <select id="scrollAllForIndexing" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
+ <select id="scrollAllForIndexing" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto" fetchSize="${_scrollFetchSize}"
+ resultSetType="FORWARD_ONLY">
<include refid="scrollAllForIndexingSql"/>
</select>
- <select id="scrollByUuidsForIndexing" parameterType="map" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
+ <select id="scrollByUuidsForIndexing" parameterType="map" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto"
+ fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
<include refid="scrollAllForIndexingSql"/>
where ar.uuid in
<foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
</select>
- <select id="scrollByRuleProfileUuidForIndexing" parameterType="String" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
+ <select id="scrollByRuleProfileUuidForIndexing" parameterType="String" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto"
+ fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
<include refid="scrollAllForIndexingSql"/>
where rp.uuid = #{ruleProfileUuid, jdbcType=VARCHAR}
</select>
inner join rules_profiles rp on rp.uuid = ar.profile_uuid
inner join rules r on r.uuid = ar.rule_uuid
</sql>
+
+ <select id="countMissingRules" parameterType="String" resultType="int">
+ select count(ar.rule_uuid) from active_rules ar
+ inner join rules r on r.uuid = ar.rule_uuid
+ where r.status != 'REMOVED'
+ and not exists (
+ select 1 from active_rules ar2
+ where ar.rule_uuid = ar2.rule_uuid
+ and ar2.profile_uuid = #{rulesProfileUuid,jdbcType=VARCHAR})
+ and ar.profile_uuid = #{compareToRulesProfileUuid,jdbcType=VARCHAR}
+ </select>
+
</mapper>
*/
package org.sonar.server.qualityprofile.ws;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.util.function.Consumer;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.DateUtils;
-import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
-import org.sonar.server.rule.index.RuleIndex;
-import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
+@RunWith(DataProviderRunner.class)
public class ShowActionIT {
- private static Language XOO1 = newLanguage("xoo1");
- private static Language XOO2 = newLanguage("xoo2");
- private static Languages LANGUAGES = new Languages(XOO1, XOO2);
+ private final static Language XOO1 = newLanguage("xoo1");
+ private final static Language XOO2 = newLanguage("xoo2");
+ private final static Languages LANGUAGES = new Languages(XOO1, XOO2);
@Rule
public EsTester es = EsTester.create();
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
- private RuleIndexer ruleIndexer = new RuleIndexer(es.client(), db.getDbClient());
- private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(db.getDbClient(), es.client());
- private RuleIndex ruleIndex = new RuleIndex(es.client(), System2.INSTANCE);
-
private WsActionTester ws = new WsActionTester(
- new ShowAction(db.getDbClient(), new QProfileWsSupport(db.getDbClient(), userSession), LANGUAGES, ruleIndex));
+ new ShowAction(db.getDbClient(), new QProfileWsSupport(db.getDbClient(), userSession), LANGUAGES));
@Test
public void profile_info() {
db.qualityProfiles().activateRule(sonarWayProfile, commonRule);
db.qualityProfiles().activateRule(sonarWayProfile, sonarWayRule1);
db.qualityProfiles().activateRule(sonarWayProfile, sonarWayRule2);
- ruleIndexer.indexAll();
- activeRuleIndexer.indexAll();
CompareToSonarWay result = call(ws.newRequest()
.setParam(PARAM_KEY, profile.getKee())
RuleDto commonRule = db.rules().insertRule(r -> r.setLanguage(XOO1.getKey()));
db.qualityProfiles().activateRule(profile, commonRule);
db.qualityProfiles().activateRule(sonarWayProfile, commonRule);
- ruleIndexer.indexAll();
- activeRuleIndexer.indexAll();
CompareToSonarWay result = call(ws.newRequest()
.setParam(PARAM_KEY, profile.getKee())
.containsExactly(sonarWayProfile.getKee(), sonarWayProfile.getName(), 0L);
}
- @Test
- public void no_comparison_when_sonar_way_does_not_exist() {
- QProfileDto anotherSonarWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("Another Sonar way").setLanguage(XOO1.getKey()));
- QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
-
- ShowResponse result = call(ws.newRequest()
- .setParam(PARAM_KEY, profile.getKee())
- .setParam(PARAM_COMPARE_TO_SONAR_WAY, "true"));
-
- assertThat(result.hasCompareToSonarWay()).isFalse();
+ @DataProvider
+ public static Object[][] dataForComparison() {
+ Consumer<QProfileDto> sonarWay = p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey());
+ Consumer<QProfileDto> notBuiltInSonarWay = p -> p.setIsBuiltIn(false).setName("Sonar way").setLanguage(XOO1.getKey());
+ Consumer<QProfileDto> anotherSonarWay = p -> p.setIsBuiltIn(true).setName("Another Sonar way").setLanguage(XOO1.getKey());
+ Consumer<QProfileDto> anotherBuiltIn = p -> p.setIsBuiltIn(true).setLanguage(XOO1.getKey());
+ Consumer<QProfileDto> profile = p -> p.setLanguage(XOO1.getKey());
+ return new Object[][] {
+ {profile, anotherSonarWay, "true"},
+ {anotherBuiltIn, sonarWay, "true"},
+ {profile, notBuiltInSonarWay, "true"},
+ {profile, sonarWay, "false"}};
}
@Test
- public void no_comparison_when_profile_is_built_in() {
- QProfileDto sonarWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey()));
- QProfileDto anotherBuiltInProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setLanguage(XOO1.getKey()));
-
- ShowResponse result = call(ws.newRequest()
- .setParam(PARAM_KEY, anotherBuiltInProfile.getKee())
- .setParam(PARAM_COMPARE_TO_SONAR_WAY, "true"));
-
- assertThat(result.hasCompareToSonarWay()).isFalse();
- }
-
- @Test
- public void no_comparison_if_sonar_way_is_not_built_in() {
- QProfileDto sonarWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(false).setName("Sonar way").setLanguage(XOO1.getKey()));
- QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
-
- ShowResponse result = call(ws.newRequest()
- .setParam(PARAM_KEY, profile.getKee())
- .setParam(PARAM_COMPARE_TO_SONAR_WAY, "true"));
-
- assertThat(result.hasCompareToSonarWay()).isFalse();
- }
-
- @Test
- public void no_comparison_when_param_is_false() {
- QProfileDto sonarWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey()));
- QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
+ @UseDataProvider("dataForComparison")
+ public void response_shouldNotHaveCompareToSonarWay(Consumer<QProfileDto> profileData, Consumer<QProfileDto> profileToCompareData, String paramCompareToSonarWay) {
+ db.qualityProfiles().insert(profileToCompareData);
+ QProfileDto profile = db.qualityProfiles().insert(profileData);
ShowResponse result = call(ws.newRequest()
.setParam(PARAM_KEY, profile.getKee())
- .setParam(PARAM_COMPARE_TO_SONAR_WAY, "false"));
+ .setParam(PARAM_COMPARE_TO_SONAR_WAY, paramCompareToSonarWay));
assertThat(result.hasCompareToSonarWay()).isFalse();
}
@Test
public void compare_to_sonar_way_over_sonarqube_way() {
QProfileDto sonarWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey()));
- QProfileDto sonarQubeWayProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("SonarQube way").setLanguage(XOO1.getKey()));
+ db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setName("SonarQube way").setLanguage(XOO1.getKey()));
QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO1.getKey()));
CompareToSonarWay result = call(ws.newRequest()
public void fail_if_profile_language_is_not_supported() {
QProfileDto profile = db.qualityProfiles().insert(p -> p.setKee("unknown-profile").setLanguage("kotlin"));
- assertThatThrownBy(() -> {
- call(ws.newRequest().setParam(PARAM_KEY, profile.getKee()));
- })
+ TestRequest testRequest = ws.newRequest().setParam(PARAM_KEY, profile.getKee());
+ assertThatThrownBy(() -> call(testRequest))
.isInstanceOf(NotFoundException.class)
.hasMessage("Quality Profile with key 'unknown-profile' does not exist");
}
@Test
public void fail_if_profile_does_not_exist() {
- assertThatThrownBy(() -> {
- call(ws.newRequest().setParam(PARAM_KEY, "unknown-profile"));
- })
+ TestRequest testRequest = ws.newRequest().setParam(PARAM_KEY, "unknown-profile");
+ assertThatThrownBy(() -> call(testRequest))
.isInstanceOf(NotFoundException.class)
.hasMessage("Quality Profile with key 'unknown-profile' does not exist");
}
.forEach(project -> db.qualityProfiles().associateWithProject(project, profile));
ws = new WsActionTester(
- new ShowAction(db.getDbClient(), new QProfileWsSupport(db.getDbClient(), userSession), new Languages(cs), ruleIndex));
+ new ShowAction(db.getDbClient(), new QProfileWsSupport(db.getDbClient(), userSession), new Languages(cs)));
String result = ws.newRequest().setParam(PARAM_KEY, profile.getKee()).execute().getInput();
assertJson(result).ignoreFields("rulesUpdatedAt", "lastUsed", "userUpdatedAt").isSimilarTo(ws.getDef().responseExampleAsString());
assertThat(action.since()).isEqualTo("6.5");
WebService.Param profile = action.param("key");
+ assertThat(profile).isNotNull();
assertThat(profile.isRequired()).isTrue();
assertThat(profile.isInternal()).isFalse();
assertThat(profile.description()).isNotEmpty();
WebService.Param compareToSonarWay = action.param("compareToSonarWay");
+ assertThat(compareToSonarWay).isNotNull();
assertThat(compareToSonarWay.isRequired()).isFalse();
assertThat(compareToSonarWay.isInternal()).isTrue();
assertThat(compareToSonarWay.description()).isNotEmpty();