You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigurationRepositoryTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.component;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.config.Configuration;
  25. import org.sonar.api.impl.config.MapSettings;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  28. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  29. import org.sonar.ce.task.projectanalysis.analysis.ProjectConfigurationFactory;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.property.PropertyDto;
  34. import org.sonar.server.project.Project;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.when;
  38. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  39. import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
  40. public class ConfigurationRepositoryTest {
  41. private static Project PROJECT = Project.from(newPrivateProjectDto(newOrganizationDto()));
  42. @Rule
  43. public final DbTester db = DbTester.create(System2.INSTANCE);
  44. private DbClient dbClient = db.getDbClient();
  45. private MapSettings globalSettings = new MapSettings();
  46. private Branch branch = mock(Branch.class);
  47. private AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  48. private ConfigurationRepository underTest;
  49. @Before
  50. public void setUp() {
  51. when(branch.getName()).thenReturn("branchName");
  52. analysisMetadataHolder.setBranch(branch);
  53. underTest = new ConfigurationRepositoryImpl(analysisMetadataHolder, new ProjectConfigurationFactory(globalSettings, dbClient));
  54. }
  55. @Test
  56. public void get_project_settings_from_global_settings() {
  57. analysisMetadataHolder.setProject(PROJECT);
  58. globalSettings.setProperty("key", "value");
  59. Configuration config = underTest.getConfiguration();
  60. assertThat(config.get("key")).hasValue("value");
  61. }
  62. @Test
  63. public void get_project_settings_from_db() {
  64. ComponentDto project = db.components().insertPrivateProject();
  65. analysisMetadataHolder.setProject(Project.from(project));
  66. insertProjectProperty(project, "key", "value");
  67. Configuration config = underTest.getConfiguration();
  68. assertThat(config.get("key")).hasValue("value");
  69. }
  70. @Test
  71. public void call_twice_get_project_settings() {
  72. analysisMetadataHolder.setProject(PROJECT);
  73. globalSettings.setProperty("key", "value");
  74. Configuration config = underTest.getConfiguration();
  75. assertThat(config.get("key")).hasValue("value");
  76. config = underTest.getConfiguration();
  77. assertThat(config.get("key")).hasValue("value");
  78. }
  79. @Test
  80. public void project_settings_override_global_settings() {
  81. globalSettings.setProperty("key", "value1");
  82. ComponentDto project = db.components().insertPrivateProject();
  83. insertProjectProperty(project, "key", "value2");
  84. analysisMetadataHolder.setProject(Project.from(project));
  85. Configuration config = underTest.getConfiguration();
  86. assertThat(config.get("key")).hasValue("value2");
  87. }
  88. @Test
  89. public void project_settings_are_cached_to_avoid_db_access() {
  90. ComponentDto project = db.components().insertPrivateProject();
  91. insertProjectProperty(project, "key", "value");
  92. analysisMetadataHolder.setProject(Project.from(project));
  93. Configuration config = underTest.getConfiguration();
  94. assertThat(config.get("key")).hasValue("value");
  95. db.executeUpdateSql("delete from properties");
  96. db.commit();
  97. assertThat(config.get("key")).hasValue("value");
  98. }
  99. @Test
  100. public void branch_settings() {
  101. ComponentDto project = db.components().insertMainBranch();
  102. ComponentDto branchDto = db.components().insertProjectBranch(project);
  103. Branch branch = mock(Branch.class);
  104. when(branch.getName()).thenReturn(branchDto.getBranch());
  105. analysisMetadataHolder
  106. .setProject(Project.from(project))
  107. .setBranch(branch);
  108. globalSettings.setProperty("global", "global value");
  109. insertProjectProperty(project, "project", "project value");
  110. insertProjectProperty(branchDto, "branch", "branch value");
  111. Configuration config = underTest.getConfiguration();
  112. assertThat(config.get("global")).hasValue("global value");
  113. assertThat(config.get("project")).hasValue("project value");
  114. assertThat(config.get("branch")).hasValue("branch value");
  115. }
  116. private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) {
  117. db.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setResourceId(project.getId()));
  118. }
  119. }