import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
-import org.sonar.api.resources.Qualifiers;
import static java.util.Objects.requireNonNull;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.db.component.ComponentValidator.checkComponentKey;
import static org.sonar.db.component.ComponentValidator.checkComponentName;
import static org.sonar.db.component.ComponentValidator.checkComponentQualifier;
this.organizationUuid = builder.organizationUuid;
this.key = builder.key;
this.branch = builder.branch;
- this.qualifier = builder.qualifier == null ? Qualifiers.PROJECT : checkComponentQualifier(builder.qualifier);
+ this.qualifier = builder.qualifier;
this.name = builder.name;
}
private String organizationUuid;
private String key;
private String branch;
- private String qualifier;
+ private String qualifier = PROJECT;
private String name;
private Builder() {
public NewComponent build() {
requireNonNull(organizationUuid, "organization uuid can't be null");
- checkComponentKey(requireNonNull(key, "key can't be null"));
- checkComponentName(requireNonNull(name, "name can't be null"));
+ checkComponentKey(key);
+ checkComponentName(name);
+ checkComponentQualifier(qualifier);
return new NewComponent(this);
}
}
WebService.NewAction action = context.createAction(ACTION_CREATE)
.setDescription("Create a project.<br/>" +
"Requires 'Create Projects' permission<br/>" +
- "Since 6.3, the response has been updated and does not contain the database ID anymore")
+ "Since 6.3, the response format has been updated and does not contain the database ID anymore<br/>" +
+ "Since 6.3, the 'key' parameter has been renamed 'project'")
.setSince("4.0")
.setPost(true)
.setResponseExample(getClass().getResource("create-example.json"))
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.resources.Qualifiers;
import static com.google.common.base.Strings.repeat;
import static org.assertj.core.api.Java6Assertions.assertThat;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.server.component.NewComponent.newComponentBuilder;
public class NewComponentTest {
}
@Test
- public void build_throws_NPE_when_key_is_null() {
+ public void build_throws_IAE_when_key_is_null() {
underTest.setOrganizationUuid(ORGANIZATION_UUID);
- expectBuildException(NullPointerException.class, "key can't be null");
+ expectBuildException(IllegalArgumentException.class, "Component key can't be empty");
+ }
+
+ @Test
+ public void build_throws_IAE_when_key_is_empty() {
+ underTest
+ .setKey("")
+ .setOrganizationUuid(ORGANIZATION_UUID);
+
+ expectBuildException(IllegalArgumentException.class, "Component key can't be empty");
}
@Test
}
@Test
- public void build_fails_with_NPE_when_name_is_null() {
+ public void build_fails_with_IAE_when_name_is_null() {
underTest.setOrganizationUuid(ORGANIZATION_UUID)
.setKey(KEY);
- expectBuildException(NullPointerException.class, "name can't be null");
+ expectBuildException(IllegalArgumentException.class, "Component name can't be empty");
+ }
+
+ @Test
+ public void build_fails_with_IAE_when_name_is_empty() {
+ underTest.setOrganizationUuid(ORGANIZATION_UUID)
+ .setKey(KEY)
+ .setName("");
+
+ expectBuildException(IllegalArgumentException.class, "Component name can't be empty");
}
@Test
"Component name length (2001) is longer than the maximum authorized (2000)");
}
+ @Test
+ public void build_fails_with_IAE_when_qualifier_is_null() {
+ underTest.setOrganizationUuid(ORGANIZATION_UUID)
+ .setKey(KEY)
+ .setName(NAME)
+ .setQualifier(null);
+
+ expectBuildException(IllegalArgumentException.class, "Component qualifier can't be empty");
+ }
+
+ @Test
+ public void build_fails_with_IAE_when_qualifier_is_empty() {
+ underTest.setOrganizationUuid(ORGANIZATION_UUID)
+ .setKey(KEY)
+ .setName(NAME)
+ .setQualifier("");
+
+ expectBuildException(IllegalArgumentException.class, "Component qualifier can't be empty");
+ }
+
@Test
public void build_fails_with_IAE_when_qualifier_is_longer_than_10_characters() {
underTest.setOrganizationUuid(ORGANIZATION_UUID)
@Test
public void getQualifier_returns_PROJECT_when_no_set_in_builder() {
NewComponent newComponent = underTest.setOrganizationUuid(ORGANIZATION_UUID)
- .setKey(KEY)
- .setName(NAME)
- .build();
+ .setKey(KEY)
+ .setName(NAME)
+ .build();
- assertThat(newComponent.qualifier()).isEqualTo(Qualifiers.PROJECT);
+ assertThat(newComponent.qualifier()).isEqualTo(PROJECT);
}
private void expectBuildException(Class<? extends Exception> expectedExceptionType, String expectedMessage) {
package org.sonar.db.component;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Strings.isNullOrEmpty;
import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH;
public class ComponentValidator {
}
public static String checkComponentName(String name) {
+ checkArgument(!isNullOrEmpty(name), "Component name can't be empty");
checkArgument(name.length() <= MAX_COMPONENT_NAME_LENGTH, "Component name length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
name.length(), MAX_COMPONENT_NAME_LENGTH, name);
return name;
}
public static String checkComponentKey(String key) {
+ checkArgument(!isNullOrEmpty(key), "Component key can't be empty");
checkArgument(key.length() <= MAX_COMPONENT_KEY_LENGTH, "Component key length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
key.length(), MAX_COMPONENT_KEY_LENGTH, key);
return key;
}
public static String checkComponentQualifier(String qualifier) {
+ checkArgument(!isNullOrEmpty(qualifier), "Component qualifier can't be empty");
checkArgument(qualifier.length() <= MAX_COMPONENT_QUALIFIER_LENGTH, "Component qualifier length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
qualifier.length(), MAX_COMPONENT_QUALIFIER_LENGTH, qualifier);
return qualifier;