From 98ed407cda3655c6eb7627320744bd62096b11c4 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 25 Aug 2016 16:52:14 +0200 Subject: [PATCH] SONAR-7970 handle deprecated key in /api/settings/values WS --- .../server/settings/ws/ValuesAction.java | 25 +++++++++++++------ .../server/settings/ws/ValuesActionTest.java | 17 +++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java index 4528b6227c6..47cd44ede70 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java @@ -29,6 +29,8 @@ import java.util.Map; 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; @@ -36,7 +38,6 @@ import org.sonar.api.server.ws.Response; 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; @@ -107,7 +108,8 @@ public class ValuesAction implements SettingsWsAction { Optional component = getComponent(dbSession, valuesRequest); checkAdminPermission(component); Set keys = new HashSet<>(valuesRequest.getKeys()); - return new ValuesResponseBuilder(loadSettings(dbSession, component, keys), component).build(); + Map keysToDisplayMap = getKeysToDisplayMap(keys); + return new ValuesResponseBuilder(loadSettings(dbSession, component, keysToDisplayMap.keySet()), component, keysToDisplayMap).build(); } finally { dbClient.closeSession(dbSession); } @@ -161,16 +163,23 @@ public class ValuesAction implements SettingsWsAction { .collect(Collectors.toList()); } + private Map getKeysToDisplayMap(Set keys) { + return keys.stream() + .collect(Collectors.toMap(propertyDefinitions::validKey, Function.identity())); + } + private class ValuesResponseBuilder { private final List settings; private final Optional component; private final ValuesWsResponse.Builder valuesWsBuilder = ValuesWsResponse.newBuilder(); private final Map settingsBuilderByKey = new HashMap<>(); + private final Map keysToDisplayMap; - ValuesResponseBuilder(List settings, Optional component) { + ValuesResponseBuilder(List settings, Optional component, Map keysToDisplayMap) { this.settings = settings; this.component = component; + this.keysToDisplayMap = keysToDisplayMap; } ValuesWsResponse build() { @@ -181,18 +190,18 @@ public class ValuesAction implements SettingsWsAction { 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; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ValuesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ValuesActionTest.java index 2b17aa2c664..081d5adaa96 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ValuesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ValuesActionTest.java @@ -327,6 +327,23 @@ public class ValuesActionTest { 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(); -- 2.39.5