3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.common.almintegration;
22 import org.apache.commons.lang3.RandomStringUtils;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.sonar.core.util.UuidFactory;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.when;
33 import static org.sonar.server.common.almintegration.ProjectKeyGenerator.MAX_PROJECT_KEY_SIZE;
34 import static org.sonar.server.common.almintegration.ProjectKeyGenerator.PROJECT_KEY_SEPARATOR;
36 @RunWith(MockitoJUnitRunner.class)
37 public class ProjectKeyGeneratorTest {
39 private static final int MAX_UUID_SIZE = 40;
40 private static final String UUID_STRING = RandomStringUtils.randomAlphanumeric(MAX_UUID_SIZE);
43 private UuidFactory uuidFactory;
46 private ProjectKeyGenerator projectKeyGenerator;
50 when(uuidFactory.create()).thenReturn(UUID_STRING);
54 public void generateUniqueProjectKey_shortProjectName_shouldAppendUuid() {
55 String fullProjectName = RandomStringUtils.randomAlphanumeric(10);
57 assertThat(projectKeyGenerator.generateUniqueProjectKey(fullProjectName))
58 .isEqualTo(generateExpectedKeyName(fullProjectName));
62 public void generateUniqueProjectKey_projectNameEqualsToMaximumSize_shouldTruncateProjectNameAndPreserveUUID() {
63 String fullProjectName = RandomStringUtils.randomAlphanumeric(MAX_PROJECT_KEY_SIZE);
65 String projectKey = projectKeyGenerator.generateUniqueProjectKey(fullProjectName);
66 assertThat(projectKey)
67 .hasSize(MAX_PROJECT_KEY_SIZE)
68 .isEqualTo(generateExpectedKeyName(fullProjectName.substring(fullProjectName.length() + UUID_STRING.length() + 1 - MAX_PROJECT_KEY_SIZE)));
72 public void generateUniqueProjectKey_projectNameBiggerThanMaximumSize_shouldTruncateProjectNameAndPreserveUUID() {
73 String fullProjectName = RandomStringUtils.randomAlphanumeric(MAX_PROJECT_KEY_SIZE + 50);
75 String projectKey = projectKeyGenerator.generateUniqueProjectKey(fullProjectName);
76 assertThat(projectKey)
77 .hasSize(MAX_PROJECT_KEY_SIZE)
78 .isEqualTo(generateExpectedKeyName(fullProjectName.substring(fullProjectName.length() + UUID_STRING.length() + 1 - MAX_PROJECT_KEY_SIZE)));
82 public void generateUniqueProjectKey_projectNameContainsSlashes_shouldBeEscaped() {
83 String fullProjectName = "a/b/c";
85 assertThat(projectKeyGenerator.generateUniqueProjectKey(fullProjectName))
86 .isEqualTo(generateExpectedKeyName(fullProjectName.replace("/", "_")));
89 private String generateExpectedKeyName(String truncatedProjectName) {
90 return truncatedProjectName + PROJECT_KEY_SEPARATOR + UUID_STRING;