]> source.dussan.org Git - sonarqube.git/blob
3751d757bda55ce8d7ae9ad19ba4a76d6e37dad2
[sonarqube.git] /
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
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;
30
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;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class ProjectKeyGeneratorTest {
38
39   private static final int MAX_UUID_SIZE = 40;
40   private static final String UUID_STRING = RandomStringUtils.randomAlphanumeric(MAX_UUID_SIZE);
41
42   @Mock
43   private UuidFactory uuidFactory;
44
45   @InjectMocks
46   private ProjectKeyGenerator projectKeyGenerator;
47
48   @Before
49   public void setUp() {
50     when(uuidFactory.create()).thenReturn(UUID_STRING);
51   }
52
53   @Test
54   public void generateUniqueProjectKey_shortProjectName_shouldAppendUuid() {
55     String fullProjectName = RandomStringUtils.randomAlphanumeric(10);
56
57     assertThat(projectKeyGenerator.generateUniqueProjectKey(fullProjectName))
58       .isEqualTo(generateExpectedKeyName(fullProjectName));
59   }
60
61   @Test
62   public void generateUniqueProjectKey_projectNameEqualsToMaximumSize_shouldTruncateProjectNameAndPreserveUUID() {
63     String fullProjectName = RandomStringUtils.randomAlphanumeric(MAX_PROJECT_KEY_SIZE);
64
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)));
69   }
70
71   @Test
72   public void generateUniqueProjectKey_projectNameBiggerThanMaximumSize_shouldTruncateProjectNameAndPreserveUUID() {
73     String fullProjectName = RandomStringUtils.randomAlphanumeric(MAX_PROJECT_KEY_SIZE + 50);
74
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)));
79   }
80
81   @Test
82   public void generateUniqueProjectKey_projectNameContainsSlashes_shouldBeEscaped() {
83     String fullProjectName = "a/b/c";
84
85     assertThat(projectKeyGenerator.generateUniqueProjectKey(fullProjectName))
86       .isEqualTo(generateExpectedKeyName(fullProjectName.replace("/", "_")));
87   }
88
89   private String generateExpectedKeyName(String truncatedProjectName) {
90     return truncatedProjectName + PROJECT_KEY_SEPARATOR + UUID_STRING;
91   }
92 }