import org.sonar.db.rule.SeverityUtil;
public class ExportRuleDto {
- private Integer activeRuleId;
- private String repository;
- private String rule;
- private String name;
- private String description;
- private String extendedDescription;
- private String template;
- private Integer severity;
- private Integer type;
- private String tags;
+ private Integer activeRuleId = null;
+ private String repository = null;
+ private String rule = null;
+ private String name = null;
+ private String description = null;
+ private String extendedDescription = null;
+ private String template = null;
+ private Integer severity = null;
+ private Integer type = null;
+ private String tags = null;
private List<ExportRuleParamDto> params;
package org.sonar.db.qualityprofile;
public class ExportRuleParamDto {
- private Integer activeRuleId;
- private String kee;
- private String value;
+ private Integer activeRuleId = null;
+ private String kee = null;
+ private String value = null;
public Integer getActiveRuleId() {
return activeRuleId;
List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
assertThat(results).isNotNull();
- assertThat(results).asList()
+ assertThat(results)
.extracting("ruleKey")
.containsOnly(rule1.getKey(), rule2.getKey(), rule3.getKey());
}
public void selectRulesByProfile_verify_columns() {
String language = "java";
RuleDefinitionDto ruleTemplate = createRule(language);
- RuleDefinitionDto rule = createRule(language, RuleStatus.READY, ruleTemplate.getId());
- RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto()
- .setRuleId(rule.getId())
+ RuleDefinitionDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getId());
+ RuleMetadataDto customRuleMetadata = createRuleMetadata(new RuleMetadataDto()
+ .setRuleId(customRule.getId())
.setOrganizationUuid(db.getDefaultOrganization().getUuid())
.setNoteData("Extended description")
.setTags(Sets.newHashSet("tag1", "tag2", "tag3")));
+ RuleDefinitionDto rule = createRule(language, RuleStatus.READY, null);
+ RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto()
+ .setRuleId(rule.getId())
+ .setOrganizationUuid(db.getDefaultOrganization().getUuid()));
+
QProfileDto profile = createProfile(language);
- List<ActiveRuleDto> activeRules = activate(profile, rule);
+ List<ActiveRuleDto> activeRules = activate(profile, customRule, rule);
List<ExportRuleDto> results = underTest.selectRulesByProfile(dbSession, profile);
assertThat(results).isNotNull();
- assertThat(results).asList().isNotEmpty();
-
- ExportRuleDto exportRuleDto = results.get(0);
+ assertThat(results).isNotEmpty();
+
+ //verify custom rule
+ ExportRuleDto exportCustomRuleDto = results.stream().filter(ExportRuleDto::isCustomRule).findFirst().get();
+ assertThat(exportCustomRuleDto).isNotNull();
+ assertThat(exportCustomRuleDto.isCustomRule()).isTrue();
+ assertThat(exportCustomRuleDto.getParams()).isEmpty();
+ assertThat(exportCustomRuleDto.getDescription()).isEqualTo(customRule.getDescription());
+ assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRuleMetadata.getNoteData());
+ assertThat(exportCustomRuleDto.getName()).isEqualTo(customRule.getName());
+ assertThat(exportCustomRuleDto.getRuleKey()).isEqualTo(customRule.getKey());
+ assertThat(exportCustomRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(customRule.getType()));
+ assertThat(exportCustomRuleDto.getTags()).isEqualTo(String.join(",", customRuleMetadata.getTags()));
+ assertThat(exportCustomRuleDto.getTemplateRuleKey()).isEqualTo(ruleTemplate.getKey());
+
+ ActiveRuleDto activeCustomRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(customRule.getKey())).findFirst().get();
+ assertThat(exportCustomRuleDto.getSeverityString()).isEqualTo(activeCustomRule.getSeverityString());
+
+ //verify regular rule
+ ExportRuleDto exportRuleDto = results.stream().filter(regularRule -> !regularRule.isCustomRule()).findFirst().get();
assertThat(exportRuleDto).isNotNull();
- assertThat(exportRuleDto.getParams()).asList().isEmpty();
+ assertThat(exportRuleDto.isCustomRule()).isFalse();
+ assertThat(exportRuleDto.getParams()).isEmpty();
assertThat(exportRuleDto.getDescription()).isEqualTo(rule.getDescription());
assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(ruleMetadata.getNoteData());
assertThat(exportRuleDto.getName()).isEqualTo(rule.getName());
assertThat(exportRuleDto.getRuleKey()).isEqualTo(rule.getKey());
assertThat(exportRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(rule.getType()));
- assertThat(exportRuleDto.getSeverityString()).isEqualTo(activeRules.get(0).getSeverityString());
- assertThat(exportRuleDto.getTags()).isEqualTo(String.join(",", ruleMetadata.getTags()));
- assertThat(exportRuleDto.getTemplateRuleKey()).isEqualTo(ruleTemplate.getKey());
+
+ ActiveRuleDto activeRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(rule.getKey())).findFirst().get();
+ assertThat(exportRuleDto.getSeverityString()).isEqualTo(activeRule.getSeverityString());
}
@Test
.extracting("activeRuleId")
.containsOnly(firstActivatedRule.getId(), secondActivatedRule.getId());
- assertThat(otherProfileResults).asList()
+ assertThat(otherProfileResults)
.extracting("activeRuleId")
.containsOnly(thirdActivatedRule.getId());
import static org.sonar.server.ce.ws.CeWsParameters.PARAM_ONLY_CURRENTS;
import static org.sonar.server.ce.ws.CeWsParameters.PARAM_STATUS;
import static org.sonar.server.ce.ws.CeWsParameters.PARAM_TYPE;
-import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
import static org.sonar.server.exceptions.BadRequestException.checkRequest;
+import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class ActivityAction implements CeWsAction {
private static final int MAX_PAGE_SIZE = 1000;
private static final String[] POSSIBLE_QUALIFIERS = new String[] {Qualifiers.PROJECT, Qualifiers.APP, Qualifiers.VIEW};
+ private static final String INVALID_QUERY_PARAM_ERROR_MESSAGE = "%s and %s must not be set at the same time";
private final UserSession userSession;
private final DbClient dbClient;
.setOnlyCurrents(String.valueOf(request.paramAsBoolean(PARAM_ONLY_CURRENTS)))
.setPs(String.valueOf(request.mandatoryParamAsInt(Param.PAGE_SIZE)));
- checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getQ() == null, "%s and %s must not be set at the same time",
+ checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getQ() == null, INVALID_QUERY_PARAM_ERROR_MESSAGE,
PARAM_COMPONENT_ID, TEXT_QUERY);
- checkRequest(activityWsRequest.getComponent() == null || activityWsRequest.getQ() == null, "%s and %s must not be set at the same time",
+ checkRequest(activityWsRequest.getComponent() == null || activityWsRequest.getQ() == null, INVALID_QUERY_PARAM_ERROR_MESSAGE,
PARAM_COMPONENT, TEXT_QUERY);
- checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getComponent() == null, "%s and %s must not be set at the same time",
+ checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getComponent() == null, INVALID_QUERY_PARAM_ERROR_MESSAGE,
PARAM_COMPONENT_ID, PARAM_COMPONENT);
return activityWsRequest;
private static final String PARAM_PROJECT = "project";
private static final String PARAM_TYPE = "type";
private static final String PARAM_VALUE = "value";
+ private static final String BEGIN_LIST = "<ul>";
+ private static final String END_LIST = "</ul>";
+ private static final String BEGIN_ITEM_LIST = "<li>";
+ private static final String END_ITEM_LIST = "</li>";
+
private static final Set<NewCodePeriodType> OVERALL_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS);
private static final Set<NewCodePeriodType> PROJECT_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS);
private static final Set<NewCodePeriodType> BRANCH_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS, SPECIFIC_ANALYSIS);
WebService.NewAction action = context.createAction("set")
.setPost(true)
.setDescription("Updates the setting for the New Code Period on different levels:<br>" +
- "<ul>" +
- "<li>Project key must be provided to update the value for a project</li>" +
- "<li>Both project and branch keys must be provided to update the value for a branch\n</li>" +
- "</ul>" +
+ BEGIN_LIST +
+ BEGIN_ITEM_LIST + "Project key must be provided to update the value for a project" + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "Both project and branch keys must be provided to update the value for a branch" + END_ITEM_LIST +
+ END_LIST +
"Requires one of the following permissions: " +
- "<ul>" +
- "<li>'Administer System' to change the global setting</li>" +
- "<li>'Administer' rights on the specified project to change the project setting</li>" +
- "</ul>")
+ BEGIN_LIST +
+ BEGIN_ITEM_LIST + "'Administer System' to change the global setting" + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "'Administer' rights on the specified project to change the project setting" + END_ITEM_LIST +
+ END_LIST)
.setSince("8.0")
.setHandler(this);
.setRequired(true)
.setDescription("Type<br/>" +
"New code periods of the following types are allowed:" +
- "<ul>" +
- "<li>" + SPECIFIC_ANALYSIS.name() + " - can be set at branch level only</li>" +
- "<li>" + PREVIOUS_VERSION.name() + " - can be set at any level (global, project, branch)</li>" +
- "<li>" + NUMBER_OF_DAYS.name() + " - can be set can be set at any level (global, project, branch)</li>" +
- "</ul>");
+ BEGIN_LIST +
+ BEGIN_ITEM_LIST + SPECIFIC_ANALYSIS.name() + " - can be set at branch level only" + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + PREVIOUS_VERSION.name() + " - can be set at any level (global, project, branch)" + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + NUMBER_OF_DAYS.name() + " - can be set can be set at any level (global, project, branch)" + END_ITEM_LIST +
+ END_LIST
+ );
action.createParam(PARAM_VALUE)
.setDescription("Value<br/>" +
"For each type, a different value is expected:" +
- "<ul>" +
- "<li>the uuid of an analysis, when type is " + SPECIFIC_ANALYSIS.name() + "</li>" +
- "<li>no value, when type is " + PREVIOUS_VERSION.name() + "</li>" +
- "<li>a number, when type is " + NUMBER_OF_DAYS.name() + "</li>" +
- "</ul>");
+ BEGIN_LIST +
+ BEGIN_ITEM_LIST + "the uuid of an analysis, when type is " + SPECIFIC_ANALYSIS.name() + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "no value, when type is " + PREVIOUS_VERSION.name() + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "a number, when type is " + NUMBER_OF_DAYS.name() + END_ITEM_LIST +
+ END_LIST
+ );
}
@Override
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Projects.CreateWsResponse;
+import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.abbreviate;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH;
return organization;
}
- @CheckForNull
public String getProjectKey() {
return projectKey;
}
- @CheckForNull
public String getName() {
return name;
}
return this;
}
- public Builder setProjectKey(@Nullable String projectKey) {
+ public Builder setProjectKey(String projectKey) {
+ requireNonNull(projectKey);
this.projectKey = projectKey;
return this;
}
- public Builder setName(@Nullable String name) {
+ public Builder setName(String name) {
+ requireNonNull(name);
this.name = name;
return this;
}
}
public CreateRequest build() {
+ requireNonNull(projectKey);
+ requireNonNull(name);
return new CreateRequest(this);
}
}
import java.util.List;
+import static java.util.Objects.requireNonNull;
+
class ImportedQProfile {
private final String profileName;
private final String profileLang;
private final List<ImportedRule> rules;
public ImportedQProfile(String profileName, String profileLang, List<ImportedRule> rules) {
+ requireNonNull(profileName, "Profile name should not be empty!");
+ requireNonNull(profileLang, "Profile language should not be empty!");
this.profileName = profileName;
this.profileLang = profileLang;
this.rules = rules;
import org.sonar.api.rule.RuleKey;
class ImportedRule {
- private RuleKey ruleKey;
- private RuleKey templateKey;
- private String name;
- private String type;
- private String severity;
- private String description;
+ private RuleKey ruleKey = null;
+ private RuleKey templateKey = null;
+ private String name = null;
+ private String type = null;
+ private String severity = null;
+ private String description = null;
+ private Map<String, String> parameters = null;
public Map<String, String> getParameters() {
return parameters;
}
- private Map<String, String> parameters;
-
public RuleKey getRuleKey() {
return ruleKey;
}
*/
package org.sonar.server.qualityprofile;
-import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
+import java.util.stream.Collectors;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.lang.StringUtils;
@ServerSide
public class QProfileParser {
- private static final Joiner RULE_KEY_JOINER = Joiner.on(", ").skipNulls();
-
private static final String ATTRIBUTE_PROFILE = "profile";
private static final String ATTRIBUTE_NAME = "name";
private static final String ATTRIBUTE_LANGUAGE = "language";
private static final String ATTRIBUTE_PARAMETER_KEY = "key";
private static final String ATTRIBUTE_PARAMETER_VALUE = "value";
- public QProfileParser() {
- }
-
public void writeXml(Writer writer, QProfileDto profile, Iterator<ExportRuleDto> rulesToExport) {
XmlWriter xml = XmlWriter.of(writer).declaration();
xml.begin(ATTRIBUTE_PROFILE);
}
if (!duplicatedKeys.isEmpty()) {
throw new IllegalArgumentException("The quality profile cannot be restored as it contains duplicates for the following rules: " +
- RULE_KEY_JOINER.join(duplicatedKeys));
+ duplicatedKeys.stream().map(RuleKey::toString).filter(Objects::nonNull).collect(Collectors.joining(", ")));
}
return activations;
}
@ServerSide
public class RuleCreator {
+ private static final String TEMPLATE_KEY_NOT_EXIST_FORMAT = "The template key doesn't exist: %s";
private final System2 system2;
private final RuleIndexer ruleIndexer;
OrganizationDto defaultOrganization = dbClient.organizationDao().selectByUuid(dbSession, defaultOrganizationUuid)
.orElseThrow(() -> new IllegalStateException(format("Could not find default organization for uuid '%s'", defaultOrganizationUuid)));
RuleDto templateRule = dbClient.ruleDao().selectByKey(dbSession, defaultOrganization.getUuid(), templateKey)
- .orElseThrow(() -> new IllegalArgumentException(format("The template key doesn't exist: %s", templateKey)));
+ .orElseThrow(() -> new IllegalArgumentException(format(TEMPLATE_KEY_NOT_EXIST_FORMAT, templateKey)));
checkArgument(templateRule.isTemplate(), "This rule is not a template rule: %s", templateKey.toString());
- checkArgument(templateRule.getStatus() != RuleStatus.REMOVED, "The template key doesn't exist: %s", templateKey.toString());
+ checkArgument(templateRule.getStatus() != RuleStatus.REMOVED, TEMPLATE_KEY_NOT_EXIST_FORMAT, templateKey.toString());
validateCustomRule(newRule, dbSession, templateKey);
RuleKey customRuleKey = RuleKey.of(templateRule.getRepositoryKey(), newRule.ruleKey());
checkArgument(!templateRules.isEmpty() && templateKeys.size() == templateRules.size(), "Rule template keys should exists for each custom rule!");
templateRules.values().forEach(ruleDto -> {
checkArgument(ruleDto.isTemplate(), "This rule is not a template rule: %s", ruleDto.getKey().toString());
- checkArgument(ruleDto.getStatus() != RuleStatus.REMOVED, "The template key doesn't exist: %s", ruleDto.getKey().toString());
+ checkArgument(ruleDto.getStatus() != RuleStatus.REMOVED, TEMPLATE_KEY_NOT_EXIST_FORMAT, ruleDto.getKey().toString());
}
);
package org.sonar.server.project.ws;
import com.google.common.base.Strings;
+import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The 'project' parameter is missing");
- call(CreateRequest.builder().setName(DEFAULT_PROJECT_NAME).build());
+ call(null, null, DEFAULT_PROJECT_NAME);
}
@Test
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The 'name' parameter is missing");
- call(CreateRequest.builder().setProjectKey(DEFAULT_PROJECT_KEY).build());
+ call(null, DEFAULT_PROJECT_KEY, null);
}
@Test
assertThat(name.description()).isEqualTo("Name of the project. If name is longer than 500, it is abbreviated.");
}
+ @Test
+ public void fail_when_set_null_project_name_on_create_request_builder() {
+ expectedException.expect(NullPointerException.class);
+
+ CreateRequest.builder()
+ .setProjectKey(DEFAULT_PROJECT_KEY)
+ .setName(null);
+ }
+
+ @Test
+ public void fail_when_set_null_project_key_on_create_request_builder() {
+ expectedException.expect(NullPointerException.class);
+
+ CreateRequest.builder()
+ .setProjectKey(null)
+ .setName(DEFAULT_PROJECT_NAME);
+ }
+
+ @Test
+ public void fail_when_project_key_not_set_on_create_request_builder() {
+ expectedException.expect(NullPointerException.class);
+ CreateRequest.builder()
+ .setName(DEFAULT_PROJECT_NAME)
+ .build();
+ }
+
+ @Test
+ public void fail_when_project_name_not_set_on_create_request_builder() {
+ expectedException.expect(NullPointerException.class);
+
+ CreateRequest.builder()
+ .setProjectKey(DEFAULT_PROJECT_KEY)
+ .build();
+ }
+
private CreateWsResponse call(CreateRequest request) {
+ return call(request.getOrganization(), request.getProjectKey(), request.getName());
+ }
+
+ private CreateWsResponse call(@Nullable String organization, @Nullable String projectKey, @Nullable String projectName) {
TestRequest httpRequest = ws.newRequest()
.setMethod(POST.name());
- ofNullable(request.getOrganization()).ifPresent(org -> httpRequest.setParam("organization", org));
- ofNullable(request.getProjectKey()).ifPresent(key -> httpRequest.setParam("project", key));
- ofNullable(request.getName()).ifPresent(name -> httpRequest.setParam("name", name));
+ ofNullable(organization).ifPresent(org -> httpRequest.setParam("organization", org));
+ ofNullable(projectKey).ifPresent(key -> httpRequest.setParam("project", key));
+ ofNullable(projectName).ifPresent(name -> httpRequest.setParam("name", name));
return httpRequest.executeProtobuf(CreateWsResponse.class);
}
package org.sonar.api.impl.utils;
import java.util.TimeZone;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import static org.assertj.core.api.Assertions.assertThat;
+
public class TestSystem2Test {
public void test_tick() {
underTest.setNow(1000L);
underTest.tick();
- Assert.assertEquals(underTest.now(), 1001L);
+ assertThat(underTest.now()).isEqualTo(1001L);
}
@Test
public void test_now() {
underTest.setNow(1000L);
- Assert.assertEquals(underTest.now(), 1000L);
+ assertThat(underTest.now()).isEqualTo(1000L);
}
@Test
public void test_default_time_zone() {
underTest.setDefaultTimeZone(TimeZone.getDefault());
TimeZone result = underTest.getDefaultTimeZone();
- Assert.assertNotNull(result);
- Assert.assertEquals(result.getID(), TimeZone.getDefault().getID());
+ assertThat(result.getID()).isEqualTo(TimeZone.getDefault().getID());
}
@Test