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.

DefaultTemplatesResolverImplIT.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.server.common.permission;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.db.DbSession;
  25. import org.sonar.db.DbTester;
  26. import org.sonar.db.component.ResourceTypesRule;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. import static org.sonar.api.resources.Qualifiers.APP;
  30. import static org.sonar.api.resources.Qualifiers.PROJECT;
  31. import static org.sonar.api.resources.Qualifiers.VIEW;
  32. public class DefaultTemplatesResolverImplIT {
  33. @Rule
  34. public DbTester db = DbTester.create(System2.INSTANCE);
  35. private ResourceTypesRule resourceTypesWithPortfoliosInstalled = new ResourceTypesRule().setRootQualifiers(PROJECT, APP, VIEW);
  36. private ResourceTypesRule resourceTypesWithApplicationInstalled = new ResourceTypesRule().setRootQualifiers(PROJECT, APP);
  37. private ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(PROJECT);
  38. private DefaultTemplatesResolverImpl underTestWithPortfoliosInstalled = new DefaultTemplatesResolverImpl(db.getDbClient(), resourceTypesWithPortfoliosInstalled);
  39. private DefaultTemplatesResolverImpl underTestWithApplicationInstalled = new DefaultTemplatesResolverImpl(db.getDbClient(), resourceTypesWithApplicationInstalled);
  40. private DefaultTemplatesResolverImpl underTest = new DefaultTemplatesResolverImpl(db.getDbClient(), resourceTypes);
  41. @Test
  42. public void get_default_templates_when_portfolio_not_installed() {
  43. db.permissionTemplates().setDefaultTemplates("prj", null, null);
  44. assertThat(underTest.resolve(db.getSession()).getProject()).contains("prj");
  45. assertThat(underTest.resolve(db.getSession()).getApplication()).isEmpty();
  46. assertThat(underTest.resolve(db.getSession()).getPortfolio()).isEmpty();
  47. }
  48. @Test
  49. public void get_default_templates_always_return_project_template_even_when_all_templates_are_defined_but_portfolio_not_installed() {
  50. db.permissionTemplates().setDefaultTemplates("prj", "app", "port");
  51. assertThat(underTest.resolve(db.getSession()).getProject()).contains("prj");
  52. assertThat(underTest.resolve(db.getSession()).getApplication()).isEmpty();
  53. assertThat(underTest.resolve(db.getSession()).getPortfolio()).isEmpty();
  54. }
  55. @Test
  56. public void get_default_templates_always_return_project_template_when_only_project_template_and_portfolio_is_installed_() {
  57. db.permissionTemplates().setDefaultTemplates("prj", null, null);
  58. assertThat(underTestWithPortfoliosInstalled.resolve(db.getSession()).getProject()).contains("prj");
  59. assertThat(underTestWithPortfoliosInstalled.resolve(db.getSession()).getApplication()).contains("prj");
  60. assertThat(underTestWithPortfoliosInstalled.resolve(db.getSession()).getPortfolio()).contains("prj");
  61. }
  62. @Test
  63. public void get_default_templates_for_all_components_when_portfolio_is_installed() {
  64. db.permissionTemplates().setDefaultTemplates("prj", "app", "port");
  65. assertThat(underTestWithPortfoliosInstalled.resolve(db.getSession()).getProject()).contains("prj");
  66. assertThat(underTestWithPortfoliosInstalled.resolve(db.getSession()).getApplication()).contains("app");
  67. assertThat(underTestWithPortfoliosInstalled.resolve(db.getSession()).getPortfolio()).contains("port");
  68. }
  69. @Test
  70. public void get_default_templates_always_return_project_template_when_only_project_template_and_application_is_installed_() {
  71. db.permissionTemplates().setDefaultTemplates("prj", null, null);
  72. assertThat(underTestWithApplicationInstalled.resolve(db.getSession()).getProject()).contains("prj");
  73. assertThat(underTestWithApplicationInstalled.resolve(db.getSession()).getApplication()).contains("prj");
  74. assertThat(underTestWithApplicationInstalled.resolve(db.getSession()).getPortfolio()).isEmpty();
  75. }
  76. @Test
  77. public void get_default_templates_for_all_components_when_application_is_installed() {
  78. db.permissionTemplates().setDefaultTemplates("prj", "app", null);
  79. assertThat(underTestWithApplicationInstalled.resolve(db.getSession()).getProject()).contains("prj");
  80. assertThat(underTestWithApplicationInstalled.resolve(db.getSession()).getApplication()).contains("app");
  81. assertThat(underTestWithApplicationInstalled.resolve(db.getSession()).getPortfolio()).isEmpty();
  82. }
  83. @Test
  84. public void fail_when_default_template_for_project_is_missing() {
  85. DbSession session = db.getSession();
  86. assertThatThrownBy(() -> underTestWithPortfoliosInstalled.resolve(session))
  87. .isInstanceOf(IllegalStateException.class)
  88. .hasMessage("Default template for project is missing");
  89. }
  90. }