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.

PopulateDefaultPermTemplateOnOrganizationsTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.server.platform.db.migration.version.v74;
  21. import java.sql.SQLException;
  22. import java.util.stream.Collectors;
  23. import javax.annotation.Nullable;
  24. import org.assertj.core.groups.Tuple;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.sonar.api.impl.utils.TestSystem2;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.db.CoreDbTester;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.tuple;
  33. public class PopulateDefaultPermTemplateOnOrganizationsTest {
  34. private final static long PAST = 10_000_000_000L;
  35. private final static long NOW = 50_000_000_000L;
  36. @Rule
  37. public ExpectedException expectedException = ExpectedException.none();
  38. @Rule
  39. public CoreDbTester db = CoreDbTester.createForSchema(PopulateDefaultPermTemplateOnOrganizationsTest.class, "organizations.sql");
  40. private System2 system2 = new TestSystem2().setNow(NOW);
  41. private PopulateDefaultPermTemplateOnOrganizations underTest = new PopulateDefaultPermTemplateOnOrganizations(db.database(), system2);
  42. @Test
  43. public void test_is_reentrant() throws SQLException {
  44. insertOrganization("aa", "aa-1");
  45. insertOrganization("bb", null);
  46. underTest.execute();
  47. underTest.execute();
  48. assertOrganizations(
  49. tuple("aa", "aa-1", "aa-1", "aa-1", NOW),
  50. tuple("bb", null, null, null, PAST)
  51. );
  52. }
  53. @Test
  54. public void test_with_organizations() throws SQLException {
  55. insertOrganization("aa", "aa-1");
  56. insertOrganization("bb", "bb-1");
  57. insertOrganization("cc", null);
  58. underTest.execute();
  59. assertOrganizations(
  60. tuple("aa", "aa-1", "aa-1", "aa-1", NOW),
  61. tuple("bb", "bb-1", "bb-1", "bb-1", NOW),
  62. tuple("cc", null, null, null, PAST)
  63. );
  64. }
  65. @Test
  66. public void without_governance_no_modifications() throws SQLException {
  67. insertOrganization("default-organization", null);
  68. underTest.execute();
  69. assertOrganizations(
  70. tuple("default-organization", null, null, null, PAST)
  71. );
  72. }
  73. private void assertOrganizations(Tuple... expectedTuples) {
  74. assertThat(db.select("SELECT uuid, default_perm_template_view, default_perm_template_app, default_perm_template_port, updated_at FROM organizations")
  75. .stream()
  76. .map(row -> new Tuple(row.get("UUID"), row.get("DEFAULT_PERM_TEMPLATE_VIEW"), row.get("DEFAULT_PERM_TEMPLATE_APP"), row.get("DEFAULT_PERM_TEMPLATE_PORT"), row.get("UPDATED_AT")))
  77. .collect(Collectors.toList()))
  78. .containsExactlyInAnyOrder(expectedTuples);
  79. }
  80. private void insertOrganization(String uuid, @Nullable String defaultPermTemplateView) {
  81. db.executeInsert("ORGANIZATIONS",
  82. "UUID", uuid,
  83. "KEE", uuid,
  84. "NAME", uuid,
  85. "GUARDED", false,
  86. "DEFAULT_PERM_TEMPLATE_VIEW", defaultPermTemplateView,
  87. "DEFAULT_QUALITY_GATE_UUID", "111",
  88. "NEW_PROJECT_PRIVATE", false,
  89. "SUBSCRIPTION", "sonarqube",
  90. "CREATED_AT", PAST,
  91. "UPDATED_AT", PAST);
  92. }
  93. }