From 30168dba2aa286ca6400b38253c7199fb96dec7c Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 12 Jan 2017 13:51:02 +0100 Subject: [PATCH] SONAR-7300 Return global values when component does not exist --- .../it/settings/DeprecatedPropertiesWsTest.java | 7 +++++++ .../org/sonar/server/property/ws/IndexAction.java | 13 +++++-------- .../sonar/server/property/ws/IndexActionTest.java | 13 +++++++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java b/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java index a6d02dbf232..cff8e72f41c 100644 --- a/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java +++ b/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java @@ -212,6 +212,13 @@ public class DeprecatedPropertiesWsTest { assertThat(getProperty("sonar.coverage.exclusions", PROJECT_KEY).getValue()).isEqualTo("file"); } + @Test + public void get_global_value_when_component_is_unknown() throws Exception { + setProperty("some-property", "value", null); + + assertThat(getProperty("some-property", PROJECT_KEY).getValue()).isEqualTo("value"); + } + @Test public void get_all_component_settings() throws Exception { List properties = getProperties(PROJECT_KEY); diff --git a/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java index d4626462d92..d8db9d0fb60 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java @@ -44,7 +44,6 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.property.PropertyDto; -import org.sonar.server.component.ComponentFinder; import org.sonar.server.user.UserSession; import org.sonar.server.ws.WsAction; @@ -67,13 +66,11 @@ public class IndexAction implements WsAction { public static final String PARAM_COMPONENT = "resource"; private final DbClient dbClient; - private final ComponentFinder componentFinder; private final UserSession userSession; private final PropertyDefinitions propertyDefinitions; - public IndexAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, PropertyDefinitions propertyDefinitions) { + public IndexAction(DbClient dbClient, UserSession userSession, PropertyDefinitions propertyDefinitions) { this.dbClient = dbClient; - this.componentFinder = componentFinder; this.userSession = userSession; this.propertyDefinitions = propertyDefinitions; } @@ -122,15 +119,15 @@ public class IndexAction implements WsAction { if (component == null) { return Optional.empty(); } - return Optional.of(loadComponent(dbSession, component)); + return loadComponent(dbSession, component); } - private ComponentDto loadComponent(DbSession dbSession, String component) { + private Optional loadComponent(DbSession dbSession, String component) { try { Long componentId = Long.parseLong(component); - return componentFinder.getById(dbSession, componentId); + return Optional.ofNullable(dbClient.componentDao().selectById(dbSession, componentId).orNull()); } catch (NumberFormatException e) { - return componentFinder.getByKey(dbSession, component); + return Optional.ofNullable(dbClient.componentDao().selectByKey(dbSession, component).orNull()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java index e5a6530b255..7efbf222358 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java @@ -38,7 +38,6 @@ import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.property.PropertyDbTester; -import org.sonar.server.component.ComponentFinder; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; @@ -77,7 +76,7 @@ public class IndexActionTest { ComponentDto project; - WsActionTester ws = new WsActionTester(new IndexAction(dbClient, new ComponentFinder(dbClient), userSession, definitions)); + WsActionTester ws = new WsActionTester(new IndexAction(dbClient, userSession, definitions)); @Before public void setUp() throws Exception { @@ -165,6 +164,16 @@ public class IndexActionTest { executeAndVerify(project.key(), null, "return_project_values.json"); } + @Test + public void return_global_values_when_project_does_not_exist() throws Exception { + setAuthenticatedUser(); + definitions.addComponent(PropertyDefinition.builder("property").defaultValue("default").build()); + propertyDb.insertProperties( + newGlobalPropertyDto().setKey("property").setValue("one")); + + executeAndVerify("unknown", null, "return_global_values.json"); + } + @Test public void return_values_even_if_no_property_definition() throws Exception { setAuthenticatedUser(); -- 2.39.5