@@ -36,11 +36,10 @@ import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.component.ComponentDto; | |||
import org.sonar.server.component.ComponentFinder; | |||
import org.sonar.server.setting.ws.SettingValidator.SettingData; | |||
import org.sonar.server.setting.ws.SettingValidations.SettingData; | |||
import org.sonar.server.user.UserSession; | |||
import org.sonarqube.ws.client.setting.ResetRequest; | |||
import static org.sonar.server.setting.ws.SettingValidator.validateScope; | |||
import static org.sonar.server.setting.ws.SettingsWsComponentParameters.addComponentParameters; | |||
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; | |||
import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_RESET; | |||
@@ -54,16 +53,16 @@ public class ResetAction implements SettingsWsAction { | |||
private final SettingsUpdater settingsUpdater; | |||
private final UserSession userSession; | |||
private final PropertyDefinitions definitions; | |||
private final SettingValidator settingValidator; | |||
private final SettingValidations validations; | |||
public ResetAction(DbClient dbClient, ComponentFinder componentFinder, SettingsUpdater settingsUpdater, UserSession userSession, PropertyDefinitions definitions, | |||
SettingValidator settingValidator) { | |||
SettingValidations validations) { | |||
this.dbClient = dbClient; | |||
this.settingsUpdater = settingsUpdater; | |||
this.userSession = userSession; | |||
this.componentFinder = componentFinder; | |||
this.definitions = definitions; | |||
this.settingValidator = settingValidator; | |||
this.validations = validations; | |||
} | |||
@Override | |||
@@ -96,8 +95,8 @@ public class ResetAction implements SettingsWsAction { | |||
Optional<ComponentDto> component = getComponent(dbSession, resetRequest); | |||
checkPermissions(component); | |||
resetRequest.getKeys().forEach(key -> { | |||
SettingData data = new SettingData(key, definitions.get(key), component.orElse(null)); | |||
ImmutableList.of(validateScope(), settingValidator.validateQualifier()) | |||
SettingData data = new SettingData(key, component.orElse(null)); | |||
ImmutableList.of(validations.scope(), validations.qualifier()) | |||
.forEach(validation -> validation.validate(data)); | |||
}); | |||
@@ -56,13 +56,12 @@ import org.sonar.scanner.protocol.GsonHelper; | |||
import org.sonar.server.component.ComponentFinder; | |||
import org.sonar.server.exceptions.BadRequestException; | |||
import org.sonar.server.platform.SettingsChangeNotifier; | |||
import org.sonar.server.setting.ws.SettingValidator.SettingData; | |||
import org.sonar.server.setting.ws.SettingValidations.SettingData; | |||
import org.sonar.server.user.UserSession; | |||
import org.sonar.server.ws.KeyExamples; | |||
import org.sonarqube.ws.client.setting.SetRequest; | |||
import static com.google.common.base.Strings.isNullOrEmpty; | |||
import static org.sonar.server.setting.ws.SettingValidator.validateScope; | |||
import static org.sonar.server.ws.WsUtils.checkRequest; | |||
import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_SET; | |||
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID; | |||
@@ -82,10 +81,10 @@ public class SetAction implements SettingsWsAction { | |||
private final UserSession userSession; | |||
private final SettingsUpdater settingsUpdater; | |||
private final SettingsChangeNotifier settingsChangeNotifier; | |||
private final SettingValidator settingValidator; | |||
private final SettingValidations validations; | |||
public SetAction(PropertyDefinitions propertyDefinitions, I18n i18n, DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, | |||
SettingsUpdater settingsUpdater, SettingsChangeNotifier settingsChangeNotifier, SettingValidator settingValidator) { | |||
SettingsUpdater settingsUpdater, SettingsChangeNotifier settingsChangeNotifier, SettingValidations validations) { | |||
this.propertyDefinitions = propertyDefinitions; | |||
this.i18n = i18n; | |||
this.dbClient = dbClient; | |||
@@ -93,7 +92,7 @@ public class SetAction implements SettingsWsAction { | |||
this.userSession = userSession; | |||
this.settingsUpdater = settingsUpdater; | |||
this.settingsChangeNotifier = settingsChangeNotifier; | |||
this.settingValidator = settingValidator; | |||
this.validations = validations; | |||
} | |||
@Override | |||
@@ -208,8 +207,8 @@ public class SetAction implements SettingsWsAction { | |||
private void commonChecks(SetRequest request, @Nullable PropertyDefinition definition, Optional<ComponentDto> component) { | |||
checkValueIsSet(request); | |||
SettingData settingData = new SettingData(request.getKey(), definition, component.orElse(null)); | |||
ImmutableList.of(validateScope(), settingValidator.validateQualifier()).stream() | |||
SettingData settingData = new SettingData(request.getKey(), component.orElse(null)); | |||
ImmutableList.of(validations.scope(), validations.qualifier()).stream() | |||
.forEach(validation -> validation.validate(settingData)); | |||
} | |||
@@ -24,27 +24,34 @@ import java.util.Locale; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.config.PropertyDefinition; | |||
import org.sonar.api.config.PropertyDefinitions; | |||
import org.sonar.api.i18n.I18n; | |||
import org.sonar.db.component.ComponentDto; | |||
import static org.sonar.server.ws.WsUtils.checkRequest; | |||
public class SettingValidator { | |||
public class SettingValidations { | |||
private final PropertyDefinitions definitions; | |||
private final I18n i18n; | |||
public SettingValidator(I18n i18n) { | |||
public SettingValidations(PropertyDefinitions definitions, I18n i18n) { | |||
this.definitions = definitions; | |||
this.i18n = i18n; | |||
} | |||
public static SettingValidation validateScope() { | |||
return data -> checkRequest(data.component != null || data.definition == null || data.definition.global() || isGlobal(data.definition), | |||
"Setting '%s' cannot be global", data.key); | |||
public SettingValidation scope() { | |||
return data -> { | |||
PropertyDefinition definition = definitions.get(data.key); | |||
checkRequest(data.component != null || definition == null || definition.global() || isGlobal(definition), | |||
"Setting '%s' cannot be global", data.key); | |||
}; | |||
} | |||
public SettingValidation validateQualifier() { | |||
public SettingValidation qualifier() { | |||
return data -> { | |||
String qualifier = data.component == null ? "" : data.component.qualifier(); | |||
checkRequest(data.component == null || data.definition == null || data.definition.qualifiers().contains(data.component.qualifier()), | |||
PropertyDefinition definition = definitions.get(data.key); | |||
checkRequest(data.component == null || definition == null || definition.qualifiers().contains(data.component.qualifier()), | |||
"Setting '%s' cannot be set on a %s", data.key, i18n.message(Locale.ENGLISH, "qualifier." + qualifier, null)); | |||
}; | |||
} | |||
@@ -61,13 +68,10 @@ public class SettingValidator { | |||
public static class SettingData { | |||
private final String key; | |||
@CheckForNull | |||
private final PropertyDefinition definition; | |||
@CheckForNull | |||
private final ComponentDto component; | |||
public SettingData(String key, @Nullable PropertyDefinition definition, @Nullable ComponentDto component) { | |||
public SettingData(String key, @Nullable ComponentDto component) { | |||
this.key = key; | |||
this.definition = definition; | |||
this.component = component; | |||
} | |||
@@ -36,6 +36,6 @@ public class SettingsWsModule extends Module { | |||
GenerateSecretKeyAction.class, | |||
CheckSecretKeyAction.class, | |||
SettingsUpdater.class, | |||
SettingValidator.class); | |||
SettingValidations.class); | |||
} | |||
} |
@@ -81,11 +81,11 @@ public class ResetActionTest { | |||
ComponentFinder componentFinder = new ComponentFinder(dbClient); | |||
PropertyDefinitions definitions = new PropertyDefinitions(); | |||
SettingsUpdater settingsUpdater = new SettingsUpdater(dbClient, definitions); | |||
SettingValidator settingValidator = new SettingValidator(i18n); | |||
SettingValidations settingValidations = new SettingValidations(definitions, i18n); | |||
ComponentDto project; | |||
ResetAction underTest = new ResetAction(dbClient, componentFinder, settingsUpdater, userSession, definitions, settingValidator); | |||
ResetAction underTest = new ResetAction(dbClient, componentFinder, settingsUpdater, userSession, definitions, settingValidations); | |||
WsActionTester ws = new WsActionTester(underTest); | |||
@Before |
@@ -83,12 +83,12 @@ public class SetActionTest { | |||
ComponentFinder componentFinder = new ComponentFinder(dbClient); | |||
I18nRule i18n = new I18nRule(); | |||
PropertyDefinitions propertyDefinitions = new PropertyDefinitions(); | |||
PropertyDefinitions definitions = new PropertyDefinitions(); | |||
FakeSettingsNotifier settingsChangeNotifier = new FakeSettingsNotifier(dbClient); | |||
SettingsUpdater settingsUpdater = new SettingsUpdater(dbClient, propertyDefinitions); | |||
SettingValidator settingValidator = new SettingValidator(i18n); | |||
SettingsUpdater settingsUpdater = new SettingsUpdater(dbClient, definitions); | |||
SettingValidations validations = new SettingValidations(definitions, i18n); | |||
SetAction underTest = new SetAction(propertyDefinitions, i18n, dbClient, componentFinder, userSession, settingsUpdater, settingsChangeNotifier, settingValidator); | |||
SetAction underTest = new SetAction(definitions, i18n, dbClient, componentFinder, userSession, settingsUpdater, settingsChangeNotifier, validations); | |||
WsActionTester ws = new WsActionTester(underTest); | |||
@@ -174,7 +174,7 @@ public class SetActionTest { | |||
@Test | |||
public void persist_property_set_setting() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -211,7 +211,7 @@ public class SetActionTest { | |||
@Test | |||
public void update_property_set_setting() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -258,7 +258,7 @@ public class SetActionTest { | |||
@Test | |||
public void update_property_set_on_component_setting() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -318,7 +318,7 @@ public class SetActionTest { | |||
@Test | |||
public void persist_global_property_with_deprecated_key() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.deprecatedKey("my.old.key") | |||
.name("foo") | |||
@@ -375,7 +375,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_data_and_type_do_not_match() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -393,7 +393,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_data_and_type_do_not_match_with_unknown_error_key() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -410,7 +410,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_global_with_property_only_on_projects() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -428,7 +428,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_view_property_when_on_projects_only() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -456,7 +456,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_multi_definition_and_single_value_provided() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -473,7 +473,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_single_definition_and_multi_value_provided() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -498,7 +498,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_property_set_with_unknown_field() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -521,7 +521,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_property_set_has_field_with_incorrect_type() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -544,7 +544,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_property_set_has_a_null_field_value() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -567,7 +567,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_property_set_with_invalid_json() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -591,7 +591,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_property_set_with_json_of_the_wrong_format() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") | |||
@@ -615,7 +615,7 @@ public class SetActionTest { | |||
@Test | |||
public void fail_when_property_set_on_component_of_global_setting() { | |||
propertyDefinitions.addComponent(PropertyDefinition | |||
definitions.addComponent(PropertyDefinition | |||
.builder("my.key") | |||
.name("foo") | |||
.description("desc") |