import java.util.Objects;
import java.util.Optional;
import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.core.util.stream.Collectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
Optional<ComponentDto> component = getComponent(dbSession, valuesRequest);
checkAdminPermission(component);
Set<String> keys = new HashSet<>(valuesRequest.getKeys());
- return new ValuesResponseBuilder(loadSettings(dbSession, component, keys), component).build();
+ Map<String, String> keysToDisplayMap = getKeysToDisplayMap(keys);
+ return new ValuesResponseBuilder(loadSettings(dbSession, component, keysToDisplayMap.keySet()), component, keysToDisplayMap).build();
} finally {
dbClient.closeSession(dbSession);
}
.collect(Collectors.toList());
}
+ private Map<String, String> getKeysToDisplayMap(Set<String> keys) {
+ return keys.stream()
+ .collect(Collectors.toMap(propertyDefinitions::validKey, Function.identity()));
+ }
+
private class ValuesResponseBuilder {
private final List<Setting> settings;
private final Optional<ComponentDto> component;
private final ValuesWsResponse.Builder valuesWsBuilder = ValuesWsResponse.newBuilder();
private final Map<String, Settings.Setting.Builder> settingsBuilderByKey = new HashMap<>();
+ private final Map<String, String> keysToDisplayMap;
- ValuesResponseBuilder(List<Setting> settings, Optional<ComponentDto> component) {
+ ValuesResponseBuilder(List<Setting> settings, Optional<ComponentDto> component, Map<String, String> keysToDisplayMap) {
this.settings = settings;
this.component = component;
+ this.keysToDisplayMap = keysToDisplayMap;
}
ValuesWsResponse build() {
private void processSettings() {
settings.forEach(setting -> {
- Settings.Setting.Builder valueBuilder = getOrCreateValueBuilder(setting.getKey());
+ Settings.Setting.Builder valueBuilder = getOrCreateValueBuilder(keysToDisplayMap.get(setting.getKey()));
valueBuilder.setDefault(setting.isDefault());
setInherited(setting, valueBuilder);
setValue(setting, valueBuilder);
});
}
- private Settings.Setting.Builder getOrCreateValueBuilder(String propertyKey) {
- Settings.Setting.Builder valueBuilder = settingsBuilderByKey.get(propertyKey);
+ private Settings.Setting.Builder getOrCreateValueBuilder(String key) {
+ Settings.Setting.Builder valueBuilder = settingsBuilderByKey.get(key);
if (valueBuilder == null) {
- valueBuilder = valuesWsBuilder.addSettingsBuilder().setKey(propertyKey);
- settingsBuilderByKey.put(propertyKey, valueBuilder);
+ valueBuilder = valuesWsBuilder.addSettingsBuilder().setKey(key);
+ settingsBuilderByKey.put(key, valueBuilder);
}
return valueBuilder;
}
assertSetting(result.getSettings(3), "moduleProperty", "module", false, true);
}
+ @Test
+ public void return_value_of_deprecated_key() throws Exception {
+ setUserAsSystemAdmin();
+ propertyDefinitions.addComponent(PropertyDefinition
+ .builder("foo")
+ .deprecatedKey("deprecated")
+ .build());
+ insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one"));
+
+ ValuesWsResponse result = newRequestForGlobalProperties("deprecated");
+ assertThat(result.getSettingsList()).hasSize(1);
+
+ Settings.Setting value = result.getSettings(0);
+ assertThat(value.getKey()).isEqualTo("deprecated");
+ assertThat(value.getValue()).isEqualTo("one");
+ }
+
@Test
public void test_example_json_response() {
setUserAsSystemAdmin();