import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.BadRequestException;
import static java.lang.String.format;
}
if (definition.type() == PropertyType.METRIC) {
- metric(data);
+ validateMetric(data);
+ } else if (definition.type() == PropertyType.USER_LOGIN) {
+ validateLogin(data);
} else {
- otherTypes(data, definition);
+ validateOtherTypes(data, definition);
}
}
- private void otherTypes(SettingData data, PropertyDefinition definition) {
+ private void validateOtherTypes(SettingData data, PropertyDefinition definition) {
data.values.stream()
.map(definition::validate)
.filter(result -> !result.isValid())
});
}
- private void metric(SettingData data) {
+ private void validateMetric(SettingData data) {
try (DbSession dbSession = dbClient.openSession(false)) {
- List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, data.values);
+ List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, data.values).stream().filter(MetricDto::isEnabled).collect(Collectors.toList());
checkRequest(data.values.size() == metrics.size(), "Error when validating metric setting with key '%s' and values [%s]. A value is not a valid metric key.",
data.key, data.values.stream().collect(Collectors.joining(", ")));
}
}
+
+ private void validateLogin(SettingData data) {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ List<UserDto> users = dbClient.userDao().selectByLogins(dbSession, data.values).stream().filter(UserDto::isActive).collect(Collectors.toList());
+ checkRequest(data.values.size() == users.size(), "Error when validating login setting with key '%s' and values [%s]. A value is not a valid login.",
+ data.key, data.values.stream().collect(Collectors.joining(", ")));
+ }
+ }
}
}
import org.sonar.db.property.PropertyDbTester;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
+import org.sonar.db.user.UserDbTester;
import org.sonar.scanner.protocol.GsonHelper;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import static org.sonar.db.metric.MetricTesting.newMetricDto;
import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto;
import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
public class SetActionTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
PropertyDbTester propertyDb = new PropertyDbTester(db);
+ UserDbTester userDb = new UserDbTester(db);
ComponentDbTester componentDb = new ComponentDbTester(db);
DbClient dbClient = db.getDbClient();
DbSession dbSession = db.getSession();
assertGlobalSetting("my.key", "metric.key.1,metric.key.2");
}
+ @Test
+ public void persist_multi_value_with_type_login() {
+ definitions.addComponent(PropertyDefinition
+ .builder("my.key")
+ .name("foo")
+ .description("desc")
+ .category("cat")
+ .subCategory("subCat")
+ .type(PropertyType.USER_LOGIN)
+ .defaultValue("default")
+ .multiValues(true)
+ .build());
+ userDb.insertUser(newUserDto().setLogin("login.1"));
+ userDb.insertUser(newUserDto().setLogin("login.2"));
+
+ callForMultiValueGlobalSetting("my.key", newArrayList("login.1", "login.2"));
+
+ assertGlobalSetting("my.key", "login.1,login.2");
+ }
+
@Test
public void user_setting_is_not_updated() {
propertyDb.insertProperty(newGlobalPropertyDto("my.key", "my user value").setUserId(42L));
.multiValues(true)
.build());
dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric.key"));
+ dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric.disabled.key").setEnabled(false));
dbSession.commit();
expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Error when validating metric setting with key 'my.key' and values [metric.key, My Value]. A value is not a valid metric key.");
+ expectedException.expectMessage("Error when validating metric setting with key 'my.key' and values [metric.key, metric.disabled.key]. A value is not a valid metric key.");
+
+ callForMultiValueGlobalSetting("my.key", newArrayList("metric.key", "metric.disabled.key"));
+ }
+
+ @Test
+ public void fail_when_data_and_login_type_with_invalid_login() {
+ definitions.addComponent(PropertyDefinition
+ .builder("my.key")
+ .name("foo")
+ .description("desc")
+ .category("cat")
+ .subCategory("subCat")
+ .type(PropertyType.USER_LOGIN)
+ .defaultValue("default")
+ .multiValues(true)
+ .build());
+ userDb.insertUser(newUserDto().setLogin("login.1"));
+ userDb.insertUser(newUserDto().setLogin("login.2").setActive(false));
+
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage("Error when validating login setting with key 'my.key' and values [login.1, login.2]. A value is not a valid login.");
- callForMultiValueGlobalSetting("my.key", newArrayList("metric.key", "My Value"));
+ callForMultiValueGlobalSetting("my.key", newArrayList("login.1", "login.2"));
}
@Test