import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.util.HashMap;
import java.util.Map;
return severity;
}
- public ActiveRuleChange setSeverity(@Nullable String severity) {
- this.severity = severity;
+ public ActiveRuleChange setSeverity(@Nullable String s) {
+ this.severity = s;
return this;
}
- public ActiveRuleChange setInheritance(@Nullable ActiveRule.Inheritance inheritance) {
- this.inheritance = inheritance;
+ public ActiveRuleChange setInheritance(@Nullable ActiveRule.Inheritance i) {
+ this.inheritance = i;
return this;
}
@CheckForNull
public ActiveRule.Inheritance getInheritance() {
- return this.inheritance;
+ return inheritance;
}
- @CheckForNull
public Map<String, String> getParameters() {
return parameters;
}
public Map<String, String> getDetails() {
HashMap<String, String> details = new HashMap<String, String>();
- if (getKey() != null) {
- details.put("key", getKey().toString());
- details.put("ruleKey", getKey().ruleKey().toString());
- details.put("profileKey", getKey().qProfile().toString());
- }
- if (!parameters.isEmpty()) {
- for (Map.Entry<String, String> param : parameters.entrySet()) {
- if (!param.getKey().isEmpty()) {
- details.put("param_" + param.getKey(), param.getValue());
- }
+ details.put("key", getKey().toString());
+ details.put("ruleKey", getKey().ruleKey().toString());
+ details.put("profileKey", getKey().qProfile().toString());
+
+ for (Map.Entry<String, String> param : parameters.entrySet()) {
+ if (!param.getKey().isEmpty()) {
+ details.put("param_" + param.getKey(), param.getValue());
}
}
if (StringUtils.isNotEmpty(severity)) {
@Override
public String getAction() {
- return this.type.name();
+ return type.name();
}
}
RuleDto ruleDto = db.ruleDao().getNullableByKey(session, profileActivity.ruleKey());
profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null);
- UserDto user = db.userDao().selectActiveUserByLogin(profileActivity.login(), session);
- profileActivity.authorName(user != null ? user.getName() : null);
+ String login = profileActivity.login();
+ if (login != null) {
+ UserDto user = db.userDao().selectActiveUserByLogin(login, session);
+ profileActivity.authorName(user != null ? user.getName() : null);
+ }
result.getHits().add(profileActivity);
}
return result;
}
boolean isSame(ActiveRuleChange change) {
- if (change.getInheritance() != null && !change.getInheritance().name().equals(activeRule.getInheritance())) {
+ ActiveRule.Inheritance inheritance = change.getInheritance();
+ if (inheritance != null && !inheritance.name().equals(activeRule.getInheritance())) {
return false;
}
- if (change.getSeverity() != null && !change.getSeverity().equals(activeRule.getSeverityString())) {
+ String severity = change.getSeverity();
+ if (severity != null && !severity.equals(activeRule.getSeverityString())) {
return false;
}
for (Map.Entry<String, String> changeParam : change.getParameters().entrySet()) {
private RuleActivatorContext create(QualityProfileDto profile, RuleKey ruleKey, DbSession session, RuleActivatorContext context) {
initRule(ruleKey, context, session);
initActiveRules(profile.getKey(), ruleKey, context, session, false);
- if (profile.getParentKee() != null) {
- initActiveRules(profile.getParentKee(), ruleKey, context, session, true);
+ String parentKee = profile.getParentKee();
+ if (parentKee != null) {
+ initActiveRules(parentKee, ruleKey, context, session, true);
}
return context;
}
public ActiveRuleDoc(Map<String, Object> fields) {
super(fields);
- this.key = ActiveRuleKey.parse((String) getNullableField(ActiveRuleNormalizer.ActiveRuleField.KEY.field()));
+ this.key = ActiveRuleKey.parse((String) getField(ActiveRuleNormalizer.ActiveRuleField.KEY.field()));
Preconditions.checkArgument(key!=null, "Invalid ActiveRuleKey!");
}
import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleNormalizer;
import org.sonar.server.rule.index.RuleQuery;
}
@CheckForNull
- public org.sonar.server.rule.Rule getByKey(RuleKey key) {
+ public Rule getByKey(RuleKey key) {
return index.getByKey(key);
}
+ public Rule getNonNullByKey(RuleKey key) {
+ Rule rule = index.getByKey(key);
+ if (rule == null) {
+ throw new NotFoundException("Rule not found: " + key);
+ }
+ return rule;
+ }
+
public RuleQuery newRuleQuery() {
return new RuleQuery();
}
return qProfileKey;
}
- public RuleQuery setQProfileKey(String qProfileKey) {
- this.qProfileKey = qProfileKey;
+ public RuleQuery setQProfileKey(@Nullable String s) {
+ this.qProfileKey = s;
return this;
}
return this;
}
- public RuleQuery setAvailableSince(Date since) {
- this.availableSince = since;
+ public RuleQuery setAvailableSince(@Nullable Date d) {
+ this.availableSince = d;
return this;
}
return this.availableSince;
}
- public RuleQuery setInternalKey(String internalKey) {
- this.internalKey = internalKey;
+ public RuleQuery setInternalKey(@Nullable String s) {
+ this.internalKey = s;
return this;
}
return internalKey;
}
- public RuleQuery setRuleKey(String ruleKey) {
- this.ruleKey = ruleKey;
+ public RuleQuery setRuleKey(@Nullable String s) {
+ this.ruleKey = s;
return this;
}
void completeSearch(RuleQuery query, Collection<Rule> rules, JsonWriter json) {
json.name("actives").beginObject();
- if (query.getQProfileKey() != null) {
+ String profileKey = query.getQProfileKey();
+ if (profileKey != null) {
// Load details of active rules on the selected profile
for (Rule rule : rules) {
- ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(query.getQProfileKey(), rule.key()));
+ ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(profileKey, rule.key()));
if (activeRule != null) {
writeActiveRules(rule.key(), Arrays.asList(activeRule), json);
}
.prop("qProfile", activeRule.key().qProfile().toString())
.prop("inherit", activeRule.inheritance().toString())
.prop("severity", activeRule.severity());
- if (activeRule.parentKey() != null) {
- json.prop("parent", activeRule.parentKey().toString());
+ ActiveRuleKey parentKey = activeRule.parentKey();
+ if (parentKey != null) {
+ json.prop("parent", parentKey.toString());
}
json.name("params").beginArray();
for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
}
private void writeResponse(Response response, RuleKey ruleKey) {
- Rule rule = service.getByKey(ruleKey);
+ Rule rule = service.getNonNullByKey(ruleKey);
JsonWriter json = response.newJsonWriter().beginObject().name("rule");
mapping.write((BaseDoc) rule, json);
json.endObject().close();
}
private void write409(Response response, RuleKey ruleKey) {
- Rule rule = service.getByKey(ruleKey);
+ Rule rule = service.getNonNullByKey(ruleKey);
Response.Stream stream = response.stream();
stream.setStatus(409);
}
private void writeResponse(Response response, RuleKey ruleKey) {
- Rule rule = service.getByKey(ruleKey);
+ Rule rule = service.getNonNullByKey(ruleKey);
JsonWriter json = response.newJsonWriter().beginObject().name("rule");
mapping.write((BaseDoc) rule, json);
json.endObject().close();
import org.sonar.core.persistence.DbSession;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.db.DbClient;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.rule.db.RuleDao;
import org.sonar.server.rule.index.RuleIndex;
import java.util.Set;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
public class RuleServiceMediumTest {
Rule rule = service.getByKey(key);
assertThat(rule).isNotNull();
+
+ assertThat(service.getByKey(RuleKey.of("un", "known"))).isNull();
+ }
+
+ @Test
+ public void get_non_null_rule_by_key() throws Exception {
+ MockUserSession.set()
+ .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
+ .setLogin("me");
+
+ RuleKey key = RuleKey.of("java", "S001");
+
+ dao.insert(dbSession, RuleTesting.newDto(key));
+ dbSession.commit();
+ dbSession.clearCache();
+
+ assertThat(service.getNonNullByKey(key)).isNotNull();
+ try {
+ service.getNonNullByKey(RuleKey.of("un", "known"));
+ fail();
+ } catch (NotFoundException e) {
+ assertThat(e).hasMessage("Rule not found: un:known");
+ }
}
@Test