From: Jacek <52388493+jacek-poreda-sonarsource@users.noreply.github.com> Date: Tue, 15 Oct 2019 10:05:53 +0000 (+0200) Subject: Fix some code smell debt (#2143) X-Git-Tag: 8.1.0.31237~194 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b83cfa7f76f6732058ea0bb2dc54a824c9df6781;p=sonarqube.git Fix some code smell debt (#2143) --- diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java index 9e0c123cef5..44eab6830a2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java @@ -26,16 +26,16 @@ import org.sonar.api.rules.RuleType; 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 params; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java index 1222480dd5a..2597d24d124 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java @@ -20,9 +20,9 @@ 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; diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java index beed341c565..d5512df26b0 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java @@ -63,7 +63,7 @@ public class QualityProfileExportDaoTest { List results = underTest.selectRulesByProfile(dbSession, profile); assertThat(results).isNotNull(); - assertThat(results).asList() + assertThat(results) .extracting("ruleKey") .containsOnly(rule1.getKey(), rule2.getKey(), rule3.getKey()); } @@ -72,32 +72,55 @@ public class QualityProfileExportDaoTest { 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 activeRules = activate(profile, rule); + List activeRules = activate(profile, customRule, rule); List 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 @@ -149,7 +172,7 @@ public class QualityProfileExportDaoTest { .extracting("activeRuleId") .containsOnly(firstActivatedRule.getId(), secondActivatedRule.getId()); - assertThat(otherProfileResults).asList() + assertThat(otherProfileResults) .extracting("activeRuleId") .containsOnly(thirdActivatedRule.getId()); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java index 7053ba1757c..07c68d70769 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java @@ -67,13 +67,14 @@ import static org.sonar.server.ce.ws.CeWsParameters.PARAM_MIN_SUBMITTED_AT; 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; @@ -303,13 +304,13 @@ public class ActivityAction implements CeWsAction { .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; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java index 3effb0b21e4..bb35dec2f02 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java @@ -56,6 +56,11 @@ public class SetAction implements NewCodePeriodsWsAction { 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 = "
    "; + private static final String END_LIST = "
"; + private static final String BEGIN_ITEM_LIST = "
  • "; + private static final String END_ITEM_LIST = "
  • "; + private static final Set OVERALL_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS); private static final Set PROJECT_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS); private static final Set BRANCH_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS, SPECIFIC_ANALYSIS); @@ -79,15 +84,15 @@ public class SetAction implements NewCodePeriodsWsAction { WebService.NewAction action = context.createAction("set") .setPost(true) .setDescription("Updates the setting for the New Code Period on different levels:
    " + - "
      " + - "
    • Project key must be provided to update the value for a project
    • " + - "
    • Both project and branch keys must be provided to update the value for a branch\n
    • " + - "
    " + + 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: " + - "
      " + - "
    • 'Administer System' to change the global setting
    • " + - "
    • 'Administer' rights on the specified project to change the project setting
    • " + - "
    ") + 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); @@ -99,19 +104,21 @@ public class SetAction implements NewCodePeriodsWsAction { .setRequired(true) .setDescription("Type
    " + "New code periods of the following types are allowed:" + - "
      " + - "
    • " + SPECIFIC_ANALYSIS.name() + " - can be set at branch level only
    • " + - "
    • " + PREVIOUS_VERSION.name() + " - can be set at any level (global, project, branch)
    • " + - "
    • " + NUMBER_OF_DAYS.name() + " - can be set can be set at any level (global, project, branch)
    • " + - "
    "); + 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
    " + "For each type, a different value is expected:" + - "
      " + - "
    • the uuid of an analysis, when type is " + SPECIFIC_ANALYSIS.name() + "
    • " + - "
    • no value, when type is " + PREVIOUS_VERSION.name() + "
    • " + - "
    • a number, when type is " + NUMBER_OF_DAYS.name() + "
    • " + - "
    "); + 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 diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java index e00eb10bc24..622275a3ffa 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java @@ -34,6 +34,7 @@ import org.sonar.server.project.Visibility; 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; @@ -161,12 +162,10 @@ public class CreateAction implements ProjectsWsAction { return organization; } - @CheckForNull public String getProjectKey() { return projectKey; } - @CheckForNull public String getName() { return name; } @@ -196,12 +195,14 @@ public class CreateAction implements ProjectsWsAction { 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; } @@ -212,6 +213,8 @@ public class CreateAction implements ProjectsWsAction { } public CreateRequest build() { + requireNonNull(projectKey); + requireNonNull(name); return new CreateRequest(this); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedQProfile.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedQProfile.java index be10bdbc503..2f7db341a8e 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedQProfile.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedQProfile.java @@ -21,6 +21,8 @@ package org.sonar.server.qualityprofile; import java.util.List; +import static java.util.Objects.requireNonNull; + class ImportedQProfile { private final String profileName; private final String profileLang; @@ -28,6 +30,8 @@ class ImportedQProfile { private final List rules; public ImportedQProfile(String profileName, String profileLang, List 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; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedRule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedRule.java index 484bc00c207..1e725790106 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedRule.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedRule.java @@ -23,19 +23,18 @@ import java.util.Map; 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 parameters = null; public Map getParameters() { return parameters; } - private Map parameters; - public RuleKey getRuleKey() { return ruleKey; } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileParser.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileParser.java index fad9bd08447..a74e2b8574c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileParser.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileParser.java @@ -19,7 +19,6 @@ */ 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; @@ -29,7 +28,9 @@ import java.util.HashSet; 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; @@ -45,8 +46,6 @@ import org.sonar.db.qualityprofile.QProfileDto; @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"; @@ -65,9 +64,6 @@ public class QProfileParser { 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 rulesToExport) { XmlWriter xml = XmlWriter.of(writer).declaration(); xml.begin(ATTRIBUTE_PROFILE); @@ -190,7 +186,7 @@ public class QProfileParser { } 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; } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/RuleCreator.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/RuleCreator.java index e841491dde7..21ba16cd14a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/RuleCreator.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/RuleCreator.java @@ -56,6 +56,7 @@ import static org.sonar.server.exceptions.BadRequestException.checkRequest; @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; @@ -78,9 +79,9 @@ public class RuleCreator { 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()); @@ -108,7 +109,7 @@ public class RuleCreator { 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()); } ); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java index bb97891c2e4..fed535f4526 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java @@ -20,6 +20,7 @@ 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; @@ -291,7 +292,7 @@ public class CreateActionTest { 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 @@ -301,7 +302,7 @@ public class CreateActionTest { 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 @@ -360,12 +361,51 @@ public class CreateActionTest { 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); } diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java index bedcbc6f4a3..622a749f8b1 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java @@ -20,12 +20,13 @@ 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 { @@ -43,21 +44,20 @@ 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