Browse Source

SONAR-9448 Sanitize api/qualityprofiles/deactivate_rule

tags/6.5-M2
Teryk Bellahsene 7 years ago
parent
commit
43fcd85776

+ 14
- 13
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRuleAction.java View File

@@ -20,22 +20,20 @@
package org.sonar.server.qualityprofile.ws;

import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.user.UserSession;

import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_PROFILE_KEY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_RULE_KEY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;

@ServerSide
public class DeactivateRuleAction implements QProfileWsAction {

private final DbClient dbClient;
@@ -53,26 +51,29 @@ public class DeactivateRuleAction implements QProfileWsAction {
public void define(WebService.NewController controller) {
WebService.NewAction deactivate = controller
.createAction(ACTION_DEACTIVATE_RULE)
.setDescription("Deactivate a rule on a Quality profile")
.setDescription("Deactivate a rule on a Quality profile.<br> " +
"Requires to be logged in and the 'Administer Quality Profiles' permission.")
.setHandler(this)
.setPost(true)
.setSince("4.4");

deactivate.createParam(PARAM_PROFILE_KEY)
.setDescription("Key of Quality profile, can be obtained through <code>api/qualityprofiles/search</code>")
deactivate.createParam(PARAM_PROFILE)
.setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>")
.setDeprecatedKey("profile_key", "6.5")
.setRequired(true)
.setExampleValue(Uuids.UUID_EXAMPLE_01);
.setExampleValue(UUID_EXAMPLE_01);

deactivate.createParam(PARAM_RULE_KEY)
.setDescription("Key of the rule")
deactivate.createParam(PARAM_RULE)
.setDescription("Rule key")
.setDeprecatedKey("rule_key", "6.5")
.setRequired(true)
.setExampleValue("squid:AvoidCycles");
}

@Override
public void handle(Request request, Response response) throws Exception {
RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE_KEY));
String qualityProfileKey = request.mandatoryParam(PARAM_PROFILE_KEY);
RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE));
String qualityProfileKey = request.mandatoryParam(PARAM_PROFILE);
userSession.checkLoggedIn();
try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(qualityProfileKey));

+ 26
- 18
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/DeactivateRuleActionTest.java View File

@@ -25,7 +25,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
@@ -49,7 +48,10 @@ import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;

public class DeactivateRuleActionTest {
@Rule
@@ -57,7 +59,7 @@ public class DeactivateRuleActionTest {
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public ExpectedException thrown = ExpectedException.none();
public ExpectedException expectedException = ExpectedException.none();

private DbClient dbClient = db.getDbClient();
private RuleActivator ruleActivator = mock(RuleActivator.class);
@@ -78,17 +80,22 @@ public class DeactivateRuleActionTest {
WebService.Action definition = wsActionTester.getDef();
assertThat(definition).isNotNull();
assertThat(definition.isPost()).isTrue();
assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("profile_key", "rule_key");
assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("profile", "rule");
WebService.Param profileKey = definition.param("profile");
assertThat(profileKey.deprecatedKey()).isEqualTo("profile_key");
WebService.Param ruleKey = definition.param("rule");
assertThat(ruleKey.deprecatedKey()).isEqualTo("rule_key");
}

@Test
public void should_fail_if_not_logged_in() {
TestRequest request = wsActionTester.newRequest()
.setMethod("POST")
.setParam("rule_key", RuleTesting.newRuleDto().getKey().toString())
.setParam("profile_key", randomAlphanumeric(UUID_SIZE));
.setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString())
.setParam(PARAM_PROFILE, randomAlphanumeric(UUID_SIZE));

expectedException.expect(UnauthorizedException.class);

thrown.expect(UnauthorizedException.class);
request.execute();
}

@@ -98,10 +105,11 @@ public class DeactivateRuleActionTest {
QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
TestRequest request = wsActionTester.newRequest()
.setMethod("POST")
.setParam("rule_key", RuleTesting.newRuleDto().getKey().toString())
.setParam("profile_key", qualityProfile.getKee());
.setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString())
.setParam(PARAM_PROFILE, qualityProfile.getKee());

expectedException.expect(ForbiddenException.class);

thrown.expect(ForbiddenException.class);
request.execute();
}

@@ -112,10 +120,10 @@ public class DeactivateRuleActionTest {
QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true));
TestRequest request = wsActionTester.newRequest()
.setMethod("POST")
.setParam("rule_key", RuleTesting.newRuleDto().getKey().toString())
.setParam("profile_key", qualityProfile.getKee());
.setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString())
.setParam(PARAM_PROFILE, qualityProfile.getKee());

thrown.expect(BadRequestException.class);
expectedException.expect(BadRequestException.class);

request.execute();
}
@@ -127,15 +135,15 @@ public class DeactivateRuleActionTest {
RuleKey ruleKey = RuleTesting.randomRuleKey();
TestRequest request = wsActionTester.newRequest()
.setMethod("POST")
.setParam("rule_key", ruleKey.toString())
.setParam("profile_key", qualityProfile.getKee());
.setParam(PARAM_RULE, ruleKey.toString())
.setParam(PARAM_PROFILE, qualityProfile.getKee());

TestResponse response = request.execute();

assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
ArgumentCaptor<RuleKey> ruleKeyCaptor = ArgumentCaptor.forClass(RuleKey.class);
ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class);
Mockito.verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), ruleKeyCaptor.capture());
verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), ruleKeyCaptor.capture());
assertThat(ruleKeyCaptor.getValue()).isEqualTo(ruleKey);
assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee());
}
@@ -148,15 +156,15 @@ public class DeactivateRuleActionTest {
TestRequest request = wsActionTester.newRequest()
.setMethod("POST")
.setParam("organization", organization.getKey())
.setParam("rule_key", ruleKey.toString())
.setParam("profile_key", qualityProfile.getKee());
.setParam(PARAM_RULE, ruleKey.toString())
.setParam(PARAM_PROFILE, qualityProfile.getKee());

TestResponse response = request.execute();

assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
ArgumentCaptor<RuleKey> captor = ArgumentCaptor.forClass(RuleKey.class);
ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class);
Mockito.verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), captor.capture());
verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), captor.capture());
assertThat(captor.getValue()).isEqualTo(ruleKey);
assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee());
}

+ 85
- 84
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java View File

@@ -75,29 +75,30 @@ public class QProfilesWsMediumTest {
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester)
.logIn().setRoot();

private DbClient db;
private DbSession session;
private WsTester wsTester;
private DbClient dbClient;
private DbSession dbSession;
private RuleIndexer ruleIndexer = tester.get(RuleIndexer.class);
private ActiveRuleIndexer activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
private OrganizationDto organization;

private WsTester ws;

@Before
public void setUp() {
tester.clearDbAndIndexes();
db = tester.get(DbClient.class);
wsTester = tester.get(WsTester.class);
session = db.openSession(false);
dbClient = tester.get(DbClient.class);
ws = tester.get(WsTester.class);
dbSession = dbClient.openSession(false);

ruleIndexer = tester.get(RuleIndexer.class);
activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
organization = OrganizationTesting.newOrganizationDto().setKey("org-123");
db.organizationDao().insert(session, organization, false);
dbClient.organizationDao().insert(dbSession, organization, false);
}

@After
public void after() {
session.close();
dbSession.close();
}

@Test
@@ -105,22 +106,22 @@ public class QProfilesWsMediumTest {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto");
createActiveRule(rule, profile);
session.commit();
dbSession.commit();
ruleIndexer.indexRuleDefinition(rule.getKey());
activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1);

// 1. Deactivate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_DEACTIVATE_RULE);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee());
request.setParam(ActivateActionParameters.PARAM_RULE_KEY, rule.getKey().toString());
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_DEACTIVATE_RULE);
request.setParam(PARAM_PROFILE, profile.getKee());
request.setParam(PARAM_RULE, rule.getKey().toString());
request.execute();
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();
}

@Test
@@ -134,20 +135,20 @@ public class QProfilesWsMediumTest {
createActiveRule(rule2, profile);
createActiveRule(rule3, profile);
createActiveRule(rule1, profile);
session.commit();
dbSession.commit();
activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(4);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(4);

// 1. Deactivate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee());
WsTester.Result result = request.execute();
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();
}

@Test
@@ -160,21 +161,21 @@ public class QProfilesWsMediumTest {
createActiveRule(rule1, profile);
createActiveRule(rule0, php);
createActiveRule(rule1, php);
session.commit();
dbSession.commit();
activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(2);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(2);

// 1. Deactivate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee());
WsTester.Result result = request.execute();
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(0);
assertThat(db.activeRuleDao().selectByProfileUuid(session, php.getKee())).hasSize(2);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(0);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).hasSize(2);
}

@Test
@@ -184,61 +185,61 @@ public class QProfilesWsMediumTest {
RuleDefinitionDto rule1 = createRule(profile.getLanguage(), "world");
createActiveRule(rule0, profile);
createActiveRule(rule1, profile);
session.commit();
dbSession.commit();
activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(2);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(2);

// 1. Deactivate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee());
request.setParam(Param.TEXT_QUERY, "hello");
WsTester.Result result = request.execute();
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1);
}

@Test
public void activate_rule() throws Exception {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto");
session.commit();
dbSession.commit();
ruleIndexer.indexRuleDefinition(rule.getKey());

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();

// 1. Activate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE);
request.setParam(PARAM_PROFILE, profile.getKee());
request.setParam(PARAM_RULE, rule.getKey().toString());
WsTester.Result result = request.execute();
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1);
}

@Test
public void activate_rule_diff_languages() throws Exception {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule("php", "toto");
session.commit();
dbSession.commit();
ruleIndexer.indexRuleDefinition(rule.getKey());

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();

try {
// 1. Activate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE);
request.setParam(PARAM_PROFILE, profile.getKee());
request.setParam(PARAM_RULE, rule.getKey().toString());
request.execute();
session.clearCache();
dbSession.clearCache();
fail();
} catch (BadRequestException e) {
assertThat(e.getMessage()).isEqualTo("Rule blah:toto and profile pjava have different languages");
@@ -249,24 +250,24 @@ public class QProfilesWsMediumTest {
public void activate_rule_override_severity() throws Exception {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto");
session.commit();
dbSession.commit();
ruleIndexer.indexRuleDefinition(rule.getKey());

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();

// 1. Activate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE);
request.setParam(PARAM_PROFILE, profile.getKee());
request.setParam(PARAM_RULE, rule.getKey().toString());
request.setParam(PARAM_SEVERITY, "MINOR");
WsTester.Result result = request.execute();
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile, rule.getKey());

Optional<ActiveRuleDto> activeRuleDto = db.activeRuleDao().selectByKey(session, activeRuleKey);
Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, activeRuleKey);
assertThat(activeRuleDto.isPresent()).isTrue();
assertThat(activeRuleDto.get().getSeverityString()).isEqualTo(Severity.MINOR);
}
@@ -278,20 +279,20 @@ public class QProfilesWsMediumTest {
createRule(profile.getLanguage(), "tata");
createRule(profile.getLanguage(), "hello");
createRule(profile.getLanguage(), "world");
session.commit();
dbSession.commit();

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();

// 1. Activate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee());
request.setParam(PARAM_LANGUAGES, "java");
request.execute().assertJson(getClass(), "bulk_activate_rule.json");
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(4);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(4);
}

@Test
@@ -302,20 +303,20 @@ public class QProfilesWsMediumTest {
createRule(java.getLanguage(), "tata");
createRule(php.getLanguage(), "hello");
createRule(php.getLanguage(), "world");
session.commit();
dbSession.commit();

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, php.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).isEmpty();

// 1. Activate Rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, php.getKee());
request.setParam(PARAM_LANGUAGES, "php");
request.execute().assertJson(getClass(), "bulk_activate_rule_not_all.json");
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, php.getKee())).hasSize(2);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).hasSize(2);
}

@Test
@@ -325,30 +326,30 @@ public class QProfilesWsMediumTest {
createRule(profile.getLanguage(), "tata");
createRule(profile.getLanguage(), "hello");
createRule(profile.getLanguage(), "world");
session.commit();
dbSession.commit();

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();

// 1. Activate Rule with query returning 0 hits
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee());
request.setParam(Param.TEXT_QUERY, "php");
request.execute();
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(0);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(0);

// 1. Activate Rule with query returning 1 hits
request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee());
request.setParam(Param.TEXT_QUERY, "world");
request.execute();
session.commit();
dbSession.commit();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1);
}

@Test
@@ -356,10 +357,10 @@ public class QProfilesWsMediumTest {
QProfileDto profile = createProfile("java");
RuleDefinitionDto rule0 = createRule(profile.getLanguage(), "toto");
RuleDefinitionDto rule1 = createRule(profile.getLanguage(), "tata");
session.commit();
dbSession.commit();

// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty();
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty();

// 2. Assert ActiveRule with BLOCKER severity
assertThat(tester.get(RuleIndex.class).search(
@@ -367,14 +368,14 @@ public class QProfilesWsMediumTest {
new SearchOptions()).getIds()).hasSize(2);

// 1. Activate Rule with query returning 2 hits
WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
request.setParam(ActivateRulesAction.PROFILE_KEY, profile.getKee());
request.setParam(ActivateRulesAction.SEVERITY, "MINOR");
request.execute();
session.commit();
dbSession.commit();

// 2. Assert ActiveRule with MINOR severity
assertThat(tester.get(ActiveRuleDao.class).selectByRuleId(session, organization, rule0.getId()).get(0).getSeverityString()).isEqualTo("MINOR");
assertThat(tester.get(ActiveRuleDao.class).selectByRuleId(dbSession, organization, rule0.getId()).get(0).getSeverityString()).isEqualTo("MINOR");
assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery()
.setQProfile(profile)
.setKey(rule0.getKey().toString())
@@ -390,59 +391,59 @@ public class QProfilesWsMediumTest {
QProfileDto phpProfile = createProfile("php");
createRule(phpProfile.getLanguage(), "hello");
createRule(phpProfile.getLanguage(), "world");
session.commit();
dbSession.commit();

// 1. Activate Rule
WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
WsTester.TestRequest request = ws.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION);
request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, javaProfile.getKee());
request.setParam(PARAM_QPROFILE, javaProfile.getKee());
request.setParam("activation", "false");
request.execute().assertJson(getClass(), "does_not_return_warnings_when_bulk_activate_on_profile_and_rules_exist_on_another_language_than_profile.json");
session.clearCache();
dbSession.clearCache();

// 2. Assert ActiveRule in DAO
assertThat(db.activeRuleDao().selectByProfileUuid(session, javaProfile.getKee())).hasSize(2);
assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, javaProfile.getKee())).hasSize(2);
}

@Test
public void reset() throws Exception {
QProfileDto profile = QProfileTesting.newXooP1(organization);
QProfileDto subProfile = QProfileTesting.newXooP2(organization).setParentKee(QProfileTesting.XOO_P1_KEY);
db.qualityProfileDao().insert(session, profile, subProfile);
dbClient.qualityProfileDao().insert(dbSession, profile, subProfile);

RuleDefinitionDto rule = createRule(profile.getLanguage(), "rule");
ActiveRuleDto active1 = ActiveRuleDto.createFor(profile, rule)
.setSeverity(rule.getSeverityString());
ActiveRuleDto active2 = ActiveRuleDto.createFor(subProfile, rule)
.setSeverity("MINOR");
db.activeRuleDao().insert(session, active1);
db.activeRuleDao().insert(session, active2);
dbClient.activeRuleDao().insert(dbSession, active1);
dbClient.activeRuleDao().insert(dbSession, active2);

session.commit();
dbSession.commit();
activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes());

// 0. assert rule child rule is minor
Optional<ActiveRuleDto> activeRuleDto = db.activeRuleDao().selectByKey(session, active2.getKey());
Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, active2.getKey());
assertThat(activeRuleDto.isPresent()).isTrue();
assertThat(activeRuleDto.get().getSeverityString()).isEqualTo(Severity.MINOR);

// 1. reset child rule
WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ACTION_ACTIVATE_RULE);
WsTester.TestRequest request = ws.newPostRequest(API_ENDPOINT, ACTION_ACTIVATE_RULE);
request.setParam(PARAM_PROFILE, subProfile.getKee());
request.setParam(PARAM_RULE, rule.getKey().toString());
request.setParam(PARAM_RESET, "true");
request.execute();
session.clearCache();
dbSession.clearCache();

// 2. assert rule child rule is NOT minor
activeRuleDto = db.activeRuleDao().selectByKey(session, active2.getKey());
activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, active2.getKey());
assertThat(activeRuleDto.isPresent()).isTrue();
assertThat(activeRuleDto.get().getSeverityString()).isNotEqualTo(Severity.MINOR);
}

private QProfileDto createProfile(String lang) {
QProfileDto profile = QProfileTesting.newQProfileDto(organization, new QProfileName(lang, "P" + lang), "p" + lang);
db.qualityProfileDao().insert(session, profile);
dbClient.qualityProfileDao().insert(dbSession, profile);
return profile;
}

@@ -451,8 +452,8 @@ public class QProfilesWsMediumTest {
.setLanguage(lang)
.setSeverity(Severity.BLOCKER)
.setStatus(RuleStatus.READY);
db.ruleDao().insert(session, rule);
session.commit();
dbClient.ruleDao().insert(dbSession, rule);
dbSession.commit();
ruleIndexer.indexRuleDefinition(rule.getKey());
return rule;
}
@@ -460,7 +461,7 @@ public class QProfilesWsMediumTest {
private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) {
ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
.setSeverity(rule.getSeverityString());
db.activeRuleDao().insert(session, activeRule);
dbClient.activeRuleDao().insert(dbSession, activeRule);
return activeRule;
}
}

+ 0
- 1
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java View File

@@ -24,7 +24,6 @@ public class QualityProfileWsParameters {
public static final String CONTROLLER_QUALITY_PROFILES = "api/qualityprofiles";
public interface ActivateActionParameters {
String PARAM_PROFILE_KEY = "profile_key";
String PARAM_RULE_KEY = "rule_key";
}
public static final String ACTION_RESTORE = "restore";


+ 2
- 4
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java View File

@@ -39,7 +39,6 @@ import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_RESTORE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SET_DEFAULT;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.CONTROLLER_QUALITY_PROFILES;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_FROM_KEY;
@@ -55,7 +54,6 @@ import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_UUID;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RESET;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE_KEY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SEVERITY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO_NAME;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.RestoreActionParameters.PARAM_BACKUP;
@@ -79,8 +77,8 @@ public class QualityProfilesService extends BaseService {

public void deactivateRule(String profileKey, String ruleKey) {
PostRequest httpRequest = new PostRequest(path(ACTION_DEACTIVATE_RULE));
httpRequest.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profileKey);
httpRequest.setParam(ActivateActionParameters.PARAM_RULE_KEY, ruleKey);
httpRequest.setParam(PARAM_PROFILE, profileKey);
httpRequest.setParam(PARAM_RULE, ruleKey);
call(httpRequest);
}


+ 2
- 2
sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java View File

@@ -153,8 +153,8 @@ public class QualityProfilesServiceTest {

serviceTester.assertThat(request)
.hasPath("deactivate_rule")
.hasParam(QualityProfileWsParameters.ActivateActionParameters.PARAM_PROFILE_KEY, "P1")
.hasParam(QualityProfileWsParameters.ActivateActionParameters.PARAM_RULE_KEY, "R1")
.hasParam(PARAM_PROFILE, "P1")
.hasParam(PARAM_RULE, "R1")
.andNoOtherParam();
}


Loading…
Cancel
Save