Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UpdatePermissionTooLongTemplateKeysTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.v71;
  21. import com.google.common.collect.ImmutableList;
  22. import java.sql.SQLException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Random;
  26. import java.util.stream.IntStream;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.sonar.core.util.UuidFactory;
  30. import org.sonar.core.util.UuidFactoryFast;
  31. import org.sonar.db.CoreDbTester;
  32. import static java.util.stream.Collectors.toList;
  33. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. public class UpdatePermissionTooLongTemplateKeysTest {
  36. @Rule
  37. public CoreDbTester dbTester = CoreDbTester.createForSchema(UpdatePermissionTooLongTemplateKeysTest.class, "templates.sql");
  38. private static final Random RANDOM = new Random();
  39. private final RecordingUuidFactory UUID_FACTORY = new RecordingUuidFactory();
  40. private final UpdatePermissionTooLongTemplateKeys underTest = new UpdatePermissionTooLongTemplateKeys(dbTester.database(), UUID_FACTORY);
  41. @Test
  42. public void keys_whose_length_is_greater_than_40_should_be_updated() throws SQLException {
  43. // Create 10 permission templates with keys' length greater or equals than 40
  44. List<String> tooLongKeys = IntStream.range(0, 10)
  45. .mapToObj(i -> insertTemplate(randomAlphanumeric(41 + RANDOM.nextInt(60))))
  46. .collect(toList());
  47. underTest.execute();
  48. assertThat(UUID_FACTORY.getRecordedUuids()).hasSize(tooLongKeys.size());
  49. List<String> kees = dbTester.select("select kee from permission_templates").stream()
  50. .map(r -> (String) r.get("KEE")).collect(toList());
  51. assertThat(kees).containsExactlyInAnyOrder(UUID_FACTORY.getRecordedUuids().toArray(new String[] {}));
  52. }
  53. @Test
  54. public void keys_whose_length_is_lower_than_40_should_not_be_updated() throws SQLException {
  55. // Create 10 permission templates with keys' length lower or equals than 40
  56. List<String> correctKeys = IntStream.range(0, 10)
  57. .mapToObj(i -> insertTemplate(randomAlphanumeric(RANDOM.nextInt(41))))
  58. .collect(toList());
  59. underTest.execute();
  60. assertThat(UUID_FACTORY.getRecordedUuids()).hasSize(0);
  61. List<String> kees = dbTester.select("select kee from permission_templates").stream()
  62. .map(r -> (String) r.get("KEE")).collect(toList());
  63. assertThat(kees).containsExactlyInAnyOrder(correctKeys.toArray(new String[] {}));
  64. }
  65. private String insertTemplate(String kee) {
  66. dbTester.executeInsert(
  67. "PERMISSION_TEMPLATES",
  68. "NAME", randomAlphanumeric(50),
  69. "ORGANIZATION_UUID", "uuid",
  70. "KEE", kee);
  71. return kee;
  72. }
  73. private static final class RecordingUuidFactory implements UuidFactory {
  74. private final List<String> generatedUuids = new ArrayList<>();
  75. private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
  76. @Override
  77. public String create() {
  78. String uuid = uuidFactory.create();
  79. generatedUuids.add(uuid);
  80. return uuid;
  81. }
  82. public void clear() {
  83. generatedUuids.clear();
  84. }
  85. public List<String> getRecordedUuids() {
  86. return ImmutableList.copyOf(generatedUuids);
  87. }
  88. }
  89. }