]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some code smell debt (#2143)
authorJacek <52388493+jacek-poreda-sonarsource@users.noreply.github.com>
Tue, 15 Oct 2019 10:05:53 +0000 (12:05 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 15 Oct 2019 18:21:05 +0000 (20:21 +0200)
12 files changed:
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleParamDto.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedQProfile.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ImportedRule.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileParser.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/RuleCreator.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/CreateActionTest.java
sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/utils/TestSystem2Test.java

index 9e0c123cef55c7d467cd67e050ce8eb5c8a5bb37..44eab6830a2ca0216d8aa3942842ef6831e5bc0b 100644 (file)
@@ -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<ExportRuleParamDto> params;
 
index 1222480dd5aa0ec76e159a40f92bd7ec7206bc43..2597d24d124ea0ba08f3781bde5cfd89eef76a6e 100644 (file)
@@ -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;
index beed341c565319e2656f38b2739767f0a8bb901e..d5512df26b0022277767d19551e287ea46128845 100644 (file)
@@ -63,7 +63,7 @@ public class QualityProfileExportDaoTest {
 
     List<ExportRuleDto> 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<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
@@ -149,7 +172,7 @@ public class QualityProfileExportDaoTest {
       .extracting("activeRuleId")
       .containsOnly(firstActivatedRule.getId(), secondActivatedRule.getId());
 
-    assertThat(otherProfileResults).asList()
+    assertThat(otherProfileResults)
       .extracting("activeRuleId")
       .containsOnly(thirdActivatedRule.getId());
 
index 7053ba1757c7e114b927961b8293fd3ee5ab8c12..07c68d707694c589aee17165c9996cdcf6df3453 100644 (file)
@@ -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;
index 3effb0b21e416026f2f9335b0ac620b86293dc5e..bb35dec2f028f6ec5472bda83b1bd401b886b5e3 100644 (file)
@@ -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 = "<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);
@@ -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:<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);
 
@@ -99,19 +104,21 @@ public class SetAction implements NewCodePeriodsWsAction {
       .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
index e00eb10bc24950028726da7021aa8ec6d1700527..622275a3ffa7dfd0554518634397059f715c1dfa 100644 (file)
@@ -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);
     }
   }
index be10bdbc5039bf65fc0e8d00ab60a9a544e7dbc3..2f7db341a8e26454503f1f0f21a72f890c84f226 100644 (file)
@@ -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<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;
index 484bc00c2074a39b8cf8bf83a5db8d6731888fcc..1e7257901069684cdbd0436f909c9740cdaaacad 100644 (file)
@@ -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<String, String> parameters = null;
 
   public Map<String, String> getParameters() {
     return parameters;
   }
 
-  private Map<String, String> parameters;
-
   public RuleKey getRuleKey() {
     return ruleKey;
   }
index fad9bd08447553682efad73f31e5ba14a8a12517..a74e2b8574c2d32c648c881e9a5147a5a7659ee2 100644 (file)
@@ -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<ExportRuleDto> 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;
   }
index e841491dde73ba61399728d562c4af01f611fa73..21ba16cd14abf6e527a8248c3934d16cff919345 100644 (file)
@@ -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());
       }
     );
 
index bb97891c2e4ef4a7030701a25651bd947b1fe759..fed535f4526bc841dd8661b56898de401e371463 100644 (file)
@@ -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);
   }
 
index bedcbc6f4a35a38f0dc2293dce2679bc9bbf4d43..622a749f8b135a3807de5ef219c44e643b103d53 100644 (file)
 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