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.

ProjectKeyGeneratorTest.java 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.almintegration;
  21. import org.apache.commons.lang3.RandomStringUtils;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import org.mockito.InjectMocks;
  26. import org.mockito.Mock;
  27. import org.mockito.junit.MockitoJUnitRunner;
  28. import org.sonar.core.util.UuidFactory;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.mockito.Mockito.when;
  31. import static org.sonar.server.common.almintegration.ProjectKeyGenerator.MAX_PROJECT_KEY_SIZE;
  32. import static org.sonar.server.common.almintegration.ProjectKeyGenerator.PROJECT_KEY_SEPARATOR;
  33. @RunWith(MockitoJUnitRunner.class)
  34. public class ProjectKeyGeneratorTest {
  35. private static final int MAX_UUID_SIZE = 40;
  36. private static final String UUID_STRING = RandomStringUtils.randomAlphanumeric(MAX_UUID_SIZE);
  37. @Mock
  38. private UuidFactory uuidFactory;
  39. @InjectMocks
  40. private ProjectKeyGenerator projectKeyGenerator;
  41. @Before
  42. public void setUp() {
  43. when(uuidFactory.create()).thenReturn(UUID_STRING);
  44. }
  45. @Test
  46. public void generateUniqueProjectKey_shortProjectName_shouldAppendUuid() {
  47. String fullProjectName = RandomStringUtils.randomAlphanumeric(10);
  48. assertThat(projectKeyGenerator.generateUniqueProjectKey(fullProjectName))
  49. .isEqualTo(generateExpectedKeyName(fullProjectName));
  50. }
  51. @Test
  52. public void generateUniqueProjectKey_projectNameEqualsToMaximumSize_shouldTruncateProjectNameAndPreserveUUID() {
  53. String fullProjectName = RandomStringUtils.randomAlphanumeric(MAX_PROJECT_KEY_SIZE);
  54. String projectKey = projectKeyGenerator.generateUniqueProjectKey(fullProjectName);
  55. assertThat(projectKey)
  56. .hasSize(MAX_PROJECT_KEY_SIZE)
  57. .isEqualTo(generateExpectedKeyName(fullProjectName.substring(fullProjectName.length() + UUID_STRING.length() + 1 - MAX_PROJECT_KEY_SIZE)));
  58. }
  59. @Test
  60. public void generateUniqueProjectKey_projectNameBiggerThanMaximumSize_shouldTruncateProjectNameAndPreserveUUID() {
  61. String fullProjectName = RandomStringUtils.randomAlphanumeric(MAX_PROJECT_KEY_SIZE + 50);
  62. String projectKey = projectKeyGenerator.generateUniqueProjectKey(fullProjectName);
  63. assertThat(projectKey)
  64. .hasSize(MAX_PROJECT_KEY_SIZE)
  65. .isEqualTo(generateExpectedKeyName(fullProjectName.substring(fullProjectName.length() + UUID_STRING.length() + 1 - MAX_PROJECT_KEY_SIZE)));
  66. }
  67. @Test
  68. public void generateUniqueProjectKey_projectNameContainsSlashes_shouldBeEscaped() {
  69. String fullProjectName = "a/b/c";
  70. assertThat(projectKeyGenerator.generateUniqueProjectKey(fullProjectName))
  71. .isEqualTo(generateExpectedKeyName(fullProjectName.replace("/", "_")));
  72. }
  73. private String generateExpectedKeyName(String truncatedProjectName) {
  74. return truncatedProjectName + PROJECT_KEY_SEPARATOR + UUID_STRING;
  75. }
  76. }